Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

--------------------------------------------------AppleTree.java-------------------------------------------------- public class AppleTree extends Tree { public AppleTree(String type, double height, int numberBranches) { super(type, height, numberBranches); } } ---------------------------------------------------PearTree.java--------------------------------------------------- public class PearTree extends

image text in transcribed

image text in transcribed

--------------------------------------------------AppleTree.java--------------------------------------------------

public class AppleTree extends Tree

{

public AppleTree(String type, double height, int numberBranches)

{

super(type, height, numberBranches);

}

}

---------------------------------------------------PearTree.java---------------------------------------------------

public class PearTree extends Tree

{

public PearTree(String type, double height, int numberBranches)

{

super(type, height, numberBranches);

}

}

------------------------------------------------------Tree.java------------------------------------------------------

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;

}

}

Overview: In this lab, you will create a generic class, and the type parameter of the generic class will be constrained. This builds on the Tree example from your previous assignment. A. The Tree class In the previous assignment, you used the Tree class and created a subclass AppleTree. The Tree,java source code is again provided here. If you don't have your AppleTree code from the assignment handy, you should either: begin the lab by quickly creating a couple of very simple subclasses of Tree, just to play around withfor this lab. For example, AppleTree and PearTree. It is okay if they just extend Tree and have a constructor and do nothing else. What matters here is just that we have an inheritance hierarchy to play around with. or, use the provided AppleTree and PearTree classes, which simply extend Tree and call the superclass constructor (they don't do anything else). B. The OrchardKeeper class You will now create a class called OrchardKeeper, which represents a person who tends and cares for an orchard of trees. The OrchardKeeper class should have the following properties It should be generic. You should define a type parameter T for the class that extends Tree. When we create an OrchardKeeper object, T will be instantiated with a specific subtype of Tree, e.g. AppleTree or PearTree. It should have a single instance field orchard, which is an ArrayList of the type T you defined above. This represents the trees Its constructor should take a single parameter, which is an ArrayList of type T, and the constructor should assign this ArrayList object to the instance field orchard. There should be a method addTree)that takes a single object of type T and adds it to the orchard. There should be a method printTrees() that loops over the trees in the orchard, and prints out the type and number of branches for each one. C. Tester class In the main) method of your tester class, you will need to create an ArrayList of one of your

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions