Question: TREE :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ public class Tree { // the type of Tree, e.g. Spruce, Cedar etc. private String type; // the height of the tree in

TREE :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Tree {
// the type of Tree, e.g. "Spruce", "Cedar" etc.
private String type;
// the height of the tree in feet
private double height;
// the number of branches
private int numberBranches;
// Constructor
public Tree(String type, double height, int numberBranches)
{
this.type = type;
this.height = height;
this.numberBranches = numberBranches;
}
// getters
public String getType()
{
return type;
}
public double getHeight()
{
return height;
}
public int getBranches()
{
return numberBranches;
}
// a method that prunes a specified number of branches.
// if the number to prune is higher than the actual number of branches,
// an error message is displayed.
public void pruneTree(int branchesToPrune)
{
if (numberBranches - branchesToPrune
{
System.out.println("Sorry, there aren't that many branches to prune.");
}
else
{
numberBranches = numberBranches - branchesToPrune;
System.out.println(numberBranches + " remaining");
}
}
// String representation of a Tree, using its height and type
public String toString()
{
return "This is a "+height+"-foot "+type+" tree";
}
// Two trees are equal if they have the same type, same height, and same number of branches;
public boolean equals(Object other)
{
boolean result = false;
if (other instanceof Tree)
{
Tree that = (Tree) other;
result = (type.equals(that.type) && height == that.height && numberBranches == that.numberBranches);
}
return result;
}
}
TREE TESTER :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class TreeTester {
public static void main(String[] args) {
/*
* Create a few Trees and AppleTrees
*/
Tree cedar = new Tree("Cedar", 35, 10);
Tree gravenstein = new AppleTree("Gravenstein", 10, 9, 30);
AppleTree jonagold = new AppleTree("Jonagold", 8.5, 6, 25);
AppleTree jg2 = new AppleTree("Jonagold", 8.5, 6, 25);
// Create a Tree array
Tree[] orchard = {cedar, gravenstein, jonagold, jg2};
/*
Display each tree, using its toString() method. Should display as:
This is a 35-foot Cedar tree
This is a 10-foot Gravenstein tree with 30 apples.
This is a 8.5-foot Jonagold tree with 25 apples.
This is a 8.5-foot Jonagold tree with 25 apples.
*/
for (Tree t: orchard)
{
System.out.println(t);
}
// Two AppleTrees should only be equal if they have
// the same type, height, number of branches and number of appples.
// should print true
System.out.println(jg2.equals(jonagold));
// should print false
System.out.println(jg2.equals(gravenstein));
// should print "5 apples remaining"
jg2.pickApples(20);
// should print "Sorry, we don't have that many apples to pick."
jg2.pickApples(10);
}
}
Overview: In this assignment you are given a class called Tree, and asked to create a subclass called AppleTree. You are also provided with a tester class to verify that your new class is correct. A. The Tree class You are provided with a class called Tree, representing some basic characteristics of trees. It has instance fields representing tree type, height, and number of branches. It contains getter methods for all of the instance fields, as well as a constructor. There is also a pruneTree) method to prune some number of branches. The equals) method returns true only if the two Trees have the same type, height and number of branches. The toString)0 method returns a String representing the Tree's type and height Before you continue the assignment, look closely at the code for the Tree class. B. Creating an AppleTree class You will create an Apple Tree class that must have the following properties: o inheritance: it is a subclass of Tree instance fields: it has an int instance field numberApples containing the number of apples getter methods: it has a getter method that returns the numberApples instance field constructor: its constructor should take a tree type, height, number of branches, and number of apples. The AppleTree constructor should call the superclass Tree constructor with the first three parameters, and directly set the numberApples with the fourth parameter o o o o pickApples): it should have a method with the following header, which either decreases numberApples by the specified amount, or else displays an error message that there are not enough apples left to pick that amount. public void pickApples (int applesToPick) o toString): its toString ) method should return a String representing the tree according to its type, height and number of apples, e.g "This is a 85-foot Jonagold tree with 25 apples." o equals): ts equals) method should return true when two AppleTrees have the same type, height, number of branches and number of apples. Within the AppleTree equals() method, you should call the superclass equals) method. comments: place appropriate comments throughout your AppleTree class. o name and student number: type your name and student number in a comment at the top of the AppleTree class o You have been provided with a Tester class. If you have implemented AppleTree correctly, the tester class main() method should not have any compilation errors or runtime errors. The outputs should be as indicated in the main) method comments. You should not need to change anything in the tester class. C. Create a UML diagram Use the drawing program of your choice (e.g. Google Docs, LibreOffice Draw, Inkscape, etc.) to draw a UML diagram showing the following elements: the instance fields, constructors and methods of the Tree class the instance fields, constructors and methods of the AppleTree class the relationship between Tree and AppleTree o o Submit your UML diagram in a standard image file format such as png, ipg or pdf Deliverables: Zip up your AppleTree.java source code and your UML image file in a zip file and submit via BBLearn. You will be penalized if you submit incorrectly
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
