All pastes #792156 Raw Edit

Untitled

public text v1 · immutable
·published 2007-11-21 05:26 UTC
rendered paste body
/*
 * Write an application that reads daily temperatures for 12 months and allows
 * the user to get statistics. Support at least three options: monthly average
 * of a given month, yearly average, and lowest and highest temperatures of a
 * given month. Use a text file to store temperatures. A line in the text file
 * contains daily temperatures for one month. The first line in the text file
 * contains temperatures for January; the second line, those for February; and
 * so forth. Using StringTokenizer to parse a line into temperatures of type
 * float. For a data structure, consider using either an array of Month or a
 * two-dimensional array of float. Month is a class you define yourself.
 */

import java.io.*;
import java.util.*;
import java.text.*;

class homeWork6 {
    
    public static void main( String[] args) throws IOException {
        
        File inFile = new File("templog.txt");
        FileReader fileReader = new FileReader(inFile);
        BufferedReader bufReader = new BufferedReader(fileReader);
        
        DecimalFormat df = new DecimalFormat("0.00");
        
        float[] days = new float[31];
        float[] months = new float[12];
        
        String[] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "June",
        "July", "Aug", "Sept", "Oct", "Nov", "Dec" };
        
        
        float min = 0, max = 0, avgy = 0, sumy = 0;
        
        float lol = 0;
        
        Month data;
        
        for(int i = 0; i < 12; i++) {
            String str = bufReader.readLine();
            StringTokenizer parser = new StringTokenizer(str);
            int count = 0;
            while(parser.hasMoreTokens()) {
                String next = parser.nextToken();
                float newvar = Float.parseFloat(next);
                days [count]= newvar;
                count++;
            }
            data = new Month();
            max = days[0];
            min = days[0];
            for(int j = 0; j < 31; j++) {
                data.setTemp(days[j]);
                //System.out.println(data.getTemp());
                if (max < days[j]){
                    max = days[j];
                }
                data.setMax(max);
                if (min > days[j]){
                    min = days[j];
                }
                data.setMin(min);
            }
            //System.out.println(monthName[i] + ": AVG = " + df.format(data.getAvgM()) + " MIN = " + df.format(data.getMin()) + " MAX = " + df.format(data.getMax()));
            
            months[i] = data.getAvgM();
            
            
            sumy += data.getAvgM();
            
        }
        
        for(int j = 0; j < 12; j++) {
            System.out.println(df.format(months[j]));
        }
        
        System.out.println("Yearly: SUM = " + df.format(sumy));
        avgy = sumy / 12;
        System.out.println("Yearly: AVG = " + df.format(avgy));
        
        bufReader.close();
    }
}

class Month {
    
    float temp, summ, avgm, sumy, avgy, min1, max1;
    
    public void setTemp(float days) {
        temp = days;
        summ += temp;
    }
    public void setMax(float max) {
        max1 = max;
    }
    public void setMin(float min) {
        min1 = min;
    }
    public float getTemp() {
        return temp;
    }
    public float getAvgM() {
        avgm = summ / 31;
        return avgm;
    }
    public float getMax() {
        return max1;
    }
    public float getMin() {
        return min1;
    }
}