After a long and tedious journey of helping Alexander and Elizabeth we feel we’v
ID: 3856869 • Letter: A
Question
After a long and tedious journey of helping Alexander and Elizabeth we feel we’ve done our part in helping and plan to get on the road in the morning. Our bags were packed and set at the edge of the bed waiting for us to awake, but when we wake up our bags are missing! It seems thieves came in through the open window last night and stole our belongings. Before we can get back on the road we need to gather some money in order to purchase new equipment. To help raise money we have talked to a local shop owner and he mentioned that he liked Alexander’s flower pack, but would like it to hold more than just flowers. The new flower pack should hold plants which can be flowers, fungus or weed. • Create a plant class that has three child classes (flower, fungus and weed). o Each subclass shares certain qualities (ID and Name) The ID should be something the program creates such as a sequential number; the user should not be asked to indicate the ID o Flower traits include (Color, Thorns, and Smell) o Fungus traits include (Color and Poisonous) o Weed traits include (Color, Poisonous, Edible and Medicinal) • All items should be able to be displayed. • Be able to add, remove, search and filter these plants – by name. • For traits that are Yes or No such as “Is the Weed Poisonous?” – consider using Booleans. Don’t use a string for all traits. • Submit 5 files: Final.java, Flower.java, Fungus.java, Plant.java and Weed.java No sample code is provided the final.
Explanation / Answer
Plant.java
public class Plant
{
public static int next = 0;//static variable to increment and assign to id when instace of plant is made
//common variables for all kinds of plants
private int id ;
private String name;
public Plant(String name)
{
id = ++next; //incremented value of next is assigned to plant id
this.name = name;
}
public String toString()
{
return " ID : "+id +" Name : "+name;
}
}
Flower.java
public class Flower extends Plant
{
public String color;
public boolean thorns;
public String smell;
public Flower(String name,String color,boolean thorns,String smell)
{
super(name);//calling base class constructor
this.color = color;
this.thorns = thorns;
this.smell = smell;
}
public String toString()
{
System.out.println(" Flower : ");
return super.toString()+" Color : "+color+" Have Thorns : "+thorns+" Smell : "+smell;
}
}
Fungus.java
public class Fungus extends Plant
{
public String color;
public boolean poisonous;
public Fungus(String name,String color,boolean poisonous)
{
super(name);
this.color = color;
this.poisonous = poisonous;
}
public String toString()
{
System.out.println(" Fungus : ");
return super.toString()+" Color : "+color+" Is poisonous : "+poisonous;
}
}
Weed.java
public class Weed extends Plant
{
public String color;
public boolean poisonous;
public boolean edible;
public boolean medicinal;
public Weed(String name,String color,boolean poisonous,boolean edible,boolean medicinal)
{
super(name);
this.color = color;
this.poisonous = poisonous;
this.edible = edible;
this.medicinal = medicinal;
}
public String toString()
{
System.out.println(" Weed : ");
return super.toString() +" Color : "+color+" Is poisonous : "+poisonous+" Is edible : "+edible+" Is medicinal : "+medicinal;
}
}
Final.java
public class Final
{
public static void main (String[] args)
{
Plant fl = new Flower("Rose","red",true,"sweet");
Plant w = new Weed("Mint","green",false,true,true);
Plant f = new Fungus("f1","green",true);
System.out.println(fl);
System.out.println(w);
System.out.println(f);
}
}
Output:
Flower :
ID : 1 Name : Rose
Color : red Have Thorns : true Smell : sweet
Weed :
ID : 2 Name : Mint
Color : green Is poisonous : false Is edible : true Is medicinal : true
Fungus :
ID : 3 Name : f1
Color : green Is poisonous : true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.