1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
tigry1 [53]
3 years ago
15

9.18 LAB: Plant information (ArrayList)Given a base Plant class and a derived Flower class, complete main() to create an ArrayLi

st 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.Ex. If the input is:

Computers and Technology
1 answer:
mina [271]3 years ago
6 0

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

You might be interested in
In a computer-controlled greenhouse, a temperature sensor and a window motor are connected to the computer.
GrogVix [38]
Hope this helps solve it

8 0
3 years ago
Witch of the following is a valid why a scientist might a scientific theory
ad-work [718]

c i think because i new piece of info came in which changes peoples theorys

7 0
3 years ago
I'm 11, except my profile says I'm 15.
aleksandr82 [10.1K]

Answer:

Hello there, I dont think there is a way to change you age, but maybe you shall try again tomorrow and see if an error message pop up.  If the Error Message pops up, contact custumaor support!

Glad I could Help!

3 0
4 years ago
QUESTION: Which is not an example of a video port or cable?
Vikki [24]

Answer:

Radio Corporation of America (RCA)

Explanation:

A corporation is not a video connector.

8 0
3 years ago
What did the Lorax mean when he said i speak for the trees
kirill [66]

Answer:

The trees have no tongues, And the Lorax disapproves of the use of the Tree and the Thneed.

3 0
4 years ago
Other questions:
  • Xml is used to format the structure and style of a web page. true or false
    7·1 answer
  • A type of memory that is expensive and therefore is often used only in cache memory applications.
    15·2 answers
  • A5.3 1012 kg satellite is 1,800 m from another satellite that has a mass of 3.5 x 108 kg. What is the gravitational
    6·1 answer
  • "What technology will examine the current state of a network device before allowing it can to connect to the network and force a
    6·1 answer
  • A _________________ operating system accepts random enquires from remote locations and provides an instantaneous response
    5·1 answer
  • What are the reasonsfor documenting business rules​
    6·1 answer
  • 01110101<br> +00100100<br> 00010001
    8·1 answer
  • What is the first phone ever made?
    6·1 answer
  • Programming that relies on the use of objects and methods to control complexity and solve problems is known as what type of prog
    7·1 answer
  • How can the two independent clauses below be combined to form a correct complete sentence? Check all that apply.
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!