October 2017 was one of the hottest October months on record for the City of Los
ID: 3711964 • Letter: O
Question
October 2017 was one of the hottest October months on record for the City of Los Angeles. We are going to create a Java console application to see just how hot it was. In order to accomplish this challenge, I would like you to create an array of 31 elements to store each of the day's high temperatures in LA during the month of October. Note that the daily temperatures below are reported as whole numbers, just as they are among all forecasts, so please create an integer array.
Your application should prompt the user to enter all 31 days (high temperatures only), then report the highest temperature recorded during the month, the "average high" temperature and the most frequent high temperature (e.g. the mode).
Please see the following for a sample transaction with a user:
Please enter daily high for October 01: 78
Please enter daily high for October 02: 79
Please enter daily high for October 03: 78
Please enter daily high for October 04: 79
Please enter daily high for October 05: 85
Please enter daily high for October 06: 92
Please enter daily high for October 07: 95
Please enter daily high for October 08: 82
Please enter daily high for October 09: 87
Please enter daily high for October 10: 88
Please enter daily high for October 11: 76
Please enter daily high for October 12: 77
Please enter daily high for October 13: 87
Please enter daily high for October 14: 95
Please enter daily high for October 15: 96
Please enter daily high for October 16: 95
Please enter daily high for October 17: 94
Please enter daily high for October 18: 86
Please enter daily high for October 19: 78
Please enter daily high for October 20: 75
Please enter daily high for October 21: 95
Please enter daily high for October 22: 93
Please enter daily high for October 23: 102
Please enter daily high for October 24: 104
Please enter daily high for October 25: 100
Please enter daily high for October 26: 93
Please enter daily high for October 27: 86
Please enter daily high for October 28: 80
Please enter daily high for October 29: 73
Please enter daily high for October 30: 69
Please enter daily high for October 31: 69
~~~~~~~~~~Temperature Statistics~~~~~~~~~~
October's highest daily temperature was: 104
October's average high temperature was: 86.0
October's most frequent high temp was: 95
Explanation / Answer
package January;
import java.util.Scanner;
// Class DailyTemperatures definition
public class DailyTemperatures
{
// To store temperature of 31 days
int temperature[] = new int[31];
/*int temperature[] = {78, 79, 78, 79, 85, 92, 95, 82, 87, 88, 76, 77, 87, 95, 96,
95, 94, 86, 78, 75, 95, 93, 102, 104, 100, 93, 86, 80, 73, 69, 69};*/
// Instance variable to store calculated result
int highestDailyTemperature;
double averageHighTemperature;
int temperatureFrequency[] = new int[31];
int mostFrequentHighTemperature;
// Method to calculate
void calculateTemperature()
{
// Initially zero index position of the temperature is considered as highest daily temperature
highestDailyTemperature = temperature[0];
// Initially zero index position of the temperature is assigned to total temperature
int total = temperature[0];
// Counter for daily frequency temperature
int temperatureFrequencyCounter;
// Loops from one to 31
// Starts from one because zero is considered above
for(int c = 1; c < 31; c++)
{
// Initialize the frequency counter to zero for each day temperature
temperatureFrequencyCounter = 0;
// Calculates the total temperature
total += temperature[c];
// Checks if the current index position temperature is greater than the earlier highest daily temperature
if(temperature[c] > highestDailyTemperature)
// Update the highest daily temperature by assigning current daily temperature
highestDailyTemperature = temperature[c];
// Calculates daily temperature frequency
// Loops from one to 31
// Starts from one because zero is considered above
for(int d = 1; d < 31; d++)
{
// Checks if the c - 1 index position temperature is equals to d index temperature
// Initially [c - 1] is zero, because c is taken as 1
if(temperature[c -1] == temperature[d])
// Increase the frequency counter by one
temperatureFrequencyCounter++;
}// End of inner for loop
// Assigns the temperature frequency at c - 1 index position of temperatureFrequency array
temperatureFrequency[c - 1] = temperatureFrequencyCounter;
}// End of outer for loop
// Calculates average temperature
averageHighTemperature = (double)total / 31;
// Initially zero index position of the temperatureFrequency is considered as most highest temperature frequency
int max = temperatureFrequency[0];
// Loops from one to 31
// Starts from one because zero is considered above
for(int c = 1; c < 31; c++)
{
// Checks if the current temperature frequency is greater than the earlier max
if(temperatureFrequency[c] > max)
{
// Update the max temperature by assigning temperatureFrequency current index position data
max = temperatureFrequency[c];
// Assigns the temperature current index position data as the most frequent highest temperature
mostFrequentHighTemperature = temperature[c];
}// End of if condition
}// End of for loop
}// End of method
// Method to accept temperature
void acceptTemperature()
{
// Scanner class object declared
Scanner sc = new Scanner(System.in);
// Loops for 31 times
for(int c = 0; c < 31; c++)
{
// Accepts the temperature
System.out.printf(" Please enter daily high for October %d : ", (c + 1));
temperature[c] = sc.nextInt();
}// End of for loop
// Close the scanner
sc.close();
}// End of method
// Method to display temperature information
void displayTemperatureInformation()
{
System.out.printf(" October's highest daily temperature was: %d", highestDailyTemperature);
System.out.printf(" October's average high temperature was: %.2f",averageHighTemperature);
System.out.printf(" October's most frequent high temp was: %d", mostFrequentHighTemperature);
}// End of for loop
// main method definition
public static void main(String[] args)
{
// Declares DailyTemperatures class object
DailyTemperatures dt = new DailyTemperatures();
// Calls the method to accept temperature data
dt.acceptTemperature();
// Calls the method to calculate temperature
dt.calculateTemperature();
// Calls the method to display temperature information
dt.displayTemperatureInformation();
}// End of main method
}// End of class
Sample Output:
Please enter daily high for October 1 : 78
Please enter daily high for October 2 : 79
Please enter daily high for October 3 : 78
Please enter daily high for October 4 : 79
Please enter daily high for October 5 : 85
Please enter daily high for October 6 : 92
Please enter daily high for October 7 : 95
Please enter daily high for October 8 : 82
Please enter daily high for October 9 : 87
Please enter daily high for October 10 : 88
Please enter daily high for October 11 : 76
Please enter daily high for October 12 : 77
Please enter daily high for October 13 : 87
Please enter daily high for October 14 : 95
Please enter daily high for October 15 : 96
Please enter daily high for October 16 : 95
Please enter daily high for October 17 : 94
Please enter daily high for October 18 : 86
Please enter daily high for October 19 : 78
Please enter daily high for October 20 : 75
Please enter daily high for October 21 : 95
Please enter daily high for October 22 : 93
Please enter daily high for October 23 : 102
Please enter daily high for October 24 : 104
Please enter daily high for October 25 : 100
Please enter daily high for October 26 : 93
Please enter daily high for October 27 : 86
Please enter daily high for October 28 : 80
Please enter daily high for October 29 : 73
Please enter daily high for October 30 : 69
Please enter daily high for October 31 : 69
October's highest daily temperature was: 104
October's average high temperature was: 86.00
October's most frequent high temp was: 95
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.