Write a java program that uses a two-dimensional array to store the highest and
ID: 3686402 • Letter: W
Question
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:
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: 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.
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
// Source Code to Copy
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class YearTemp
{
private int data[];
YearTemp(int data[]){
this.data =data;
}
// getters and setters for data[] if we want to use in future
public int[] getData() {
return data;
}
public void setData(int[] data) {
this.data = data;
}
public float averageHigh(){
float sum =0;
for(int i=0; i< this.data.length/2; i++ ){
sum += this.data[i];
}
return (sum/(this.data.length/2));
}
public float averageLow(){
float sum =0;
for(int i= (this.data.length/2) ; i < this.data.length; i++ ){
sum += this.data[i];
}
return (sum/(this.data.length/2));
}
public int highestTemp(){
int maxIndex = 0;
for (int i = 1; i < this.data.length/2; i++){
int newnumber = this.data[i];
if ((newnumber > this.data[maxIndex])){
maxIndex = i;
}
}
return maxIndex;
}
public int lowestTemp(){
int minIndex = 0;
for (int i = this.data.length/2; i < this.data.length; i++){
int newnumber = this.data[i];
if ((newnumber < this.data[minIndex])){
minIndex = i;
}
}
return minIndex;
}
// impemented method
public String toString() {
String str = "";
for(int i=0; i<this.data.length; i++){
str = str + Integer.toString((this.data[i])) + " ";
}
return str;
}
public static void main (String[] args) throws java.lang.Exception
{
// For Scanning From input.txt file
Scanner scanner = new Scanner(new File("/Users/user1/Java_Practice/src/input.txt"));
int [] data = new int [24];
int i = 0;
while(scanner.hasNextInt())
{
data[i++] = scanner.nextInt();
}
YearTemp yearTemp = new YearTemp(data);
System.out.println(yearTemp.toString());
// For Showing Output in output.txt File
File outputFile = new File("output.txt");
FileWriter fileWriter = new FileWriter(outputFile, true);
PrintWriter printWriter = new PrintWriter(
new BufferedWriter(fileWriter));
printWriter.println(" Current Year Tempearature Array : ");
printWriter.println(yearTemp.toString());
printWriter.println( " Average high for the year ");
printWriter.println(yearTemp.averageHigh());
printWriter.println( " Average low for the year ");
printWriter.println(yearTemp.averageLow());
printWriter.println( " Index of Highest Temperature for the year "); // Index Starts from 0
printWriter.println(yearTemp.highestTemp());
printWriter.println( " Index of Lowest Temperature for the year "); // Index Starts from 0
printWriter.println(yearTemp.lowestTemp());
printWriter.close();
}
}
// Output For this , assuming you have input array stoared in input.txt
Current Year Tempearature Array :
30 40 45 60 70 90 89 95 79 90 70 40 10 -10 20 30 50 75 85 79 50 80 30 20
Average high for the year
66.5
Average low for the year
43.25
Index of Highest Temperature for the year
7
Index of Lowest Temperature for the year
13
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.