Spring Semester 2005


Lecture Notes Fifteen: Recipes as Static Methods.

Here's a class like we have written many so far:

class One {
  public static void main(String[] args) {
    System.out.println("Hello. This is your program. How are you?"); 
  } 
}
Let's make a modification that we may have discussed briefly already:
class One {
  public static void main(String[] args) {
    System.out.println("Hello. This is your program. How are you?"); 
  } 

  public static void lineOne() {
    System.out.println("Eeny, meeny, miney mo,");  
  } 

  public static void lineTwo() {
    System.out.println("Catch a tiger by its toe, ");  
  } 

  public static void lineThree() {
    System.out.println("If he hollers let him go, ");  
  } 

} 
There is a lot of potential in this class.

Let's learn how to use it, and let's make it to use right away.

class One {
  public static void main(String[] args) {
    System.out.println("Hello. This is your program. How are you?");     
    System.out.println("-----------------------------------------"); 
    One.lineOne(); 
    One.lineTwo(); 
    One.lineThree(); 
    One.lineOne(); 
    System.out.println("-----------------------------------------"); 
  } 

  public static void lineOne() {
    System.out.println("Eeny, meeny, miney mo,");  
  } 

  public static void lineTwo() {
    System.out.println("Catch a tiger by its toe, ");  
  } 

  public static void lineThree() {
    System.out.println("If he hollers let him go, ");  
  } 

} 
We need to clarify the meaning of
One.lineTwo();
A useful summary would be this:

Now we need more practice.

Let's define another class with two useful recipes:

class One {
  public static void main(String[] args) {
    System.out.println("Hello. This is your program. How are you?"); 
  } 

  public static void lineOne() {
    System.out.println("Eeny, meeny, miney mo,");  
  } 

  public static void lineTwo() {
    System.out.println("Catch a tiger by its toe, ");  
  } 

  public static void lineThree() {
    System.out.println("If he hollers let him go, ");  
  } 

} 
Now, how would you write the second one?

There are two options (at least).

One of them would be to use a string tokenizer.

But that might count non-numbers as well.

Still, let's do it that way, for simplicity.

import java.util.*; 

class Two {
    public static void main(String[] args) {
	ConsoleReader c = new ConsoleReader(System.in); 
        System.out.print("Type something: "); 
        String line = c.readLine(); 
        Two.calculateSum(line); 
        Two.countElements(line); 
    }

    public static void calculateSum(String line) {
        StringTokenizer st = new StringTokenizer(line); 
        double sum = 0; 
        while (st.hasMoreTokens()) {
	    String token = st.nextToken(); 
	    double number = Double.parseDouble(token); 
	    sum = sum + number;
        }
        System.out.println("The sum is: " + sum); 
    }

    public static void countElements(String line) {
	StringTokenizer st = new StringTokenizer(line); 
	System.out.println("The number of elements is: " + st.countTokens()); 
    }
}
Run this program, everything looks good.

You might think you can immediately calculate the average.

But you're stuck. The two values are on the screen, not in the program.

The two values are out of our reach.

How could we keep them in the program?

We need to learn an additional concept.

A method can return a value.

To whom? To the caller, of course. (That's what, for example, Math.sqrt() does.)

What, then, is different in the next program?

(Find at least 9 differences with the previous one).

import java.util.*; 

class Two {
    public static void main(String[] args) {
	ConsoleReader c = new ConsoleReader(System.in); 
        System.out.print("Type something: "); 
        String line = c.readLine(); 
        double sum = Two.calculateSum(line); 
        double count = Two.countElements(line); 
        System.out.println("The average is: " + sum / count); 
        System.out.print("One more time, that value is: ");
        System.out.println(Two.calculateSum(line)/Two.countElements(line)); 
    }

    public static double calculateSum(String line) {
        StringTokenizer st = new StringTokenizer(line); 
        double sum = 0; 
        while (st.hasMoreTokens()) {
	    String token = st.nextToken(); 
	    double number = Double.parseDouble(token); 
	    sum = sum + number;
        }
        return sum; 
    }

    public static double countElements(String line) {
	StringTokenizer st = new StringTokenizer(line); 
	return st.countTokens(); 
    }
}
Now how do we calculate the standard deviation?

Functional decomposition of purposes.

Let's start planning:

import java.util.*; 

class Three {
    public static void main(String[] args) {
	ConsoleReader c = new ConsoleReader(System.in); 
        System.out.print("Type something: "); 
        String line = c.readLine(); 
        double b = Three.sumOfSquares(line); 
        double a = Math.pow(Two.calculateSum(line), 2); 
        double n = Three.countElements(line); 
	double stdDev = Math.sqrt((b - a / n)/(n - 1));

        System.out.println("The standard deviation is: " + stdDev); 

    }
    public static double calculateSum(String line) {
        StringTokenizer st = new StringTokenizer(line); 
        double sum = 0; 
        // ... 
        return sum; 
    }

    public static double countElements(String line) {
	StringTokenizer st = new StringTokenizer(line); 
	return st.countTokens(); 
    }

    public static double sumOfSquares(String line) {
        StringTokenizer st = new StringTokenizer(line); 
        double sum = 0; 
        // ... 
        return sum; 
    } 

}
So here's the final program with parts of it obscured for fun and practice:
import java.____.*; 

class _____ {
    public ______ void main(_____________) {
	_______Reader _ = new ConsoleReader(System.__); 
        System.out._____("Type something: "); 
        ______ line = c.readLine(); 
        double _ = Three.sumOfSquares(line); 
        ______ a = Math.___(Two.calculateSum(____), 2); 
        double n = Three._____________(line); 
	double stdDev = Math.____((b - a / _)/(n - 1));

        System.___.println("The standard deviation is: " + stdDev); 

    }
    public static double ____________(String line) {
        String_________ __ = new StringTokenizer(line); 
        double sum = 0; 
        while (st._____________()) {
	    String token = st.nextToken(); 
	    double number = Double.parseDouble(_____); 
	    sum = sum + ______;
        }
        __________; 
    }

    public static ______ countElements(______ line) {
	______Tokenizer st = new StringTokenizer(____); 
	return st.___________(); 
    }

    ______ static double sumOfSquares(___________) {
        StringTokenizer st = new StringTokenizer(line); 
        double _______; 
        while (__________________) {
	    String _____ = st.nextToken(); 
	    double number = Double.parseDouble(token); 
	    sum = sum + _______________;
	}
        ______ sum; 
    } 

}

This was an introduction to static methods using concepts discussed in Homework Five.


Last updated by Adrian German for A201/A597/I210 on Tue Mar 1 09:57:00 EST 2005