Java

OOP GUIDE / WORK

 

BOTTLE CLASS 3 SOLUTIONS

 

 

TASK – PART 1 – CODE ANALYSIS

 

ARRAY 1 CODE

 

Bottle[] bot = new Bottle[10];

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

{

     if (Math.random() < 0.5)

           bot[i] = new Bottle(500, "Silver");

     else

           bot[i] = new Bottle(500, "Blue");

}

 

ARRAY 1 DESCRIPTION

 

The array bot contains 10 bottles.  All are 500mL in volume.  There is a 50% chance that a bottle is Silver and a 50% chance that it is Blue.  All bottles are empty.

 

ARRAY 2 CODE

 

Bottle[] boo = {new Bottle(500, "Red"), new Bottle(250, "Green"),

                new Bottle(355, "Blue"), new Bottle(600, "Pink"),

                new Bottle(400, "Yellow"), new Bottle(240, "Teal")};

 

ARRAY 2 DESCRIPTION

 

The array boo contains 6 bottles.  They are all different volumes and colours.  They are all empty.

 

ARRAY 3 CODE

 

Bottle[] barr = new Bottle[100];

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

{

     int v = ((int)(Math.random() * 3 + 1)) * 250;

     barr[i] = new Bottle(v, "Clear");

}

 

ARRAY 3 DESCRIPTION

 

The array barr contains 100 bottles.  All bottles are clear.  There are three sizes of bottles: 250mL, 500mL and 750mL and the size is chosen randomly.  All bottles are empty.

 

ARRAY 4 CODE

 

Bottle[] botarr = new Bottle[50];

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

{

     botarr[i] = new Bottle(500, "Green");

     botarr[i].addLiquid("water", 500);

}

 

ARRAY 4 DESCRIPTION

 

The array botarr contains 50 bottles.  All bottles are 500mL in size, green and filled with water.

 

ARRAY 5 CODE

 

Bottle[] drinks = new Bottle[20];

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

{

     drinks[i] = new Bottle(500, "Blue");

     drinks[i].addLiquid("lemonade", 250);

     drinks[i].addLiquid("iced tea", 250);

}

 

ARRAY 5 DESCRIPTION

 

The drinks array contains 20 blue bottles that are all 500mL in size.  They all contain half lemonade and half iced tea.  (Fun fact: This drinks is called an Arnold Palmer after the golfer who used to order it regularly).