Answer:
The complete main() is:
import java.util.Scanner; // Scanner class is used to take input from user
import java.util.ArrayList; //class to store dynamic re-sizable arrays
import java.util.StringTokenizer; // class to break strings to tokens
public class PlantArrayListExample{ //to create myGarden ArrayList and display its contents in the output
public static void printArrayList(ArrayList<Plant>myGarden) {
//declares ArrayList myGarden to hold object of type Plant
/* the above line has a method printArrayList(), that uses the printInfo() methods defined in the Plant and Flower classes and prints an ArrayList myGarden of plants or flower objects */
for (int i = 0; i < myGarden.size(); ++i) { /* the loop iterates through the ArrayList myGarden and get method gets the elements of plant information from respective classes */
myGarden.get(i).printInfo(); } }
public static void main(String[] args) { //start of main() function body
Scanner scan = new Scanner(System.in); //create Scanner class object
String input;
//ArrayList myGarden of Plant type
ArrayList<Plant> myGarden = new ArrayList<>();
String plantName; //declare string type variable for name of plant
String plantCost; //declare string type variable for cost of plant
String colorOfFlowers; //declare string type variable for flower color
boolean isAnnual; //declare boolean type variable to set the value of isAnuual to true or false
input = scan.next(); // reads the input values (plant or flower) from user
while(!input.equals("-1")) { // the program takes and reads the plants and flowers from user repeatedly until the user enters -1 to stop
plantName = scan.next(); // scans and reads the input plant name
plantCost = scan.next(); // scans and reads the input the plant cost
//checks if input is a plant or a flower
if (input.equals("plant")) { // if user enters plant
Plant myPlant = new Plant(); //creates object myPlant of Plant type
/* the following three statement store the values of plant name and plant cost as Plant object if the user enters plant input and then adds each plant information to ArrayList myGarden */
myPlant.setPlantName(plantName); /*sets the name of the plant in plantName using the Plant class method setPlantName() accessed through object myPlant */
myPlant.setPlantCost(plantCost); /*sets the cost of the plant in plantCost using the Plant class method setPlantCost() accessed through object myPlant */
myGarden.add(myPlant); } // adds the above plant information to ArrayList myGarden
else if (input.equals("flower")) {
// if user enters flower
Flower myFlower = new Flower(); //creates object myFlower of Flower type
/* the following three statement store the values of plant name and plant cost, isAnnual and colors of flowers as Flower object if the user enters flower input and then adds each flower information to ArrayList myGarden */
myFlower.setPlantName(plantName); /*sets the name of the flower in plantName using method setPlantName() accessed through object myFlower */
myFlower.setPlantCost(plantCost); /*sets the cost of the flower in plantCost using method setPlantCost() accessed through object myFlower */
isAnnual = scan.nextBoolean(); //scans and reads the value of isAnnual (true of false) input by user
colorOfFlowers = scan.next(); //scans and reads the value of flower color input by user
myFlower.setPlantType(isAnnual); /*sets the value of isAnnual to true or false in isAnnual using Flower class method setPlantType() accessed through object myFlower */
myFlower.setColorOfFlowers(colorOfFlowers); /*sets the value of colorOfFlowers using Flower class method setColorOfFlowers() accessed through object myFlower */
myGarden.add(myFlower); } // adds the above flower information to ArrayList myGarden
input = scan.next(); } //keeps reading the input (flower or plant) until user enters -1 to stop
printArrayList(myGarden); }} //calls printArrayList() method to print the elements of the myGarden ArrayList
Explanation:
The program is well explained in the comments mentioned with each line of the program.
The program creates an ArrayList myGarden. The ArrayList is used to store objects that belong to the Plant class or the Flower class. A method printArrayList() uses the printInfo() methods defined in the respective classes Flower and Plant and prints each element in ArrayList myGarden . The program scans and reads plants or flowers from input (ending with -1), adding each Plant or Flower to the myGarden ArrayList, and output each element in myGarden using the printArrayList() method. If the user enters "plant" then the plant information from Plant class is added to the ArrayList using add() method and displayed in output and if the user enters "flower" in input then the flower information from Flower class is added in ArrayList myGarden and displayed in output.
The program along with its output is attached