Java Beginner: Write a java program that uses a two-dimensional array to store t
ID: 3820228 • Letter: J
Question
Java Beginner: Write a java program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The input should be read from a text file using the following data: 30 40 45 60 70 90 89 95 79 90 70 40 -10 -8 20 30 50 75 85 79 50 80 30 12 For example, 30 represent the highest temperature in the month of January and -10 represent the lowest temperature in the month of January.
The program should calculate the following:
A method that calculates the Average high for the year
A method that calculates the Average low for the year
A method that calculates and returns the index of the Highest temperature of the year
A method that calculates and returns the index of the Lowest temperature of the year
Should have all other standard methods such as setters, getters, constructors (default, alternate, and copy), makCopy, getCopy, equals, toString.
Your output must be written to an output file Note that your program should have at least two classes
Explanation / Answer
WeatherTest.java
package weather;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class WeatherTest {
static int arr[][] = new int[12][12];
static String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September",
"October", "November", "December" };
static Weather[] weather=new Weather[12];
public static void main(String[] args) {
File input = new File("D:\workspace\chegg\src\temperatures.txt");
inputFromFile(input);
createWeather();
System.out.println("Average High of the year: " + avgHigh(arr));
System.out.println("Average Low of the Year: "+avgLow(arr));
higTempIndex();
lowTempIndex();
}
/*
* Method to read the int values from the txt file
*/
private static void inputFromFile(File myFile) {
Scanner in;
try {
// read the details from the given file
in = new Scanner(myFile);
} catch (FileNotFoundException e) {
System.out.println("Input file not found : " + myFile);
return;
}
int rows = 2, cols = 12, i, j;
// Read from input file and set to 2d array
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
arr[i][j] = in.nextInt();
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
in.close();
}
/*
* To calculate Average High of the year
*/
public static int avgHigh(int[][] temp) {
int sum = 0;
for (int j = 0; j < 12; j++) {
sum += weather[j].getHighTemp();
}
return sum / 12;
}
/*
* To calculate Average Low of the year
*/
public static int avgLow(int[][] temp) {
int sum = 0;
for (int j = 0; j < 12; j++) {
sum += weather[j].getLowTemp();
}
return sum / 12;
}
/*
* Set the values month, high temperature and low temperatures
*/
public static void createWeather(){
for (int j = 0; j < 12; j++) {
weather[j]=new Weather(months[j],arr[0][j],arr[1][j]);
}
for (int j = 0; j < 12; j++) {
System.out.println(weather[j].toString());
}
}
/*
* Find highest temperature
*/
public static void higTempIndex(){
//set first temp value
int max=weather[0].getHighTemp();
int index=0;
for(int i = 0; i < 12; i++)
{
if(max < weather[i].getHighTemp())
{
max = weather[i].getHighTemp();
index=i;
}
}
System.out.println("Highest temperature: "+max);
System.out.println("Index: "+index);
}
/*
* Find lowest temperature
*/
public static void lowTempIndex(){
//set initial value to min
int min=weather[0].getLowTemp();
int index=0;
for(int i = 0; i < 12; i++)
{
if(min > weather[i].getLowTemp())
{
min = weather[i].getLowTemp();
index=i;
}
}
System.out.println("Lowest temperature: "+min);
System.out.println("Index: "+index);
}
}
Weather.java
package weather;
public class Weather {
private int highTemp;
private int lowTemp;
private String month;
public Weather(String month, int highTemp, int lowTemp){
this.month=month;
this.highTemp= highTemp;
this.lowTemp= lowTemp;
}
public int getHighTemp() {
return highTemp;
}
public void setHighTemp(int highTemp) {
this.highTemp = highTemp;
}
public int getLowTemp() {
return lowTemp;
}
public void setLowTemp(int lowTemp) {
this.lowTemp = lowTemp;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public String toString(){
return month+" "+" "+highTemp+" "+lowTemp;
}
}
temperatures.txt:
30 40 45 60 70 90 89 95 79 90 70 40
-10 -8 20 30 50 75 85 79 50 80 30 12
Sample Output:
30 40 45 60 70 90 89 95 79 90 70 40
-10 -8 20 30 50 75 85 79 50 80 30 12
January 30 -10
February 40 -8
March 45 20
April 60 30
May 70 50
June 90 75
July 89 85
August 95 79
September 79 50
October 90 80
November 70 30
December 40 12
Average High of the year: 66
Average Low of the Year: 41
Highest temperature: 95
Index: 7
Lowest temperature: -10
Index: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.