Find the minimum temperature and the time that temperature


You need a variable that will keep track of the maximum of the values processed so far. This variable must be initialized before the loop. There are two standard techniques for initialization: one is to initialize the variable to some value smaller than any possible value being processed; another is to initialize the variable to the first value processed. In either case, after the first value is processed the maximum variable should contain the first value. For the temperature program declare a variable maxTemp to hold the maximum temperature we need to Initialize it to -1000 (a value less than any legitimate temperature).

The maximum variable must be updated each time through the loop. This is done by comparing the maximum to the current value being processed. If the current value is larger, then the current value is the new maximum. So, in the temperature program, add an if statement inside the loop to compare the current temperature read in to maxTemp. If the current temperature is larger set maxTemp to that temperature. and : If the current temperature is NOT larger, DO NOTHING!

also need to Add code to print out the maximum after the loop.

want to keep track of the time (hour) the maximum temperature occurred. save the current value of the hour variable when update the maxTemp variable. This of course requires a new variable to store the time (hour) that the maximum occurs. Declare timeOfMax (type int) to keep track of the time (hour) the maximum temperature occurred. Modify if statment so that in addition to updating maxTemp you also save the value of hour in the timeOfMax variable. (WARNING: you are now doing TWO things when the if condition is TRUE.)

need to print out the time the maximum temperature occurred along with the maximum.

also need to add code to find the minimum temperature and the time that temperature occurs. The idea is the same as for the maximum. need a separate if when updating the minimum temperature variable (that is, don't add an else clause to the if that is already there).

// Temps.java

//

// This program reads in a sequence of hourly temperature

// readings (beginning with midnight) and prints the maximum

// temperature (along with the hour, on a 24-hour clock, it

// occurred) and the minimum temperature (along with the hour

// it occurred).

// **********************************************************


import java.util.Scanner;


public class Temps

{

//----------------------------------------------------

// Reads in a sequence of temperatures and finds the

// maximum and minimum read in.

//----------------------------------------------------

public static void main (String[] args)

{

final int HOURS_PER_DAY = 24;


int temp; // a temperature reading


Scanner scan = new Scanner(System.in);


// print program heading

System.out.println ();

System.out.println ("Temperature Readings for 24 Hour Period");

System.out.println ();


for (int hour = 0; hour < HOURS_PER_DAY; hour++)

{

System.out.print ("Enter the temperature reading at " + hour +

" hours: ");

temp = scan.nextInt();

}


// Print the results

}

}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Find the minimum temperature and the time that temperature
Reference No:- TGS02709779

Now Priced at $10 (50% Discount)

Recommended (97%)

Rated (4.9/5)