Spaces:
Running
Running
UPPERCASEVALUES - for Java Constants
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/01B. All Cardinal Directions
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write two enum classes
|
| 2 |
+
|
| 3 |
+
CardinalDirection and IntermediateCardinalDirection
|
| 4 |
+
|
| 5 |
+
Cardinal directions are north, south, east, and west, and
|
| 6 |
+
the intermediate cardinal directions are northeast, southeast, southwest, and northwest.
|
| 7 |
+
|
| 8 |
+
Remember the correct naming convention!
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
import java.util.Random;
|
| 15 |
+
import java.util.ArrayList;
|
| 16 |
+
import java.util.Arrays;
|
| 17 |
+
import java.util.Collections;
|
| 18 |
+
import java.util.stream.Collectors;
|
| 19 |
+
|
| 20 |
+
public class Test {
|
| 21 |
+
public static void main(String[] args) {
|
| 22 |
+
final Random r = new Random();
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
System.out.println("Testing enum CardinalDirection...");
|
| 26 |
+
CardinalDirection[] a = CardinalDirection.values();
|
| 27 |
+
ArrayList<String> al = Arrays.stream(a).map(s -> s.toString()).collect(Collectors.toCollection(ArrayList::new));
|
| 28 |
+
Collections.sort(al);
|
| 29 |
+
al.stream().forEach(i -> System.out.println(i));
|
| 30 |
+
|
| 31 |
+
System.out.println("Testing enum IntermediateCardinalDirection...");
|
| 32 |
+
IntermediateCardinalDirection[] va = IntermediateCardinalDirection.values();
|
| 33 |
+
ArrayList<String> a2 = Arrays.stream(va).map(s -> s.toString()).collect(Collectors.toCollection(ArrayList::new));
|
| 34 |
+
Collections.sort(a2);
|
| 35 |
+
a2.stream().forEach(i -> System.out.println(i));
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
//add
|
| 41 |
+
enum CardinalDirection {
|
| 42 |
+
// north, south, east, west
|
| 43 |
+
NORTH, SOUTH, EAST, WEST
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
enum IntermediateCardinalDirection {
|
| 47 |
+
// northeast, southeast, southwest, northwest
|
| 48 |
+
NORTHEAST, SOUTHEAST, SOUTHWEST, NORTHWEST
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
Testing enum CardinalDirection...
|
| 54 |
+
EAST
|
| 55 |
+
NORTH
|
| 56 |
+
SOUTH
|
| 57 |
+
WEST
|
| 58 |
+
|
| 59 |
+
Testing enum IntermediateCardinalDirection...
|
| 60 |
+
NORTHEAST
|
| 61 |
+
NORTHWEST
|
| 62 |
+
SOUTHEAST
|
| 63 |
+
SOUTHWEST
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|