Further examples of methods in Java
Here is another simple example of a class containing several methods and attributes :-
public class Password {
private static String key = "XXXXXX";
public static boolean checkValid(String word){
return ( key.equals(word) );
} //end checkValid
public static boolean changePass(String old, String new) {
if ( key.equals(old) ){
key = new;
return true;
} else
return false;
} //end changePass
public static void reset() {
key = "XXXXXX";
} //end reset
} // end class Password
This class could then be used in a main method as follows :-
public class Application {
public static void main(String [] args) {
Password.changePass("XXXXXX","CABLOG");
if (Password.checkValid("CABLOG"))
System.out.println("Password correct");
else
System.out.println("Password wrong"):
} //end main
} //end class Application
Looking at the above code, a number of observations can be made :-
Formal and actual parameters
When writing sets of instructions which you wish to put inside a procedure it is often required to generalize the procedure so that it can be used in a variety of ways.
For instance a procedure to draw a square is not much use if it can only draw the square in one position, of one size and one colour. It would be better to pass the information regarding colour, position and size into the procedure (method) and write it in such a way as to use this information.
The information is passed to the method through variables called parameters and these are given in a parameter list in the method header.
The following example illustrates their use :-
public class Interest {
public static double simple(double amount, int years, float rate) {
double result; //somewhere to place the result of calculation
result = amount * (1 + rate/100) * years;
return result;
} //end simple
public static double compound(double amount, int years, float rate) {
double result;
result = amount * Math.pow((1 + rate/100), years);
return result;
} //end compound
} //end class Interest
This would then be used in another program as follows :-
public class Application {
public static void main(String [] args) {
double simp,comp;
int period = 5;
simp = Interest.simple(1000.0, period, 15.0);
comp = Interest.compound(1000.0, period, 15.0);
System.out.println("Simple interest = " + simp);
System.out.println("Compound interest = " + comp);
} //end main
} //end class Application
Note the following points :-
There are two ways that information can be conveyed to methods - through parameters or through attributes. The previous example could have been designed as follows :-
public class Interest {
private static double principal;
private static int period;
public static setData(double amount, int years) {
principal = amount;
period = years; //stores the data in class attributes
} //end setData
public static double simple(float rate) {
double result; //somewhere to place the result of calculation
result = principal * (1 + rate/100) * period;
return result;
} //end simple
public static double compound(float rate) {
double result;
result = principal * Math.pow((1 + rate/100), period);
return result;
} //end compound
} //end class Interest
This would then be used in another program as follows :-
public class Application {
public static void main(String [] args) {
double simp,comp;
int period = 5;
Interest.setData(1000.0, period);
simp = Interest.simple(15.0);
comp = Interest.compound(15.0);
System.out.println("Simple interest = " + simp);
System.out.println("Compound interest = " + comp);
} //end main
} //end class Application