CPSC 170 Lab 2: Arrays

Quiz grading

The file Quiz.java contains the skeleton for a program to grade a 5-question multiple-choice quiz. It assumes that the user already has the questions (presumably the quiz itself has been distributed separately); the user simply enters the letters of their answers (a,b,c,d, or e) in response to the program's prompts, and the program then tells the user's score.

Fill in code as indicated by the comments to make the program behave as described above.

What happens if you want to add more questions to the quiz? You could adjust the size of the array and then add another line to initialize each new entry in the array. This could take up a lot of space in your program, and is frankly very cumbersome. Fortnately, there is a shortcut. We can use an initializer list to assign the contents of an array and implicitly determine its size in a single line of code. See page 345 of your text for an example of this.

Modify your program to use an initializer list to declare and initialize key to contain 10 answers. Verify that the rest of your program is flexible enough to handle this without changing any other lines of code.

Command Line Arguments

Since the beginning of CS120, you have been including the following line in your code:

public static void main (String[] args)

Now we finally can start to understand what we have been doing all this time. Looking at that line of code, we can deduce that there is an array of Strings; a numbered collection of strings. Aren't you just dying to know what is in this collection?

Let's find out. Write a class called CmdArgs that simply prints out that list.

Stop reading until you have tried to run your program at least once.

You didn't cheat, did you? This will be much more gratifying if you ran your program at least once.

I bet your program didn't print anything did it? I'm going to give you the benefit of the doubt and assume that your code is correct. So what is happening? Can you amend your code to prove that it is working correctly?

How is that array of Strings supposed to get a value? What role does it play in the program. It is a formal parameter to the main method of your CmdArgs class. Where does the actual parameter come from? That depend on what calls the main method, right?

When the program is invoked, there can be a collection of command-line arguments. Think about the mkdir command. When you want to use this, you actually type:

mkdir cpsc170

In this case, the command is "mkdir" and "cpsc170" is a parameter that is passed into the program. Likewise, when we can pass parameters into our programs. Try this:

  1. Go to the xterm
  2. move to the Labs directory
  3. type "java lab2/CmdArgs test this"

What is really happening here is that you are running the JVM with 3 parameters: "lab2/CmdArgs", "test", and "this". But this has the effect of executing your program with the parameters "test" and "this". Can we specify parameters through Eclipse? You betcha!

  1. With your program highlighted, expand the Run menu and select the run option.
  2. In the dialog that pops up, verify that you are running the correct program: CmdArgs, and then select the "Arguments" tab.
  3. Enter some text in the "Program Arguments" box and press Run.

Putting args to good use

It is a bit of a drag to retype the input every time you want to test your programs. This is especially true when we are working with lots of data - as is often the case when working with arrays. One of the ways we can get around this is to use File I/O.

Recall that we generally set up the scanner object to reads from a special object called "System.in". (By default this is interpreted to mean keyboard input.) However, we learned last term that you can also read input from a file (pp 144-147). There were three key points:

A good use of the args parameter can be to specify a filename that contains the data you wish to test your program against.

Grading a class in batch mode.

Copy the Quiz.java to ClassQuiz.java and make the following adjustments:

This is a managable chunk of work that you should implement and test. Remember that writing small peices of code and testing can help minimize the ammount of time you spend finding errors in your program. Your code should work with the following data files quiz1Answers.txt and quiz2Answers.txt.

Finally, we would like to provide a summary of the class results. Specifically, it would be useful if we knew how many students scored in the A range, the B range, etc. Create an array of integers that keeps a count of how many students scored in each of the 10 point bands - i.e 0-9, 10-19, 20-29 etc. Every time a student score is calculated you should update the count in the appropriate array element. For example, if a student scores a 56, the counter for the 50-59 band should be incremented.

Before you start coding, think carefully about:

When you have graded the quizes for the entire class, you should print out the grade distribution. Your output should look something like the following:

****************
Summary
****************
0-9: 0
10-19: 0
20-29: 2
30-39: 3
40-49: 8
50-59: 2
60-69: 2
70-79: 2
80-89: 1
90-99: 0
100: 0


Hand In: Tar your lab directory and e-mail it to your instructor with cpsc170 lab2 in the subject.