PayFees() is a fine method as methods go, but what do we do if we want to pay a smaller amount and still keep track of how much is owing, rather than pay all the fees at once?
The answer to this is a very important idea that allows us to generalize the idea of a method. Not only can we give the computer a set of instructions to follow in a method, but we can write one set of instructions and have something 'different' happen each time we call the method! We can do this by parameterizing the method; each time we call the method, we'll send a value to it, and what the method does will depend on the value. Let's illustrate this by changing the definition of our Student class to the following:
public class Student { public void reportFeesOwing() { System.out.println("Owing: " + feesOwing); } public void PayFees(double someamount) { feesOwing = feesOwing - someamount; } private String name; private int age; private String courses; private double feesOwing = 5000.00; private boolean scholarship; }
So how is this different? Look at the lines
public void PayFees(double someamount) { feesOwing = feesOwing - someamount; }
Now PayFees takes a parameter and that parameter is called someamount, and is of type double. Recalling that the '=' sign means 'write to memory', we have feesOwing decreased by someamount and written back to the memory location designated by feesOwing. This is what the method does, but how do we call such a method?
To call such a method we need to type in the name of the method as usual, but because we want to transmit a value to PayFees which 'magically' becomes someamount, we have to type in that value as well:
PayFees(1200.56);
What happens here? Because we have defined our method to take a parameter, the compiler knows to issue the appropriate instructions to associate the value 1200.56 with the variable name someamount. This association only lasts for the duration of the method however.
We can also pass in the value of a variable using the following construction:
double amountToPay = 1200.56; PayFees(amountToPay);
This accomplishes the same task as the previous construction - the amount owing will be decreased by 1200.56. In both cases, the value that is passed to the method is called the argument.