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.
  1. Open an xterm.
  2. In your home directory, create a subdirectory called cpsc170 (mkdir).
  3. Open Eclipse.
  4. From the File menu, select the Switch Workspace option.
  5. 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)
  6. From the File menu, expand the New option and select Project.
  7. In the following dialog, verify that "Java Project" is hightlighted and select "next".
  8. In the "Project Name" field, type "Labs" (Note the capital "L") and press Finish.
  9. Repeat this process to create an "Assign" project.
  10. 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:
  1. Go to the xterm
  2. move to the Labs directory
  3. 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!

  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 246-249). 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:

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: