تم الحل ✓
categoryهندسة الحاسبات
schoolبكالوريوس
event_available2026-07-15
السؤال
Transcribed Image Text:
8.3 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
import java.util.Scanner;
Current file: PlantArrayListExample.java
Load default template...
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, color Of Flowers, is Annual
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
File is marked as read only
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);
Current file: Plant.java
File is marked as read only
public class Flower extends Plant {
}
private boolean isAnnual;
private String colorOfFlowers;
public void setPlantType(boolean userlsAnnual) {
isAnnual = userlsAnnual;
public boolean getPlantType(){
}
return is Annual;
Current file: Flower.java
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);
check_circle الجواب — حل مفصل خطوة بخطوة
hourglass_top
🔒
الحل الكامل متاح للمشتركين
اشترك في أرشيف الأسئلة لعرض هذا الحل وآلاف الحلول المفصلة خطوة بخطوة من معلمين معتمدين.