AP Comp Sci A
Chapter 3: User-Defined MethodsWriting Methods and Calling Them |
Introduction.
To be able to use or even write a method, you must know three things:
Void methods.
Void methods do NOT return a value. Their job is to do some type of work, but that work does not involve returning a value. It may involve just printing information or possibly calculating a value and then printing it or some other work that needs to be done like printing a row of stars for a flag using graphics drawing commands. Let's look at some examples:
Note: When the method is inside a console program that includes a main() method, it must have the word static added to its method signature.
Example 1. Write the method signature for the method printAddress that receives a string variable named address. The method does NOT return a value. Assume this method is public and available to other methods or classes.
public static void printAddress ( String address)
{
System.out.println("The address is :" + address);
}
Look at the method signature.
First, is the word public ... this makes the method usable by this class or other classes.
Second, is the word static ... this allows the method to be used in a driver file that has a main method.
Third, is the word void ... this is the return type. Void means nothing. So the method returns nothing.
Fourth, is the word printAddress. This is the name of the method.
Fifth, is the parameter list inside the ( ). There is one parameter named address and it is of type String.
Sixth, you see a left curly brace. This is an opening curly brace that marks the start of the lines of code in the method.
Seventh, you see one line of code in the "body" of the method. The body is all of the lines in the method.
Eighth, you see a right curly brace. This is a closing curly brace that marks the end of the lines of code in the method.
Notice there is no return statement inside the body of this method. You haven't seen one yet but you will below when you continue reading!!
Here is how you would call this method from main() or some other method:
Scanner reader = new Scanner (System.in);
System.out.print ("Enter the address: ");
String address = reader.nextLine();
printAddress (address); .... just the name of the method followed by the parameter the method needs!
Example 2 . Write the method signature for the method printStudentID that receives an integer variable named id. The method does NOT return a value. Assume this method is public and available to other methods or classes.
public static void printStudentID ( int id)
{
System.out.println("The student ID is :" + id);
}
Here is how you would call this method from main() or some other method:
Scanner reader = new Scanner (System.in);
System.out.print ("Enter the student id number: ");
int studentID = reader.nextInt();
printStudentID (studentID); .... just the name of the method followed by the parameter the method needs!
Note: the parameter in the method call (studentID) can be different from the parameter in the method signature (id). One is a nickname for the other.
You're probably asking yourself ... why create a method just to have one println statement? Well, the answer is we really wouldn't. What we're really trying to do is show you how to write a method in its simplest form.
Example 3. Write the method signature for the method printMilesPerHour that receives a floating point variable named hours and a floating point variable named distance . The method does NOT return a value. Assume this method is public and available to other methods or classes.
public static void printMilesPerHour ( double hours, double distance)
{
double mph = distance / hours;
System.out.println("The speed is :" + mph);
}
Note: there is a comma between the two parameters hours and distance!
Here is how you would call this method from main() or some other method:
Scanner reader = new Scanner (System.in);
System.out.print ("Enter the time: ");
double hrs = reader.nextDouble();
System.out.print ("Enter the distance: ");
double miles = reader.nextDouble();
printMilesPerHour (hrs, miles); .... just the name of the method followed by the parameter the method needs!
Example 4. Write the method signature for the method paintRocket that receives a Graphics variable named g . The method does NOT return a value. Assume this method is public and available to other methods or classes.
public static void paintRocket( Graphics g)
{
g.paintRect ...
g.paintOval ...
...
}
Here is how you would call this method from main() or some other method:
(Assume g is already declared somewhere in the program.)
paintRocket (g); .... just the name of the method followed by the parameter the method needs!
Methods that return a value.
Methods may return just about any type of value. Their job is to do some work, possibly a calculation, and then return a value. The value returned could be a primitive data value like a numeric value, character, or boolean or it could be an object type like String. Let's look at some examples:
Note: Again, when the method is inside a console program that includes a main() method, it must have the word static added to its method signature.
Example 5. Write the method signature for the method getName that receives no parameters . The method returns a String value. Assume this method is public and available to other methods or classes.
public static String getName ( )
{
Scanner reader = new Scanner (System.in);
System.out.println("Enter the name: ");
String name = reader.nextLine();
return name;
}
Notice there is a return statement inside the body of this method.
Here is how you would call this method from main() or some other method:
String nm = getName ();
For the last line of code above, since the method returns a value we need a variable on the left side of the assignment statement to catch the value returned by the method. Notice the method call on the right side of the assignment statement with just the name of the method followed by the parameters the method needs!
Example 6. Write the method signature for the method getAge that receives no parameters . The method returns an integer value. Assume this method is public and available to other methods or classes.
public static int getAge ( )
{
Scanner reader = new Scanner (System.in);
System.out.println("Enter the age: ");
int age = reader.nextInt();
return age;
}
Note that the data type of the variable age must match the type of data that is returned by the method.
Here is how you would call this method from main() or some other method:
int age = getAge ( );
For the last line of code above, we need a variable on the left of the assignment statement and the method call on the right side, with just the name of the method followed by the parameters the method needs!
Example 7. Write the method signature for the method getAverage that receives no parameters . The method returns a floating point value. Assume this method is public and available to other methods or classes.
public static double getAverage ( )
{
Scanner reader = new Scanner (System.in);
System.out.println("Enter the average: ");
double average = reader.nextDouble();
return average;
}
Here is how you would call this method from main() or some other method:
double ave = getAverage ( );
For the last line of code above, we need a variable on the left of the assignment statement and the method call on the right side, with just the name of the method followed by the parameters the method needs!
Example 8. Write the method signature for the method calculateCelsius that receives a floating point variable named fahrenheit. The method returns a floating point value. Assume this method is public and available to other methods or classes.
public static double calculateCelsius ( double fahrenheit)
{
double celsius;
celsius = (fahrenheit - 32) * 5.0 / 9.0;
return celsius;
}
Here is how you would call this method from main() or some other method:
Scanner reader = new Scanner (System.in);
System.out.print ("Enter the Fahrenheit temperature: ");
double fahr = reader.nextDouble();
double celsius = calculateCelsius (fahr);
For the last line of code above, we need a variable on the left of the assignment statement and the method call on the right side with just the name of the method followed by the parameters the method needs!