All puzzles
Per page
P
Instance variables · Constructor · Mutator

A bank account

Write the Account class in this order: two instance variables, the constructor, then deposit(). The owner's name is passed in to the constructor, and the balance starts at 0. deposit() adds to the balance.

After Account a = new Account("Kim"); and a.deposit(25.5);, the balance is 25.5.

12 strips: P1 to P12

balance += amount;
P1
owner = name;
P2
public Account(String name) {
P3
public class Account {
P4
}
P5
public void deposit(double amount) {
P6
balance = 0.0;
P7
public void Account(String name) {
P8
}
P9
private String owner;
P10
}
P11
private double balance;
P12

Answer key: P. A bank account

Order: P4, P10, P12, P3, P2, P7, P9, P6, P1, P11, P5. Extra: P8

Lines with the same mark can go in either order.

public class Account {
	private String owner;
	private double balance;
	public Account(String name) {
		owner = name;
		balance = 0.0;
	}
	public void deposit(double amount) {
		balance += amount;
	}
}

Extra strip: public void Account(String name) {. A constructor has no return type. With void, this is a method named Account, and new Account("Kim") does not compile.