Spaces:
Running
Running
TurkuBasicOOPinJava / Week 7: Enum, Generic Type, Streams, write to file, class diagram /01B. All Cardinal Directions
| Write two enum classes | |
| CardinalDirection and IntermediateCardinalDirection | |
| Cardinal directions are north, south, east, and west, and | |
| the intermediate cardinal directions are northeast, southeast, southwest, and northwest. | |
| Remember the correct naming convention! | |
| import java.util.Random; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.Collections; | |
| import java.util.stream.Collectors; | |
| public class Test { | |
| public static void main(String[] args) { | |
| final Random r = new Random(); | |
| System.out.println("Testing enum CardinalDirection..."); | |
| CardinalDirection[] a = CardinalDirection.values(); | |
| ArrayList<String> al = Arrays.stream(a).map(s -> s.toString()).collect(Collectors.toCollection(ArrayList::new)); | |
| Collections.sort(al); | |
| al.stream().forEach(i -> System.out.println(i)); | |
| System.out.println("Testing enum IntermediateCardinalDirection..."); | |
| IntermediateCardinalDirection[] va = IntermediateCardinalDirection.values(); | |
| ArrayList<String> a2 = Arrays.stream(va).map(s -> s.toString()).collect(Collectors.toCollection(ArrayList::new)); | |
| Collections.sort(a2); | |
| a2.stream().forEach(i -> System.out.println(i)); | |
| } | |
| } | |
| //add | |
| enum CardinalDirection { | |
| // north, south, east, west | |
| NORTH, SOUTH, EAST, WEST | |
| } | |
| enum IntermediateCardinalDirection { | |
| // northeast, southeast, southwest, northwest | |
| NORTHEAST, SOUTHEAST, SOUTHWEST, NORTHWEST | |
| } | |
| Testing enum CardinalDirection... | |
| EAST | |
| NORTH | |
| SOUTH | |
| WEST | |
| Testing enum IntermediateCardinalDirection... | |
| NORTHEAST | |
| NORTHWEST | |
| SOUTHEAST | |
| SOUTHWEST | |