Java

OOP GUIDE / WORK

 

BOTTLE CLASS 2 SOLUTIONS

 

 

TASK – PART 1 – SETUP

 

No solution needed.

 

 

TASK – PART 2 to 4

 

Here is my solution for all remaining steps:

 

 

public class BottleArray

{

       public static void main(String[] args)

       {

             //PART 2 - CREATE A BOTTLE ARRAY OF SIZE 5. 

             //GIVE EACH BOTTLE A DIFFERENT VOLUME AND COLOUR.

            

             Bottle[] bottles = new Bottle[5];

             bottles[0] = new Bottle(1000, "Green");

             bottles[1] = new Bottle(250, "Yellow");

             bottles[2] = new Bottle(1500, "Orange");

             bottles[3] = new Bottle(750, "Blue");

             bottles[4] = new Bottle(650, "Red");

            

             //PART 3 - DISPLAY THE ARRAY USING A FOR LOOP

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

             {

                    System.out.println(bottles[i]);

             }

            

             //PART 4 - OUTPUT BIGGEST BOTTLE'S COLOUR (SEE FUNCTION BELOW)

             System.out.println("Biggest bottle: " + biggestBottleColour(bottles));

       }

      

       //=============================================

       //PART 4 - FUNCTION IMPLEMENTATION

      

       public static String biggestBottleColour(Bottle[] b)

       {

             int maxVol = b[0].getMaxVolume();

             String colour = b[0].getColour();

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

             {

                    if (b[i].getMaxVolume() > maxVol)

                    {

                           maxVol = b[i].getMaxVolume();

                           colour = b[i].getColour();

                    }

             }

             return colour;

       }

}

 

 

The above code outputs the following:

 

Green bottle (1000mL) contains nothing (0.0% full).

Yellow bottle (250mL) contains nothing (0.0% full).

Orange bottle (1500mL) contains nothing (0.0% full).

Blue bottle (750mL) contains nothing (0.0% full).

Red bottle (650mL) contains nothing (0.0% full).

Biggest bottle: Orange