CS 1723 -- Extended Example
A Simple List of Numbers, with Completed Code
Extended Example -- A List of Numbers With Completed Code:
// ListMain: make use of the List class
public class ListMain {
public static void main(String[] args) {
List list1 = new List();
GetNext getNext = new GetNext();
int license;
while(true) {
license = getNext.getNextInt();
if (list1.contains(license))
System.out.println("TOW " + license);
else {
System.out.println("WARN " + license);
if (!list1.add(license))
System.out.println("*** no room for " + license);
}
list1.dump();
}
}
}
% java ListMain
123456
WARN 123456
Dump of List, Current size: 1, Maximum size: 6, Empty? false, Full? false
123456 End of Dump
987654
WARN 987654
Dump of List, Current size: 2, Maximum size: 6, Empty? false, Full? false
123456 987654 End of Dump
333333
WARN 333333
Dump of List, Current size: 3, Maximum size: 6, Empty? false, Full? false
123456 987654 333333 End of Dump
987654
TOW 987654
Dump of List, Current size: 3, Maximum size: 6, Empty? false, Full? false
123456 987654 333333 End of Dump
444444
WARN 444444
Dump of List, Current size: 4, Maximum size: 6, Empty? false, Full? false
123456 987654 333333 444444 End of Dump
333333
TOW 333333
Dump of List, Current size: 4, Maximum size: 6, Empty? false, Full? false
123456 987654 333333 444444 End of Dump
555555
WARN 555555
Dump of List, Current size: 5, Maximum size: 6, Empty? false, Full? false
123456 987654 333333 444444 555555 End of Dump
666666
WARN 666666
Dump of List, Current size: 6, Maximum size: 6, Empty? false, Full? true
123456 987654 333333 444444 555555 666666 End of Dump
444444
TOW 444444
Dump of List, Current size: 6, Maximum size: 6, Empty? false, Full? true
123456 987654 333333 444444 555555 666666 End of Dump
777777
WARN 777777
*** no room for 777777
Dump of List, Current size: 6, Maximum size: 6, Empty? false, Full? true
123456 987654 333333 444444 555555 666666 End of Dump
^C (ctrl-C to terminate)
%
Here are the two other classes (with completed code in
boldface):
// List: a simple list of numbers
public class List {
// fields
private int[] listArray; // the actual list
private int currentSize; // current number of elements
private final int INITIAL_SIZE = 6; // initial maximum size
// List: constructor
public List () {
currentSize = 0; // not needed (already done)
listArray = new int[INITIAL_SIZE]; // allocate array
}
// isEmpty: is the list empty? (initially true)
public boolean isEmpty() {
return size() == 0;
}
// isFull: is the list full? (initially false)
public boolean isFull() {
return size() == listArray.length;
}
// add: add x to the list. No check for duplicates
public boolean add(int x) {
if (currentSize < listArray.length) {
listArray[currentSize++] = x;
return true;
}
return false;
}
// contains: is x in the list?
public boolean contains(int x) {
for (int i = 0; i < currentSize; i++)
if (listArray[i] == x) return true;
return false;
}
// size: return number of elements in the list
public int size() {
return currentSize;
}
// dump: print data for debugging
public void dump() {
System.out.println("Dump of List, Current size: " + size() +
", Maximum size: " + listArray.length +
", Empty? " + isEmpty() +
", Full? " + isFull());
for (int i = 0; i < currentSize; i++)
System.out.print(" " + listArray[i]);
System.out.println(" End of Dump\n");
}
}
// GetNext: fetch next char or unsigned integer
import java.io.*;
public class GetNext {
private Reader in; // internal file name for input stream
// GetNext: constructor
public GetNext () {
in = new InputStreamReader(System.in);
}
// getNextChar: fetches next char
private char getNextChar() {
char ch = ' '; // = ' ' to keep compiler happy
try {
ch = (char)in.read();
} catch (IOException e) {
System.out.println("Exception reading character");
}
return ch;
}
// getNextInt: fetch unsigned int
public int getNextInt() {
String s ;
char ch = getNextChar();
while (ch == ' ' || ch == '\n')
ch = getNextChar();
s = "" + ch;
while (Character.isDigit(ch = getNextChar()))
s += ch;
return Integer.parseInt(s);
}
}