Question

LAB: Plant information (ArrayList)

10.16 LAB: Plant information (ArrayList)

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.

Ex. If the input is:

plant Spirea 10 
flower Hydrangea 30 false lilac 
flower Rose 6 false white
plant Mint 4
-1

the output is:

Plant Information: 
   Plant name: Spirea
   Cost: 10

Plant Information: 
   Plant name: Hydrengea
   Cost: 30
   Annual: false
   Color of flowers: lilac

Plant Information: 
   Plant name: Rose
   Cost: 6
   Annual: false
   Color of flowers: white

Plant Information: 
   Plant name: Mint
   Cost: 4



Flower.java

public class Flower extends Plant {

   private boolean isAnnual;
   private String colorOfFlowers;

   public void setPlantType(boolean userIsAnnual) {
      isAnnual = userIsAnnual;
   }

   public boolean getPlantType(){
      return isAnnual;
   }

   public void setColorOfFlowers(String userColorOfFlowers) {
      colorOfFlowers = userColorOfFlowers;
   }

   public String getColorOfFlowers(){
      return colorOfFlowers;
   }
   
   @Override
   public void printInfo(){
      System.out.println("Plant Information: ");
      System.out.println("   Plant name: " + plantName);
      System.out.println("   Cost: " + plantCost);
      System.out.println("   Annual: " + isAnnual);
      System.out.println("   Color of flowers: " + colorOfFlowers);
   }
}


Plant.java

public class Plant {
   protected String plantName;
   protected String plantCost;

   public void setPlantName(String userPlantName) {
      plantName = userPlantName;
   }

   public String getPlantName() {
      return plantName;
   }

   public void setPlantCost(String userPlantCost) {
      plantCost = userPlantCost;
   }

   public String getPlantCost() {
      return plantCost;
   }

   public void printInfo() {
      System.out.println("Plant Information: ");
      System.out.println("   Plant name: " + plantName);
      System.out.println("   Cost: " + plantCost);
   }
}


PlantArrayListExample.java

import java.util.Scanner;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class PlantArrayListExample {

   // TODO: Define a PrintArrayList method that prints an ArrayList of plant (or flower) objects                                                       
   
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      String input;
      // TODO: Declare an ArrayList called myGarden that can hold object of type plant

      // TODO: Declare variables - plantName, plantCost, colorOfFlowers, isAnnual
      
      input = scnr.next();
      while(!input.equals("-1")){
         // TODO: Check if input is a plant or flower
         //       Store as a plant object or flower object
         //       Add to the ArrayList myGarden
      
         input = scnr.next();
      }
      
      // TODO: Call the method PrintArrayList to print myGarden
      
   }
}


1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

PlantArrayListExample.java

import java.util.Scanner;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class PlantArrayListExample {
   
   public static void printArrayList(ArrayListmyGarden) {
      
      int i;
      
      for (i = 0; i < myGarden.size(); ++i) {
         
         myGarden.get(i).printInfo();
         System.out.println();
         
      }
      
   }
   
   public static void main(String[] args) {
      
      Scanner scnr = new Scanner(System.in);
      String input;
      
      ArrayList myGarden = new ArrayList<>();
      
      String plantName;
      String plantCost;
      
      String colorOfFlowers;
      boolean isAnnual;
      
      input = scnr.next();
      
      while(!input.equals("-1")) {
         
         plantName = scnr.next();
         plantCost = scnr.next();
         
         if (input.equals("plant")) {
            
            Plant myPlant = new Plant();
            
            myPlant.setPlantName(plantName);
            myPlant.setPlantCost(plantCost);
            
            myGarden.add(myPlant);
            
         }
         else if (input.equals("flower")) {
            
            Flower myFlower = new Flower();
            
            myFlower.setPlantName(plantName);
            myFlower.setPlantCost(plantCost);
            
            isAnnual = scnr.nextBoolean();
            colorOfFlowers = scnr.next();
            
            myFlower.setPlantType(isAnnual);
            myFlower.setColorOfFlowers(colorOfFlowers);
            
            myGarden.add(myFlower);
            
         }
         
         input = scnr.next();
         
      }
      
      printArrayList(myGarden);
      
   }
   
}


