If I have the following code, what java code do I need to calculate the range of
ID: 657965 • Letter: I
Question
If I have the following code, what java code do I need to calculate the range of the average high and low temperatures of each month and what code (and where do I put it) do I need to format a table that lists the months, the highs, lows and range of each month and the annual average, highest month, and lowest month. For examples
Months Highs Lows Range
january 41 10 31
....
Annual average 65 47 18
highest month july aug january
lowest month feb. jan sept
This is the code
import java.util.*;
public class Temperatures
{
public static Scanner input = new Scanner(System.in);
public static int highTemperature, lowTemperature,averageHigh, averageLow, zip;
public static int index;//keeps track of months
public static int indexOfHighestTemp=0, indexOfLowestTemp=0;
public static int[][] highAndLowTemps = new int [12][2];//array for highs and lows
public 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]);
}
public static void inputTempForMonth(int[][] highAndLowTemps)
{
System.out.println("Enter the zipcode");
zip =input.nextInt();
System.out.println("Enter the average high for "+months[index]+":");
highTemperature = input.nextInt();//inputs months high temp
highAndLowTemps[index][0]=highTemperature;
System.out.println("Enter the average low for "+months[index]+":");
lowTemperature = input.nextInt();//inputs months low temp
highAndLowTemps[index][1]=lowTemperature;
}
public 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;
}
public 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;
}
public 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;
}
public 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;
}
public 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;
}
}
Explanation / Answer
Program:
package temper;
import java.util.*;
public class Temper
{
public static Scanner input = new Scanner(System.in);
public static int highTemperature, lowTemperature, zip;
public static int index;//keeps track of months
public static int indexOfHighestTemp=0, indexOfLowestTemp=0;
public static int[][] highAndLowTemps = new int [12][2];//array for highs and lows
public static String[] months = new String[12];//array of monthss
public static void main(String[] args)
{
inputTempForYear();
System.out.print(" "+String.format("%20s","Month")+String.format("%20s","Highs")
+String.format("%20s","Lows")+String.format("%20s","Range")+" ");
for(int i=0;i<12;i++)
{
System.out.print(" "+String.format("%20s",months[i])+String.format("%20s",highAndLowTemps[i][0])
+String.format("%20s",highAndLowTemps[i][1])+String.format("%20s",
(highAndLowTemps[i][0] -highAndLowTemps[i][1]))+" ");
// System.out.println(months[i]+" "+highAndLowTemps[i][0]+" "+
// highAndLowTemps[i][1]+" "+(highAndLowTemps[i][0] -highAndLowTemps[i][1])+" ");
}
int averageHigh=calculateAverageHigh(highAndLowTemps);
int averageLow=calculateAverageLow(highAndLowTemps);
System.out.print(" "+String.format("%20s","Annual average")+String.format("%20s",averageHigh)
+String.format("%20s",averageLow)+String.format("%20s",(averageHigh -averageLow))+" ");
//System.out.println("Annual average"+" "+ averageHigh+" "+ averageLow+
//" "+(averageHigh -averageLow)+" ");
int[] mon;
mon=HighestMonth(highAndLowTemps);
System.out.print(" "+String.format("%20s","Highest Month")+String.format("%20s",months[mon[11]])
+String.format("%20s",months[mon[10]])+String.format("%20s",months[mon[9]])+" ");
//System.out.println("Highest Month"+" "+ months[mon[11]]+" "+ months[mon[10]]+
//" "+months[mon[9]]+" ");
mon=LowestMonth(highAndLowTemps);
System.out.print(" "+String.format("%20s","Lowest Month")+String.format("%20s",months[mon[0]])
+String.format("%20s",months[mon[1]])+String.format("%20s",months[mon[2]])+" ");
//System.out.println("Lowest Month"+" "+ months[mon[0]]+" "+ months[mon[1]]+
// " "+months[mon[2]]+" ");
// code u have
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]);
}
public static void inputTempForMonth(int[][] highAndLowTemps)
{
System.out.println("Enter the zipcode");
zip =input.nextInt();
System.out.println("Enter the average high for "+months[index]+":");
highTemperature = input.nextInt();//inputs months high temp
highAndLowTemps[index][0]=highTemperature;
System.out.println("Enter the average low for "+months[index]+":");
lowTemperature = input.nextInt();//inputs months low temp
highAndLowTemps[index][1]=lowTemperature;
}
public 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;
}
public static int[] HighestMonth(int[][] tempHL)
{
int mm[]=new int[12], t;
for(int i=0;i<12;i++)
mm[i]=i;
for(int i=0;i<12;i++)
{
for(int j=0;j<12;j++)
{
if(tempHL[i][0] > tempHL[j][0])
{
t=mm[i];
mm[i]=j;
j=t;
}
}
}
return mm;
}
public static int[] LowestMonth(int[][] tempHL)
{
int mm[]=new int[12], t;
for(int i=0;i<12;i++)
mm[i]=i;
for(int i=0;i<12;i++)
{
for(int j=0;j<12;j++)
{
if(tempHL[i][1] > tempHL[j][1])
{
t=mm[i];
mm[i]=j;
j=t;
}
}
}
return mm;
}
public static int calculateAverageHigh(int[][] highAndLowTemps)
{
int averageHigh=0;
for(int i=0;i<=11;i++)//finds sum of high temps
{
averageHigh+=highAndLowTemps[i][0];
}
averageHigh/=12;//calculates average
return averageHigh;
}
public static int calculateAverageLow(int[][] highAndLowTemps)
{
int averageLow=0;
for(int i=0;i<=11;i++)//finds sum of low temps
{
averageLow=averageLow+highAndLowTemps[i][1];
}
averageLow/=12;//calculates average
return averageLow;
}
public 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;
}
public 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;
}
}
Output:
Enter the zipcode
1
Enter the average high for January:
34
Enter the average low for January:
1
Enter the zipcode
2
Enter the average high for Febuary:
78
Enter the average low for Febuary:
34
Enter the zipcode
3
Enter the average high for March:
90
Enter the average low for March:
67
Enter the zipcode
4
Enter the average high for April:
79
Enter the average low for April:
34
Enter the zipcode
5
Enter the average high for May:
90
Enter the average low for May:
67
Enter the zipcode
6
Enter the average high for June:
100
Enter the average low for June:
89
Enter the zipcode
7
Enter the average high for July:
79
Enter the average low for July:
23
Enter the zipcode
8
Enter the average high for August:
99
Enter the average low for August:
67
Enter the zipcode
9
Enter the average high for September:
68
Enter the average low for September:
56
Enter the zipcode
10
Enter the average high for October:
111
Enter the average low for October:
78
Enter the zipcode
11
Enter the average high for November:
56
Enter the average low for November:
23
Enter the zipcode
12
Enter the average high for December:
54
Enter the average low for December:
32
Month Highs Lows Range
January 34 1 33
Febuary 78 34 44
March 90 67 23
April 79 34 45
May 90 67 23
June 100 89 11
July 79 23 56
August 99 67 32
September 68 56 12
October 111 78 33
November 56 23 33
December 54 32 22
Annual average 78 47 31
Highest Month January December March
Lowest Month January December November
Average High: 78
Average Low: 47
Highest Temp and Month: 111 October
Lowest Temp and Month: 1 January
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.