CSci 151: Foundations of computer science II
Home Syllabus Assignments Tests

printable version

Review 7: Stacks & queues

Section 7.1: [1] [2] [3] [4] [5]
Section 7.2: [1] [2] [3] [4] [5] [6] [7] [8]

Problem 7.1.1.

Given the two classes below, what will main print?

public class Worker {
    private String job;

    public Worker(String val) {
        job = val;
    }
    public void setJob(String val) {
        job = val;
    }
    public String getJob() {
        return job;
    }
}
public class Main {
    public static void main(String[] args) {
        Stack<Worker> s = new Stack<Worker>();
        Worker you = new Worker("student");
        Worker me = new Worker("professor");
        s.push(you);
        s.push(me);
        you.setJob("millionaire");
        me = you;
        while(!s.isEmpty()) {
            Worker w = s.pop();
            System.out.println(w.getJob());
        }
    }
}