Lab 3
Histogram of Ratings
Due Wednesday, February 15th, 11:55pm. Submission through SVN.
Please submit your work in SVN directoryhttps://www.cs.usfca.edu/svn/< your username >/cs112/lab3
e.g. https://www.cs.usfca.edu/svn/ejung/cs112/lab3
Goal
Design and implement an application Histogram.java that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of doubles that are in the range 0.0 to 5.0 inclusive from a file (sample input file); then produce a chart like the one below that indicates how many input values fell in the range 4 to 5 star, 3 to 4 star, and so on. Print one asterisk for each value entered.
16 reviews
4-5 star: * (1)
3-4 star: * (1)
2-3 star: **** (4)
1-2 star: ****** (6)
0-1 star: **** (4)
OR
16 reviews
4-5 star: * (1)
3-4 star: * (1)
2-3 star: **** (4)
1-2 star: ****** (6)
0-1 star: **** (4)
Specifications
- Get the file name from the command-line arguments. See the video for the demo.
- In Eclipse, right-click on the class (e.g. Histogram), Run As, Run Configurations, Arguments tab, and then enter the file name in Program arguments.
public class Histogram {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
The code above prints the file name in Program arguments, e.g. histogram.txt
Error handling:If the file is not found, ask user for the file name. Repeat until the file exists.
Read the numbers from the file, while counting the frequencies. The exact ranges are below.
- 4-5 star: 4< rating <=5
- 3-4 star: 3< rating <=4
- 2-3 star: 2< rating <=3
- 1-2 star: 1< rating <=2
- 0-1 star: 0<= rating <=1
Error handling: If there are any number less than 0 or greater than 5 in the file, ignore the number.
Error handling: If there are any input in the file that is not a number, then stop reading numbers from the file.
Print the historgram as above.
What to submit
Submit README that explains how your program works, and Histogram.java to your SVN. In your README, explain how you handle errors.