[6 pts]
Given the two classes below, what will main
display?
public class Worker { private String job; public Worker(String s) { job = s; } public void setJob(String s) { job = s; } public String getJob() { return job; } } | public class Main { public static void main(String[] args) { Stack<Worker> s = new Stack<Worker>(); Worker gwb = new Worker("president"); Worker bho = new Worker("senator"); Worker you = new Worker("student"); s.push(gwb); s.push(bho); s.push(you); gwb.setJob("none"); you = bho; you.setJob("pres"); bho = gwb; System.out.println(s.pop().getJob()); System.out.println(s.pop().getJob()); System.out.println(s.pop().getJob()); } } |
[8 pts] Suppose we remove from the heap whose tree diagram is below. What will its tree diagram then be?
[8 pts] Suppose we add 3 into the heap represented by the array below. What will the array then be?
[8 pts]
Suppose that we have a Manager
class that implements the
ActionListener
interface.
The class has a no-parameter constructor.
For each of the following fragments, indicate whether it would
compile (at least one would), and for those that would not,
indicate what the compiler would complain about.
a. | void reg(JButton b, Manager m) { b.addActionListener(m); } void run() { JButton b = new JButton("OK"); reg(b, new Manager()); } | b. | void reg(JButton b, ActionListener m) { b.addActionListener(m); } void run() { JButton b = new JButton("OK"); reg(b, new Manager()); } |
c. | void reg(JButton b, Manager m) { b.addActionListener(m); } void run() { JButton b = new JButton("OK"); reg(b, new ActionListener()); } | d. | void reg(JButton b, ActionListener m) { b.addActionListener(m); } void run() { JButton b = new JButton("OK"); reg(b, new ActionListener()); } |