Question: The fooddatalist is here https://filebin.net/edcrepjotuk9ykbv Task 1. Begin by creating a new Sandbox project. (Java main) 2. In this lab we will examine the nutritional
The fooddatalist is here
https://filebin.net/edcrepjotuk9ykbv
Task 1. Begin by creating a new Sandbox project. (Java main) 2. In this lab we will examine the nutritional information of 8790 fresh, packaged, and restaurant foods. On Schoology, find the file food_data_final.csv . Download the file and add it to your Sandbox (Click New and select Upload in the files area on the left side of your screen.). Now, CSV (which stands for Comma Separated Values) is a standard file type where each piece of data is separated by a comma. Below is a sample from the file. Excel shows it in columns, but if you opened it in notepad, you would see the commas. Each food includes a description, calories, protein, fat, carbohydrates, fiber, sugar, cholesterol, and nutrient index . The nutrient index considers 23 vitamins and minerals and gives a rough measurement of how effectively the food meets our daily nutritional needs (based on FDA guidelines). All quantities are for 100 grams of the food. 3. Our first task is to create a class called FoodItem that will store the data for one food. a. Add the class to your Sandbox again using the New button on the left (select File this time, and give it the name FoodItem.java . b. Utilizing data hiding, add 9 instance variables that will hold the data for a food. c. Create the parts of the class. i. Create a parameterized constructor that will take in each of the 9 pieces of data for a food and set the instance variables. ii. Next, create setters and getters for all fields iii. Finally, override the toString method that includes all fields printed out with labels and separated by commas. 4. Back in your myProgram.java class, we now need to input and keep track of 8790 different foods. a. At the class-level, make a new array that can hold 8790 FoodItem objects. Note that this array must be static since it is in the Main class, otherwise it cannot exist without making a Main object - which of course we never do. b. In the main method, add the following line of code to create a Scanner object that can read our data file: Scanner file = new Scanner(new File("food_data_final.csv")); Note that you may need some import statements! c. You may notice that the Scanner still shows an error. This is caused by the fact that an Exception may be thrown if the file is not found. Add a try/catch statement (see below) d. In the catch block, print File Error (see below) e. Complete the input in the try block as shown: (this is not part of the AP curriculum) f. The last task for the try block is to add each FoodItem object to the array. Add the necessary code to do this . 5. Before going further, you should verify that the input is working correctly. If it isnt, nothing else will work. a. Add a print statement after the line that prints --Input Complete--. Consider what items you could print out to confirm that all seems to be working properly? i. Print out the food that was imported first. What was the first food item read in? ii. Print out the food at index 3309. What is the food at index 3309? iii. Print out the food item at the very end of the list. What is the food at index 3309? 6. Next, you will implement a Search . This simply means being able to iterate through the array of foods for foods that meet certain criteria. a. Add a new method below the main method and call it Search . Remember.. that you must make it static, because it does not belong to any instance of any object. b. Add one parameter to the method header called toFind . c. Complete the body of the method as described: i. Using a for/each loop, compare toFind to the descriptions of all FoodItems. ii. Instead of using the String equals method, use contains so that we dont have to input exact matches. iii. All descriptions are in upper cased letters, so make your search case-insensitive by adding code that makes toFind upper-cased. iv. Print the FoodItem whenever a match is found. 7. Now you will test your Search a. After the input in the main method, perform a linear search for Applebees. b. Looking at your output, which food from Applebees has the highest calorie content ? c. Next, perform a search for cactus . What is the output? d. If you have not already done so, modify the method so it will print toFind + not found when the search fails. Verify that your changes work. 8. Write a method called getFoodsInRange() that asks the user for a calorie range (a minimum and a maximum). (This is listed as Energy Kcal in the table.) The method should then print out all the foods that have calories within that range. 9. The nutrient index is a measure of the total nutrient value of the amount of vitamins & minerals in a food. a. Write another new method called getHealthiest () that will print the healthiest food in the entire list according to this index. b. Test your method and write down the output: c. Modify the method so that it will ask the user for a value n . Then the method should print the top n healthiest foods according to the nutrient index. Test this method. d. What trends do you see in your list? Do you see anything that doesnt belong? Why or why not? 10. Maybe considering only nutrient content does not give a total picture of healthiness. Using the available data, come up with your own formula for healthiness and write it below. Then, make another version of your getHealthiest () method that uses your formula. (Call the method something different such as getHealthiestV2 ) Are you getting better results?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
