-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrove.java
More file actions
52 lines (41 loc) · 1.19 KB
/
Grove.java
File metadata and controls
52 lines (41 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package homework1;
import java.util.ArrayList;
public class Grove{
// Instance variables
private ArrayList<Tree> trees;
private String groveName;
// Parameterized constructor
public Grove(String groveName) {
this.groveName = groveName;
this.trees = new ArrayList<>(12); // maximum size of grove is 12
}
// Plant a Tree in the first available spot
public int plantTree(Tree tree) {
if (trees.size() < 12) {
trees.add(tree);
return trees.size() - 1; // Index where the tree is planted
} else {
return -1; // No spots available
}
}
// Remove a Tree from a given spot
public Tree removeTree(int index) {
if (index >= 0 && index < trees.size()) {
return trees.remove(index);
} else {
return null; //Invalid index
}
}
// ToString method to print the number of Trees in the array
public String toString() {
return String.valueOf(trees.size());
}
// Getter method for grove name
public String getGroveName() {
return groveName;
}
// Getter method for number of trees in the grove
public int getNumberOfTrees() {
return trees.size();
}
}