Settlers (Methods)

Grass plains

Eventually the marauders, taken with the rich soil and natural resources of Britain, became settlers. Stronger militarily than the Romanized Britons it was possible for the invaders to push the Britons further and further west. As they pushed the native people out of their lands the Anglo-Saxons and Jutes became the new inhabitants of a large section of Britain.

The new settlers built carts to carry their farm goods to market. Therefore, you can make a class file to declare fields that are common to wagons.

Here is the code for a class that establishes fields for an ancient farmer's cart. The display code at the bottom of the file establishes a method. The method tells Java what to do if the method is called from the calling class.

  1. import static java.lang.System.out;
  2. public class SaxonCart {
  3. //The double variable type allows the system to
  4. //work with decimal numbers.
  5. String cartname;
  6. int wheelsize;
  7. double avgspeed;
  8. /* A method is a list of instructions
  9. * that you want the computer to process.
  10. * An advantage of an Object Oriented
  11. * programming language like Java is
  12. * that the defining class can perform
  13. * tasks using the data passed from the
  14. * calling class.
  15. */
  16. public void display() {
  17. out.println();
  18. out.println("Cartname: " + cartname);
  19. out.println("Wheel Size: " + wheelsize);
  20. out.println("Average Speed: " + avgspeed);
  21. }
  22. //The above display code is a method that can be
  23. //called by the calling class
  24. }

Java code

Here is the code for the calling class. Notice how the UseSaxonCart class communicates with the SaxonCart class. Two objects are created: annaSaxonCart and cenaSaxonCart. The annaSaxonCart.display(); and the cenaSaxonCart.display(); lines call the display method from the SaxonCart class.

 

  1. import static java.lang.System.out;
  2. public class UseSaxonCart {
  3. public static void main(String[] args) {
  4. //SaxonCart saves a space in memory for an object. annaSaxonCart = new SaxonCart actually creates the object.
  5. SaxonCart annaSaxonCart = new SaxonCart();
  6. SaxonCart cenaSaxonCart = new SaxonCart();
  7. annaSaxonCart.cartname = "Anna's Little Cart";
  8. annaSaxonCart.wheelsize = 3;
  9. annaSaxonCart.avgspeed = .4;
  10. cenaSaxonCart.cartname = "Cena's Big Monster Cart";
  11. cenaSaxonCart.wheelsize = 5;
  12. cenaSaxonCart.avgspeed = .6;
  13. annaSaxonCart.display();
  14. cenaSaxonCart.display();
  15. }
  16. }

Java code

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

Your output should look like this:

c:\javamike>java UseSaxonCart

Cartname: Anna's Little Cart

Wheel Size: 3

Average Speed: 0.4

Cartname: Cena's Big Monster Cart

Wheel Size: 5

Average Speed: 0.6

 

< Sails (Child File) Parameters >