Java

OOP GUIDE / WORK

 

CASE CLASS SOLUTIONS

 

 

TASK – PART 1 – SETUP

 

No solution needed.

 

 

TASK – PART 2 – CASE CLASS

 

Here is my solution:

 

 

public class Case

{

     private Bottle[] bots;

    

     public Case(int totalBottles)

     {

           bots = new Bottle[totalBottles];

           for (int i=0; i<bots.length; i++)

           {

                bots[i] = new Bottle(750, "Blue");

           }

     }

    

     public int getNumberOfBottles()

     {

           return bots.length;

     }

    

     public Bottle getBottle(int index)

     {

           return bots[index];

     }

    

     public String toString()

     {

           String s = "";

           for (int i=0; i<bots.length; i++)

           {

                s = s + bots[i].toString() + "\n";

           }

           return s;

     }

    

     public void fillBottles(String liquid)

     {

           for (int i=0; i<bots.length; i++)

           {

                int remainingVol = bots[i].getRemainingVolume();

                bots[i].addLiquid(liquid, remainingVol);

           }

     }

    

}

 

 

 

TASK – PART 3 – TESTING THE CASE CLASS

 

Here is my solution:

 

 

public class CaseTester

{

     public static void main(String[] args)

     {

           Case c = new Case(10);

           System.out.println(c);

          

           c.getBottle(9).addLiquid("water", 100);

           System.out.println(c);

          

           c.fillBottles("juice");

           System.out.println(c);

     }

 

}