Question: Task: (1). The problem statement - Calculating the amount of energy in an EnergyDrink use the following breakdown per ounce: 8% protein (protein generates 10






Task:
(1). The problem statement -
Calculating the amount of energy in an EnergyDrink use the following breakdown per ounce:
8% protein (protein generates 10 units of energy per ounce)
15% sugar (sugar generates 20 units of energy per ounce)
27% caffeine (caffeine generates 33 units of energy per ounce)
The remainder is water, which has no energy value
Hint: Resist the temptation to have your class store calculated values in instance variables, as this can be a source of bugs
(2). Make use of this EnergyDrink information, along with the code present inside EnergyDrinkDemo to complete the EnergyDrink class. Be sure to follow the style best practices. When complete, you should be able to run the project and have the EnergyDrinkDemo code run correctly.
For example if you open the EnergyDrinkDemo file right now, you'll see the line:
this.redBull = new EnergyDrink(8.4);
Causes a compiler error that states:
The constructor EnergyDrink(double) is undefined
This makes sense because there is no constructor code (or any other code) inside the EnergyDrink class. Fix this by visiting the EnergyDrink class and adding a constructor that accepts this double value and does something appropriate with it. (You might need to read through the problem statement above and/or use other code in the EnergyDrinkDemo file for ideas on what this double value might be and how to complete the code for this constructor.)
(3). With the EnergyDrink class up and running, it's now time to turn to the Student class and get it working:
The Problem Statement Student
Every Student has an amount of energy stored, is capable of consuming an
EnergyDrink can do homework for a specified number of minutes (which consumes energy), and will utter it's catchphrase expression ("Howdy Neighbor!")
(4). Make use of this Student information, along with the code present inside StudentDemo to complete the Student class. Be sure to include appropriate style best practices in your code.
When complete, you should be able to run the entire project and both the EnergyDrinkDemo and StudentDemo code should run. Be sure to carefully read all output and confirm that it is giving you the information that you would expect to see (including the size of the EnergyDrink after it has been consumed). Correct any logic and style errors in EnergyDrink and Student before submitting.
Here is EnergyDrinkDemo
public class EnergyDrinkDemo { private EnergyDrink redBull; private EnergyDrink monster;
/** * Creates a new EnergyDrinkDemo object with * 2 EnergyDrink objects */ public EnergyDrinkDemo() { this.redBull = new EnergyDrink(8.4); this.monster = new EnergyDrink(16.0); } /** * This method will: * * print information about each EnergyDrink * * has each EnergyDrink be consumed * * prints the information about each EnergyDrink (after * being consumed) */ public void testEnergyDrink() { System.out.println("Red Bull created."); System.out.println("Expected: 8.4 ounces, 106.764 energy units"); this.displayEnergyDrink(this.redBull); System.out.println(); System.out.println("Monster created."); System.out.println("Expected: 16.0 ounces, 203.360 energy units"); this.displayEnergyDrink(this.monster); System.out.println(); System.out.println("The drinks are consumed."); this.redBull.consume(); this.monster.consume(); System.out.println(); System.out.println("The new Red Bull information:"); System.out.println("Expected: 0.0 ounces, 0.000 energy units"); this.displayEnergyDrink(this.redBull); System.out.println(); System.out.println("The new Monster information:"); System.out.println("Expected: 0.0 ounces, 0.000 energy units"); this.displayEnergyDrink(this.monster); }
/** * This method accepts an EnergyDrink object and displays to the screen * the EnergyDrink's size in ounces and number of energy units * * @param theDrink The EnergyDrink to be described */ private void displayEnergyDrink(EnergyDrink theDrink) { System.out.printf("Actual: %.1f ounces, ", theDrink.getSize()); System.out.printf("%.3f energy units ", theDrink.getEnergy()); }
}
Here is StudentDemo
public StudentDemo() { this.orangeDrink = new EnergyDrink(12.0); this.redDrink = new EnergyDrink(15.6); this.graduateStudent = new Student(1234.0); }
/** * Tests the functionality of the Student object */ public void testStudent() { this.displayStudent("The student's initial energy", 1234.0); System.out.print("The student says: "); System.out.println(this.graduateStudent.getCatchPhrase()); System.out.println(); this.graduateStudent.consume(this.orangeDrink); this.displayStudent("The student's energy after consuming the orange drink", 1386.52); System.out.print("The orange drink's size after being consumed: "); System.out.println(this.orangeDrink.getSize()); System.out.println(); this.graduateStudent.doHomework(30); this.displayStudent("The student's energy after homework", 126.52); this.graduateStudent.consume(this.redDrink); this.displayStudent("The student's energy after consuming the red drink", 324.796); System.out.print("The red drink's size after being consumed: "); System.out.println(this.orangeDrink.getSize()); System.out.println(); this.graduateStudent.doHomework(5); this.displayStudent("The student's energy after homework", 114.796); }
/** * Helper method to display a message and the expected amount of energy * * @param message The message to be displayed * @param expectedEnergy The expected energy of the Student */ private void displayStudent(String message, double expectedEnergy) { System.out.println(message); System.out.printf("Expected: %.3f ", expectedEnergy); System.out.printf("Actual: %.3f", this.graduateStudent.getEnergy()); System.out.println(" "); }
}
The Demos can be use to answer both questions.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
