Parameters

Hadrian's wall

Methods can pass parameters back and forth from their classes.

Here is code that creates a method. Within the method a parameter is created and then passed between the defining class and the calling class.

  1. import static java.lang.System.out;
  2. public class SaxonCartParameter {
  3. //The double variable type allows the system to
  4. //work with decimal numbers.
  5. String cartname;
  6. int wheelsize;
  7. double avgspeed;
  8. /*
  9. * A method is a list of instructions
  10. * that you want the computer to process.
  11. */
  12. public double getCurrentSpeed(double rpmIncrease) {
  13. return avgspeed * rpmIncrease;
  14. }
  15. /*
  16. * The above method establishes a parameter of rpmIncrease
  17. * with a type of double.
  18. * The method expects to receive a variable value = to rpmIncrease
  19. * The result of the return calculation can be called as the method
  20. * name - getCurrentSpeed.
  21. */
  22. public void display() {
  23. out.println();
  24. out.println("Cartname: " + cartname);
  25. out.println("Wheel Size: " + wheelsize);
  26. out.println("Average Speed: " + avgspeed);
  27. }
  28. }

Java code

Here is the code for the calling class. Notice how the UseSaxonCartParameter class communicates with the SaxonCartParameter class. Two objects are created: annaSaxonCartParameter and cenaSaxonCartParameter. The annaSaxonCartParameter.display(); and the cenaSaxonCartParameter.display(); lines call the display method from the SaxonCartParameter class. In addition the new getCurrentSpeed() method establishes a double variable request parameter named rpmIncrease, does a calculation on the value, and prepares to return that value to the calling class.

  1. import static java.lang.System.out;
  2. public class UseSaxonCartParameter {
  3. public static void main(String[] args) {
  4. //SaxonCart saves a space in memory for an object. annaSaxonChip = new SaxonCart actually creates the object.
  5. SaxonCartParameter annaSaxonCartParameter = new SaxonCartParameter();
  6. SaxonCartParameter cenaSaxonCartParameter = new SaxonCartParameter();
  7. annaSaxonCartParameter.cartname = "Anna's Little Cart";
  8. annaSaxonCartParameter.wheelsize = 3;
  9. annaSaxonCartParameter.avgspeed = .4;
  10. cenaSaxonCartParameter.cartname = "Cena's Big Monster Cart";
  11. cenaSaxonCartParameter.wheelsize = 5;
  12. cenaSaxonCartParameter.avgspeed = .6;
  13. annaSaxonCartParameter.display();
  14. double annacurrentspeed = annaSaxonCartParameter.getCurrentSpeed(5.8);
  15. out.println("Anna's Little Cart's wheels are currently rotating at 5.8 times per minute.");
  16. out.println("Anna's Little Cart has a current speed in miles per hour of: " + annacurrentspeed);
  17. cenaSaxonCartParameter.display();
  18. out.println("Cena's Big Monster Cart's wheels are currently rotating at 9.2 times per minute.");
  19. out.println("Cena's Big Monster Cart has a current speed in miles per hour of: " + cenaSaxonCartParameter.getCurrentSpeed(9.2));
  20. }
  21. }

Java code

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

Your output should look like this:

c:\javamike>java UseSaxonCartParameter

Cartname: Anna's Little Cart

Wheel Size: 3

Average Speed: 0.4

Anna's Little Cart's wheels are currently rotating at 5.8 times per minute.

Anna's Little Cart has a current speed in miles per hour of: 2.32

 

Cartname: Cena's Big Monster Cart

Wheel Size: 5

Average Speed: 0.6

Cena's Big Monster Cart's wheels are currently rotating at 9.2 times per minute.

Cena's Big Monster Cart has a current speed in miles per hour of: 5.52

 

< Settlers (Methods) Vikings (Accessors) >