Norman Invasion (Constructors)
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.
- import static java.lang.System.out;
- // This is a class that uses a declared constructor instead of
- // the Java default constructor.
- class CastleConstructor {
- int number;
- String name;
- String castlelocation;
- double castlesqfeet;
- int num1;
- int num2;
- double num3;
- double num4;
- double totfeet;
- // The following is the constructor. Notice the name of
- // the constructor is the same as the name of the class.
- CastleConstructor(int number, String name, String location, double feet) {
- this.number = number;
- this.name = name;
- castlelocation = location;
- castlesqfeet = feet;
- }
- /*
- * Notice the "this" keyword in the above and below lines.
- * In line 21 you would not be able to code number = number because Java
- * would be unable to determine whether to use the class
- * variable or the parameter variable. When you use the
- * "this" keyword you specify that the first variable in the
- * variable declaration is the class variable.
- */
- CastleConstructor (int num1, int num2, double num3, double num4){
- this.num1 = num1;
- this.num2 = num2;
- this.num3 = num3;
- this.num4 = num4;
- totfeet = num1 + num2 + num3 + num4;
- }
- public void display() {
- out.println();
- out.println("Norman Castle: " + number);
- out.println("Castle Name: " + name);
- out.println("Castle Location: " + castlelocation);
- out.println("Square Feet of Castle = " + castlesqfeet);
- }
- public void display1() {
- out.println();
- out.println("The Total Square Feet of all Castles = " + totfeet);
- }
- }
Here is the calling code for the constructor class.
- import static java.lang.System.out;
- public class UseCastleConstructor {
- public static void main(String args[]) {
- // The CastleConstructors actually create the instances of objects.
- CastleConstructor t1 = new CastleConstructor( 12, "Richmond", "North", 125 );
- CastleConstructor t2 = new CastleConstructor( 20, "Tamworth", "West", 230 );
- CastleConstructor t3 = new CastleConstructor( 31, "Conchester", "East", 450.5 );
- CastleConstructor t4 = new CastleConstructor( 14, "Dover", "South", 199.3 );
- // This second constructor uses the same name, however, since
- // the parameter types are different, Java can determine
- // the correct constructor to use.
- CastleConstructor t5 = new CastleConstructor(125, 230, 450.5, 199.3);
- t1.display();
- t2.display();
- t3.display();
- t4.display();
- t5.display1();
- }
- }
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