Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Midterm Exam; Unite 2 programming problem 3- Temperature; Write a program that u

ID: 3590241 • Letter: M

Question

Midterm Exam;

Unite 2 programming problem 3- Temperature;

Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the calendar year. The temperatures will be entered at the keyboard. This program must output the average high, average low, and highest and lowest temperatures of the year. The results will be printed on the console.

Write a complete method (including method header and body) names inputTempForMonth whose purpose temperature for a specific month. the month and the array of temperature will both be passed as input args method will not have a return value.

Explanation / Answer

Temperature.java

import java.util.Scanner;
public class Temperatures {
public static Scanner keyboard = new Scanner(System.in);
private static int highTemperature, lowTemperature,averageHigh, averageLow;
private static int index;//keeps track of months
private static int indexOfHighestTemp=0, indexOfLowestTemp=0;
private static int[][] highAndLowTemps = new int [12][2];//array for highs and lows
private static String[] months = new String[12];//array of monthss
public static void main(String[] args) {
inputTempForYear();
calculateAverageHigh(highAndLowTemps);
calculateAverageLow(highAndLowTemps);
findHighestTemp(highAndLowTemps);
findLowestTemp(highAndLowTemps);
//outputs results
System.out.println("Average High: "+averageHigh);
System.out.println("Average Low: "+averageLow);
System.out.println("Highest Temp and Month: "+highAndLowTemps[indexOfHighestTemp][0]+" "+months[indexOfHighestTemp]);
System.out.println("Lowest Temp and Month: "+highAndLowTemps[indexOfLowestTemp][1]+" "+months[indexOfLowestTemp]);
}
private static void inputTempForMonth(int[][] highAndLowTemps)
{
System.out.println("Input the high temperature for "+months[index]+":");
highTemperature = keyboard.nextInt();//inputs months high temp
highAndLowTemps[index][0]=highTemperature;
System.out.println("Input the low temperature for "+months[index]+":");
lowTemperature = keyboard.nextInt();//inputs months low temp
highAndLowTemps[index][1]=lowTemperature;
}
private static int[][] inputTempForYear()
{
months[0]="January";
months[1]="Febuary";
months[2]="March";
months[3]="April";
months[4]="May";
months[5]="June";
months[6]="July";
months[7]="August";
months[8]="September";
months[9]="October";
months[10]="November";
months[11]="December";//fills month array
for (index=0;index<=11;index++)//fills array with highs and lows
{
inputTempForMonth(highAndLowTemps);
}
return highAndLowTemps;
}
private static int calculateAverageHigh(int[][] highAndLowTemps)
{
for(int i=0;i<=11;i++)//finds sum of high temps
{
averageHigh=averageHigh+highAndLowTemps[i][0];
}
averageHigh/=12;//calculates average
return averageHigh;
}
private static int calculateAverageLow(int[][] highAndLowTemps)
{
for(int i=0;i<=11;i++)//finds sum of low temps
{
averageLow=averageLow+highAndLowTemps[i][1];
}
averageLow/=12;//calculates average
return averageLow;
}
private static int findHighestTemp(int[][] highAndLowTemps)
{
double max=highAndLowTemps[0][0];
int indexHigh;//index for highest
for(indexHigh=0;indexHigh<11;indexHigh++)//find highest high temp
{
if(highAndLowTemps[indexHigh][0]>max)
{
max=highAndLowTemps[indexHigh][0];
indexOfHighestTemp=indexHigh;
}
}
return indexOfHighestTemp;
}
private static int findLowestTemp(int[][] highAndLowTemps)
{
double min=highAndLowTemps[0][1];
int indexLow;//index for lowest
for(indexLow=0;indexLow<11;indexLow++)//finds lowest low temp
{
if(highAndLowTemps[indexLow][1]<min)
{
min=highAndLowTemps[indexLow][1];
indexOfLowestTemp=indexLow;
}
}
return indexOfLowestTemp;
}
}


// Run Tem.java file
Temp.java

import java.util.Scanner;
public class Temp {
private Scanner keyboard; // declaring variables
private int[][] temperatures;
public static final String[] MONTHS = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
public Temp() {
keyboard = new Scanner(System.in); //intialize variables
temperatures = new int[12][2];
inputTempForYear(); // prints temps
System.out.println("Average low: " + calculateAverageLow());
System.out.println("Average high: " + calculateAverageHigh());
System.out.println("Month with lowest temperature is " + MONTHS[findLowestTemp()]);
System.out.println("Month with highest temperature is " + MONTHS[findHighestTemp()]);
}
public void inputTempForYear() { //allows input for temp for the year
for (int i=0; i<12; i++)
inputTempForMonth(i);
}
public void inputTempForMonth(int month) { // allows input for temp for the month
System.out.println("Please enter the low temperature for " + MONTHS[month]);
temperatures[month][1] = keyboard.nextInt();
System.out.println("Please enter the high temperature for " + MONTHS[month]);
temperatures[month][0] = keyboard.nextInt();
}
public float calculateAverageLow() { // calculate avg low temp
float sum = 0;
for (int i=0; i<temperatures.length; i++)
sum += temperatures[i][1];
return sum /= temperatures.length;
}
public float calculateAverageHigh() { // calculate avg high temp
float sum = 0;
for (int i=0; i<temperatures.length; i++)
sum += temperatures[i][0];
return sum /= temperatures.length;
}
public int findLowestTemp() { //looks for lowest temp
int min = 0;
int indexLow = 0;
for (int i=0; i<temperatures.length; i++)
if (temperatures[i][1] < min) {
min = temperatures[i][1];
indexLow = 0;
}
return indexLow;
}
public int findHighestTemp() { // looks for highest temp
int max = 0;
int indexHigh = 0;
for (int i=0; i<temperatures.length; i++)
if (temperatures[i][0] > max) {
max = temperatures[i][0];
indexHigh = i;
}
return indexHigh;
}
public static void main(String[] args) { //runs program
new Temp();
}
}