Sails (Child File)

Sail ships

It would have been very difficult to row across the water that separated Britain from the mainland, so the Saxons probably used a mast and sail as well as oars to propel their ships.

To add extra fields to your program you can create an additional class file. The calling file UseSaxonShip.java is the parent file and the two class files SaxonShip.java and SaxonShipTwo.java are considered to be child files.

Here is the code for the additional class file.

  1. import static java.lang.System.out;
  2. public class SaxonShipTwo {
  3. int sailsize;
  4. int mastheight;
  5. }

Here is the code for the calling class. Notice how the UseSaxonShip class communicates with the SaxonShip and the SaxonShipTwo classes. Four objects are created: annaSaxonShip and cenaSaxonShip as well as annaSaxonShipTwo and cenaSaxonShipTwo.

  1. Java codeimport static java.lang.System.out;
  2. public class UseSaxonShip {
  3. public static void main(String[] args) {
  4. /*
  5. * The code on the two lines after the comments reduce the prior four lines of code to just two.
  6. * SaxonShip annaSaxonShip
  7. * SaxonShip cenaSaxonShip
  8. * annaSaxonShip = new SaxonShip();
  9. * cenaSaxonShip = new SaxonShip();
  10. */
  11. //SaxonShip saves a space in memory for an object. annaSaxonShip = new SaxonShip actually creates the object.
  12. SaxonShip annaSaxonShip = new SaxonShip();
  13. SaxonShip cenaSaxonShip = new SaxonShip();
  14. //These lines add an additional field from the new child class named SaxonShipTwo.
  15. SaxonShipTwo annaSaxonShipTwo = new SaxonShipTwo();
  16. SaxonShipTwo cenaSaxonShipTwo = new SaxonShipTwo();
  17. annaSaxonShip.name = "Mystic Warrior";
  18. annaSaxonShip.hullwidth = 20;
  19. cenaSaxonShip.name = "Battle Hardened";
  20. cenaSaxonShip.hullwidth = 40;
  21. annaSaxonShipTwo.mastheight = 100;
  22. cenaSaxonShipTwo.mastheight = 150;
  23. out.println();
  24. out.println("Anna's ship named: " + annaSaxonShip.name);
  25. out.println("has a hull width of: " + annaSaxonShip.hullwidth);
  26. out.println("and a mast height of: " + annaSaxonShipTwo.mastheight);
  27. out.println();
  28. out.println("Cena's ship named: " + cenaSaxonShip.name);
  29. out.println("has a hull width of: " + cenaSaxonShip.hullwidth);
  30. out.println("and a mast height of: " + cenaSaxonShipTwo.mastheight);
  31. }
  32. }

Java code

Use Save As to save the code to your folder as: SaxonShipTwo.java. Then compile the code in the Command prompt window. Use javac SaxonShipTwo.java to create the .class file. Then use the same process to save the code of the UseSaxonShip.java file. Use javac UseSaxonShip.java to compile the parent file. Use java UseSaxonShip to run the program.

Your output should look like this:

c:\javamike>java UseSaxonShip

Anna's ship named: Mystic Warrior

has a hull width of: 20

and a mast height of: 100

 

Cena's ship named: Battle Hardened

has a hull width of : 40

and a mast height of: 150

 

< Anglo-Saxons (Class) Settlers (Method) >