email me at borlaj@portlandschools.org
notes previous (09/07/06) submit the dump links  
 

Methods

 

Methods or functions tell the program to do something. Just like in a math, y=2x+3; we set the x value as 7, y is going to be 17.

 

We have been looking at methods throughout the course so far.

 

For example, we have used a declaration of a method with

public void lifeLeft(String gender, int age)
{
	//blah
}

 

We have implemented a method with

g.drawString("Hello world!", x, 25)

 

Note, this method does not return anything.

 

Or if we had a String firstName

firstName.length();

firstName.charAt(5);

if (firstName.equals("Jeff"))...

 

Or in bluej, we call methods directly.

 

Methods are incredibly important in programming (just as in every day life).

 

Advantages to methods:

  • makes it easier to read/write code
  • makes the code more reusable
  • makes the program more object oriented (objects should do specific things)

 

Lets dissect a method

public static int convertToFahrenheit(int celsius)
{
    int fahrenheit = celsius * 9/5 + 32;
    return fahrenheit;
}

 

 

This is a very basic method, lets look at it:

  • public - who can access the method
  • static - we can call it directly from bluej, we dont need an instance of the class(we will get more in to this soon)
  • int - the type of variable that is return, if one is returned, (if no variable is returned, use void)
  • convertToFahrenheit - name of the method, just like drawString, or paint were names of methods
  • int celsius - the arguments, what the program takes in; so this program takes in an integer and it will be called celsius
  • return fahrenheit - this is the value (remember it supposed to be an integer) that is returned to the program that calls this method.

All methods have the form

public [or private] return_type nameofmethod (type of argument1 name of argument1, type of argument2 name of argument 2, so on for each argument)
{
    do something
    return something - optional
}

 

So a re-examination of our paint method

public void paint(Graphics g) 
{
    blah
}

 

What do we see?

  • public - it is accessible from outside the program
  • void - paint does not return any values
  • paint - which is the name of the method
  • Graphics g - which is the argument that the method takes in
  • blah - what is done

Declaring

methods

 

Methods need to be declared outside of other methods.

 


Using a method that is outside of your class

 

To use a method, you need to call it, on an object.

g.drawString("Hello world!", x, 25)

We used this method by calling it, and giving it the appropriate arguments.

 

Where drawOval is declared it might look like

public void drawOval(int x, int y, int width, int height)
{
	 //blah
}      


When you used it, you just said g.drawOval(15,30,25,25)

You did not re-declare 15 as int 15.

 

To call it:

  • Call it by its name and give the necessary arguments
    • ie: g.drawOval(15,30,25,25)
    • ie2:firstName.substring(2,3) or firstName.substring(3);
  • You need to take the return value if there is one.
    • String shortName=firstName.substring (4);

Using a method that is in your class

 

To use a method, that you have definited in your current class, you can just call its name, you dont need to say what object it is.

 

ie:

  • int fahr;
  • fahr = convertToFahrenheit (35)

 

To use your method, you just call it from any other method, for example see below.

 

	
		  /*
 * This class will automatically prompt the user for a temp in cels 
 * and call our method to convert it to fahr and then
 * display the result.
 * 
 * @author Jeff Borland
 */
import java.util.Scanner;
import java.io.*;  //this method is necessary for input output (hence the io)   
//This is a simple class that will make conversions and play with methods.
public class Conversions { 

    //this method asks for a temperature in celsius and returns it as Fahr
    public double convertToFahrenheit(double celsius)
    {
        double fahrenheit = celsius * 9/5 + 32;
        return fahrenheit;
    }

    public void testConversions()
    {
        Scanner scan=new Scanner(System.in);
        System.out.println("Type in a temperature in Celsius and I will convert it to Fahr");
        double celsius = scan.nextDouble();
        double fahr = convertToFahrenheit(celsius);
        System.out.println("The temperature in Fahrenheit is " + fahr);
    }
        
    
}  // end of conversion class