Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My neighbor’s daughter has been working on a science project where she has colle

ID: 3812810 • Letter: M

Question

My neighbor’s daughter has been working on a science project where she has collected the high temperature for each day of an entire month. Now she needs to display information regarding variations in temperature by highlighting those days that might be considered part of a heat wave or cold front. You are to write a program to help.

All of the data has been stored in a text file. Because there are several months of data and she’ll need to execute the program on many different data files, your program will read the file name using command line arguments.

The information is stored as follows:

•The first line of the file contains the name of the month followed by the year that the data was collected.

• The remaining lines will contain one value per line which represents the high temperature for that day.

• All values are integers.

You are to write a Java program which first reads in the month name and year; determines the number of days in the month the data was collected. You must use at least one separate method for this procedure and the method header must be, private static int numDaysInMonth (String month). The program will read in the temperatures; calculate and print the average temperature (two decimal places) for the month.

For each day of the month, print the day number and the temperature that was read from the original file. For every day that is in part of a set of 3 or more consecutive days above the average, display a “+” beside the temperature. For every day that is in part of a set of 3 or more consecutive days below the average, display a “—” beside the temperature. Numbers should line up so that the ones digits are all in the same column. Your program must use at least one array. Save your program in a file called DataAnalysis.java.

Output for the sample file:

The average temperature for April was 91.97

1 85

2 87

3 92 +

4 95 +

5 97 +

6 98 +

7 90 -

8 84 -

9 87 -

10 93 +

11 95 +

12 96 +

13 96 +

14 94 +

15 90

16 91

17 93 +

18 96 +

19 94 +

20 95 +

21 96 +

22 94 +

23 95 +

24 89 -

25 89 -

26 89 -

27 87 -

28 97

29 88

30 87

example of input file:

April 2012
85
87
92
95
97
98
90
84
87
93
95
96
96
94
90
91
93
96
94
95
96
94
95
89
89
89
87
97
88
87

Explanation / Answer


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Chegg {
  
   private static Map<String, java.util.List<Integer>> inputData = new HashMap<>();
  
   public static void main(String args[]) throws FileNotFoundException{
       /*if(args.length < 0){
           System.out.println("Please provide file name as command line argument. exiting..");
           return;
       }*/
       String filename = null;
//       filename = args[0];
       filename = "/home/kumar/Desktop/cpp/Data.txt";
       storeInputData(filename);
//       System.out.println("Here1"+ inputData);
       Map<String, Double> averages = getAverages();
       boolean isHigh = false;
      
//       System.out.println("Here1"+ averages);
       for(String month: inputData.keySet()){
           List<Integer> temps = inputData.get(month);
           Double avg = averages.get(month);
           if(avg == null) continue;
           if(temps.get(0) > avg) isHigh = true;
           int count = 0;
           List<String> outputs = new ArrayList<>();
           String line = "The average for "+month + " was "+ avg;
           outputs.add(line);
           int sno = 1;
           // Adding + and - has to be handled properly
           for(Integer temp: temps){
               line = String.format("%2d", sno) + " "+ temp;
               sno++;
               if(temp > avg){
                   if(isHigh){
                       count++;
                       if(count == 3){
                           outputs.set(outputs.size() - 2, outputs.get(outputs.size() - 2)+" +");
                           outputs.set(outputs.size() - 1, outputs.get(outputs.size() - 1)+" +");
                           line+=" +";
                       } else if(count > 3){
                           line+=" +";
                       }
                      
                   } else {
                       isHigh =true;
                       count=1;                      
                   }
               } else {
                   if(!isHigh){
                       count++;
                       if(count == 3){
                           outputs.set(outputs.size() - 2, outputs.get(outputs.size() - 2)+" -");
                           outputs.set(outputs.size() - 1, outputs.get(outputs.size() - 1)+" -");
                           line+=" -";
                       } else if(count > 3){
                           line+=" -";
                       }
                      
                   } else {
                       isHigh =false;
                       count=1;                      
                   }
               }
              
               outputs.add(line);
           }
           for(String output : outputs){
               System.out.println(output);
           }
       }
      
      
      
   }
  
   private static Map<String, Double> getAverages(){
       Map<String, Double> averages = new HashMap<>();
       for(String month: inputData.keySet()){
           List<Integer> temps = inputData.get(month);
           if(temps.size() > 0){
               double avg = 0;
               for(Integer temp:temps){
                   avg+=temp;
               }
               avg= avg/temps.size();
               averages.put(month, avg);
           }
          
       }
       return averages;
   }
  
   private static void storeInputData(String filename) throws FileNotFoundException{
      
      
       Scanner sc = new Scanner(new File(filename));
       String month = "";
       while(sc.hasNext()){
           String token = sc.next();
          
           try{
               int temp = Integer.parseInt(token);
               inputData.get(month).add(temp);
              
           } catch(NumberFormatException e){
               month = token + "-";
               if(sc.hasNext()) month += sc.nextInt();
               else continue;
               if(inputData.get(month) == null) inputData.put(month, new ArrayList<Integer>());
           }
       }
      
   }
  
   private static int numDaysInMonth (String month){
       if(inputData.get(month) == null) return 0;
       return inputData.get(month).size();
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote