Java

OOP GUIDE / WORK

 

BOTTLE CLASS 3

 

Topics

  • Object arrays

 

 

TASK – PART 1 – CODE ANALYSIS

 

Consider the code below and describe each Bottle array that is created.  You might need to refer to the Bottle class which you can find in the Bottle Class work solutions.

 

 

public class BottleArray2

{

     public static void main(String[] args)

     {

           //ARRAY 1

           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 2

           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 3

           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 4

           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 5

           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);

           }

     }

}