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 functions throughout the course so far.

 

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

public void findAverageWeight(String gender, int age, int weight)
{
	//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 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
  • 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 of 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
 * @date 11-1-6
 */
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 

In a class called HWJ7_MethodPractice, create these methods: All students do 3, advanced do 6.

  1. isEven that will take in an int as a parameter and will return true or false depending if its even.
    • to do this you will need to know about the % operator. You already know +-*/. % (Remainder or Mod) operator finds the remainder. So:
      • 16%3 would be 1 (if 16 is divided by 3 the remainder is 1)
      • 24%5 would be 4
      • 435%100 would be 35
      • 4%6 would be 4
  2. nextMultiple of 10 that will take in an int as a parameter and will return the next multiple of 10. (again use % remainder operator)
  3. toGrams that will take two doubles in pounds and ounces and return the number of grams
  4. daysInMonth that will take in an int (1-12) for the number of the month and return how many days it has in the month (this year)
  5. daysInMonth that will take in 2 ints (one for for the number of the month and one for the year) it will return an int and return how many days it has in that month.(for simplicity lets say every year divisible by 4 is a leap year)
  6. stringBackwards that will take in a string as a parameter and return the string reversed.