Building Java Programs, 3rd edition

Lab: Static Methods

Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Today's lab

Goals for today:

Static methods

Recall the syntax for writing a static method. Methods are useful for representing a program's structure and capturing common code to avoid redundancy:

public static void name() {
    statements;
}

Example:

public static void song() {
    System.out.println("This is the song that never ends,");
    System.out.println("Yes, it goes on and on, my friends.");
}

Exercise : Spikey practice-it

Exercise : Lanterns practice-it

Exercise : Spell your name

Exercise : FightSong practice-it

Go, team, go!
You can do it.

Go, team, go!
You can do it.
You're the best,
In the West.
Go, team, go!
You can do it.

Go, team, go!
You can do it.
You're the best,
in the West.
Go, team, go!
You can do it.

Go, team, go!
You can do it.

The following program produces the output at left, but it has poor structure and redundancy. Download it and open it in jGRASP, then add at least two static methods.

continued on the next slide...

Exercise : FightSong, cont'd

Go, team, go!
You can do it.
Go, team, go! 
You can do it.
You're the best,
In the West.
Go, team, go!
You can do it.
Go, team, go! 
You can do it.
You're the best,
In the West.
Go, team, go!
You can do it.
Go, team, go!
You can do it.

Did you choose your methods well? Avoid the following pitfalls:

Compare your method choice with your neighbor's. Discuss any different decisions you made and ask a TA if you have questions about them.