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

Hello, I\'m wondering if someone can help me out. I have a TempInFile to fill my

ID: 3702049 • Letter: H

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 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--------------

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("TempInFile.txt"));

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.println("The highest temp was in "+TempStat.findHighestTempMonth(temps)+".");
outFile.println("The coldest temp was in "+TempStat.findLowestTempMonth(temps)+".");
outFile.close();

}// 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:

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