java Programming Problem 3 - Temperatures Write a program that uses a two dimens
ID: 3664219 • Letter: J
Question
java Programming Problem 3 - Temperatures
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. The program must include the following methods:
A method named inputTempForMonth whose purpose is to input a high and a low temperature for a specific month. The month and the array of temperatures will both be passed as input arguments to the method. The method will not have a return value.
A method named inputTempForYear whose purpose is to input a high and a low temperature for every month of the year. There are no input arguments for this method, but the method does return a completed multidimensional array of temperatures for the year.
A method named calculateAverageHigh whose purpose is to calculate the average high temperature for the year. This method will take the array of temperatures as input and will return the average high temperature for the year.
A method named calculateAverageLow whose purpose is to calculate the average low temperature for the year. This method will take the array of temperatures as input and will return the average low temperature for the year.
A method named findHighestTemp whose purpose is to return the index value of the highest temperature for the year. If the highest temperature of the year occurs more than once in the year, then the method should return the index of the first month that had the temperature. The method will take the array of temperatures as an input argument and return the index of the highest temperature.
A method named findLowestTemp whose purpose is to return the index value of the lowest temperature for the year. If the lowest temperature of the year occurs more than once in the year, then the method should return the index of the first month that had the temperature. The method will take the array of temperatures as an input argument and return the index of the lowest temperature.
A main method that uses the previous methods to determine the average high temperature, average low temperature, and highest and lowest temperatures for the year. The main method must print out these average temperatures as well as the month and temperature for the highest and lowest temperatures for the year.
Directions
You may only use statements that are discussed in the book through Chapter 7.
You must not use an ArrayList class from Java libraries to solve this problem.
Console input and output must be used to solve this problem.
Use the following input values for the final test of this program:
January has a High of 40 and Low of -10
February has a High of 55 and Low of 25
March has a High of 60 and Low of 40
April has a High of 88 and Low of 20
May has a High of 72 and Low of 55
June has a High of 95 and Low of 80
July has a High of 97 and Low of 87
August has a High of 110 and Low of 98
September has a High of 79 and Low of 68
October has a High of 31 and Low of 30
November has a High of 58 and Low of -25
December has a High of 32and Low of -20
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;
}
}
Another method
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();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.