Lab 4: Writing simple methods

The primary goal of this lab is to get practice with basic methods. The logic is very simple in this lab; the point is to become familiar with the syntax of methods and how to document them. This lab also gives you an opportunity for more practice with Mercurial.

1. Setup

Clone the repository for this lab from the shared class directory:

$ hg clone /cslab/class/csci235/labs/lab4
After you clone, you can cd into the lab4 directory that holds your new working copy. Today you will be working on the ArrayManip.java that you saw in class last week.

If you try hg status, you should see nothing, because the working copy and the repository match. If you try hg log, you should see the history of revisions that were made over two days of class and in preparation for this lab.

2. Overview

You are going to be filling in some methods, writing new methods, and modifying the cases within the main() method's switch statement to use the methods you are writing. Before you start making changes, take a few minutes to review the code. These are the key changes that I have already made to the main() method (pages 2 and 3 of the printout):

Before main() (on the first page) there are some comments about methods to write, plus the complete method fillRandom() and the skeleton for isSorted(). If you look closely, you may notice that the places for writing methods are in alphabetical order.

The comments before fillRandom() and isSorted are examples of how we will document (describe) each method we write. Here is the comment for isSorted():

    /**
     * Test whether an array is sorted (in ascending order).
     * @param a The array to test.
     * @return Whether a is sorted.
     */

Note how it is formatted and that it starts with '/**'. The first line of text is a sentence stating what the method should do. The next two lines have tags; they describe (in order) the parameters of the method and the value that it returns.

The comment for fillRandom() is similar. There is no @return tag, because the return type is void. The POSTCONDITION line states what is true after the method returns. Some methods will have a PRECONDITION line to say what must be true before the method is called (see the comment for enlarge()).

3. What to do

Open ArrayManip.java in an editor and add your names to the @author tag in the header comment.

Make the following changes, one at a time. As you finish each, test to make sure it works correctly, and commit the change to your repository. Do as many of the changes as you have time for.

  1. Complete the isSorted() method so that case 8 prints the correct answer. An array is sorted when every element is greater than or equal to the element preceding it.
  2. Change case 7 to fill the array using the fillRandom() method.
  3. Write a method print(), and call that method to do case 4. Write the required comment before this method.
  4. Write the method enlarge() to do case 5. The comment has already been written.
  5. Write the method average() to do the calculation for case 2. Include the required comment.
  6. Write the method reverse(), with its comment, to do case 6, reversing the array in place. (This one will have a postcondition.)
  7. Write the heading and body of the method changeLength() that makes a new array that is either larger or smaller than the one passed to it. Modify case 5 to use this method instead of enlarge(), and fix the description of the operations.

4. Turn in

Remove your .class file. Then create a script file in which you compile and run your program. Do enough operations to demonstrate each of the methods you wrote. After you have run the program, include hg log before you end the script.

Use handin to turn in your source file and the script file as lab4. Do not turn in a printed copy.

5. Cloning a repository to your account

You can copy the repository (with all of its history) to your account as a Mercurial clone. Assuming that your account name is mortimer.snerd and that you already have a subdirectory named csci235, you'd do the command

$ hg clone . ssh://mortimer.snerd@cslab25/csci235/lab4/
You'll be prompted for your password, of course.

The script file is not in your repository. If it is named typescript you can copy it with the command

$ scp typescript mortimer.snerd@cslab25:csci235/lab4/

Cary Gray
Last modified: Sun Sep 21 22:10:41 CDT 2014