Week 8 lecture topics

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 :-

  1. static is used to prefix both class methods and attributes.
  2. Methods are generally (but not always) prefixed by public
  3. Attributes are generally (but not always) prefixed by private
  4. The name of a method is immediately preceeded by the type of reply
  5. Methods which do not make a reply are prefixed with void
  6. Method names are followed by parameter lists in parentheses ()
  7. Methods which dont need parameters still have the parentheses.
  8. Methods which have a non void reply type must include a return statement.
  9. Methods which have a void reply type must not include a return statement.
Points 2 and 3 will be explained later in the course. The use of parameters follows.

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

Ten Key Points
  1. Methods designated public can be called from outside the class
  2. Methods designated private can only be called from within the class
  3. All class methods are designated static
  4. The parameters referred to in the method definition are called formal parameters
  5. The data values supplied in calling a method are called actual parameters
  6. A class may contain many methods with the same name so long as the number and/or types of their parameters are different
  7. When several methods share the same name they are said to be overloaded
  8. A method which doesn't return a value is designated void
  9. A void method can only contain return; statements
  10. A non-void method must contain return <expression> statements