package com.mycompany.
lab7_3a;
import java.io.FileNotFoundException;
/**
*Emmanuel Mutomb
* LAB7_3A
* we are working on temperatures
*/
public class Lab7_3A {
public static void main(String[] args) throws FileNotFoundException
{
MonthTemperatures month1= new MonthTemperatures("Lab7_3A1.txt");
MonthTemperatures month2= new MonthTemperatures("Lab7_3A2.txt");
month1.fillArray();
System.out.println("Month 1: "+month1);
System.out.println();
month2.fillArray();
System.out.println("Month 2: "+month2);
System.out.println();
}
}
package com.mycompany.lab7_3a;
import java.io.*;
import java.util.*;
/**
* The month temperature class
*/
public class MonthTemperatures
{
ArrayList<Double> tempArray;
private String textFileName;
private int month;
private int year;
private double highest;
private double lowest;
private double average;
public MonthTemperatures(String ifile)
{
tempArray = new ArrayList<Double>();
textFileName=ifile;
highest=0;
average=0;
lowest=999;
}
public void fillArray() throws FileNotFoundException
{
File file=new File(textFileName);
Scanner infile=new Scanner(file);
month= infile.nextInt();
year=infile.nextInt();
while(infile.hasNext())
{
double temp= infile.nextDouble();
tempArray.add(temp);
}
System.out.println(tempArray);
infile.close();
findHighest();
findLowest();
computeAverage();
}
public void findHighest()
{
for(int i=0; i<tempArray.size();i++)
{
if(tempArray.get(i)>highest)
{
highest=tempArray.get(i);
}
}
}
public void findLowest()
{
for(int i=0; i<tempArray.size();i++)
{
if(tempArray.get(i)<lowest)
{
lowest=tempArray.get(i);
}
}
}
public void computeAverage()
{
double total=0;
for(double temp: tempArray)
{
total+=temp;
}
average=(double)(total/tempArray.size());
}
public String toString()
{
return "Month: " + month + ", Year: " + year + ", Highest: " + highest +",
Lowest: " + lowest + String.format("Average: %,.3f" , average);
}
}