Comp 170 Lab 2 - Lists of Numbers (NumList), Feb 1

Goals

Overview

For this lab you are to create an object NumList, which contains a list of numbers (ints), stored in an array called nums:
    private int[] nums;

I've given you a starter/demo project to get started, that creates arrays of fixed size 5, and contains add(int y) and print() methods. The entire zipped project is here; the java file alone is here.

There are two ways to put numbers into a NumList: you can add() them one at a time, up to the array capacity (done in the demo), or you can call fillrandom(int max) to fill the array with random numbers in the range 1..max (you are to implement this). Initially the array is just empty slots. You can either add() numbers one at a time, or call fillrandom() to fill the whole array at once. Because at any given moment you might be using the add() method and not be finished yet, in general the array will only go up to position pos, where pos <= nums.length. Thus, the field pos represents the current "size" of the NumList: not the size of the array itself (which doesn't change), but how much data has been added.

Capacity is to be specified in the constructor. There is no provision for resizing. The constructor will contain something like
    nums = new int[size];
There is no loop in the constructor.

Adding numbers

If you create a NumList of size, say, 5, then initially it has nothing in it. We keep track of the current position with a field variable pos, initially 0. When you add a number to the NumList, it goes in the next available position; you can not specify, for example, that the next value is to go into position 8, as in A[8] = y. Take a look at the starter project to be sure you understand how add() works. If pos == nums.length, then the array is full and so adding anything else simply has no effect (an error message prints out). Otherwise pos represents the position of the next number to be added; after adding, pos is incremented.

The intent of fillRandom(int max) is to completely refill the array with random values, that is, overwriting anything that has been inserted with add(). That is, fillRandom will fill positions 0 through nums.length - 1. (It is a trivial change, however, to only fill positions starting at pos instead of 0, thus preserving anything already added.) The demo code contains the creation of a Random object, rand; in your loop you will then assign
    nums[i] = rand.nextInt(max);
That is, each time you call rand.nextInt(max) it returns a new random integer in the range 0 to max-1.

Methods

You are to provide the following methods to do the indicated things with the numbers:

print(): prints the contents of the array, from position 0 to position pos-1. Provided for you.

revprint(): prints the contents of the array in reverse order.

sum(): returns the sum of all the entries

max(): returns the largest entry. The array must be nonempty to work.

Take a look at the loop in print(), provided for you. The array's occupied positions are nums[0] through nums[pos-1], inclusive. The loop is

         int i=0;
         while (i<pos) {
            System.out.println(i + ":  " + nums[i]);
            i++;    // alternative shorthand for i=i+1
        }
Note how starting at i=0, continuing as long as i<pos, and incrementing i at the end, all work together to make sure that the slots printed are nums[0] through nums[pos-1]. We could change the loop condition to while (i<=pos-1); that would be exactly equivalent (why?). But the use of "less than N" in loops, to mean continuing until position N-1, is very common.

sum(): Here's a simple loop to add up the numbers 0..N-1. The sum() method is related; it's adding up the numbers in the array. In the sum() method, the loop condition would be while (i<pos).

    int sum = 0;
    int i = 0;
    while (i<N) {
        sum = sum + i;
        i = i+1;
    }
Inside a function, this would be followed with return sum;.

The max() function is similar; you're going to keep a variable max representing the maximum seen so far, and for the ith element of the array you're going to do something like:

	if (nums[i] > max)
	    max = nums[i];		// new max value found
This goes into a while loop, again while (i<pos). One minor catch is what you start with as the value of max. You can set max=0 if you're sure your list has no negative values, but it's safer if you set max=nums[0]. Do you see why max() doesn't make sense for an empty array, but sum() of an empty array is fine (and is simply zero)?

Email me your completed NumList.java file (do this even if you also show it to me).