CPSC 170 Lab 0: 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 lab0. Do your work for this lab in the lab0 directory.

Now open Mozilla 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.

Open emacs.

Figure Skating Scores

The Winter Olympics are coming! For this lab you are going to learn about arrays in the context of computing scores for the Figure Skating events. As you may know, after competing, a skater receives a score of 0 to 6 from seven different judges. The high and the low score are dropped and the remaining five scores are averaged to produce the athelete's final score. Write a java program that reads the score from sevne judges from the keyboard and outputs a list of the scores as well as the final score. For each judge you should list both the judge's number and either the score recorded or a message "dropped" if the score was not included in the final score. For example output may appear as follows:
Judge #1: 5.5
Judge #2: Dropped
Judge #3: 4.5
Judge #4: 5.5
Judge #5: 4.5
Judge #6: Dropped
Judge #7: 5.0

Final score: 5.0

Some things to consider:

Implementation

Let's decompose the problem into several steps to implement our solution
  1. Create a file called skater.java that consists of two loops. The first loop reads seven numbers into an array and the second loop should prints the scores out again and compute the average.
  2. The real challenging part of any program is figuring out exactly how to manipulate the data to produce the desired result. You might end up testing a program alot and you don't want to spend all your time typing in numbers. So our next step is to modify the program to read numbers from a file.
  3. One of the advantages of being able to read from a file is that we can quickly provide consistent input to our program. Moreover, we can provide different sets of input to our program that are designed to isolate certain conditions. In the "Things to consider" section, we identified some special cases that our program should handle a) What if there are multiple instances of the high/low score? b) What if all the scores were the same? Create two more data files that could be used to test these conditions called Score2 and Score3
  4. Unfortunately, the name of our test file is hardwired into the program. You could edit the source code when you want to test a different data file, but then you would have to recompile each time. To get by this, we will use a command line argument. Notice that in you declaration of the main method, there is a parameter passed in called args. This parameter is an array of Strings. When we run a program, we are used to saying something like:

    java myProg

    Alternatively, we can specify a series (an array) of parameters:

    java myProg foo test something

    In this case, "foo" "test" and "something" are passed into the program as command line arguments. They are automatically stored in the array of Strings called args. So:

    args[0] = "foo"
    args[1] = "test"
    args[2] = "something"

    Modify your code so that you can pass in the name of the data file you wish to read as a command line argument.

  5. Now we are ready to get back to the main objective - computing the scores for a figure skating competition. You need to devise a way to isolate a unique high score and unique single low score. Check your solution against all three test conditions and verify that your solution works.
  6. Print this program to turn in.