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
} Q1public Player(String name) { Q2} Q3public class Player { Q4score += points; Q5private int score; Q6public void addPoints(int points) { Q7private String name; Q8return score; Q9this.name = name; Q10name = name; Q11score = 0; Q12public int getScore() { Q13} Q14} Q15Order: 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.