Question

In: Computer Science

Write in drjava Problem Design and implement these 4 files: A parent class called Plant with...

Write in drjava

Problem

Design and implement these 4 files:

  1. A parent class called Plant with name (eg Rose, Douglas Fir) and lifespan (could be in days, weeks, months or years) attributes
  2. Tree inherits from Plant and adds a height attribute
  3. Flower inherits from Plant and adds a color attribute
  4. A driver file to test the 3 classes above.

The classes described in #1, 2 and 3 above should have the usual constructors (default and parameterized), get (accessor) and set (mutator) methods for each attribute, and a toString method

Child classes should call parent methods whenever possible to minimize code duplication.

The driver program must test all the methods in each of the classes. Include comments in your output to describe what you are testing, for example   System.out.println(“testing Plant toString, accessor and mutator”);. Print out some blank lines in the output to make it easier to read and understand what is being output.

Assignment Submission:

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

Marking Checklist

  1. Does EACH class have all the usual methods?
  2. Are all methods in EACH class tested, including child objects calling inherited parent methods?
  3. Does the child class call the parent’s constructor?
  4. Does the child class override the parent’s toString?
  5. Does the output produced have lots of comments explaining what is being output?
  6. Does each class, and the output, have blank lines and appropriate indenting to make them more readable?

Solutions

Expert Solution

class Plant{
    String name;
    String lifeSpan;

    //Default Constructor
    public Plant(){

    }

    //Parametrized Constructor
    public Plant(String name,String lifeSpan){
        this.name=name;
        this.lifeSpan=lifeSpan;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLifeSpan() {
        return lifeSpan;
    }

    public void setLifeSpan(String lifeSpan) {
        this.lifeSpan = lifeSpan;
    }

    public String toString(){
        return "\n\tName:"+name+"\n\tLifeSpan:"+lifeSpan;
    }
}

class Tree extends Plant{
    float height;

    //Default Constructor
    public Tree(){

    }

    //Parametrized Constructor
    public Tree(float height,String name,String lifeSpan){
        super(name,lifeSpan); //Super Class Constructor
        this.height=height;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    public String toString(){
        return "\n\t"+super.toString()+"\n\tHeight:"+height;
    }

}

class Flower extends Plant{
    String color;

    //Default Constructor
    public Flower(){

    }

    //Parametrized Constructor
    public Flower(String color,String name,String lifeSpan){
        super(name,lifeSpan); //Super Class Constructor
        this.color=color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String toString(){
        return "\n\t"+super.toString()+"\n\tColor:"+color;
    }
}


public class drjava{
    public static void main(String args[]){

        System.out.println("\n\nPlant DETAILS\n");
        Plant plant=new Plant();
        System.out.println("Testing Plant Setter and Getter and toString Methods");
        plant.setName("Rose");
        plant.setLifeSpan("2Years");
        System.out.println("Plant Name:"+plant.getName());
        System.out.println("Plant LifeSpan:"+plant.getLifeSpan());
        System.out.println("Plant toString:"+plant.toString());

        System.out.println("\n\nTREE DETAILS\n");
        Tree tree=new Tree();
        System.out.println("Testing Tree Setter and Getter and toString Methods");
        tree.setName(plant.getName());
        tree.setLifeSpan(plant.getLifeSpan());
        tree.setHeight(3.565f);
        System.out.println("Tree Name:"+tree.getName());
        System.out.println("Tree Height:"+tree.getHeight());
        System.out.println("Tree LifeSpan"+tree.getLifeSpan());
        System.out.println("Tree toString:"+tree.toString());

        System.out.println("\n\nFlower DETAILS\n");
        Flower flower=new Flower();
        System.out.println("Testing Flower Setter and Getter and toString Methods");
        flower.setName("Rose Flower");
        flower.setLifeSpan("2days");
        flower.setColor("Red");
        System.out.println("Flower Name:"+flower.getName());
        System.out.println("Flower Lifespan:"+flower.getLifeSpan());
        System.out.println("Flower Color:"+flower.getColor());
        System.out.println("Flower toString:\n"+flower.toString());
    }
}

output:

Plant DETAILS

Testing Plant Setter and Getter and toString Methods
Plant Name:Rose
Plant LifeSpan:2Years
Plant toString:
   Name:Rose
   LifeSpan:2Years


TREE DETAILS

Testing Tree Setter and Getter and toString Methods
Tree Name:Rose
Tree Height:3.565
Tree LifeSpan2Years
Tree toString:
  
   Name:Rose
   LifeSpan:2Years
   Height:3.565


Flower DETAILS

Testing Flower Setter and Getter and toString Methods
Flower Name:Rose Flower
Flower Lifespan:2days
Flower Color:Red
Flower toString:

  
   Name:Rose Flower
   LifeSpan:2days
   Color:Red


Related Solutions

Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the following instance variables: employee’s name reportID: this should be unique. The first reportID must have a value of 1000 and for each new reportID you should increment by 10. hourly wage Include a suitable collection of constructors, mutator methods, accessor methods, and toString method. Also, add methods to perform the following tasks: Compute yearly salary - both the gross pay and net pay Increase...
Design and implement a class called circle_location to keep track of the position of a single...
Design and implement a class called circle_location to keep track of the position of a single point that travels around a circle. An object of this class records the position of the point as an angle, measured in a clockwise direction from the top of the circle. Include these public member functions: • A default constructor to place the point at the top of the circle. • Another constructor to place the point at a specified position. • A function...
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give...
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give in the end of question) a method to return the botanical (Latin) name of the plant a method that describes how the plant is used by humans (as food, to build houses, etc) Add a Vegetable class with a flavor variable (sweet, salty, tart, etc) and 2 methods that return the following information: list 2 dishes (meals) that the vegetable can be used in...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program 2) Create a class called TestScore that has a constructor that accepts an array...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
Write the header and the implementation files (.h and .cpp separately) for a class called Course,...
Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic...
Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an...
Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an infix expression to a postfix expression. The PostFixCalculator class evaluates a postfix expression. This means that the expressions will have already been converted into correct postfix form. Write a main method that prompts the user to enter an expression in the infix form, converts it into postfix, displays the postfix expression as well as it's evaluation. For simplicity, use only these operators, + ,...
Write a program that will recognize and evaluate prefix expressions. First design and implement a class...
Write a program that will recognize and evaluate prefix expressions. First design and implement a class of prefix expressions. This class should contain methods to recognize and evaluate prefix expressions. This chapter discusses the algorithms you will need to implement these methods. Walls and Mirrors 3rd edition Chapter 6 Programming Practice 8. Programming Language: Java.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT