Java
OOP GUIDE / WORK
BOTTLE CLASS
Topics
- Advanced class design
- Advanced method design
- Encapsulation
TASK –
PART 1 – BOTTLE CLASS
Design
the Bottle class with the following specifications:
INSTANCE VARIABLES
- private String colour – The colour of the
bottle.
- private int maxVolume
– The volume of the bottle in mL.
- private int filledVolume
– Amount of liquid in the bottle in mL.
- private String content – The type
of liquid in the bottle. Possible
examples include “” (when empty), “water”, “milk”, “pop & water”, “coffee
& tea & water”.
CONSTRUCTOR
- One constructor that gets the colour and bottle’s volume. New bottles always start off empty.
METHODS – PART 1 – BASIC GET
METHODS
- getColour – Returns the colour as a String
- getMaxVolume – Returns the bottle’s volume
- getFilledVolume – Returns the amount of liquid
in the bottle
- getContent – Returns the type of liquid in
the bottle. However, if the
bottle is empty, it returns the String “nothing”.
METHODS – PART 2 – MORE METHODS
- getRemainingVolume() – Returns the amount of room
left in the bottle (in mL).
- getPercentFull – Returns the percentage (as a decimal
between 0.0 and 1.0) of the bottle that is full. So, 0.55 means that the bottle is 55%
full.
- getPercentEmpty – Returns the percentage (as a
decimal between 0.0 and 1.0) of the bottle that is empty.
- isEmpty() – Returns true if the bottle is
empty and false otherwise.
- isFull() – Returns true if the bottle is
full and false otherwise.
- contains(String liquid) – Returns true if
the content of the bottle already includes liquid. You can make use of the String class’
contains method here for an easy solution.
METHODS – PART 3 – TOSTRING
- Returns a String representing the
bottle. See below for two
examples:
Green bottle
(1000mL) contains water (20.0% full).
Blue bottle (500mL)
contains nothing (0.0% full).
METHODS – PART 4 – MORE
METHODS
- pourOut() – Sets the bottle to empty.
- pourOut(int vol)
– Removes vol from the filledVolume. You need to consider what happens if
the bottle is empty after this action.
Also, if the value of vol is greater than
filledVolume, simply empty the bottle and output:
Error. The bottle doesn't have that much in
it. Bottle emptied.
METHODS – PART 5 – ADDLIQUID() METHOD
- addLiquid(String newLiquid,
int volumeToAdd) – If the bottle has room in
it, it adds the newLiquid to it. If the bottle was already full, you
should display the msg:
Overflow - bottle is already
full.
If there is not enough room to place
all the liquid in, you should fill the bottle and display:
Overflow. Bottle is now full.
Note: The content of the liquid
starts off as “”. If you add “water”
the content is then “water”. If you add “milk”, the content is then “water
& milk”. If you add “water” again,
the content remains “water
& milk”. If you add “tea”, the
content becomes “water & milk &
tea”.
TASK –
PART 2 – BOTTLE CLASS TESTER
Use
the following BottleTester class to test your
Bottle class:
public class BottleTester
{
public static void
main(String[] args)
{
Bottle b = new Bottle(1000, "Green");
System.out.println(b);
b.addLiquid("water",
200);
System.out.println(b);
b.addLiquid("water",
300);
System.out.println(b);
b.addLiquid("milk",
200);
System.out.println(b);
b.addLiquid("milk",
100);
System.out.println(b);
b.pourOut(200);
System.out.println(b);
b.pourOut();
System.out.println(b);
}
}
|
|
It should
output the following:
Green
bottle (1000mL) contains nothing (0.0% full).
Green
bottle (1000mL) contains water (20.0% full).
Green
bottle (1000mL) contains water (50.0% full).
Green
bottle (1000mL) contains water & milk (70.0% full).
Green
bottle (1000mL) contains water & milk (80.0% full).
Green
bottle (1000mL) contains water & milk (60.0% full).
Green bottle (1000mL) contains nothing (0.0% full).
|
TASK –
PART 3 – BOTTLE CLASS TESTER 2
Use
the following BottleTester2 class to test your Bottle class:
public class
BottleTester2
{
public static void
main(String[] args)
{
Bottle bot = new Bottle(500, "Red");
System.out.println(bot);
bot.addLiquid("coffee",
250);
System.out.println(bot);
bot.addLiquid("tea",
300);
System.out.println(bot);
bot.pourOut(400);
System.out.println(bot);
bot.addLiquid("pop",
400);
System.out.println(bot);
bot.addLiquid("juice",
100);
System.out.println(bot);
}
}
|
It should
output the following:
Red
bottle (500mL) contains nothing (0.0% full).
Red
bottle (500mL) contains coffee (50.0% full).
Overflow. Bottle is now full.
Red
bottle (500mL) contains coffee & tea (100.0% full).
Red
bottle (500mL) contains coffee & tea (20.0% full).
Red
bottle (500mL) contains coffee & tea & pop (100.0% full).
Overflow
- bottle is already full.
Red bottle (500mL) contains coffee & tea &
pop (100.0% full).
|
|