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.
Clone the repository for this lab from the shared class directory:
After you clone, you can$ hg clone /cslab/class/csci235/labs/lab4
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.
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()
).
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.
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.
fillRandom()
method.
print()
, and call that method to do
case 4. Write the required comment before this method.
enlarge()
to do case 5. The
comment has already been written.
average()
to do the calculation
for case 2. Include the required comment.
reverse()
, with its comment, to do
case 6, reversing the array in place. (This one will have a
postcondition.)
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.
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.
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
You'll be prompted for your password, of course.$ hg clone . ssh://mortimer.snerd@cslab25/csci235/lab4/
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/