Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program that uses a two-dimensional array to store and process the

ID: 3707082 • Letter: W

Question

Write a java program that uses a two-dimensional array to store and process 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

In this table rows represent temperatures and columns represent months. Row zero represents the highest temps while row 1 represents the lowest temps. For example, 30 represents the highest temperature in the month of January and -10 represents the lowest temperature in the month of January.

The program should calculate the following:

(1) A method that calculates the Average high for the year

(2) A method that calculates the Average low for the year

(3) A method that calculates and returns the index of the Highest temperature of the year. Note the index and not the temp.

(4) A method that calculates and returns the index of the Lowest temperature of the year. Note the index and not the temp.

Implementation Details Your program should have the normal two classes: TempStat and the driver.

Your TempStat class should have all the standard methods: getters; setters; constructors (default, at least one alternate, and copy), makeCopy, equals and toString. The methods described above should be in the TempStat class and not the driver.

You are not using array of objects in this project, just a simple object in main should be enough.

Your output should be sent to an outut file.

Make sure your input and output file are part of your zipped file.

Explanation / Answer

Driver.java


import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;

public class Driver {

public static void main(String[] args) throws Exception {

Scanner inFile = new Scanner(new FileReader("D:\TempInFile.txt"));

PrintWriter outFile = new PrintWriter("D:\outFile.txt");

int[][] temps = new int[2][12]; // create array to store temps

while (inFile.hasNextInt()) // populate the array from inFile

for (int row = 0; row < temps.length; row++) {

for (int col = 0; col < temps[row].length; col++) {

temps[row][col] = inFile.nextInt();

outFile.print(temps[row][col] + " ");

} // end of inner loop

outFile.println(" ");

} // end of outer loop

outFile.println("The average high temp is " + TempStat.calcAvgHigh(temps) + ".");

outFile.println("The average low temp is " + TempStat.calcAvgLow(temps) + ".");
outFile.println("The highest temp was in "+TempStat.findHighestTempMonth(temps)+".");
outFile.println("The coldest temp was in "+TempStat.findLowestTempMonth(temps)+".");
outFile.close();
System.out.println("File has been generated");
}// end main

}// end class

TempStat.java

public class TempStat {

public static int temp;

static String month[] = {"January", "Fabruary", "March", "April", "May", "June", "July", "August", "September","October","November","December"};

public static int avgHigh;

public static int avgLow;

public TempStat() {

temp = 0;

avgHigh = 0;

avgLow = 0;

}// end default

public TempStat(int Temp) {

temp = Temp;

}// end alt1

public TempStat(int Temp, int High, int Low) {

temp = Temp;

avgHigh = High;

avgLow = Low;

}// end alt2

public TempStat(TempStat tempCopy) {

this.temp = tempCopy.temp;

this.avgHigh = tempCopy.avgHigh;

this.avgLow = tempCopy.avgLow;

}// end copy constructor

// setter for temp

public void setTemp(int Temp) {

temp = Temp;

}

// getter for temp

public int getTemp() {

return temp;

}

// setter for avgHigh

public void setAvgHigh(int High) {

avgHigh = High;

}

// getter for avgHigh

public int getAvgHigh() {

return avgHigh;

}

// setter for avgLow

public void setAvgLow(int Low) {

avgLow = Low;

}

// getter for avgLow

public int getAvgLow() {

return avgLow;

}

public static int calcAvgHigh(int[][] temps) {

int sum = 0;

// get sum of first row

for (int i : temps[0])

sum = sum + i;

int avgHigh = (sum / 12);

return avgHigh;

}

public static String findHighestTempMonth(int[][] temps) {

int a[]= temps[0];

int max = a[0];

int maxIndex = 0;

for(int i=0;i<a.length;i++) {

if(max<a[i]) {

max = a[i];

maxIndex=i;

}

}

return month[maxIndex];

}

public static String findLowestTempMonth(int[][] temps) {

int a[]= temps[1];

int min = a[0];

int minIndex = 0;

for(int i=0;i<a.length;i++) {

if(min>a[i]) {

min = a[i];

minIndex=i;

}

}

return month[minIndex];

}

public static int calcAvgLow(int[][] temps) {

int sum = 0;

// get sum of second row

for (int i : temps[1])

sum = sum + i;

int avgLow = (sum / 12);

return avgLow;

}

public String toString() {

String str = "The avg high is: " + avgHigh + ". The avg low is: " + avgLow + ".";

return str;

}

}// end of class

Output:

File has been generated

30 40 45 60 70 90 89 95 79 90 70 40  
-10 -8 20 30 50 75 85 79 50 80 30 12  
The average high temp is 66.
The average low temp is 41.
The highest temp was in August.
The coldest temp was in January.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote