Question

In: Computer Science

Write a program that uses a two dimensional array to store the highest and lowest 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

Solutions

Expert Solution

Source Code in Java:

import java.util.Scanner;
class TempStats
{
//creating array to store name of months
static String months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};;
static void inputTempForMonth(int temps[][],int month) //method to input temperature for a given month
{
Scanner sc=new Scanner(System.in); //creating Scanner object for inputs
System.out.println("Enter the high and low temperatures for the month of "+months[month]+": ");
temps[month][0]=sc.nextInt(); //input
temps[month][1]=sc.nextInt(); //input
}
static int[][] inputTempForYear() //method to input temperature for an year
{
int temps[][]=new int[12][2]; //creating 2D array to store temperatures for an year
for(int i=0;i<12;i++)
inputTempForMonth(temps,i); //using method to input every month
return temps; //returning the 2D array
}
static double calculateAverageHigh(int temps[][]) //method to calculate and return average high temperature
{
double sum=0; //to store sum of all high temperatures
for(int i=0;i<12;i++)
sum+=temps[i][0]; //adding every high temperature to sum
return sum/12; //returning the average
}
static double calculateAverageLow(int temps[][]) //method to calculate and return average low temperature
{
double sum=0; //to store sum of all low temperatures
for(int i=0;i<12;i++)
sum+=temps[i][1]; //adding every low temperature to sum
return sum/12; //returning the average
}
static int findHighestTemp(int temps[][]) //method to find highest temperature for the year
{
int max=0; //taking first month as highest initially
for(int i=1;i<12;i++)
{
if(temps[i][0]>temps[max][0]) //comparing every month to current max month
max=i;
}
return max; //returning index of max month
}
static int findLowestTemp(int temps[][]) //method to find lowest temperature for the year
{
int min=0; //taking first month as lowest initially
for(int i=1;i<12;i++)
{
if(temps[i][1]<temps[min][1]) //comparing every month to current min month
min=i;
}
return min; //returning index of min month
}
public static void main(String args[])
{
//calling other methods and printing output
int temps[][]=inputTempForYear();
System.out.println("Average high temperature: "+calculateAverageHigh(temps));
System.out.println("Average low temperature: "+calculateAverageLow(temps));
int max=findHighestTemp(temps),min=findLowestTemp(temps);
System.out.println("Highest temperature is "+temps[max][0]+" from the month of "+months[max]);
System.out.println("Lowest temperature is "+temps[min][1]+" from the month of "+months[min]);
}
}

Output:


Related Solutions

Write a program that uses a two-dimensional array to store the highest and lowest temperatures for...
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. These methods MUST be your original code.   Your program should output all the values in the array and then output the average high and the...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
Write a program that uses a DYNAMIC two-dimensional array to store the highest and lowest temperatures for each month of the year (temperature is a decimal value)
In c++ Write a program that uses a DYNAMIC two-dimensional array to store the highest and lowest temperatures for each month of the year (temperature is a decimal value). The program should output the highest and lowest temperatures for the year. Your program must consist of the following functions: a. Function getData: This function reads and stores data in the two-dimensional array. b. Function indexHighTemp: This function returns the index of the highest high temperature in the array. c. Function...
Write a program that uses a DYNAMIC two-dimensional array to store the highest and lowest temperatures for each month of the year (temperature is a decimal value)
In c++ Write a program that uses a DYNAMIC two-dimensional array to store the highest and lowest temperatures for each month of the year (temperature is a decimal value). The program should output the highest and lowest temperatures for the year. Your program must consist of the following functions: a. Function getData: This function reads and stores data in the two-dimensional array. b. Function indexHighTemp: This function returns the index of the highest high temperature in the array. c. Function...
Write a program that uses a DYNAMIC two-dimensional array to store the highest and lowest temperatures for each month of the year (temperature is a decimal value)
In C++ c. The program should output the highest and lowest temperatures for the year. Your program must consist of the following functions: a. Function getData: This function reads and stores data in the two-dimensional array. b. Function indexHighTemp: This function returns the index of the highest high temperature in the array. c. Function indexLowTemp: This function returns the index of the lowest low temperature in the array. These functions must all have the appropriate parameters.
Using JAVA Write a program that uses a 2-D array to store the highest and lowest...
Using JAVA Write a program that uses a 2-D array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following methods with their appropriate parameters: a.) getData: This method reads and stores the data in the 2-D array. b.) averageHigh: This method calculates and returns the average high temperature of the year. c.)...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Write a program that uses an array of high temperatures for your hometown from last week...
Write a program that uses an array of high temperatures for your hometown from last week (Sunday – Saturday). Write methods to calculate and return the lowest high temperature (minimum) and a method to calculate and return the highest high temperature (maximum) in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array as a parameter and then creates...
Write a program that uses an array of high temperatures for your hometown from last week...
Write a program that uses an array of high temperatures for your hometown from last week (Sunday – Saturday). Write methods to calculate and return the lowest high temperature (minimum) and a method to calculate and return the highest high temperature (maximum) in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array as a parameter and then creates...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT