Fall 2012, The College of Saint Rose

Due: 11:59 PM Monday, October 22, 2012

In this lab, you will practice using loops by implementing a simple grade summary calculator, first as a Visual Logic flowchart, then as a Java program.

You are encouraged, but not required, to work with a partner on this lab.

There are some lab questions in this assignment. You should answer these in a comment at the end of your Java program.

Your program will be responsible for reading in the names and grades for a group of students, then reporting some statistics about those grades. The statistics to be gathered are the number of scores entered, the highest score and the name(s) of the student(s) who earned that score, the lowest score and the name(s) of the student(s) who earned that score, and the average score for the class.

So if the names and scores are as follows:

```Luke 76
Hermione 99
Wally 34
Linus 99
Penny 25
```

then your program would print output:

```There were 5 scores, averaging 66.6.
The lowest score was 25 by Penny.
The highest score was 99 by Hermione and Linus.
```
• The program will need loops in two places: (i) to be able to read in data repeatedly, and (ii) to ensure that any erroneous input is detected and re-read until a valid input is obtained.
• The program should read in names (single-word names are sufficient here) and scores until the name is entered as "done", at which time we stop reading names and scores and report the final statistics.
• All scores should be in a valid range 0 to 100. If a score is entered outside of that range, your program should issue an appropriate message and re-read only the score (not the name, we already know that!) until a valid score is entered.
• The program will need to keep track of a running total of the number of scores entered and the total number of points (to be used later to compute the average).
• The program will need to keep track of the highest (and lowest) score seen so far and the name(s) of the students who earned that highest (and lowest) score.
• Correct initialization of the variables you will use to keep track of high and low scores is essential to get this right. Note that the first score read in should become both the high and low score after the first time through the main loop. Think about how you can initialize those variables so that they will be set to the first score on the first loop iteration without treating that iteration as a special case (that is, the body of your loop does exactly the same thing on the first iteration as it will do on all subsequent iterations).
• Note that if there is a tie (among any number of students) for the highest and/or lowest score, all students who tied the high or low should have their name included in the printouts at the end. Hint: think string concatenation.
• If no valid names and scores are entered, a special message "You did not enter any scores!" should be printed rather than potentially erroneous stats.

Question 1: Gaddis defines a term for a special value that is used to terminate a loop, as the special name "done" does here. What is that term? (1 point)

Question 2: By using the special name "done" in this manner, we have placed a restriction on the valid names our students may have. What name would cause a problem for our program? (1 point)

Question 3: Describe in a few sentences your approach to the "correct initialization" described in the bullet item above. Why did you choose the initial values you did and how do they ensure that the first score entered will become the new high and low score without treating that as a special case. (2 points)

Getting Set Up

• Create a folder for your work on this lab. Lab6 might be a good name.
• Follow the usual procedure (you can see all the detailed steps on the Lab 1 page) to create a new BlueJ project called Lab6Progs. We will not create any Java classes just yet.

Implementing in Visual Logic

There is quite a bit to get right with the logic of this program, so you are required to develop a Visual Logic flowchart to help you to organize and visualize your algorithm before moving on to Java.

While it is not required, it is likely beneficial to start out with pencil and paper, drawing a flowchart and/or pseudocode for the program before even launching Visual Logic.

If you do a good job with the flowchart, not only will you earn a significant portion of the credit for this lab which is tied to the flowchart, but you should have an easier time getting your Java program to work once you are basing it off of a working Visual Logic flowchart.

Note: you must be able to save your flowchart for submission. If you are unable to do this for any reason, either find a partner who can and work with him/her at least on this first part, or use the Visual Logic installation on the two PCs in the Albertus 400 office suite.

A couple of reminders about Visual Logic:

• You can use `<>` to compare strings or numbers for inequality, and the `=` operator to compare for equality.
• You may use "Dialog Box" or "Console" input and output, your choice.

While there are many choices about how to go about adding functionality to your flowchart, you are strongly encouraged to develop your flowchart incrementally. Here is one possible order of attack. You should test your flowchart each time you add some new components to ensure the functionality you just added works and that you have not broken was already working previously.

2. Read in names inside a loop (forever).
3. Read in names until someone enters "done" as the name.
5. Add a loop to do error checking of those scores and repeatedly prompt for a score until a valid number is entered.
6. Make sure you do not prompt for a score at all after "done" was entered for the name.
7. Add a variable to keep track of the number of scores read in. Initialize it before your main loop, increment it in an appropriate place inside your main loop, and print it out after the loop.
8. Add a variable that will track the total number of points, to be used for computing the average. Initialize it and add to it in appropriate places. Print it out after the loop.
9. Use the number of scores and the total points to compute an average score. Replace your printout of total points with a printout of the average.
10. Add a variable to keep track of the highest score. Be careful to initialize it correctly. Update it in your main loop any time someone has a new high score. Print it out at the end.
11. Add another variable to keep track of the name of the person who has that highest score. Don't worry yet about handling ties for the highest score. In that case, just remember one of the names and print it at the end.
12. Now worry about that case. When you detect a tie for the high score, you should append the new name to the string containing the name(s) of the high score students.
13. Do the same steps for low scores.
14. Add a check at the end to make sure you only print the statistics if at least one score was entered before "done" was entered.
15. Bask in the glory of a working flowchart (then convert it to Java).

Question 4: When you created your While Loop blocks in your flowchart, you had to choose between a "pre-test" and a "post-test" loop. Which did you use, and why? (1 point)

Question 5: Some loops repeat for an unknown number of times ("variable count" loops) while others repeat a pre-determined number of times ("fixed count" loops). Which of your loop structures are variable count and which are fixed count? (1 point)

Converting to Java

With a working flowchart in hand, it should be a straightforward conversion into Java. Call your Java program GradeSummary.java. You should use terminal I/O with a Scanner for this assignment (no JOptionPanes for this one).

• Prompt for and read in both the name (which you may still assume consists of a single word) and score (which you may assume is an integer) in one step. That is, issue one prompt, then have two calls to your Scanner: one to read the name, the next to read the score. If the name is read as done, then do not attempt to read any score given on that line (nor report any error if no such score is present on the input).
• Don't forget to change any comparisons of String values to use the equals method, and any comparisons of numeric values to use `==` and `!=` as needed.
• Define named constants to represent the highest possible score and the special name that is used to stop the loop. Those definitions should be the only places in the program where I see the int literal 100 or the String literal "done".
• Remember to take the proper precautions when computing your average (which should be a double) from the scores (which are all ints). When you print your average, use a DecimalFormat object to make sure it prints with exactly one digit after the decimal point.

Style and Documentation Reminders

Before you submit your program, make sure it conforms to our guidelines for style and documentation.

In particular, you should have a comment at the top of each class that describes your program and has your name (and that of your partner if you are working with someone), the course number and section (02 for 11:15, E1 for 4:10). You should have comments throughout your programs describing your variables and any non-obvious Java statements or groups of statements.

All identifiers (class names and variable names) should be meaningful and conform to Java's naming conventions.

Your code should be nicely formatted, with new lines after any `{` or `}`, and indented as done in class examples.