CMPU 102 - Assignment 3 - February
23rd, 2017
In this lab you will practice writing a class hierarchy in Java to
produce lists as you did in CMPU 101. You are encouraged to use
NetBeans on this assignment, but you may use DrJava if you want. Please
read through Part I of the lab before starting to code. You
can use either the System.out.println() calls given in TestIList.java,
or you can convert these to JOptionPane showMessageDialog calls if you
prefer.
Part I: Structural Recursion Using an Interface
- Download the zipped starter project called Assign3 from the
course web page.
- Uncompress the Assign3 project in your cs102 directory.
- Launch NetBeans and open the project named Assign3.
- The
project includes the following Java source files: TestIList.java,
IList.java, MTList.java, and ConsList.java. The main method is in the
TestIList class.
- The file called IList.java
contains the method stubs that must be written in any class that
implements IList. There should be 2 method stubs that are not
commented out. These were shown in class on 2/22/17 and are the
length method and the sum method.
The length and sum methods are also written in the MTList and ConsList
classes, so you should be able to run the TestIList.java file right
away.
The IList file also contains commented out method stubs that you can
uncomment, one at a time, as you implement each method body in the
classes that implement IList.
- When you run the TestIList class for the first time, you should
see output that looks somewhat like this (with different random
numbers):
run:
The list of
random numbers: 42 12 18 31 69 82 59 28 18 28 81 65 84 11 31 46 96 52
90 9
The length of
the list is 20
The sum of the
numbers in the list is 952
BUILD
SUCCESSFUL (total time: 0 seconds)
Note
how the random numbers are generated in the generateNumber method of
the TestIList class. Math.random() outputs a double in the range 0 to
1, (not including 1). You can multiply the result by the range of
numbers you want to produce, adding 1 to make the returned number in
the range 1...100. The number returned by calling Math.random()
multiplied by 100 and adding one is then cast to an int.
Part II: Implementing new methods of the IList interface
IMPORTANT: Test
each method after you have written implementations for both the
ConsList and MTList classes, before going on to implement the next
method. In the
file TestIList,
we've implemented code to test all the methods specified by the IList
interface.
- Uncomment the line public IList
doubleEach(); in
the IList interface and then implement the method in both the MTList
and ConsList classes. This method should return the IList that
contains all the numbers in the list it is called on, with each number
multiplied by 2. It may help to write each MTList method before
you write each Conslist method.
After implementing doubleEach() in both subtypes of IList, uncomment
the lines to test doubleEach in the TestIList class.
- Uncomment the line public IList
onlyEvens(); in the IList interface and then implement
the method in
both the MTList and ConsList classes. This method should return
the IList that contains only the even numbers in the list it is called
on.
After implementing onlyEvens() in both subtypes of IList, uncomment the
lines to test onlyEvens in the TestIList class. When you run TestIList
after adding the implementations of the doubleEach and onlyEvens
method, the output should look something like this:
run:
The list of random numbers: 7 85 17 14 10 29 45 49 23 55 42 12 67 96 85
98 40 77 52 9
The length of the list is 20
The sum of the numbers in the list is 912
The doubled numbers in the random list are 14 170 34 28 20 58 90 98 46
110 84 24 134 192 170 196 80 154 104 18
The even numbers in the random list are 14 10 42 12 96 98 40 52
Part III: Implementing the remaining methods of the IList interface
For this part you will complete the public
IList sort() and public IList
insert(int num)
methods in both classes MTList and ConsList. Refer to the
comments below for how to proceed. These methods both return
ILists and will work together to produce a sorted list.
Here's some helpful information:
- First, it's easy to sort
an empty list, and it's easy to insert an element into the empty list.
Think about what the result of calling sort() and insert(int num) would
be for an empty list, and implement both
of these methods for class MTList.
- Next, it's easy to sort a
constructed list (ConsList), if you think about it: insert the first
element into the sorted rest of the list. Hint: you will implement
insert(int num) next, but while implementing sort(), assume insert(int
num) is already implemented.
- Finally, inserting an
element into a sorted list is easy, too: compare the element to be
inserted with the first element of the list. It either belongs in front
of it, or after it--return the appropriate newly constructed list.
- After implementing sort() and insert(int num) in both subtypes of
IList, uncomment the lines to test the sort method in the TestIList
class.
- Save and run your project. Your output should look
something like that shown below. Here is a sample output:
run:
The list of random numbers: 7 85 17 14 10 29 45 49 23 55 42 12 67 96 85
98 40 77 52 9
The length of the list is 20
The sum of the numbers in the list is 912
The doubled numbers in the random list are 14 170 34 28 20 58 90 98 46
110 84 24 134 192 170 196 80 154 104 18
The even numbers in the random list are 14 10 42 12 96 98 40 52
The random list is
7 85 17 14 10 29 45 49 23 55 42 12 67 96 85 98 40 77 52 9
The sorted list is
7 9 10 12 14 17 23 29 40 42 45 49 52 55 67 77 85 85 96 98
BUILD SUCCESSFUL (total time: 0 seconds)
Part IV Submitting Your
Work
Compress your NetBeans Project folder and submit it at the
"Assignment 3 submission link" on the course Moodle page.
The original version of this
assignment was written by Marc Smith and Jim Ten Eyck.