JAVA 0.) Create a domain class called Food, with the following 2 attributes: pri
ID: 3711414 • Letter: J
Question
JAVA
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();
computeAndPrintStats();
printArrayOfFoods();
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.
At the end of the loop, print out the values of sum of calories, average calories, and lowest calorie value. 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
Given below is the code for the question. Since the sample input file name and its contents are not given here... I have used filename inputFile.txt and given you sample contents. If you have different filename or the code does not work for your file contents, post a comment, I'll help.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
NOTE: Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.
test input file: inputFile.txt
-----------
Burger 250
Icecream 300
Juice 200
Cola 400
Food.java
-------
public class Food {
private String foodName;
private int calories;
public Food()
{
foodName = "No name";
calories = 0;
}
public Food(String name, int cal)
{
foodName = name;
calories = cal;
}
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;
}
public String toString()
{
return foodName +" => " + calories + " calories";
}
}
FinalProgram.java
--------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FinalProgram {
static Food[] myFoods = new Food[15];
static int numFood = 0;
public static void main(String[] args) {
createArrayOfFoods();
computeAndPrintStats();
printArrayOfFoods();
}
public static void createArrayOfFoods() {
File aFile = new File("inputFile.txt");
try {
Scanner myFile = new Scanner(aFile);
numFood = 0;
while(myFile.hasNext())
{
Food f = new Food();
f.setFoodName(myFile.next());
f.setCalories(myFile.nextInt());
myFoods[numFood] = f;
numFood++;
}
myFile.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
public static void computeAndPrintStats()
{
int sum = 0;
int lowIdx = 0;
for(int i = 0; i < numFood; i++)
{
sum += myFoods[i].getCalories();
if(myFoods[i].getCalories() < myFoods[lowIdx].getCalories())
lowIdx = i;
}
double avg = ((double)sum)/numFood;
System.out.println("Total calories is " + sum);
System.out.printf("Average calories is %.2f ", avg);
System.out.printf("%s has the lowest calories of %d ", myFoods[lowIdx].getFoodName(), myFoods[lowIdx].getCalories());
}
public static void printArrayOfFoods()
{
System.out.println(" The list of food items is");
for(int i = 0; i < numFood; i++)
System.out.println(myFoods[i].toString());
}
}
output
------
Total calories is 1150
Average calories is 287.50
Juice has the lowest calories of 200
The list of food items is
Burger => 250 calories
Icecream => 300 calories
Juice => 200 calories
Cola => 400 calories
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.