Please read the entire question posted. It is different than the other questions
ID: 3686452 • Letter: P
Question
Please read the entire question posted. It is different than the other questions in this scenario, hence why I'm asking for help. It uses two classes, not one.
Write a java program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The two dimensional array must be populated using the following input file (we'll call the input file "Temperature.txt"):
30 40 45 60 70 90 89 95 79 90 70 40
10 -10 20 30 50 75 85 79 50 80 30 20
For example, 30 represent the highest temperature in the month of January and 10 represent the lowest temperature in the month of January. You should the usual two classes(Very Important!!!): A driver with main and an object class that should have at least the following methods: (1) A method that calculates and returns the Average high for the year (2) A method that calculates and returns the Average low for the year (3) A method that calculates and returns the index of the Highest temperature of the year (4) A method that calculates and returns the index of the Lowest temperature of the year (5) Should have all other standard methods such as setters, getters, constructors (default, alternate, and copy), makCopy, getCopy, equals, toString. (6) Note that the 2-dim array should be in this class not in the driver or main.
Your output must be written to an output text file. (Important!)
An array of objects is not needed in the driver (main). A single object in main is enough to test the object class. Using this object make sure to test at least the first 4 methods listed above and few standard ones of your choice. The data you read from the file must be stored in the two dimensional array.
Explanation / Answer
Sample code logic-
public class Array {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("MyFile.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
int [][]data=new data[12][12];
reader.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("The average high of the year is: " + averageHigh(getData()));
System.out.println("The average low of the year is: " + averageLow(getData()));
System.out.println("The highest temperature is: " + indexHighTemp(getData()));
System.out.println("The lowest temperature is: " + indexLowTemp(getData()));
}
public static int[][] getData() {
// i - row j - collum
while ((data[][] = bufferedReader.readLine()) != null) {
System.out.println(line);
}
return data;
}
public static int averageHigh(int[][] data) { // average high = 64.667, rounds down as an int
int total = 0;
int average = 0;
for (int i = 0; i < data[0].length; i++) {
total += data[0][i];
average = total / data[0].length;
}
return average;
}
public static int averageLow(int[][] data) { //average low = 46
int total = 0;
int average = 0;
for (int i = 0; i < data[1].length; i++) {
total += data[1][i];
average = total / data[1].length;
}
return average;
}
public static int indexHighTemp(int[][] data) {
int tempVal;
int maxIndex = 0;
for (int i = 0; i < data[0].length; i++) {
tempVal = data[0][i];
if (tempVal > maxIndex) {
maxIndex = data[0][i];
}
}
return maxIndex;
}
public static int indexLowTemp(int[][] data) {
int tempVal;
int lowIndex = Integer.MAX_VALUE;
for (int i = 0; i < data[1].length; i++) {
tempVal = data[1][i];
if (lowIndex > tempVal) {
lowIndex = data[1][i];
}
}
return lowIndex;
}
}
note-by using the above sample code with required modifications the given question can be answered.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.