High Temperatures The Assignment Assume you have a file with

High Temperatures The Assignment: Assume you have a file with the daily high temperatures for the year. You are to write a program to read the high temperatures from this file and count how many days the high was at each temperature. Your output should be a histogram of the results labeled with the corresponding temperature and the actual count. For example, the output might look like the following: Results temp count 101 1 * 100 0 99 2 ** 98 2 ** 97 4 **** 96 1 * 95 0 94 5 ***** 93 3 *** and so on. What to hand in: Hand in a copy of your program and a copy of the results when run with the data file . The data file ends with a temperature of 1000. It is easy to print the results if the program writes them to a file instead of (or as well as) to the screen. Hints Keep the totals for each temperature in one spot in an array. Use the temperature as an index into the array where the totals are kept. After all data is read and the array has the final values, print one line for each spot in the array. Due date: The program is due at the start of class on Thursday, October 10th, 2016. the program should be java.

Solution

// TemperatureHistogram.java

import java.io.*;
import java.util.Scanner;
import java.io.PrintWriter;

public class TemperatureHistogram
{

   public static void main(String args[]) throws IOException
   {

      try {
         Scanner in = new Scanner(new File(\"input.txt\"));
         PrintWriter out = new PrintWriter(\"output.txt\", \"UTF-8\");
       
         int[] temperature = new int[102];
         for (int i = 0; i < 102 ;i++ )
         {
               temperature[i] = 0;
         }

         int temp;
         while (in.hasNextLine())
         {
            temp = in.nextInt();
            temperature[temp]++;
         }

          for (int i = 101; i > 0 ;i-- )
         {
               out.print(i + \" \" + temperature[i] + \" \");
               for (int j = 0; j < temperature[i] ;j++ )
               {
                  out.print(\"*\");
               }

               out.println();
         }

          if (in != null)
         {
            in.close();
         }
         if (out != null)
         {
            out.close();
         }
      }
      finally
      {
         System.out.println();
      }
   }
}

/*
input.txt
100
88
88
88
100
87
93
89
78
99
99
100
101
34
56
77
87
93
56
96
96

output.txt
101 1 *
100 3 ***
99 2 **
98 0
97 0
96 2 **
95 0
94 0
93 2 **
92 0
91 0
90 0
89 1 *
88 3 ***
87 2 **
86 0
85 0
84 0
83 0
82 0
81 0
80 0
79 0
78 1 *
77 1 *
76 0
75 0
74 0
73 0
72 0
71 0
70 0
69 0
68 0
67 0
66 0
65 0
64 0
63 0
62 0
61 0
60 0
59 0
58 0
57 0
56 2 **
55 0
54 0
53 0
52 0
51 0
50 0
49 0
48 0
47 0
46 0
45 0
44 0
43 0
42 0
41 0
40 0
39 0
38 0
37 0
36 0
35 0
34 1 *
33 0
32 0
31 0
30 0
29 0
28 0
27 0
26 0
25 0
24 0
23 0
22 0
21 0
20 0
19 0
18 0
17 0
16 0
15 0
14 0
13 0
12 0
11 0
10 0
9 0
8 0
7 0
6 0
5 0
4 0
3 0
2 0
1 0


*/

High Temperatures The Assignment: Assume you have a file with the daily high temperatures for the year. You are to write a program to read the high temperatures

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site