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:
- Assume that we have a 15 question quiz (you can make up the answers).
- Prompt the user and read in the number of students in the class.
- Read in the answers for all the students and display the results.
This is a managable chunk of work that you should implement and test
Your code should work with the following data file
classAnswers
- 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:
- What is the size of the array needed?
- How can you compute the index of the array that needs to be
updated. You should be able to do this without the use of any
conditional statements!!.
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
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:
- Your two printouts: Quiz.java and classQuiz.java
- Tar your lab1 directory and e-mail it to your instructor (hughes@roanoke.edu) with
cpsc120 lab1 in the subject