Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Instructions for Final Program 0.) Create a domain class called Food, with the f

ID: 3860034 • Letter: I

Question

Instructions for Final Program 0.) Create a domain class called Food, with the following 2 attributes: private String foodName; private int calories; Use Netbeans’ insert code feature to add the constructor, getters, setters, and toString methods. 1.) Create a driver class called FinalProgram that will have a main method that looks like this: createArrayOfFoods(); printArrayOfFoods(); computeAndPrintStats(); 2.) In the createArrayOfFoods() method, use the global static array of Foods that should be defined at the beginning of the driver class: Food[] myFoods = new Food[15]; Read the attached file of foods and their caloric value, where each record has the name of a food, and a total number of calories. Instantiate a Food object for each record, and then move each Food object into myFoods array at the appropriate index location. Use the following code to define the file: File aFile = new File("inputFile.txt"); Scanner myFile = new Scanner(aFile); Remember to create a loop to read each record in the file using the .hasNext() condition, && the length of the array as part of the loop. Remember to define an index as an int variable, and initialize it to 0 before the loop. Within the loop, remember to increment the index. 2.) Next, in the computeAndPrintStats() method, write a loop (sequential search) to find the food with the lowest amount of calories, and add all the calories in all the foods in the file, so that after the loop the average calorie can be computed. You will need 3 variables: one to store the lowest calorie of all the foods, one to store the sum of all the calories in all the foods, and one to compute the average of all the calories in the all the foods. At the end of the method, print out all of the values inside these 3 variables, with a brief description before them. Also, print out the entire Food object with the lowest calorie value. 3.) In the method called printArrayOfFoods()create a loop to iterate through the global array of foods, and print each Food object, indirectly using the Food’s toString() method. 4.) Make sure to document the functionality of the program by putting in comments at the beginning of the class, and before each method.

Explanation / Answer

FinalProgram.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class FinalProgram {

static Food[] myFoods = new Food[15];

public static void main(String[] args) throws FileNotFoundException {

createArrayOfFoods();

printArrayOfFoods();

computeAndPrintStats();

}

public static void createArrayOfFoods() throws FileNotFoundException {

File aFile = new File("inputFile.txt");

Scanner myFile = new Scanner(aFile);

for(int i=0;myFile.hasNext() &&i<myFoods.length;i++ ) {

myFoods[i] = new Food();

myFoods[i].setFoodName(myFile.next());

myFoods[i].setCalories(myFile.nextInt());

}

}

public static void computeAndPrintStats() {

int minCalories = myFoods[0].getCalories();

int minIndex = 0;

int sumCalories = 0;

int i=0;

for(i=0;i<myFoods.length;i++){

if(myFoods[i]!=null) {

if(minCalories > myFoods[i].getCalories()) {

minCalories = myFoods[i].getCalories();

minIndex = i;

}

sumCalories+=myFoods[i].getCalories();

}

}

System.out.println("The food with the lowest amount of calories: "+myFoods[minIndex].toString());

System.out.println("Average calories: "+(sumCalories/(double)i));

}

public static void printArrayOfFoods() {

for(int i=0;i<myFoods.length;i++){

if(myFoods[i]!=null) {

System.out.println(myFoods[i].toString());

}

}

}

}

Food.java

public class Food {

private String foodName;

private int calories;

public Food() {

}

public Food(String foodName, int calories) {

this.foodName=foodName;

this.calories=calories;

}

public String getFoodName() {

return foodName;

}

public void setFoodName(String foodName) {

this.foodName = foodName;

}

public int getCalories() {

return calories;

}

public void setCalories(int calories) {

this.calories = calories;

}

@Override

public String toString() {

return "Food [calories=" + calories + ", foodName=" + foodName + "]";

}

}

Output:

Food [calories=11, foodName=aaaa]
Food [calories=22, foodName=bbbb]
Food [calories=22, foodName=cccc]
Food [calories=44, foodName=dddd]
Food [calories=55, foodName=eeee]
Food [calories=66, foodName=ffff]
Food [calories=77, foodName=gggg]
Food [calories=88, foodName=hhhh]
Food [calories=99, foodName=iiii]
Food [calories=12, foodName=qqqq]
Food [calories=23, foodName=wwww]
Food [calories=45, foodName=rrrr]
Food [calories=56, foodName=tttt]
Food [calories=67, foodName=yyyy]
Food [calories=78, foodName=uuuu]
The food with the lowest amount of calories: Food [calories=11, foodName=aaaa]
Average calories: 51.0

inputFile.txt

aaaa 11
bbbb 22
cccc 22
dddd 44
eeee 55
ffff 66
gggg 77
hhhh 88
iiii 99
qqqq 12
wwww 23
rrrr 45
tttt 56
yyyy 67
uuuu 78