CSCI A202 - Introduction to Programming (II)

Lecture 13: Arrays, Vectors, Stacks, Recursion

At the beginning of the lecture we will review:

We will also go over the use of as object references and as invocations to constructors (from lecture notes 9 and, especially, 11).

In the context of assignment 6 we will briefly review:

During the lecture we will also describe assignment 7 to be posted tonight.

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 Vectors.

Arrays have drawbacks:

For the convenience of arrays without their drawbacks, Java provides vectors: Some disadvantages: Java defines the Vector class in the java.util package.

Commonly used Vector methods:

In assignment 7 you will solve three problems.

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 stacks with Vectors.

A stack has the following methods:

A stack is a last-in-first-out kind of object, just as a stack should be.

Here's the code we will develop:

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%
The third problem in the new assignment (7) will ask you to define a class whose objects can calculate a few recurrences.

In class we will develop something similar that will compute the following functions:

The code:
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%
You get the idea.