Comp 170 Lab 4 - Lists of Numbers (NumList), Sept 25
Goals
- Introduction to Arrays
- Introduction to for loops
- Random numbers
Overview
This lab deals with a class NumList, which contains a list of
numbers (ints), stored in an array called nums:
private int[] nums;
Not all of the array is necessarily used; only the first currsize
slots are (nums[0] through nums[currsize-1]). The variable currsize
thus represents the length of the NumList, not nums.length.
This way our NumList can
easily grow in length, at least up until the hard limit of nums.length.
The main task of this lab is to grow past that point.
I've given you a starter/demo project to get started. It starts out with nums.length = 5,
and contains the following methods:
- add(int y)
- int get(int n)
- void set(int n, int val)
- int size()
- void fillRandom(int max)
- int sum()
- void print()
The zipped project is here.
You are to do the following:
Implement NumList.max()
max() should return the largest element in the list. The catch is that
potentially all of the numbers can be negative, so initializing your
loop variable "max" to 0 may be too big. I started max for you in the
NumList.java; there I suggest starting with max=nums[0]. Note that max
is undefined for an empty NumList.
Make add() work even when nums[] is full
The trick here is to allocate a new array, say newnums, of larger size
(doubling works, or increasing by 10, or increasing by 1.5 although
that's trickier because of the floating-point conversions):
int newnums[] = new int[newsize];
Next you copy from nums[i] to
newnums[i], for i<currsize. This is done with a straightforward for
or while loop. Finally, you replace nums by newnums:
nums = newnums
does it. Note that there is no need to do anything with the "old" nums.
At this point, you now have space to add the new item, so you go ahead and do it.
The overall add structure looks like:
if (currsize == nums.length) {
// do the space expansion as above to ensure room
}
// now add the usual way; one way or another we know there is space!
nums[currsize] = y;
currsize += 1;
Figure out how to add zero N times
The existing constructors set currsize = 0; that is, the NumList starts
empty. But what if you want to start with N active slots? One way is to
add zero a bunch of times;
for (int i = 0; i<N; i++) NL.add(0);
That works, and I did that in the Counter class,
but it's kind of goofy; create a reasonable way to do this
all at once. One approach is to add a method addMultiple(N), or grow(N). Another is
to modify the second constructor that takes a parameter, so that
NumList NL = new NumList(10);
has the effect that the newly created NL now has currsize=10.
Of course, it will also have a nums.length >= 10.
Use a NumList in a program
Here's the program, started in class Counter:
generate 1000 random numbers in the range 0-9, and
count how many times each number appears.
Start with a NumList called counts, of currsize 10 and with all 10 positions initialized to 0.
Then, for each new random number n,
n = rand.nextInt(10);
increment the value of slot n of counts; that is, of counts.get(n).