English Cathedrals (Enums)

Durham Castle

Christianity had arrived in Britain long before the Norman invasion. However, just like their penchant for building castles of stone, the Normans had a desire to build massive, stone cathedrals. Since they were made of stone, many of these cathedrals have survived to modern times and are still in use.

Java provides you with many variable types. Some examples are strings, ints, and doubles. But you can also create your own variable types using enums. You create enums to group things that have a common purpose. For example: enums can be used to group directions.

Here is the code to group directions into an enum.

  1. import static java.lang.System.out;
  2. public enum Direction {
  3. NORTH, SOUTH, EAST, WEST, NORTHEAST, SOUTHEAST, NORTHWEST, SOUTHWEST
  4. }

 

Here is code that creates a constructor that uses the variable type established by the enum class.

Java code

  1. import static java.lang.System.out;
  2. // The (Direction loc) variable declaration shown below uses
  3. // the enum established in the file Direction.java.
  4. class CathedralConstructor {
  5. String name;
  6. Direction loc;
  7. // (Direction location) in the parameter parenthesis () below
  8. // establishes that "location" will accept a value of type Direction.
  9. CathedralConstructor(String name, Direction location) {
  10. this.name = name;
  11. loc = location;
  12. }
  13. /*
  14. * Notice the "this" keyword in the above line.
  15. * When you use the "this" keyword you specify that
  16. * the first variable in the element is the class variable.
  17. */
  18. public void display() {
  19. out.println();
  20. out.println("Cathedral Name: " + name);
  21. out.println("Cathedral Location: " + loc);
  22. }
  23. }

 

Here is code that creates a calling class for the CathedralConstructor.

Java code

  1. import static java.lang.System.out;
  2. public class UseCathedralConstructor {
  3. public static void main(String[] args) {
  4. CathedralConstructor t1 = new CathedralConstructor("Durham Cathedral", Direction.NORTH);
  5. CathedralConstructor t2 = new CathedralConstructor("Petersborough Cathedral", Direction.NORTH);
  6. CathedralConstructor t3 = new CathedralConstructor("Canterbury Cathedral", Direction.SOUTHEAST);
  7. CathedralConstructor t4 = new CathedralConstructor("Gloucester Cathedral", Direction.WEST);
  8. t1.display();
  9. t2.display();
  10. t3.display();
  11. t4.display();
  12. }
  13. }

Java code

Use Save As to save the code files to your folder. Then compile the code files in the Command prompt window. Run the UseCathedralConstructor.java class.

Your output should look like this:

c:\javamike>java UseCathedralConstructor

Cathedral Name: Durham Cathedral

Cathedral Location: NORTH

 

Cathedral Name: Petersborough Cathedral

Cathedral Location: NORTH

 

Cathedral Name: Canterbury Cathedral

Cathedral Location: SOUTHEAST

 

Cathedral Name: Gloucester Cathedral

Cathedral Location: WEST

 

< Normans (Constructors) Windows (API Objects) >