Drink class Drink is an abstract class, which represents the basic attributes of
ID: 3553562 • Letter: D
Question
Drink class
Drink is an abstract class, which represents the basic attributes of any drink in a container to be sold. It is used as the root of the drink hierarchy. It has the following attributes (should be protected):
Attribute name
Attribute type
Description
volume
int
The volume of the drink
unitPrice
double
The price per unit of the drink
totalPrice
double
The total price of the drink
drinkId
String
The Id of the drink
The following constructor method should be provided to initialize the instance variables.
public Drink(String, double)
The instance variable volume is initialized to 0, totalPrice is initialized to 0.0, unitPrice is initialized to the value of the second parameter, and drinkId is initialized to the string value of the first parameter.
The following accessor method should be provided for drinkId :
public String getDrinkId()
The class Drink also has an abstract method (which should be implemented by its child classes, DrinkInCylinder and DrinkInBox) to compute the volume of the drink:
public abstract void computeTotalPrice();
The following public method should be provided:
public String toString()
toString method returns a string of the following format:
The DrinkId: 10001
The Volume: 150
The Unit Price: 0.0015
The Total Price: $330.00
DrinkInCylinder class
DrinkInCylinder is a child of Drink class. It represents a drink in a can (cylinder). It has the following attribute in addition to the inherited ones:
Attribute name
Attribute type
Description
radius
int
The radius of the cylinder of the drink.
height
int
The height of the cylinder of the drink.
The following constructor method should be provided:
public DrinkInCylinder(String, double, int, int)
The radius is initialized to the value of the third parameter, the height is initialized to the value of the forth parameter, and the constructor of the parent class Drink should be called using the first and second parameters. Leave volume and totalPrice as their default value.
The following method should be implemented:
public void computeTotalPrice()
First, it computes the volume for the cylinder drink. (computed by PI*(radius*radius*height), the constant value PI is defined in the Math class. -- (int) (Math.PI*(radius*radius*height)) Also, compute (radius*radius*height) first since they are all integers. PI is a float point number, so you need to cast the final value to an integer ("volume" is an integer.) Then compute the total price of the drink. (Computed by volume * unitPrice)
Also, the following method should be implemented:
public String toString()
The toString() method inherited from Drink class should be used to create a new string, and display a cylinder drink's information using the following format:
The Drink in a Cylinder
The Radius: 5
The Height: 10
The DrinkId: sona200
The Volume: 785
The Unit Price: 0.0022
The Total Price: $1.73
This toString method should make use of the toString method of the parent class.
Explanation / Answer
Find the code here : http://ideone.com/a8NdKK
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.