Write a program that uses a two-dimensional array called hiLowTemperatures to st
ID: 3671812 • Letter: W
Question
Write a program that uses a two-dimensional array called hiLowTemperatures to store the highest and lowest temperatures for each month of the year. The array will have the following declaration, where the rows represent the number of months; first column is to store the highest temperature of the month and the second column is to store the lowest temperature of the month.
int hiLowTemperatures[12][2];
The program should read the highest and lowest temperatures for each month of the year from the text file and print the highest temperature and the lowest temperature of the year to the screen. For instance, if the file has the following values as the highest temperatures for each month
32 40 45 60 65 77 90 100 90 91 65 55
Then, your program should print that 100 is the highest temperature of the year. The text file should have 12 values in first line for highest temperature values and another 12 values for the lowest temperature values in the second line.
Your program must have of the following functions.
Function getData: This function reads data from the a text file into the two-dimensional array.
Function indexHighTemp: This function returns the row index of the highest temperature in the array.
Function indexLowTemp: This function returns the row index of the lowest temperature in the array.
Before you implement your functions, make sure that you come up with a correct function prototype (that is the return type + name of the function + formal parameter list) per function.
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
class HighLowTemp{
//function will read file and store high and low temp in array
public static void getData(int tempData[][]) throws IOException{
FileReader reader = new FileReader("temp.txt");
BufferedReader br = new BufferedReader(reader);
String highTemp = br.readLine(); // reading first line - high temperature records
String lowTemp = br.readLine(); // reading second line - low temperature record
String[] highT = highTemp.split("\s+"); // spliting by space
String[] lowT = lowTemp.split("\s+"); //spliting by space
for(int i=0; i<12; i++){
tempData[i][0] = Integer.parseInt(highT[i]); //storing high temp
tempData[i][1] = Integer.parseInt(lowT[i]); // storing low temp
}
}
//this function will return maximum temp
public static int getHighTem(int tempData[][]){
int max = tempData[0][0]; //declaring first month temp as max
for(int i=1; i< 12; i++){
if(max < tempData[i][0])
max = tempData[i][0];
}
return max;
}
//this function will return minimum temp
public static int getLowTem(int tempData[][]){
int min = tempData[0][0]; //declaring first month temp as min
for(int i=1; i< 12; i++){
if(min > tempData[i][0])
min = tempData[i][0];
}
return min;
}
public static void main(String[] args) throws IOException {
int[][] tempData = new int[12][2];
getData(tempData);
int highT = getHighTem(tempData);
System.out.println("Higest temp: "+ highT);
int lowT = getLowTem(tempData);
System.out.println("Lowest temp: "+ lowT);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.