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