Lab Quiz Solution, Foundations of Computing, Spring, Week 4 -- Thurs Apr 26 2001

Two files contain the following Java code (exactly as it appears here, there is nothing else in the files). The files are compiled by the Java compiler and the resulting Java program is run.

// Ticket.java
public class Ticket {
    static int n = 0;

    public static void total() { 
	System.out.println(n + " tickets have been issued");
    }

    int ticket;

    public Ticket() {
	ticket = n;
	n++;
    }

    public void speak() {
	System.out.println("I have ticket " + ticket);
    }
}

// TicketDemo.java
public class TicketDemo {
    public static void main(String[] args) {
	Ticket a, b, c;
	Ticket.total();
	a = new Ticket(); a.speak(); Ticket.total();
	b = new Ticket(); b.speak(); Ticket.total();
	c = new Ticket(); c.speak(); Ticket.total();
    }
}
  1. What is (are) the class variable(s) in the Ticket class?   n

  2. What is (are) the instance variable(s) in the Ticket class?   ticket

  3. What is (are) the class method(s) in the Ticket class?   total

  4. What is (are) the instance method(s) in the Ticket class?   speak

  5. What output does the program produce?
0 tickets have been issued
I have ticket 0
1 tickets have been issued
I have ticket 1
2 tickets have been issued
I have ticket 2
3 tickets have been issued