Question
Hello, I'm wondering if someone can help me out. I have a TempInFile to fill my 2D array with ints that are temperatures. First row
Hello, I'm wondering if someone can help me out. I have a TempInFile to fill my 2D array with ints that are temperatures. First row is the high of the year, the second row is the lows of the year. I'm supposed to print the index location of the highest temp, and of the lowest temp, but I'm not figuring out a way. So the highest temp of row 0 is [0][8], and the lowest temp of row 1 is [1][0]. But how would I get it to print out something like "the highest temp was in July" and "the coldest temp was in january", all based upon what column the temp is in?
---TempInFile--- 30 40 45 60 70 90 89 95 79 90 70 40 -10 -8 20 30 50 75 85 79 50 80 30 12 -----------end-------------- ---Driver.java---
package project5;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws Exception {
Scanner inFile = new Scanner(new FileReader("TempInFile"));
PrintWriter outFile = new PrintWriter("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.close();
}// end main
}// end class
--------------end-------------
----TempStat.java---
package project5;
public class TempStat {
public static int temp;
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 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
--------------end--------------
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started