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
What is an important theme in "Once Upon a Time"?
Maurinko [17]

Answer:

The disintegration of South African society.

Explanation:

Nadine Gordimer's "Once Upon A Time" revolves around the racist "Apartheid" time of South African society where the whites 'dominate' the blacks. the story focuses on the societal issues between the two races and how it is detrimental to the overall lives of the people involved.

One important theme of the story is racism, <em>"Apartheid"</em> which seemingly dictates who is superior and who is not. The huge divide in not only the living spheres of the two groups of citizens but also in their approaches to society is indicative of the discriminative attitude and perception of the people, especially with the whites claiming themselves to be superior and 'civilized'. The book deals largely with the colonial mindset of the whites while also the structural bigotry apparent in the mindset of the people.

Thus, the correct answer is the second option.

3 0
3 years ago
i got a set of headphones and when i plug them into my speakers the right side only works how do i fix the left side of them
Sladkaya [172]
Sorry I don't know what is wrong with your headphones. Maybe try plugging them into something else.
3 0
3 years ago
A(n) ________ collects data from various key business processes and stores the data in a single comprehensive data repository, u
xxTIMURxx [149]

Answer:

An Enterprise System

Explanation:

An enterprise system also refered to as an enterprise software is a computer software application used to handle the needs of a business or an enterprise examples are schools, production companies, government ministries and departments, charities etc.

The software provides all the business oriented services for the enterprise, services such as payment processing, students' information management, automated billing and payments etc.

Because enterprises will typically have different departments, the software is able to collect data from all the key business processes across all the departments into a single database which is useable according to different access privilages by all other parts of the enterprise.

3 0
3 years ago
ammy's Seashore Supplies rents beach equipment such as kayaks, canoes, beach chairs, and umbrellas to tourists. Write a program
melomori [17]

Answer:

// Here is SammysRentalPrice.java file

// import package

import java.util.*;

// class definition

class SammysRentalPrice

{

   // main method of the class

public static void main (String[] args) throws java.lang.Exception

{

   try{

    // object to read value from user

    Scanner scr=new Scanner(System.in);

    // ask to enter rented minutes

       System.out.print("enter rented minutes: ");

       // read minutes

       int min=scr.nextInt();

       // find hours

       int hour=min/60;

       //reamining minutes

       min=min%60;

       // total cost

       int cost=hour*40+min*1;

       // print cost

       System.out.println("total cost is: "+cost);

   }catch(Exception ex){

       return;}

}

}

Explanation:

Read rented minutes from user and assign it to variable "min".Find the hours from minutes and then update the remaining minutes.After this multiply hours with 40 and remaining minutes with 1.Sum them and this will be the cost of rented a sports equipment.

Output:

enter rented minutes: 145

total cost is: 105

8 0
4 years ago
What can a user do using the Contact Group dialog box? Check all that apply.
RSB [31]

Answer:

1,2,4,5,7

Explanation:

Had the question

3 0
3 years ago
Read 2 more answers
Other questions:
  • Heavenly bodies formed because of gravity<br><br> T or F
    10·1 answer
  • Write a Java program to print the result in the series 10, 15, 20, 25, ..., 50. Hint: You can use an iteration statement for wri
    9·1 answer
  • The united states attempted to halt the communist revolution in cuba by​
    7·1 answer
  • If a user on a laptop complains that they are unable to sign into windows even though they are certain they are entering the cor
    6·1 answer
  • Which topology enables only one person, at one time, to send data to others on the network?
    6·1 answer
  • A derived character that is shared by all members of a clade is called a ______________ of that clade.
    6·1 answer
  • Which skill refers to the ability to visualize and implement possible business solutions to problems?
    6·1 answer
  • "Two technicians are discussing FWD driveshaft service. Technician A says that removal of the driveshaft usually requires partia
    15·1 answer
  • Select the correct answer.
    15·1 answer
  • How could this code be simplified?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!