In: Computer Science
Write in drjava
Problem
Design and implement these 4 files:
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
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