Menu
Topics Index
...
`

Print min max and average temperatures Using methods - Java Example Program


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.

© meritcampus 2019

All Rights Reserved.

Open In App