ROGRAM For this program, you are tasked to implement the Beverage class which has the following private properties: name - a string value volume - this is an integer number which represents its current remaining volume in mL isChilled - this is a boolean field which is set to true if the drink is chilled It should have the following methods: isEmpty() - returns true if the volume is already 0 {toString() - returns the details of the object in the following format: {name} ({volume}mL) {"is still chilled" | "is not chilled anymore"}.Example returned strings: Beer (249mL) is still chilled Water (500mL) is not chilled
JAVA PROGRAM
For this program, you are tasked to implement the Beverage class which has the following private properties:
- name - a string value
- volume - this is an integer number which represents its current remaining volume in mL
- isChilled - this is a boolean field which is set to true if the drink is chilled
It should have the following methods:
- isEmpty() - returns true if the volume is already 0
- {toString() - returns the details of the object in the following format: {name} ({volume}mL) {"is still chilled" | "is not chilled anymore"}.Example returned strings:
- Beer (249mL) is still chilled
- Water (500mL) is not chilled anymore
- A constructor method with the following signature: public Beverage(name, volume, isChilled)
- Getter methods for all the 3 properties.
Then, create two final subclasses that inherit from this Beverage class. The first one is the Water class which has the additional private property, type, which is a String and can only be either "Purified", "Regular", or "Distilled". It should have the following methods:
- 1st constructor: public Water(volume, isChilled, type) - when calling its parent class' constructor, pass "Water" as the name
- 2nd constructor: public Water(volume, isChilled) - the type will be set to "Regular" and similar to the 1st constructor, pass "Water" as the name to its parent class' constructor
- A getter for its type
The second subclass to be created is the Beer class which has the additional private property, alcoholicContent, which is the percentage of alcoholic content in the beer in percentage (0.01 to 1.00). It should have 1 constructor only which the following signature: public Beer(volume, isChilled, alcoholicContent), and you need to pass "Beer" as the name for its parent class' constructor call. There should also be a getter for the alcoholicContent. Additionally, implement the following methods:
-
String getType()
- this returns "Flavored" if the alcoholic content is below 0.03, "Regular" if the alcoholic content is 0.03 and above but below 0.06, and "Strong" if it's 0.06 and above
-
String toString()
-
calls its parent class' toString() method and appends in the end its alcoholic percentage with 1 decimal place. For example:
- Beer (249mL) is still chilled (6.0% alcoholic content)
-
For this problem, you are also in charge in creating the main. In the main, you will ask the user for the following details:
- Type (1 for Water, 2 for Beer)
- Volume
- Is Chilled ('Y' for yes while 'N' for no)
- If the type is water, ask the user if to input the type. If the type is beer, ask the user to input the alcoholic content.
Example user prompts:
Select type (1 - Water, 2 - Beer): 1
Enter volume: 249
Is chilled (Y - yes, N - no): Y
Type: Distilled
Select type (1 - Water, 2 - Beer): 2
Enter volume: 500
Is chilled (Y - yes, N - no): N
Alcoholic content: 0.04
Then, after asking for these prompts from the user, create the object using the appropriate class (i.e. if the type inputted is 1, then create a Water object. If the type inputted is 2, then create a Beer object). Finally, call the built-in Tester.test() method and pass this object you just created.
Step by step
Solved in 5 steps with 2 images