Programming Languages‎ > ‎Java‎ > ‎

03. Java Methods

Java Methods Syntax
  • Recall that Java methods are functions.  However, unlike C/C++, they must exist inside of a class.  Also, there are different types of methods that will be discussed later.  Here is the basic syntax of a Java method definition (remember, this MUST be inside of a class:
return-type method-id ( param-declarations){
    var-declarations // These are local variables
    //(may only be directly referenced inside
    // this method)
    statements
}

Syntax to CALL a method:
 

When a method is defined in the same class as the calling method (both instance or called method is static):

method-id ( actual-parameter-list

When the static method is defined in another class as the calling method (if NOT private):

Class-id.method-id( actual-parameter-list )

 When the instance method is defined in another class as the calling method:

object-var.method-id( actual-parameter-list )

the call must be inside a method (may be main)
a call to a void method must be a statement
a call to a method that returns a value should be inside of a larger expression (e.g., right-hand side of an assignment)
the actual parameter list is a list of expressions that will be evaluated and passed at the time of call during run-time.  Each actual parameter should be the same type as the parameters list in the method definition.

Note:  ALL PARAMETERS IN JAVA ARE CALL-BY-VALUE!  However, when an object or array is passed, only the reference to the object or array is actually passed.  Primitive types cannot be passed by reference.



Methods that Return a Value:

Methods that return a value (not void) must have at least one return statement.
The syntax of the return statement:
return expression; where the expression should be the same type as defined in the method definition.

/* Exercise 3.1
1. Write a static method to return a double (parameter) rounded to the nearest 100th. Use the Math class' round method. Put this method in a class called MyMath . Hints: multiply the double parameter by 100, round that, divide the rounded result by 100.
2. Write a method to print a number (double parameter), its square and its cube, all rounded to the 100th, calling the round100th method from #1, but put this method in another class called TryMyMath
  NOTE: To use separate classes, just add another class to the same Java project that has the MyMath class.
3. Write a Java application program (main) that calls the method written in #2, passing Math.E in main. Put main in the TryMyMath class.
*/
public class Exercise_3_1_TryMyMath
{
    public static void main(String[] args)
    {
        // Test Case
        Print_Num_Square_Cube(Math.E);
    }
   
    // Method to print a number (double parameter), its square and its cube, all rounded to the 100th
    public static void Print_Num_Square_Cube(double d_number)
    {
        System.out.println("Number  : " + Exercise_3_1_MyMath.Round100th(d_number));
        System.out.println("Number^2: " + Exercise_3_1_MyMath.Round100th(Math.pow(d_number, 2)));
        System.out.println("Number^3: " + Exercise_3_1_MyMath.Round100th(Math.pow(d_number, 3)));
    }
}

public class Exercise_3_1_MyMath
{
    // static method to return a double (parameter) rounded to the nearest 100th
    public static double Round100th(double d_round100th)
    {
        // rounded to the nearest 100th
        d_round100th = (Math.round(d_round100th * 100))/100.0;      // Hints: multiply the double parameter by 100, round that, divide the rounded result by 100
       
        // System.out.println(d_round100th);                        // Test print
        return d_round100th;
    }
}

/*
Program Output:
Number  : 2.72
Number^2: 7.39
Number^3: 20.09
*/


/* Exercise 3.2 Write a Java application program to read 3 ints from the user.  Call a method (you write) that returns the average (as a rounded int), and another method (you write) that returns the largest of the 3 ints.
    Be sure to allow for 2 or 3 of the numbers having the same value.
*/
import java.util.Scanner;

public class Exercise_3_2_Scanner
{
    static Scanner oScanner = new Scanner(System.in);

    public static void main(String[] args)
    {
        int i_num1, i_num2, i_num3;
       
        System.out.println("Enter three integer number");
        i_num1 = oScanner.nextInt();
        i_num2 = oScanner.nextInt();
        i_num3 = oScanner.nextInt();
       
        System.out.println("Average of your entered three numbers: " + GetAverageInt(i_num1, i_num2, i_num3));
        System.out.println("Largest of your entered three numbers: " + GetLargestInt(i_num1, i_num2, i_num3));
    }
   
    // method that returns the     average (as a rounded int)
    private static int GetAverageInt(int num1, int num2, int num3)
    {
        return (int) ((num1 + num2 + num3) / 3);
    }
   
    // method that returns the largest of the 3 ints
    private static int GetLargestInt(int num1, int num2, int num3)
    {
        int i_largestInt;
        i_largestInt = (num1 >= num2)? num1: num2;
        i_largestInt = (i_largestInt >= num3)? i_largestInt : num3;
       
        return i_largestInt;
    }
}



Comments