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...
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.
Problem 2 (C++): Design and implement a class complexType, that can be used to process complex...
Problem 2 (C++): Design and implement a class complexType, that can be used to process complex numbers. A number of the form a +ib, in which i2 = -1 and a and b are real numbers, is called a complex number. We call a the real part and b the imaginary part of a + ib. Complex numbers can also be represented as ordered pairs (a, b). The class you will design should have the following features. Constructor Your class...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module.
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module. Think of all the things you would want to do with such a class and write corresponding member functions for your Module class. Your class declaration should be well-documented so that users will know how to use it.Write a main program that does the following: Declare an array of all your modules. The elements of the array must be of type Module....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT