87 72 85 63 78 73 51 72 72 98 70 90 66 86 -1
This assignment is due Monday, November 25.
This assignment makes use of the files contained in this zip file.
Write a program called Hw7.java
to read a list of grades given as integers in the range 50 to 100. For this assignment, grades are classified according to the following grading scale.
A+ | 97 - 100 |
A | 93 - 96 |
A- | 89 - 92 |
B+ | 85 - 88 |
B | 81 - 84 |
B- | 77 - 80 |
C+ | 73 - 76 |
C | 69 - 72 |
C- | 65 - 68 |
D+ | 61 - 64 |
D | 57 - 60 |
D- | 53 - 56 |
F | 50 - 52 |
Your program should look for a negative grade as a sentinel value to indicate the end of the list of grades. (The negative value is used only to end the loop reading grades, so do not use it in the following calculations.)
After your program reads the list of grades, your program should display the total number of grades, the number of grades in each letter-grade category, the highest grade score and the lowest score. Your program should also draw a bar graph of the distribution of all the grades.
For example, if the input is the following list of numbers
87 72 85 63 78 73 51 72 72 98 70 90 66 86 -1
then the output of your program should look exactly like this
Total number of grades = 14 Number of A+'s = 1 Number of A's = 0 Number of A-'s = 1 Number of B+'s = 3 Number of B's = 0 Number of B-'s = 1 Number of C+'s = 1 Number of C's = 4 Number of C-'s = 1 Number of D+'s = 1 Number of D's = 0 Number of D-'s = 0 Number of F's = 1 The highest grade = 98 The lowest grade = 51 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100% | | | | | | | | | | | | | | | | | | | | | |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ A+ |******* A | A- |******* B+ |********************* B | B- |******* C+ |******* C |***************************** C- |******* D+ |******* D | D- | F |*******
For each letter letter-grade category, its bar graph represents the percentage of the total grades in that category. Each asterisk in the bar graph represents 1%. Notice that there are 3 B+'s out of 14 grades which is (approximately) 21% of the total number of grades and there are 21 asterisks in the bar graph for B+.
In this assignment's zip file there is a program Hw7.java
that you should complete. In the program I put suggested definitions for two helper methods that you might find useful. You do not have to complete these two methods (if you don't want to finish them, then you should delete their code). The two methods are just suggestions that you may want to think about.
When you compute how many stars to draw in one line of the graph, you will need to compute a letter grade's percentage out of the total number of grades. You will need to convert this percentage to an integer (since you cannot print part of an asterisk). Use Java's Math.round
method to round a double to an integer like this
(int)Math.round(perCent)
In this assignment's zip file there are some files that let you test your program. Make sure your solution file Hw7.java
is in the unzipped hw7
folder and compile your Hw7.java
file to produce Hw7.class
. Then use your mouse to double-click on the file run_test_script.cmd
. That causes a window to pop up that runs your program using input from the file test_data.txt
. There should not be any error messages in the pop up window. The output from running your program will be in a newly created file called test_script_output.txt
. Your output should look exactly like the contents of the file test_script_output_correct.txt
from the assignment's zip file.
In the assignment's zip file there is a sub-folder, demo_program
, that contains files that give you another way to test your program. If you double click on either run_random_test_data.cmd
or run_curved_test_data.cmd
, they will produce a new data file containing random grades and then run a demo version of this assignment on the random data. If you copy that data file to the hw7
folder and double click on run_test_script.cmd
, then you can compare your program's output with the demo program's output. Your program's output should always be exactly like the demo program's output.
Write your program in such a way that your program does not prompt the user in any way for the input values! Your program should use a Scanner object to read one integer after another from the user, but never prompt the user for the input. Why would you want to write a program that reads in data from a user but does not prompt the user in any way? The answer is a very important idea called "I/O redirection". A program like this assignment is meant to process data, but it can be very tedious and error prone to enter a lot of data into a program like this. It is better to first enter all the data into a data file, like the one called test_data.txt
in the zip file, and then have the program process the data stored in the file. You get a Java program to read its input from a data file (instead of from the keyboard) by running the Java program at the command-line in a special way that uses "I/O Redirection".
If you look inside the file run_test_script.cmd
you will see a line of code that looks like this.
java Hw7 < test_data.txt > test_script_output.txt
This is a command-line to the Windows operating system telling Windows to run your Java program, Hw7
, and "redirect" the contents of the file test_data.txt
to your program's System.in
. This makes the file's contents what your program's Scanner object reads. And the above Windows command also "redirects" your program's System.out
to the file test_script_output.txt
so that whenever you call the method System.out.print
, the method's parameter gets written to the file test_script_output.txt
(not to the console window). The "<" character in the command-line is the "input redirection operator". The ">" character in the command-line is the "output redirection operator".
If you are interested in experimenting with I/O Redirection, you can experiment with the program called TestIORedirection.java
from this assignment's zip file. Compile this program. Then open a command-line window from the folder that holds the file TestIORedirection.class
and type, after the command prompt, the command
C:\hw7> java TestIORedirection < test_data.txt
That command runs the TestIORedirection
program and has the program read the data stored in the file test_data.txt
.
Turn in a zip file called CS123Hw7Surname.zip
(where Surname
is your last name) containing your version of Hw5.java
.
This assignment is due Monday, November 25.