Ask Question
15 September, 15:22

Given a base Plant class and a derived Flower class, complete main () to create an ArrayList called myGarden. The ArrayList should be able to store objects that belong to the Plant class or the Flower class. Create a method called printArrayList (), that uses the printInfo () methods defined in the respective classes and prints each element in myGarden. The program should read 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 printInfo () method.

+4
Answers (1)
  1. 15 September, 17:20
    0
    See explaination

    Explanation:

    import java. util. Scanner;

    import java. util. ArrayList;

    import java. util. StringTokenizer;

    public class PlantArrayListExample{

    public static void main (String[] args) {

    Scanner scnr = new Scanner (System. in);

    String input;

    ArrayList myGarden = new ArrayList ();

    String plantName, colorOfFlowers;

    boolean isAnnual;

    double plantCost;

    input = scnr. next ();

    Plant temp = null;

    while (! input. equals ("-1")) {

    plantName = scnr. next ();

    plantCost = scnr. nextDouble ();

    if (input. equals ("flower")) {

    isAnnual = scnr. next (). equals ("true");

    colorOfFlowers = scnr. next ();

    temp = new Flower ();

    temp. setPlantName (plantName);

    temp. setPlantCost (plantCost);

    ((Flower) temp). setPlantType (isAnnual);

    ((Flower) temp). setColorOfFlowers (colorOfFlowers);

    myGarden. add (temp);

    }

    else{

    temp = new Plant ();

    temp. setPlantName (plantName);

    temp. setPlantCost (plantCost);

    myGarden. add (temp);

    }

    input = scnr. next ();

    }

    printArrayList (myGarden);

    }

    public static void printArrayList (ArrayList myGarden) {

    for (Plant p:myGarden) {

    System. out. println (p);

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Given a base Plant class and a derived Flower class, complete main () to create an ArrayList called myGarden. The ArrayList should be able ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers