Norman Invasion (Constructors)

chess pieces

So, Danes and Anglo-Saxons fought each other for centuries. A king with Viking ancestory would gain the throne and then an Anglo-Saxon would rise to kingship. All the while Britain was emerging as a nation of the English. However, in 1066 a Norman duke would cross the waters and conquer Britain. William the Conqueror began building castles in order to control the English with his Norman knights.

So far, you have been creating instances of objects using Java's default constructor. However, it is better programming to create your own constructors. A constructor is similar to a method, but does not have return code. Also, a constructor always takes the name of it's class. Java distinguishes one constructor from another constructor by the number and type of the constuctor's parameters.

Here is the code for a class that uses a constructor.

Java code

  1. import static java.lang.System.out;
  2. // This is a class that uses a declared constructor instead of
  3. // the Java default constructor.
  4. class CastleConstructor {
  5. int number;
  6. String name;
  7. String castlelocation;
  8. double castlesqfeet;
  9. int num1;
  10. int num2;
  11. double num3;
  12. double num4;
  13. double totfeet;
  14. // The following is the constructor. Notice the name of
  15. // the constructor is the same as the name of the class.
  16. CastleConstructor(int number, String name, String location, double feet) {
  17. this.number = number;
  18. this.name = name;
  19. castlelocation = location;
  20. castlesqfeet = feet;
  21. }
  22. /*
  23. * Notice the "this" keyword in the above and below lines.
  24. * In line 21 you would not be able to code number = number because Java
  25. * would be unable to determine whether to use the class
  26. * variable or the parameter variable. When you use the
  27. * "this" keyword you specify that the first variable in the
  28. * variable declaration is the class variable.
  29. */
  30. CastleConstructor (int num1, int num2, double num3, double num4){
  31. this.num1 = num1;
  32. this.num2 = num2;
  33. this.num3 = num3;
  34. this.num4 = num4;
  35. totfeet = num1 + num2 + num3 + num4;
  36. }
  37. public void display() {
  38. out.println();
  39. out.println("Norman Castle: " + number);
  40. out.println("Castle Name: " + name);
  41. out.println("Castle Location: " + castlelocation);
  42. out.println("Square Feet of Castle = " + castlesqfeet);
  43. }
  44. public void display1() {
  45. out.println();
  46. out.println("The Total Square Feet of all Castles = " + totfeet);
  47. }
  48. }

Here is the calling code for the constructor class.

Java code

  1. import static java.lang.System.out;
  2. public class UseCastleConstructor {
  3. public static void main(String args[]) {
  4. // The CastleConstructors actually create the instances of objects.
  5. CastleConstructor t1 = new CastleConstructor( 12, "Richmond", "North", 125 );
  6. CastleConstructor t2 = new CastleConstructor( 20, "Tamworth", "West", 230 );
  7. CastleConstructor t3 = new CastleConstructor( 31, "Conchester", "East", 450.5 );
  8. CastleConstructor t4 = new CastleConstructor( 14, "Dover", "South", 199.3 );
  9. // This second constructor uses the same name, however, since
  10. // the parameter types are different, Java can determine
  11. // the correct constructor to use.
  12. CastleConstructor t5 = new CastleConstructor(125, 230, 450.5, 199.3);
  13. t1.display();
  14. t2.display();
  15. t3.display();
  16. t4.display();
  17. t5.display1();
  18. }
  19. }

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

Your output should look like this:

c:\javamike>java UseCastleConstructor

Norman Castle: 12

Castle Name: Richmond

Castle Location: North

Square Feet of Castle = 125.0

 

Norman Castle: 20

Castle Name: Tamworth

Castle Location: West

Square Feet of Castle = 230.0

 

Norman Castle: 31

Castle Name: Conchester

Castle Location: East

Square Feet of Castle = 450.5

 

Norman Castle: 14

Castle Name: Dover

Castle Location: South

Square Feet of Castle = 199.3

 

The total square feet of all Castles = 1004.8

 

< Viking Settlers (Subclasses) Cathedrals (Enums) >