Question

Write in java and the class need to use give in the end copy it is...

Write in java and the class need to use give in the end copy it is fine.

Problem

  1. Use the Plant, Tree, Flower and Vegetable classes you have already created. Add an equals method to Plant, Tree and Flower only (see #2 below and the code at the end).
  2. The equals method in Plant will check the name. In Tree it will test name (use the parent equals), and the height. In Flower it will check name and color. In Vegetable it will check name by calling the method it inherits from Plant.
  3. Create an array of Plants containing at least 2 plants of each type (trees, flowers, vegetables). Using polymorphism display the details of each plant in the array.
  4. Use a cast to access two plants in the array to do sets and gets of at least 2 of (height, color, and taste). Show that the code works by producing suitable outputs.
  5. Finally have the Tree class implement Comparable, and code the compareTo(Object) method to test the height. Show that the compareTo method can generate each of the 3 possible outcomes. You’ll need code similar to the equals code below since the parameter is of type Object

Assignment Submission:

Submit a print-out of each class file, the driver source code and a sample of the output.

public abstract class Plant {

private String name;

private String lifespan;

public Plant(){
name = "no name";
lifespan = "do not know";
}
public Plant(String newName, String newlife)
{
name = newName;
lifespan = newlife;
}
  
public String getName()
{
return name;
}
  
public String getlife()
{
return lifespan;
}
  
public void setName(String newName)
{
name = newName;
}
  
public void setLifeSpan(String newlife)
{
lifespan = newlife;
}
  
public String toString()
{
return "Name:"+name+"\nLifeSpan:"+lifespan;
}
  
public abstract String getBotnicalName();

public abstract String plantuse();
}

public class Tree extends Plant{
int height;
private String botnicalName;
private String plantuse;


public Tree(){
super();
height = 0;

}


public Tree(int newheight,String name,String lifeSpan){
super(name,lifeSpan);
height=newheight;
}

public int getHeight() {
return height;
}

public void setHeight(int newheight) {
height = newheight;
}
  
public String getBotnicalName() {
return botnicalName;
}

public void setBotnicalName(String newbotnicalName) {
botnicalName = newbotnicalName;
}


public String plantuse() {
return plantuse;
}

public void setplantuse(String newplantuse) {
plantuse = newplantuse;
}

public String toString(){
return "\n"+super.toString()+"\nHeight:"+height + "\nplantuse:" + plantuse + "\nlatinname:" + botnicalName;
}

}

public class Flower extends Plant{
String color;
private String botnicalName;
private String plantuse;


public Flower(){
super();
color = "do not know";

}


public Flower(String newcolor,String name,String lifeSpan){
super(name,lifeSpan);
color=newcolor;
}
  

public String getColor() {
return color;
}
  

public void setColor(String newcolor) {
color = newcolor;
}
public String getBotnicalName() {
return botnicalName;
}
  

public void setBotnicalName(String newbotnicalName) {
botnicalName = newbotnicalName;
}


public String plantuse() {
return plantuse;
}

public void setplantuse(String newplantuse) {
plantuse = newplantuse;
}

public String toString(){
return "\n"+super.toString()+"\nColor:"+color+ "\nplantuse:" + plantuse + "\nlatinname:" + botnicalName;
}

}

public class Vegetable extends Plant {

private String flavor;
private String origin;
private String dishes;
private String botnicalName;
private String plantuse;

public Vegetable(){
super();
flavor = " null";
origin = "null";
dishes= "null";

}


public Vegetable(String newName, String newlife, String newflavor) {
super(newName, newlife);
flavor = newflavor;
}


public String getFlavor() {
return flavor;
}


public void setFlavor(String newflavor) {
flavor = newflavor;
}


public String getOrigin() {

return origin;
}

public void setDishes(String newdishes) {
dishes = newdishes;
}


public String getDishes() {
return dishes;
}

public void setOrigin(String neworigin) {
origin = neworigin;
}


public String getBotnicalName() {
return botnicalName;
}

public void setBotnicalName(String newbotnicalName) {
botnicalName = newbotnicalName;
}


public String plantuse() {
return plantuse;
}


public void setplantuse(String newplantuse) {
plantuse = newplantuse;
}


public String toString() {
return super.toString() + "\nBotnicalName: " + botnicalName + "\nOrigin: " + origin + "\nDishes: " + dishes
+ "\nUses: " + plantuse;
}

}

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

public abstract class Plant {

private String name;

private String lifespan;

public Plant(){
name = "no name";
lifespan = "do not know";
}
public Plant(String newName, String newlife)
{
name = newName;
lifespan = newlife;
}
  
public String getName()
{
return name;
}
  
public String getlife()
{
return lifespan;
}
  
public void setName(String newName)
{
name = newName;
}
  
public void setLifeSpan(String newlife)
{
lifespan = newlife;
}
  
public String toString()
{
return "Name:"+name+"\nLifeSpan:"+lifespan;
}
  
public abstract String getBotnicalName();

public abstract String plantuse();

public boolean equals(Object otherPlant) {
   if(! (otherPlant instanceof Plant))
       return false;
   return (name.equals(otherPlant.getName());
}
}
public class Tree extends Plant implements Comparable<Object>{
int height;
private String botnicalName;
private String plantuse;


public Tree(){
super();
height = 0;

}


public Tree(int newheight,String name,String lifeSpan){
super(name,lifeSpan);
height=newheight;
}

public int getHeight() {
return height;
}

public void setHeight(int newheight) {
height = newheight;
}
  
public String getBotnicalName() {
return botnicalName;
}

public void setBotnicalName(String newbotnicalName) {
botnicalName = newbotnicalName;
}


public String plantuse() {
return plantuse;
}

public void setplantuse(String newplantuse) {
plantuse = newplantuse;
}

public String toString(){
return "\n"+super.toString()+"\nHeight:"+height + "\nplantuse:" + plantuse + "\nlatinname:" + botnicalName;
}

public boolean equals(Object otherTree) {
   if( ! (otherTree instanceof Tree))
       return false;
   return ((super.equals(otherTree)) && (height == otherTree.getHeight()));
}

/* implementing compareTo function from Comparable interface */
public int compareTo(Object otherTree) throws NoSuchMethodException{
   if(! (otherTree instanceof Tree))
       throw new NoSuchMethodException();
   /* If heights match then return 0
   if this.height > otherTree.height, return positive value
   if this.height < otherTree.height, return negative value
   */
   return (height - otherTree.getHeight());
}

public class Flower extends Plant{
String color;
private String botnicalName;
private String plantuse;


public Flower(){
super();
color = "do not know";

}


public Flower(String newcolor,String name,String lifeSpan){
super(name,lifeSpan);
color=newcolor;
}
  

public String getColor() {
return color;
}
  

public void setColor(String newcolor) {
color = newcolor;
}
public String getBotnicalName() {
return botnicalName;
}
  

public void setBotnicalName(String newbotnicalName) {
botnicalName = newbotnicalName;
}


public String plantuse() {
return plantuse;
}

public void setplantuse(String newplantuse) {
plantuse = newplantuse;
}

public String toString(){
return "\n"+super.toString()+"\nColor:"+color+ "\nplantuse:" + plantuse + "\nlatinname:" + botnicalName;
}

public boolean equals(Object otherFlower) {
   if(! (otherFlower instanceof Flower))
       return false;
   return ((super.equals(otherFlower) && color.equals(otherFlower.getColor())));
}
}

public class Vegetable extends Plant {

private String flavor;
private String origin;
private String dishes;
private String botnicalName;
private String plantuse;

public Vegetable(){
super();
flavor = " null";
origin = "null";
dishes= "null";

}


public Vegetable(String newName, String newlife, String newflavor) {
super(newName, newlife);
flavor = newflavor;
}


public String getFlavor() {
return flavor;
}


public void setFlavor(String newflavor) {
flavor = newflavor;
}


public String getOrigin() {

return origin;
}

public void setDishes(String newdishes) {
dishes = newdishes;
}


public String getDishes() {
return dishes;
}

public void setOrigin(String neworigin) {
origin = neworigin;
}


public String getBotnicalName() {
return botnicalName;
}

public void setBotnicalName(String newbotnicalName) {
botnicalName = newbotnicalName;
}


public String plantuse() {
return plantuse;
}


public void setplantuse(String newplantuse) {
plantuse = newplantuse;
}


public String toString() {
return super.toString() + "\nBotnicalName: " + botnicalName + "\nOrigin: " + origin + "\nDishes: " + dishes
+ "\nUses: " + plantuse;
}
}


public class MainClass {
   public static void main(String[] args) {
       Plant[] myGarden = new Plant[6]; /* myGarden has 6 plants */
      
       myGarden[0] = new Flower("Red","Rose", 5);
       myGarden[1] = new Flower("White", "lily", 2);
       myGarden[2] = new Tree(20, "Oak", 55);
       myGarden[3] = new Tree(30, "Fir", 35);
       myGarden[4] = new Vegetable("Onion", 1, "tangy");
       myGarden[5] = new Vegetable("Eggplant", 3, "sour");

       /* Print details of all plants in myGarden */
       for(Plant myPlant : myGarden) {
           System.out.println(myPlant.toString());
       }
      
       /* Typecasting */
       Flower myFavFlower = (Flower)myGarden[0];   /* myFavFlower is the rose */
       Tree myFavTree = (Tree)myGarden[2];           /* myFavTree is the Oak Tree */
      
       /* calling the set methods for typecasted objects */
       myFavFlower.setColor("Pink");
       myFavTree.setHeight(25);
      
       /* calling the get methods for typecasted methods */
       System.out.println("My Rose is now " + myFavFlower.getColor() + " in color");
       System.out.println("My Oak tree is now " + myFavTree.getHeight() + " feet tall!");
      
       Tree lemonTree = new Tree(10, "Lemon", 5); /* Assuming my neighbor has a Lemon tree in their garden */      
       int difference = lemonTree.compareTo(myGarden[3]); /* comparing a lemon tree with my 100 foot tall Fir tree */
       if(difference > 0) {
           System.out.println("The lemon tree is Taller than my Fir tree!");
       }
       else if(difference < 0) {
           System.out.println("The lemon tree is Shorter than my Fir tree!");
       }
       else {
           System.out.println("The lemon tree is as tall as my Fir tree");
       }
       /* End of program */
   }
}

Add a comment
Know the answer?
Add Answer to:
Write in java and the class need to use give in the end copy it is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • How to solve this problem? Consider the following class : public class Store Public final double...

    How to solve this problem? Consider the following class : public class Store Public final double SALES TAX_RATE = 0.063B private String name; public Store(String newName) setName ( newName); public String getName () { return name; public void setName (String newName) { name = newName; public String toString() return "name: “ + name; Write a class encapsulating a web store, which inherits from Store. A web store has the following additional attributes: an Internet Address and the programming language in...

  • 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 -...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • JAVA PROGRAMMING***** Please provide screenshots of output and write a tester class to test. The ...

    JAVA PROGRAMMING***** Please provide screenshots of output and write a tester class to test. The starter code has been provided below, just needs to be altered. For this project you will implement the Memento Design Pattern. To continue on with the food theme, you will work with Ice Cream Cones. You will need to use AdvancedIceCreamCone.java. Create at least three Ice Cream Cones - one with chocolate ice cream, one with vanilla ice cream and one with strawberry ice cream....

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • Copy the program AmusementRide.java to your computer and add your own class that extends class AmusementRide....

    Copy the program AmusementRide.java to your computer and add your own class that extends class AmusementRide. Note: AmusementRide.java contains two classes (class FerrisWheel and class RollerCoaster) that provide examples for the class you must create. Your class must include the following. Implementations for all of the abstract methods defined in abstract class AmusementRide. At least one static class variable and at least one instance variable that are not defined in abstract class AmusementRide. Override the inherited repair() method following the...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

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
ADVERTISEMENT