CPSC 170 Lab 0: Arrays
Getting Started
The first thing that we need to do is to set up your directories for CPSC170
and make Eclipse recognize them.
- Open an xterm.
- In your home directory, create a subdirectory called cpsc170 (mkdir).
- Open Eclipse.
- From the File menu, select the Switch Workspace option.
- Alter the path to reflect the cpsc170 directory you just created and
press ok. Eclipse will appear to restart. (You may need to close the
welcome screen)
- From the File menu, expand the New option and select
Project.
- In the following dialog, verify that "Java Project"
is hightlighted and select "next".
- In the "Project Name" field, type "Labs" (Note the capital "L") and
press Finish.
- Repeat this process to create an "Assign" project.
- In the Labs project, create a package called lab0
(note the lowercase "l"). Observe the JRE being used - it should read jdk
1.6. If it doesn't, you will need to re-import the RCEclipsePrefs file that
you used last term
Now open Firefox and go to the
CPSC170 home page at http://cs.roanoke.edu/CPSC170A. Bookmark this page, then
follow the link for lab0 to open this document
in the browser.
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 pp 381-382 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.
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.
String args[]
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:
- Go to the xterm
- move to the Labs directory
- type "java lab0/CmdArgs test this"
What is really happening here is that you are running the JVM with 3
parameters: "lab0/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!
- With your program highlighted, expand the Run menu and select
the run option.
- In the dialog that pops up, verify that you are running the correct
program: CmdArgs, and then select the "Arguments" tab.
- 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 246-249).
There were three key points:
- Construct your scanner with a File opbject.
- Import java.io.*
- Your main method is declared so that it "throws IOException".
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:
- Your program should get the name of the input file from args.
- The first entry in the input file is the number of questions.
- This is followed by a sequence of letters that represents the key.
- Next you should read in the number of students in the class.
- Finally 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 files
quiz1Answers and
quiz2Answers
- 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:
- 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 lab0 directory and e-mail it to your instructor
(hughes@roanoke.edu) with cpsc170 lab0 in the subject