Saxons (Class)

ocean scene

Even before the Roman armies left Britain, Germanic tribes of Anglo-Saxons and Jutes were raiding the Britons. These marauders had developed ocean going ships to cross the dangerous waters separating the British Isles from mainland Europe.

You always use a class declaration for your .java files. However, Java also uses classes to group common parts of objects. For example, Saxon ships all had names, hulls, and oar slots. Therefore, you can make a class that defines fields of a Saxon ship. The class that defines fields can not execute itself. So in this module you also create a calling class. When you execute the calling class Java looks to the file with the fields to create an object in the memory of the computer.

Here is the code for the class that establishes the Saxon ship fields.

  1. import static java.lang.System.out;
  2. public class SaxonShip {
  3. String name;
  4. int hullwidth;
  5. int hulllength;
  6. int oarslots;
  7. }


Java code

Here is the code for the calling class. Notice how the UseSaxonShip class communicates with the SaxonShip class. Two objects are created: annaSaxonShip and cenaSaxonShip.

  1. import static java.lang.System.out;
  2. public class UseSaxonShip {
  3. public static void main(String[] args) {
  4. SaxonShip annaSaxonShip;
  5. SaxonShip cenaSaxonShip;
  6. annaSaxonShip = new SaxonShip();
  7. cenaSaxonShip = new SaxonShip();
  8. annaSaxonShip.name = "Mystic Warrior";
  9. annaSaxonShip.hullwidth = 20;
  10. cenaSaxonShip.name = "Battle Hardened";
  11. cenaSaxonShip.hullwidth = 40;
  12. out.println();
  13. out.println("Anna's ship named: " + annaSaxonShip.name);
  14. out.println("has a hull width of: " + annaSaxonShip.hullwidth);
  15. out.println();
  16. out.println("Cena's ship named: " + cenaSaxonShip.name);
  17. out.println("has a hull width of: " + cenaSaxonShip.hullwidth);
  18. }
  19. }

Java code

Use Save As to save the code to your folder as: SaxonShip.java. Then compile the code in the Command prompt window. Use javac SaxonShip.java to create the .class file. Then use the same process to save the code of the UseSaxonShip.java 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

 

Cena's ship named: Battle Hardened

has a hull width of : 40

< Centuries (For) Sails (Child File) >