All pastes #792043 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-11-21 03:22 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];
        String[] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "June",
        "July", "Aug", "Sept", "Oct", "Nov", "Dec" };
        
        Month data;
        float min = 0, max = 0, avgy = 0, sumy = 0;
        
        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();
            for(int j = 0; j < 31; j++) {
                data.setTemp(days[j]);
                //System.out.println(data.getTemp());
            }
            max = days[0];
            for(int j = 0; j < 31; j++) {
                if (max < days[j]){
                    max = days[j];
                }
                data.setMax(max);
            }
            min = days[0];
            for(int j = 0; j < 31; j++) {
                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()));


            sumy += data.getAvgM();
        }
        
        
        System.out.println("Yearly Sum is: " + df.format(sumy));
        avgy = sumy / 12;
        System.out.println("Yearly Average is: " + df.format(avgy));
        
        
        Scanner scanner = new Scanner(System.in);
        
    /*    System.out.println("Home Work 6");
        System.out.println("Please choose one of the following options:");
        System.out.println("(1) Monthly Average");
        System.out.println("(2) Monthly Lowest/Highest");
        System.out.println("(3) Yearly Average");
        System.out.println("");
        int answer = scanner.nextInt();
     
        if (answer == 1){
            System.out.println("Hi There");
        }
        else if (answer == 2){
            System.out.println("Boo yah");
        }
        else{
            System.out.println("Yearly Average is: " + df.format(avgy));
        }*/
        
        bufReader.close();
    }
}


class Month {
    
    private 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;
    }
}