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

Instructions for the Final Exam Program 0.) Create a domain class called Dessert

ID: 3711847 • Letter: I

Question

Instructions for the Final Exam Program

0.) Create a domain class called Dessert, with the following 2 attributes:

private int calories;         
private String dessertName;


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:

        createArrayOfDesserts();

        computeAndPrintStats();

        printArrayOfDesserts();

2.) In the createArrayOfDesserts() method, use the global static array of Desserts that should be defined at the beginning of the driver class:

    Dessert[] myDesserts = new Dessert[10];

Read the attached file of desserts and their caloric value, where each record has the name of a dessert, and a total number of calories. Instantiate a Dessert object for each record, and then move each Dessert object into myDesserts 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 dessert with the highest amount of calories, and add all the calories in all the desserts in the file, so that after the loop the average calorie can be computed.

At the end of the loop, print out the values sum of calories, average calories, and highest calories, with a brief description before them. Also, print out the entire Dessert object with the highest calorie value.

3.) In the method called printArrayOfDesserts()create a loop to iterate through the global array of desserts, and print each Dessert object, indirectly using the Dessert’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

Below is your code.

Please add missing import statements if any

inputFile.txt
-----------
IceCreamShake 250
Icecream 300
IceSizzlers 200
ColdCoffee 400

Dessert.java

//Class Declaration

public class Dessert {

//instance variable declaration

private String dessertName;

private int calories;

//Constructor - Default

public Dessert() {

dessertName = "No name";

calories = 0;

}

//Constructor - Parameterized.

public Dessert(String name, int cal) {

dessertName = name;

calories = cal;

}

//method to get calories

public int getCalories() {

return calories;

}

//method to set calories

public void setCalories(int calories) {

this.calories = calories;

}

//over-riden toString method

public String toString() {

return dessertName + " => " + calories + " calories";

}

//method to get dessert Name

public String getDessertName() {

return dessertName;

}

//method to set dessert Name

public void setDessertName(String dessertName) {

this.dessertName = dessertName;

}

}

FinalProgram.java

//Class Declaration

public class FinalProgram {

//static array to hold desserts

static Dessert[] myDesserts = new Dessert[15];

//count variable holding number of dessert items added

static int numDessert = 0;

//main method to execute the program

public static void main(String[] args) {

createArrayOfDesserts();

computeAndPrintStats();

printArrayOfDesserts();

}

//method that uses the static array and a file

//it reads the file and create Dessert objects one by one

// and then add the dessert object to the array.

//It also keeps the note of the number of items added

//throws FileNotFoundException

public static void createArrayOfDesserts() {

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

try {

Scanner myFile = new Scanner(aFile);

numDessert = 0;

while (myFile.hasNext()) {

Dessert f = new Dessert();

f.setDessertName(myFile.next());

f.setCalories(myFile.nextInt());

myDesserts[numDessert] = f;

numDessert++;

}

myFile.close();

} catch (FileNotFoundException e) {

System.out.println(e.getMessage());

System.exit(1);

}

}

//method to calculate print the statistics of this program.

//It prints the total and average of all the calories of all the items

//it also print the item with highest number of calories

public static void computeAndPrintStats() {

int sum = 0;

int highTdx = 0;

for (int i = 0; i < numDessert; i++) {

sum += myDesserts[i].getCalories();

if (myDesserts[i].getCalories() > myDesserts[highTdx].getCalories())

highTdx = i;

}

double avg = ((double) sum) / numDessert;

System.out.println("Total calories is " + sum);

System.out.printf("Average calories is %.2f ", avg);

System.out.printf("%s has the Highest calories of %d ", myDesserts[highTdx].getDessertName(),

myDesserts[highTdx].getCalories());

}

//method to print the array of desserts

//it runs a loop to iterate the array and print each element

public static void printArrayOfDesserts() {

System.out.println(" The list of Desserts is");

for (int i = 0; i < numDessert; i++)

System.out.println(myDesserts[i]);

}

}

Output

Total calories is 1150
Average calories is 287.50
ColdCoffee has the Highest calories of 400

The list of Desserts is
IceCreamShake => 250 calories
Icecream => 300 calories
IceSizzlers => 200 calories
ColdCoffee => 400 calories