Lecture 13: Arrays, Vectors, Stacks, Recursion
At the beginning of the lecture we will review:
super
and
this
In the context of assignment 6 we will briefly review:
We have covered the very essential, most important aspects of the language.
The last aspects of inheritance and overriding will come in quite handy next
time, on Thursday, when we will simulate a Frame
and explain why
you need to redefine paint()
and how it all works.
It will be a simple, but hopefully illuminating, example.
Now we move to Vector
s.
Arrays have drawbacks:
Object
Vector
class in the java.util package.
Commonly used Vector
methods:
void addElement (Object obj)
boolean contains (Object obj)
Object elementAt (int index)
int indexOf (Object obj)
int indexOf (Object obj, int index)
void insertElementAt(Object obj, int index)
boolean isEmpty ()
void removeElement (Object obj)
void removeElementAt(int index)
void setElementAt (Object obj, int index)
int size ()
One will involve designing a small hierarchy of classes.
The second one will ask you to implement part of Vector
but
with arrays.
We will go in class over implementing stack
s with
Vector
s.
A stack
has the following methods:
void push(Object obj)
Object pop()
Object top()
boolean empty()
stack
is a last-in-first-out kind of object, just as a
stack should be. Here's the code we will develop:
The third problem in the new assignment (7) will ask you to define a class whose objects can calculate a few recurrences.tucotuco.cs.indiana.edu% emacs Stack.java tucotuco.cs.indiana.edu% cat Stack.java import java.util.*; class Stack { Vector a = new Vector(); void push(Object obj) { a.addElement(obj); } Object pop() { Object aux = top(); a.removeElementAt(a.size() - 1); return aux; } Object top() { return a.elementAt(a.size() - 1); } boolean empty() { return a.isEmpty(); } public static void main(String[] args) { Stack s = new Stack(); s.push("One"); s.push("Two"); System.out.println(s.top()); s.push("Three"); System.out.println(s.top()); s.pop(); System.out.println(s.top()); System.out.println("It is " + s.empty() + " that the stack is empty."); } } tucotuco.cs.indiana.edu% javac Stack.java tucotuco.cs.indiana.edu% java Stack Two Three Two It is false that the stack is empty. tucotuco.cs.indiana.edu%
In class we will develop something similar that will compute the following functions:
int sum(n)
the sum of the first
n >= 0
integers
int fact(n)
the product of the
first n >= 0
integers
boolean odd(int n)
and
boolean even(int n)
two mutually
recursive functions that determine if a number n
is odd
or even
You get the idea.tucotuco.cs.indiana.edu% emacs Computer.java tucotuco.cs.indiana.edu% cat Computer.java public class Computer { int sum(int n) { if (n == 0) return 0; else return n + sum(n - 1); } int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); } boolean odd(int n) { if (n == 0) return false; else return even(n - 1); } boolean even(int n) { if (n == 0) return true; else return odd (n - 1); } public static void main(String[] args) { Computer a = new Computer(); System.out.println("Sum of first 10 integers is " + a.sum(10)); System.out.println("Product of first 6 integers is " + a.fact(6)); System.out.println("It is " + a.odd(5) + " that 5 is odd."); System.out.println("It is " + a.even(12) + " that 12 is even."); System.out.println("It is " + a.even(3) + " that 3 is even."); } } tucotuco.cs.indiana.edu% javac Computer.java tucotuco.cs.indiana.edu% java Computer Sum of first 10 integers is 55 Product of first 6 integers is 720 It is true that 5 is odd. It is true that 12 is even. It is false that 3 is even. tucotuco.cs.indiana.edu%