All puzzles
Per page
Q
this · Mutator · Accessor

A player's score

Write the Player class: two instance variables, the constructor, then addPoints(), then getScore(). The constructor's parameter has the same name as the name instance variable.

After Player p = new Player("Sam"); and p.addPoints(7);, p.getScore() returns 7.

15 strips: Q1 to Q15

}
Q1
public Player(String name) {
Q2
}
Q3
public class Player {
Q4
score += points;
Q5
private int score;
Q6
public void addPoints(int points) {
Q7
private String name;
Q8
return score;
Q9
this.name = name;
Q10
name = name;
Q11
score = 0;
Q12
public int getScore() {
Q13
}
Q14
}
Q15

Answer key: Q. A player's score

Order: Q4, Q8, Q6, Q2, Q10, Q12, Q3, Q7, Q5, Q14, Q13, Q9, Q15, Q1. Extra: Q11

Lines with the same mark can go in either order.

public class Player {
	private String name;
	private int score;
	public Player(String name) {
		this.name = name;
		score = 0;
	}
	public void addPoints(int points) {
		score += points;
	}
	public int getScore() {
		return score;
	}
}

Extra strip: name = name;. Inside the constructor, name is the parameter, so name = name sets the parameter to itself. The name instance variable stays null. It needs this.name.