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

Please make sure it\'s java language My neighbor’s daughter has been working on

ID: 3813398 • Letter: P

Question

Please make sure it's java language

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

Java Code for the given problem:

Note: Make sure that the input data file Data.txt is in your project foler.

package temp;
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 Temp {
       private static Map<String, java.util.List<Integer>> tempdata = new HashMap<>();

   public static void main(String args[]) throws FileNotFoundException{
       String temperaturefile = null;
       //the file
       temperaturefile = "Data.txt";
       storeInputData(temperaturefile);
       Map<String, Double> outavgs = getAverages();
       boolean higher = false;
    
       for(String dataMonth: tempdata.keySet()){
           List<Integer> dataT = tempdata.get(dataMonth);
           Double tempAvg = outavgs.get(dataMonth);
           if(tempAvg == null) continue;
           if(dataT.get(0) > tempAvg) higher = true;
           int num = 0;
           List<String> tempOutput = new ArrayList<>();
           String outline = "The average for "+dataMonth + " was "+ tempAvg;
           tempOutput.add(outline);
           int dayNum = 1;
           for(Integer temp: dataT){
               outline = String.format("%2d", dayNum) + " "+ temp;
               dayNum++;
               if(temp > tempAvg){
                   if(higher){
                       num++;
                       if(num == 3){
                           tempOutput.set(tempOutput.size() - 2, tempOutput.get(tempOutput.size() - 2)+" +");
                           tempOutput.set(tempOutput.size() - 1, tempOutput.get(tempOutput.size() - 1)+" +");
                           outline+=" +";
                       } else if(num > 3){
                           outline+=" +";
                       }
                    
                   } else {
                       higher =true;
                       num=1;                    
                   }
               } else {
                   if(!higher){
                       num++;
                       if(num == 3){
                           tempOutput.set(tempOutput.size() - 2, tempOutput.get(tempOutput.size() - 2)+" -");
                           tempOutput.set(tempOutput.size() - 1, tempOutput.get(tempOutput.size() - 1)+" -");
                           outline+=" -";
                       } else if(num > 3){
                           outline+=" -";
                       }
                    
                   } else {
                       higher =false;
                       num=1;                    
                   }
               }
            
               tempOutput.add(outline);
           }
           for(String output : tempOutput){
               System.out.println(output);
           }
       }
    
    
    
   }

   private static Map<String, Double> getAverages(){
       Map<String, Double> outavgs = new HashMap<>();
       for(String dataMonth: tempdata.keySet()){
           List<Integer> dataT = tempdata.get(dataMonth);
           if(dataT.size() > 0){
               double tempAvg = 0;
               for(Integer temp:dataT){
                   tempAvg+=temp;
               }
               tempAvg= tempAvg/dataT.size();
               outavgs.put(dataMonth, tempAvg);
           }
        
       }
       return outavgs;
   }

   private static void storeInputData(String temperaturefile) throws FileNotFoundException{
    
    
       Scanner sacn = new Scanner(new File(temperaturefile));
       String dataMonth = "";
       while(sacn.hasNext()){
           String inputs = sacn.next();
        
           try{
               int temp = Integer.parseInt(inputs);
               tempdata.get(dataMonth).add(temp);
            
           } catch(NumberFormatException e){
               dataMonth = inputs + "-";
               if(sacn.hasNext()) dataMonth += sacn.nextInt();
               else continue;
               if(tempdata.get(dataMonth) == null) tempdata.put(dataMonth, new ArrayList<Integer>());
           }
       }
    
   }

//the specified method
   private static int numDaysInMonth (String dataMonth){
       if(tempdata.get(dataMonth) == null) return 0;
       return tempdata.get(dataMonth).size();
   }

  
}

Sample Output: