The Assignment: java programing Assume you have a file with the daily high tempe
ID: 3594152 • Letter: T
Question
The Assignment: java programing
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
Hints
1. Keep the totals for each temperature in one spot in an array.
2. Use the temperature as an index into the array where the totals are kept.
3.After all data is read and the array has the final values, print one line for each spot in the array.
data:
thank you so much
Explanation / Answer
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.