CIS 3142 Advice — Chapter 1

CISC 3142
Programming Paradigms in C++
Abstraction

Reversing Two Integers

The Original Code

	int a, b;
	a = scanner.nextInt();
	b = scanner.nextInt();

	System.out.println(b);
	System.out.println(a);

Abstraction — Introduce an Array — We May Eventually Want to Reverse and Print More than Two Values

final int CAPACITY = 100;
int [] arr = new int[CAPACITY];
int size = 0;
while (scanner.hasNextInt())
	arr[size++] = scanner.nextInt();
for (int i = size-1; i >= 0; i--)
	System.out.println(arr[i]);

Abstraction — Implement an ArrayList — Allow for a Truly Arbitrary Number of Elements

ArrayList aList = new ArrayList();
while (scanner.hasNextInt())
	aList.add(scanner.nextInt());
for (int i = aList.size()-1; i >= 0; i--)
	System.out.println(aList.get(i));
class ArrayList {
	ArrayList(int capacity) {
		this.capacity = capacity;
		arr = new int [capacity];
	}
	ArrayList() {this(100);}

	void add(int val) {
		checkSize();
		arr[size++] = val;
	}
	int get(int index) {return arr[index];}
	void set(int index, int val) {arr[index] = val;}
	int size() {return size;}
	void checkSize() {
		if (size < capacity) return;
		capacity *= 2;
		int [] newArr = new int[capacity];
		for (int i = 0; i < size; i++)
			newArr[i] = arr[i];
		arr = newArr;
	}

	int [] arr;
	int size = 0, capacity;
}

Abstraction — Use a Generic Array — Allow Other Element Types to be Reversed

ArrayList i_aList = new ArrayList();
int n = scanner.nextInt();
while (n >= 0) {
	i_aList.add(n);
	n = scanner.nextInt();
}
for (int i = i_aList.size()-1; i >= 0; i--)
	System.out.println(i_aList.get(i));

System.out.println();

ArrayList s_aList = new ArrayList();
while (scanner.hasNext())
	s_aList.add(scanner.next());
for (int i = s_aList.size()-1; i >= 0; i--)
	System.out.println(s_aList.get(i));
class ArrayList {
	ArrayList(int capacity) {
		this.capacity = capacity;
		arr = (T [])new Object[capacity];
	}
	ArrayList() {this(100);}

	void add(T val) {
		checkSize();
		arr[size++] = val;
	}
	T get(int index) {return arr[index];}
	void set(int index, T val) {arr[index] = val;}
	int size() {return size;}
	void checkSize() {
		if (size < capacity) return;
		capacity *= 2;
		T [] newArr = (T [])new Object[capacity];
		for (int i = 0; i < size; i++)
			newArr[i] = arr[i];
		arr = newArr;
	}

	T [] arr;
	int size = 0, capacity;
}

Abstraction — Introduce a Stack — Clarifying the Semantics of the Reversal

Stack i_stack = new Stack();
int n = scanner.nextInt();
while (n >= 0) {
	i_stack.push(n);
	n = scanner.nextInt();
}
while (!i_stack.isEmpty())
	System.out.println(i_stack.pop());

System.out.println();

Stack s_stack = new Stack();
while (scanner.hasNext())
	s_stack.push(scanner.next());
while (!s_stack.isEmpty())
	System.out.println(s_stack.pop());
class Stack {
	void push(T val) {aList.add(val);}
	T pop() {
		T hold = aList.get(aList.size()-1);
		aList.remove();
		return hold;
	}
	boolean isEmpty() {return aList.size() == 0;}

	ArrayList aList = new ArrayList();
}
class ArrayList {
	ArrayList(int capacity) {
		this.capacity = capacity;
		arr = (T [])new Object[capacity];
	}
	ArrayList() {this(100);}

	void add(T val) {
		checkSize();
		arr[size++] = val;
	}
	void remove() {size--;}
	T get(int index) {return arr[index];}
	void set(int index, T val) {arr[index] = val;}
	int size() {return size;}
	void checkSize() {
		if (size < capacity) return;
		capacity *= 2;
		T [] newArr = (T [])new Object[capacity];
		for (int i = 0; i < size; i++)
			newArr[i] = arr[i];
		arr = newArr;
	}

	T [] arr;
	int size = 0, capacity;
}

Abstraction — Using the Built-in structures

Going back to the original two value reverse print … even if all the above structures are already available, and don't have to be implemented, using the abstractions seems overkill.
	Stack stack;
	stack.push(scanner.nextInt());
	stack.push(scanner.nextInt());

	System.out.println(stack.pop());
	System.out.println(stack.pop());

Code Used in this Lecture