Menu
Topics Index
...
`


Methods - Importance >
Siva Nookala - 12 Apr 2016
Java supports creation of methods which help mainly in removing duplicate code, reducing code complexity and increasing its maintainability.

For example, the following program prints the max and min temperatures of a city in Celsius units. It gets the input temperatures in Fahrenheit units.
Print min max and average temperatures with out using methods
class PrintMinMaxAndAvgTemperatures
{
    public static void main(String arg[])
    {
        double min_temp_in_F = 75.86;
        double max_temp_in_F = 105.72;
        
        double avg_temp_in_F = ( min_temp_in_F + max_temp_in_F ) / 2.0;
        
        double min_temp_in_C = ( min_temp_in_F - 32.0 ) * 5.0 / 9.0;
        double max_temp_in_C = ( max_temp_in_F - 32.0 ) * 5.0 / 9.0;
        double avg_temp_in_C = ( avg_temp_in_F - 32.0 ) * 5.0 / 9.0;
        
        
        System.out.println("Min temp in F = " + min_temp_in_F + ", in C = " + min_temp_in_C);
        System.out.println("Max temp in F = " + max_temp_in_F + ", in C = " + max_temp_in_C);
        System.out.println("Avg temp in F = " + avg_temp_in_F + ", in C = " + avg_temp_in_C);
    
    }
}
OUTPUT

Min temp in F = 75.86, in C = 24.366666666666667
Max temp in F = 105.72, in C = 40.955555555555556
Avg temp in F = 90.78999999999999, in C = 32.661111111111104

DESCRIPTION

Here we are converting the temperatures in Fahrenheit to Celsius. We are doing this 3 times. The conversion expression is similar except for the variable name. Instead of duplicating the code multiple times, we could use a method and pass the variable as a parameter. The same program can be written as following.
Print min max and average temperatures Using methods
class PrintMinMaxAndAvgTemperatures
{
    public static void main(String s[])
    {
        double min_temp_in_F = 75.86;
        double max_temp_in_F = 105.72;
    
        double avg_temp_in_F = ( min_temp_in_F + max_temp_in_F ) / 2.0;
    
        double min_temp_in_C = convertToCelsius( min_temp_in_F );
        double max_temp_in_C = convertToCelsius( max_temp_in_F );
        double avg_temp_in_C = convertToCelsius( avg_temp_in_F );
    
        System.out.println("Min temp in F = " + min_temp_in_F + ", in C = " + min_temp_in_C);
        System.out.println("Max temp in F = " + max_temp_in_F + ", in C = " + max_temp_in_C);
        System.out.println("Avg temp in F = " + avg_temp_in_F + ", in C = " + avg_temp_in_C);
    
    }
    
    public static double convertToCelsius( double input )
    {
        double result = ( input - 32.0 ) * 5.0 / 9.0;
        return result;
    }
}
OUTPUT

Min temp in F = 75.86, in C = 24.366666666666667
Max temp in F = 105.72, in C = 40.955555555555556
Avg temp in F = 90.78999999999999, in C = 32.661111111111104

DESCRIPTION

Here we have created a method convertToCelsius and are passing the temperature to it. The temperature could be min_temp_in_F or max_temp_in_F or avg_temp_in_F. The variable input contains the passed temperature in Fahrenheit and it is converted to Celsius and assigned to variable result. The variable result is passed back using the return keyword.

THINGS TO TRY
  • Add one more input variable human_body_temp_in_F and assign it to 98.4. Convert this into Celsius using the convertToCelsiusmethod and print the converted temperature.
  • Create a new method average which takes min_temp_in_F, max_temp_in_F and returns the average of those two temperatures.

See Java Methods for more details about how to define methods.
Also note that here the main method is the calling method and convertToCelsius is the called method. The variables avg_temp_in_F, min_temp_in_F from the main method are not accessible in the called method convertToCelsius. The scope of the variables and how return value is sent back to the calling method is explained in Java Methods - Parameter Passing And Scope.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.