Add a comment
Know the answer?
Add Answer to:
LAB: Plant information (ArrayList)
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...

    10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions 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...

  • LAB: Pet information (derived classes)

    LAB: Pet information (derived classes)The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet class and includes a private field for dogBreed. Complete main() to:create a generic pet and print information using printInfo().create a Dog pet, use printInfo() to print information, and add a statement to print the dog's breed using the getBreed() method.Ex. If the input is:Dobby 2 Kreacher 3 German Schnauzerthe output is:Pet Information:     Name: Dobby    Age: 2 Pet Information:     Name: Kreacher    Age: 3    Breed: German SchnauzerPetInformation.javaimport java.util.Scanner; public class PetInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Pet myPet = new Pet();       Dog myDog = new Dog();              String petName, dogName, dogBreed;       int petAge, dogAge;       petName = scnr.nextLine();       petAge = scnr.nextInt();       scnr.nextLine();...

  • Original question: I need a program that simulates a flower pack with the traits listed below:...

    Original question: I need a program that simulates a flower pack with the traits listed below: - You must use a LinkedList for storage (not an Arrray list). - You must be able to display, add, remove, sort, filter, and analyze information from our flower pack. - The flower pack should hold flowers, weeds, fungus, and herbs. All should be a subclass of a plant class. - Each subclass shares certain qualities (ID, Name, and Color) – plant class -...

  • LAB: Book information (overriding member methods)

    10.15 LAB: Book information (overriding member methods)Given main() and a base Book class, define a derived class called Encyclopedia. Within the derived Encyclopedia class, define a printInfo() method that overrides the Book class' printInfo() method by printing not only the title, author, publisher, and publication date, but also the edition and number of volumes.Ex. If the input is:The Hobbit J. R. R. Tolkien George Allen & Unwin 21 September 1937 The Illustrated Encyclopedia of the Universe James W. Guthrie Watson-Guptill 2001 2nd 1the output is:Book Information:     Book Title: The Hobbit    Author: J. R. R. Tolkien    Publisher: George Allen & Unwin    Publication Date: 21 September 1937 Book Information:     Book Title: The Illustrated Encyclopedia of the Universe    Author: James W. Guthrie    Publisher: Watson-Guptill    Publication Date: 2001    Edition: 2nd    Number of Volumes: 1Note: Indentations use 3 spaces.BookInformation.javaimport java.util.Scanner; public class BookInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Book myBook = new Book();...

  • LAB: Instrument information (derived classes)

    10.14 LAB: Instrument information (derived classes)Given main() and the Instrument class, define a derived class, StringInstrument, for string instruments.Ex. If the input is:Drums Zildjian 2015 2500 Guitar Gibson 2002 1200 6 19the output is:Instrument Information:     Name: Drums    Manufacturer: Zildjian    Year built: 2015    Cost: 2500 Instrument Information:     Name: Guitar    Manufacturer: Gibson    Year built: 2002    Cost: 1200    Number of strings: 6    Number of frets: 19InstrumentInformation.javaimport java.util.Scanner; public class InstrumentInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Instrument myInstrument = new Instrument();       StringInstrument myStringInstrument = new StringInstrument();       String instrumentName, manufacturerName, stringInstrumentName, stringManufacturer;       int yearBuilt, cost, stringYearBuilt, stringCost, numStrings, numFrets;       instrumentName = scnr.nextLine();       manufacturerName = scnr.nextLine();       yearBuilt = scnr.nextInt();       scnr.nextLine();       cost = scnr.nextInt();       scnr.nextLine();       stringInstrumentName = scnr.nextLine();       stringManufacturer = scnr.nextLine();       stringYearBuilt = scnr.nextInt();       stringCost = scnr.nextInt();       numStrings = scnr.nextInt();       numFrets = scnr.nextInt();       myInstrument.setName(instrumentName);       myInstrument.setManufacturer(manufacturerName);       myInstrument.setYearBuilt(yearBuilt);       myInstrument.setCost(cost);       myInstrument.printInfo();       myStringInstrument.setName(stringInstrumentName);       myStringInstrument.setManufacturer(stringManufacturer);       myStringInstrument.setYearBuilt(stringYearBuilt);       myStringInstrument.setCost(stringCost);       myStringInstrument.setNumOfStrings(numStrings);       myStringInstrument.setNumOfFrets(numFrets);       myStringInstrument.printInfo();       System.out.println("   Number of strings: " + myStringInstrument.getNumOfStrings());       System.out.println("   Number of frets: " + myStringInstrument.getNumOfFrets());    } }Instrument.javapublic class Instrument {     protected String instrumentName;     protected String instrumentManufacturer;     protected int yearBuilt, cost;     public void setName(String userName) {...

  • After dinner with Alexander and Elizabeth we notice that Alexander is mixing some herbs as a...

    After dinner with Alexander and Elizabeth we notice that Alexander is mixing some herbs as a medication for Elizabeth which eases her pain and helps her sleep. Once the medicine is administered we begin small talk with Alexander. During the conversation he tells us the story about how his mother and father were hopelessly in love and were lucky enough to die side by side protecting the things they loved most, their children. In the middle of the story we...

  • Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input...

    Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following:  The instance variables for the class (recipeName, serving size, and...

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */    public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null;...

  • LAB: Zip code and population (generic types)

    13.5 LAB: Zip code and population (generic types)Define a class StatePair with two generic types (Type1 and Type2), a constructor, mutators, accessors, and a printInfo() method. Three ArrayLists have been pre-filled with StatePair data in main():ArrayList<StatePair> zipCodeState: Contains ZIP code/state abbreviation pairsArrayList<StatePair> abbrevState: Contains state abbreviation/state name pairsArrayList<StatePair> statePopulation: Contains state name/population pairsComplete main() to use an input ZIP code to retrieve the correct state abbreviation from the ArrayList zipCodeState. Then use the state abbreviation to retrieve the state name from the ArrayList abbrevState. Lastly,...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT