-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrove.java
More file actions
87 lines (66 loc) · 2 KB
/
Grove.java
File metadata and controls
87 lines (66 loc) · 2 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
public class Grove {
public Tree[] spots;
public String groveName;
public Grove(String newGrove){
this.spots = new Tree[12];
this.groveName = newGrove;
// for(int i = 0; i < 12; i++){
// this.spots[i] = new Tree(null,null, null);
// }
}
public int PlantTree(Tree x){
// Having problem with creating a for loop to test for empty spots in the array
// My initial attempt would exit the loop on the first attemp because of the if else statement
// if(x != null){
// for(int i = 0; i < 12; i++){
// if(spots[i] != null){
// spots[i] = x;
// return i;
// }
// }
// }
// else{
// }
for(int i = 0; i < 12; i++){
if(spots[i] == null){
spots[i] = x;
return i;
}
}
return -1;
}
public Tree RemoveTree(int y){
if(spots[y] != null){
Tree removedTree = spots[y];
spots[y] = null;
return removedTree;
}
else{
return spots[y];
// Tree removedTree = null;
// return removedTree;
// Pretty sure these both do the same thing because if the spot is null then we can just return the
// the null Tree anyway
}
// if(i == y){
// Tree removedTree = spots[i];
// spots[i] = null;
// return removedTree;
// }
// else{
// Tree removedTree = null;
// return removedTree;
// }
// Overcomplicated way of doing the same thing^
}
public String toString(){
int x = 0;
for(int i = 0; i < 12; i++){
if(spots[i] != null){
x = x + 1;
}
}
String numberTrees = x + "";
return numberTrees;
}
}