In: Computer Science
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.) averageLow: This method calculates and returns the average low temperature of the year. d.) indexHighTemp: This method returns the index of the highest temperature in the array. e.) indexLowTemp: This method returns the index of the lowest temperature in the array. Use the following input: High temp for each month: 30 40 45 60 70 90 89 95 79 90 70 40 Low temp for each month: 10 -10 20 30 50 75 85 79 50 80 30 20
So I have written a code for it but i just have a problem with the output. For the month with the highest temperature and lowest temperature, my code starts at 0 instead of 1. For example if I input that month 1 had a high of 20 and low of -10, and every other month had much warmer weather than that, it should say "The month with the lowest temperature is 1" but instead it says "The month with the lowest temperature is 0" So i just want to know what i have to change to do that. The code below is the one i have written
import java.util.Scanner;
public class Lab2 {
public static int[][] getData() {
Scanner sc = new Scanner(System.in);
int num;
System.out.println("Enter The Number of Months: ");
num = sc.nextInt();
int[][] temps = new int[2][num];
for(int i=0; i < num; i++) {
System.out.println("Enter The High Temperature For Month " + (i+1) + " : ");
temps[0][i] = sc.nextInt();
System.out.println("Enter The Low Temperature For Month " + (i+1) + " : ");
temps[1][i] = sc.nextInt();
}
return temps;
}
public static double averageHigh(int[][] temps) {
double sum = 0;
for(int i=0; i < temps[0].length; i++) {
sum += temps[0][i];
}
return sum / (double)(temps[0].length);
}
public static double averageLow(int[][] temps) {
double sum = 0;
for(int i=0; i < temps[1].length; i++) {
sum += temps[1][i];
}
return sum / (double)(temps[1].length);
}
public static int indexHighTemp(int[][] temps) {
int index = 0;
int high = temps[0][0];
for(int i=0; i < temps[0].length; i++) {
if (high < temps[0][i]) {
high = temps[0][i];
index = i;
}
}
return index;
}
public static int indexLowTemp(int[][] temps) {
int index = 0;
int low = temps[1][0];
for(int i=0; i < temps[1].length; i++) {
if (low > temps[1][i]) {
low = temps[1][i];
index = i;
}
}
return index;
}
public static void main(String[] args) {
int temps[][] = getData();
System.out.println("\nAverage High Temperature : "+averageHigh(temps));
System.out.println("\nAverage Low Temperature : "+averageLow(temps));
System.out.println("\nThe The Month With The Highest Temperature : "+indexHighT$
System.out.println("\nThe The Month With The Lowest Temperature : "+indexLowTem$
}
}
Thank you!
import java.util.Scanner;
public class Lab2 {
public static int[][] getData() {
Scanner sc = new Scanner(System.in); //Declaring a scanner object sc to take user input
System.out.print("High Temp for each month: ");
String high = sc.nextLine(); //Taking input of high temperature of each month seperated by space
String[] highArray = high.split("\\s+"); //Splitting elements seperated by whitespace and storing them in a String Array named highArray
System.out.print("Low Temp for each month: ");
String low = sc.nextLine(); //Taking input of low temperature of each month seperated by space
String[] lowArray = low.split("\\s+"); //Splitting elements seperated by whitespace and storing them in a String Array named lowArray
int[][] temps = new int[2][12]; //Declaring 2-D array to store high and low temperature of each month
for(int i =0;i<highArray.length;i++){ //Loop for storing high temperature in a 2-D array
String a = highArray[i]; //Storing element of String array of high temperature in a String variable a
temps[0][i] = Integer.parseInt(a); //Converting the String to Int and storing in a 2-D array
}
for(int j =0;j<12;j++){ //Loop for storing low temperature in a 2-D array
String b = lowArray[j]; //Storing element of String array of low temperature in a String variable b
temps[1][j] = Integer.parseInt(b); //Converting the String to Int and storing in a 2-D array
}
return temps; //returning the temperature array
}
public static double averageHigh(int[][] temps) {
double sum = 0;
for(int i=0; i < temps[0].length; i++) {
sum += temps[0][i];
}
return sum / (double)(temps[0].length);
}
public static double averageLow(int[][] temps) {
double sum = 0;
for(int i=0; i < temps[1].length; i++) {
sum += temps[1][i];
}
return sum / (double)(temps[1].length);
}
public static int indexHighTemp(int[][] temps) {
int index = 0;
int high = temps[0][0];
for(int i=0; i < temps[0].length; i++) {
if (high < temps[0][i]) {
high = temps[0][i];
index = i;
}
}
return index;
}
public static int indexLowTemp(int[][] temps) {
int index = 0;
int low = temps[1][0];
for(int i=0; i < temps[1].length; i++) {
if (low >= temps[1][i]) {
low = temps[1][i];
index = i;
}
}
return index;
}
public static void main(String[] args) {
int temps[][] = getData();
System.out.println("Average High Temperature : "+averageHigh(temps));
System.out.println("Average Low Temperature : "+averageLow(temps));
System.out.println("The Month With The Highest Temperature : "+(indexHighTemp(temps)+1)); //as Array index starts from 0 but Months start from 1 we add 1 to the index
System.out.println("The Month With The Lowest Temperature : "+(indexLowTemp(temps)+1)); //as Array index starts from 0 but Months start from 1 we add 1 to the index
}
}
Output:
The changes made in the given code is commented.
As arrays declared in the program have index starting from zero, whereas there is no such thing as Month 0(Months start from 1) so we add 1 to the output of indexHighTemp, which is done at the time of printing the index of the highest temperature.The below line in bold shows the same.
System.out.println("\nThe The Month With The Highest Temperature : "+(indexHighTemp(temps)+1)); +1 is the change made. Other method of achieving this result can be adding 1 to the return value of the function indexHighTemp(temps) instead of adding 1 in the print statement as done in the above code. Similar thing is applicable for the lowest temperature.
Secondly,
The questions asks for input of low temperature and high temperature of each month in a single line, whereas the given code has loop for taking the input. The input of all high temperature of the month is entered with spaces between the temperature of each month, which is in string format. Later a string array is created to store the temperature of each month as an element of string array. These are further converted to Integer and stored in the 2-Dimensional array declared for the purpose. Similar operation is performed for low temperature of each month and is stored in the appropriate row of the 2-Dimensional array declared for the purpose.