Java

OOP GUIDE / WORK

 

CASE CLASS

 

Topics

  • Aggregation
  • Encapsulation
  • Object array as instance variable

 

 

TASK – PART 1 – SETUP


Copy and paste the following Bottle class into your IDE.

 

 

public class Bottle

{

     private String content;  //ex: water, "" means empty

     private int maxVolume;   //in mL (so int works here)

     private int filledVolume;  //also in mL

     private String colour;

 

     public Bottle(int mv, String col)

     {

           maxVolume = mv;

           colour = col;

           content = "";

           filledVolume = 0;

     }

 

     public int getFilledVolume()

     {

           return filledVolume;

     }

 

     public int getMaxVolume()

     {

           return maxVolume;

     }

 

     public String getColour()

     {

           return colour;

     }

 

     public String getContent()

     {

           if (content.equals(""))

           {

                return "nothing";

           }

           else

           {

                return content;

           }

     }

 

     public int getRemainingVolume()

     {

           return maxVolume - filledVolume;

     }

 

     public double getPercentageFull()

     {

           return (double)filledVolume / maxVolume;   //0.00 to 1.00

     }

 

     public double getPercentageEmpty()

     {

           return (double)getRemainingVolume() / maxVolume;  //0.00 to 1.00

     }

 

     public boolean isEmpty()

     {

           return filledVolume == 0;  //interesting solution

     }

 

     public boolean isFull()

     {

           return filledVolume == maxVolume;  //interesting solution

     }

 

     public boolean contains(String liquid)

     {

           if (content.contains(liquid))

           {

                return true;

           }

           else

           {

                return false;

           }

     }

 

     public void pourOut()

     {

           content = "";

           filledVolume = 0;

     }

 

     public void pourOut(int volumeToRemove)

     {

           if (volumeToRemove > filledVolume)

           {

                System.out.println("Error.  The bottle doesn't have that much in it.  Bottle emptied.");

                pourOut();

           }

           else

           {

                filledVolume = filledVolume - volumeToRemove;

                if (filledVolume == 0)

                {

                      content = "";

                }

           }

     }

 

     public String toString()

     {

           return colour + " bottle (" + maxVolume + "mL) contains " + getContent() + " (" + (getPercentageFull()*100) + "% full).";

     }

 

     public void addLiquid(String newLiquid, double addedVolume)

     {

           if (isFull())

           {

                System.out.println("Overflow - bottle is already full.");

           }

           else if (isEmpty())

           {

                content = newLiquid;

                filledVolume += addedVolume;              

                if (filledVolume > maxVolume)

                {

                      filledVolume = maxVolume;

                      System.out.println("Overflow.  Bottle is now full.");

                }

           }

           else

           {

                if (!content.contains(newLiquid))

                {

                      content = content + " & " + newLiquid;

                }

                filledVolume += addedVolume;              

                if (filledVolume > maxVolume)

                {

                      filledVolume = maxVolume;

                      System.out.println("Overflow.  Bottle is now full.");

                }

           }

     }

}

 

 

 

 

TASK – PART 2 – CASE CLASS

 

Create the Case class that will represent a case of bottles.  It could be a case of water or a cased of juice.  Or a case of pop.  The class has the following specification:

 

INSTANCE VARIABLE

 

  • A Bottle array – An array of Bottle objects.  Size to be determined in constructor.

 

CONSTRUCTOR

 

  • A simple constructor that gets an int n for the number of bottles in the Case.  That value will be the size of the array.  Each element in the array should get a 750ML blue bottle that is empty.

 

METHODS

 

  • A getNumberOfBottles method that returns the number of bottles in the case.

  • A getBottle(int index) method that returns the bottle instance at element index. 

    Note that since Bottle is mutable, this means that the Bottle objects (and hence the Case object) can be changed from outside the Case class.  This is contrary to the idea of data encapsulation but can still be useful.

  • A toString() method that simply returns a combination of each Bottle object’s toString() method.

  • A fillBottles(String liq) method that will fill up each bottle in the Case with the new liquid.

 

 

TASK – PART 3 – TESTING THE CASE CLASS

 

In the class called CaseTester, in the main function, create a Case object with 10 bottles in it. 

 

Output the case’s content to screen.

 

Then add some water to the last bottle.

 

Output the case’s content to screen.

 

Then fill up all of the bottles with juice.

 

Output the case’s content to screen.