Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Now we are going to use the design pattern for collecting objects. We are going

ID: 3919054 • Letter: N

Question

Now we are going to use the design pattern for collecting objects. We are going to model a Orchard with trees. An Orchard uses an ArrayList to keep track of Tree objects. You will write both a Orchard class and a Tree class. A Tree has a type and a height. Provide a constructor that takes type and height, in that order. Provide getters and setters for the instance variables. This is the design pattern for managing properties of objects. An Orchard has a constructor that takes no parameters. Remember, it must initialize the instance variable. It has methods: add() - adds the specified Tree to the Orchard. tallest() - gets the type of the tallest Tree in the Orchard or null if the Orchard is empty. Initialize the tallest with the first element. contains() - determines if a Tree of the given type is in the Orchard. Returns true if a Tree of the given type is in the Orchard. Otherwise, false. treeList() - gets an ArrayList containing the types of all the Trees in the Orchard. Provide Javadoc for both classes. The tester class: import java.util.ArrayList; /** * Tester for Orchard */ public class OrchardTester { public static void main(String[] args) { Orchard trees = new Orchard(); trees.add(new Tree("lemon",12.5 )); trees.add(new Tree("apple", 20.0)); trees.add(new Tree("cherry", 13.5)); trees.add(new Tree("avocado", 35.0)); trees.add(new Tree("apricot", 17)); System.out.println(trees.treeList()); System.out.println("Expected: [lemon, apple, cherry, avocado, apricot]" ); System.out.println(trees.tallest()); System.out.println("Expected: avocado"); System.out.println(trees.contains("cherry")); System.out.println("Expected: true"); System.out.println(trees.contains("peach")); System.out.println("Expected: false"); trees = new Orchard(); trees.add(new Tree("lemon",12.5 )); trees.add(new Tree("apple", 20.0)); trees.add(new Tree("cherry", 13.5));; trees.add(new Tree("apricot", 17)); System.out.println(trees.treeList()); System.out.println("Expected: [lemon, apple, cherry, apricot]"); System.out.println(trees.tallest()); System.out.println("Expected: apple"); } } codecheck: http://www.codecheck.it/files/18032404228b3ahkjihrmhq3ahapi02afij

Explanation / Answer

Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

Tree.java
----------
public class Tree{
private String type;
private double height;
public Tree(String type, double height){
this.type = type;
this.height = height;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}


}

Orchard.java
------------
import java.util.ArrayList;

public class Orchard {
private ArrayList<Tree> trees;

public Orchard(){
trees = new ArrayList<Tree>();
}
public void add(Tree tree) {
trees.add(tree);
}

public ArrayList<String> treeList(){
ArrayList<String> list = new ArrayList<String>();
for(Tree t : trees)
list.add(t.getType());
return list;
}

public boolean contains(String type){
for(Tree t : trees){
if(t.getType().equals(type))
return true;
}

return false;
}


public String tallest(){
Tree tallest = null;

if(trees.size() > 0)
tallest = trees.get(0);

for(int i = 1; i < trees.size(); i++){
if(trees.get(i).getHeight() > tallest.getHeight())
tallest = trees.get(i);
}
if(tallest == null)
return null;
else
return tallest.getType();
}

}

output
-----
[lemon, apple, cherry, avocado, apricot]
Expected: [lemon, apple, cherry, avocado, apricot]
avocado
Expected: avocado
true
Expected: true
false
Expected: false
[lemon, apple, cherry, apricot]
Expected: [lemon, apple, cherry, apricot]
apple
Expected: apple

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote