Programming Assignment 3
CS 12300
Programming I: Java
Spring, 2021

This assignment is due Tuesday, April 27.

This assignment makes use of the files contained in this zip file.

In this assignment you will write a program called Hw3.java that reads and processes 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 Hw3.java program will read a list of input numbers looking for a negative grade that acts 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 input grades (up to the negative grade value), your program should display the total number of grades, the highest grade score, the lowest score, and then the number of grades in each letter-grade category. 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
The highest grade = 98
The lowest grade = 51
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

   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-grade category, the 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+. Similarly, there are 4 C's out of 14 grades, 4.0/14.0 is approximately 0.286, and so 29% of the grades are C's and there are 29 asterisks in the bar graph for C.

In this assignment's zip file there is a program Hw3.java that you should complete. In the program I put suggested declarations 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 roundedPerCent = (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 Hw3.java is in the unzipped hw3 folder and use DrJava to compile your Hw3.java file to produce Hw3.class. Then use your mouse to double-click on the file run_test_script.cmd from the unzipped hw3 folder. 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 unzipped hw3 folder.

In the assignment's zip file there is a sub-folder, hw3_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, test_data.txt, containing random grades and then run a demo version of this assignment on the random data. If you copy that data file, test_data.txt, to the unzipped hw3 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 Hw3.java 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 open the file run_test_script.cmd with a text editor, then you will see a line of code that looks like this.

    java Hw3  <  test_data.txt  >  test_script_output.txt

This is a command-line to the Windows operating system telling Windows to run your Java program, Hw3, 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 in the unzipped hw3folder (which now holds the file TestIORedirection.class) and type, after the command prompt, the command

     C:\hw3> 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.

Also in the zip file there is a short document, called "Input, Output and Redirection.pdf", that explains some of the basic ideas behind "I/O redirection" (the document comes from this online Java course).

Turn in a zip file called CS123Hw3Surname.zip (where Surname is your last name) containing your version of Hw3.java.

This assignment is due Tuesday, April 27.