Student Handout: Java Programming
Basic Java Methods
Java classes are built from properties and methods. This unit focuses on methods, specifically their naming conventions and the overall syntax used.
A class’s method is simply an action objects of that type can perform. In order to keep the code clear and concise so that you can more easily follow a program’s flow, it is best to write methods that perform a single task, nothing more.
Methods have a declaration, much like a variable, followed by the instructions or commands that actually perform the task. Method names in Java must meet a few requirements:
· Each name must start with a letter.
· The name should be a present-tense verb that matches the task performed.
· The first word in a method name should start with a lower-case letter, and the first letter of each subsequent word in the method name should be capitalized. The name should be followed by a pair of parentheses, which may be empty or may contain parameters.
The following are examples of method names that do not contain parameters:
showValues()
moveCarIntoGarage()
getChocolate()
Methods may invoke other methods. There are examples of this in the Song class.
We will also use two properties. Create a new project called Song. Now add a new class named Song. Open the Song class in the editor and enter the following code. Remember to compile your class as you go along.
/**
*
* Song.java creates several different methods.
* Each is used from one primary method.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Song
{
// declare and initialize properties
private String animal, noise;
public void showOldMcD()
{
// show the first part of the song
System.out.println ("Old McDonald had a farm, E I E I O.");
} // end of showOldMcD()
public void showAnimal()
{
// display part about the animal
System.out.print ("And on his farm he had a " + animal);
System.out.println ( ", E I E I O.");
} // end of showAnimal()
public void showNoise()
{
// display part about the animal’s noise
System.out.print ("With a " + noise + " ");
System.out.println ( noise + " here,");
} // end of showNoise()
public void showSong()
{
// set the properties for the first animal
animal = "cow";
noise = "mooooo";
// show each part of the song in order
showOldMcD();
showAnimal();
showNoise();
showOldMcD();
} // end of showSong()
} // end of Song
· Create a new instance of the Song class and test all of the methods
· Compile and test
· Enhance the showNoise() method to show more of that part of the nursery rhyme
· Enhance the showSong() method to display an entire verse of Old MacDonald for at least one animal
· Comment as needed, ensuring that each method includes a description of what it does
· Save, compile, and test often
EXTRAS YOU CAN ADD:
· Get creative and add new animals, sounds, and songs.
Answer the Following Questions:
|
Essential Question |
• What are methods?
|
|
Activity Questions |
• What is the naming convention for methods?
• What is a void return type?
• What happens if you run showNoise() before showSong()?
• What are the instance variables in the Song class?
• What roles do methods perform?
• Can methods invoke other methods?
• Where are the instance variables initialized?
|
Sun, Sun Microsystems, the Sun Logo are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. This Java activity was developed for The Sun Foundation by Boulder High School, 2004. 3