Question

In: Computer Science

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.) 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!

Solutions

Expert Solution

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.


Related Solutions

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.
Write a C++ program that uses array to store the salaries of 10 employees working in...
Write a C++ program that uses array to store the salaries of 10 employees working in a small firm. The program should take average of the salaries and the max and min salaries being paid to the employees
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
using javascript and without using javascript sort. Sort an array from lowest to highest const x...
using javascript and without using javascript sort. Sort an array from lowest to highest const x = [201,28,30,-5] ​function sort(array){ // put your code here return //the answer } ​ sort(x) // output: [-5,28,30,201]
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT