Class
public class Temp {
private double avgHigh;
private double avgLow;
private int highestTemp;
private int lowestTemp;
int [] [] temperature = new int [2][12];
public Temp (){
avgHigh = 0;
avgLow=0;
highestTemp=0;
lowestTemp=0;
for (int row=0; row<temperature.length; row++)
for (int col=0; col<temperature[row].length; col++)
temperature[row][col]=0;
}
public Temp (Scanner i, int [][] temp){
for (int row=0; row<temp.length; row++){
for (int col=0; col<temp[row].length; col++)
temp[row][col]=i.nextInt();}
}
public Temp (Temp obj){
avgHigh=obj.avgHigh;
avgLow=obj.avgLow;
highestTemp=obj.highestTemp;
lowestTemp=obj.lowestTemp;
}
public void setAll(double avgHigh, double avgLow, int highestTemp, int lowestTemp){
this.avgHigh=avgHigh;
this.avgLow=avgLow;
this.highestTemp=highestTemp;
this.lowestTemp=lowestTemp;
}
public void setAvgHigh(double avHigh){
avgHigh=avHigh;
}
public void setAvgLow(double avLow){
avgLow=avLow;
}
public void setHighest(int hTemp){
highestTemp=hTemp;
}
public void setLowest(int lTemp){
lowestTemp=lTemp;
}
public double getavgHigh(){
return avgHigh;
}
public double getavgLow(){
return avgLow;
}
public int getHTemp(){
return highestTemp;
}
public int getLTemp(){
return lowestTemp;
}
public double avgHigh(){
double sumHTemp = 0;
for (int i=0; i < temperature[0].length; i++){
sumHTemp = sumHTemp + temperature[0][i];
avgHigh = (sumHTemp/temperature[0].length);}
return avgHigh;
}
public double avgLow(){
int sumLTemp = 0;
for (int i=0; i < temperature[1].length; i++)
sumLTemp = sumLTemp + temperature[1][i];
return avgLow = (sumLTemp/temperature[1].length);
}
public int highTemp(){
highestTemp = temperature[0][0];
for (int col =1; col< temperature[0].length; col++)
if (highestTemp < temperature[0][col])
highestTemp = temperature[0][col];
return highestTemp;
}
public int lowTemp(){
int lowestTemp = temperature[0][0];
for (int col =1; col< temperature[1].length; col++)
if (lowestTemp > temperature[1][col])
lowestTemp = temperature[1][col];
return lowestTemp;
}
public String toString(){
String data = "\nAverage High: " + this.avgHigh + "\nAverage Low: " + this.avgLow + "\nHighest Temp: " + this.highestTemp + "\nLowest Temp: " + this.lowestTemp;
return data;
}
}
main class
public class MainClass {
public static void main(String[] args) throws FileNotFoundException {
Scanner inFile = new Scanner(new FileReader("temp.txt"));
PrintWriter pr = new PrintWriter("outdata.txt");
int [] [] temperature = new int [2][12];
Temp Temps = new Temp(inFile, temperature);
for (int row=0; row<temperature.length; row++){
for (int col=0; col<temperature[row].length; col++)
System.out.printf("%-4d", temperature[row][col]);
System.out.println();
}
System.out.println(Temps);}
}
This is my output
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: 0.0 Average Low: 0.0 Highest Temp: 0 Lowest Temp: 0

New Topic/Question
Reply



MultiQuote




|