CPSC 170 Lab 1: Arrays

Getting Started

Open an xterm. In your home directory, create a subdirectory called cpsc170. Go (cd) to cpsc170 and create another subdirectory called labs. Go (cd) to labs and create another subdirectory called lab1. Do your work for this lab in the lab1 directory.

Now open Firefox and go to the CPSC170 home page at http://cs.roanoke.edu/CPSC170A. Bookmark this page, then follow the link for lab1 to open this document in the browser.

Open emacs.

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. Try altering the size of the array by adding more elements to the initializer list. Verify that your program still works without changing any other lines of code. Remember, one of the goals of this class is to focus on code that is flexible and generalizable . When it is complete, print the program to turn in. Make sure that your name is included in the comments header at the top of the document.

I/O Redirection - Batch Processing.

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 I/O redirection at the Linux command line. Recall that your scanner object reads from a special object called "System.in". By default this is interpreted to mean keyboard input. However, when you run your program, you can Redirect the input to come from a file instead. This is accomplished by using the "<" command in the Linux shell. For example, if you type the following at the command prompt:
java Quiz < answers
Linux interprets this as an instruction to run the java interpreter to execute Quiz.class with input redirected to come from the file "answers".

Use emacs to create an "answers" file and verify that you can run your Quiz program with redirected input. Remember that (by default) the Scanner reads input that is delimited by whitespace (i.e. a space character, a tab or a carriage return).

What happens if your data file contains too many answers? too few?

Just like we can redirect input to come from a file, we can redirect the output (System.out) from the screen into a file. To accomplish this, we append ">" to our command line statement. Try the following and be able to explain what happens:

java Quiz < answers > results
java Quiz > results  < answers
java Quiz > results

Grading a class in batch mode.

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

When it is complete, print the program to turn in. Make sure that your name is included in the comments header at the top of the document.

Hand in: