-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCellType.java
More file actions
59 lines (54 loc) · 2 KB
/
Copy pathCellType.java
File metadata and controls
59 lines (54 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
package com.gradescope.spampede;
/**
* The type of a board cell (wall, open, spam, head, body).
*
* <p>
* CellType is an enumeration (aka enum) rather than an class. Enumerations are
* used when we want behavior like a class (i.e. methods), but we want to define
* a fixed set of values that an enum can take on.
* </p>
*
* <p>
* In the declaration of an enum, Java requires that the constants be defined
* first, prior to any fields or methods. Each constant calls the constructor,
* possibly with some arguments. When there are fields and methods, the list of
* enum constants must end with a semicolon.
* </p>
*
* <p>
* Because an enum can only take on particular values, we want to prevent people
* from making additional objects of the type. Notice that the constructor is
* private. We get an error if we try to make the constructor public!
* </p>
*
* <p>
* Instead of creating this enum, we could have had BoardCell store a String
* (e.g. "*", "X", "H", "B", or " ") to keep track of the type. However, with
* this approach, if we accidentally set the type to be "M" or some other
* invalid String, we would not get a compile error! But, i we use this enum
* (i.e. CellType), Java prevents us from setting the type of a BoardCell to
* something invalid.
* </p>
*
* <p>
* STYLE NOTE: No other class except BoardCell needs to know that CellType
* exists. So it would be better to define this enum and set it as private
* inside of BoardCell. But since we are introducing enums for the first time,
* we thought that approach would be more confusing! Instead, as a compromise,
* we decided to limit the enum so that nobody outside the package can access
* it.
* </p>
*
* @author CS60 instructors
*/
enum CellType {
WALL("*"), OPEN(" "), SPAM("X"), HEAD("H"), BODY("B");
private final String displayChar;
private CellType(String inputChar) {
this.displayChar = inputChar;
}
/** Returns a String representing the CellType */
public String getDisplayChar() {
return this.displayChar;
}
}