Consider class Tree and Yard defined as follows: public class Tree {private floa
ID: 3801525 • Letter: C
Question
Consider class Tree and Yard defined as follows: public class Tree {private float width; private boolean dead;} public class Yard {private ArrayList trees;//... remaining code omitted ...} (a) Assuming that all get() methods are available, complete the following method in the Yard class so that it returns the widest tree (should work for any Yard): public Tree widestTree(){(b) Write a proper public method in the Yard class called cutableTrees() that returns an array list of Tree objects in that yard that may be cut down. A tree may be cut down if it is dead or under 0.3m in width.Explanation / Answer
// 1 A)
public Tree widestTree{
// Assuming negative width of a tree is not possible
float maxWidth = -1.0;
int maxTreeIndex = -1;
for(int i=0;i<trees.size();i+=1){
if(((trees.get(i)).getWidth()) > maxWidth){
maxWidth = (trees.get(i)).getWidth();
maxTreeIndex = i;
}
}
if(maxTreeIndex == -1)
return null;
else
return trees.get(maxTreeIndex);
}
// 1 b)
public ArrayList<Tree> cutableTrees(){
ArrayList<Tree> cutTrees = new ArrayList<Tree>();
for(int i=0;i<trees.size();i+=1){
if((((trees.get(i)).getWidth()) < 0.3) || ((trees.get(i)).getDead())){
cutTrees.add(trees.get(i));
}
}
return cutTrees;
}