Mutator methods revisited
java
public class Player {
private String name;
private int health;
private int score;
public Player(String name) {
this.name = name;
this.health = 100;
this.score = 0;
}
// Mutator — decreases health
public void takeDamage(int damage) {
health -= damage;
if (health < 0) {
health = 0; // can't go below 0
}
}
// Mutator — increases score
public void addPoints(int points) {
if (points > 0) {
score += points;
}
}
// Mutator — restores health
public void heal(int amount) {
health += amount;
if (health > 100) {
health = 100; // cap at 100
}
}
// Accessor
public boolean isAlive() {
return health > 0;
}
public String toString() {
return name + " [HP: " + health + ", Score: " + score + "]";
}
}
java
Player p = new Player("Hero");
System.out.println(p); // Hero [HP: 100, Score: 0]
p.takeDamage(30);
System.out.println(p); // Hero [HP: 70, Score: 0]
p.addPoints(50);
p.heal(10);
System.out.println(p); // Hero [HP: 80, Score: 50]
p.takeDamage(200);
System.out.println(p.isAlive()); // false (health clamped to 0)
Mutators that return values
java
public class BankAccount {
private double balance;
public BankAccount(double balance) {
this.balance = balance;
}
// Returns true if withdrawal succeeded
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true; // success
}
return false; // failed
}
// Returns the old balance after setting new one
public double setBalance(double newBalance) {
double old = balance;
if (newBalance >= 0) {
balance = newBalance;
}
return old; // returns previous balance
}
}
Methods that interact with other objects
java
public class BankAccount {
private String owner;
private double balance;
public BankAccount(String owner, double balance) {
this.owner = owner;
this.balance = balance;
}
// Transfer money to another account
public boolean transfer(BankAccount other, double amount) {
if (amount > 0 && amount <= this.balance) {
this.balance -= amount; // deduct from this account
other.balance += amount; // add to other account
return true;
}
return false;
}
public double getBalance() { return balance; }
}
java
BankAccount alice = new BankAccount("Alice", 1000);
BankAccount bob = new BankAccount("Bob", 500);
alice.transfer(bob, 300);
System.out.println(alice.getBalance()); // 700
System.out.println(bob.getBalance()); // 800
Helper methods (private methods)
java
public class GradeBook {
private int[] scores;
private int count;
// Private helper — only used by other methods in this class
private double calculateAverage() {
int sum = 0;
for (int i = 0; i < count; i++) {
sum += scores[i];
}
return (double) sum / count;
}
// Public method uses the private helper
public String getLetterGrade() {
double avg = calculateAverage(); // calls private helper
if (avg >= 90) return "A";
if (avg >= 80) return "B";
if (avg >= 70) return "C";
if (avg >= 60) return "D";
return "F";
}
}
Method decomposition — FRQ style
java
public class WordGame {
private String secretWord;
private int guessesLeft;
private String revealed;
public WordGame(String word, int maxGuesses) {
this.secretWord = word.toLowerCase();
this.guessesLeft = maxGuesses;
this.revealed = "";
for (int i = 0; i < word.length(); i++) {
revealed += "_";
}
}
// Check if a letter is in the word and reveal it
public boolean guess(String letter) {
letter = letter.toLowerCase();
boolean found = false;
String newRevealed = "";
for (int i = 0; i < secretWord.length(); i++) {
if (secretWord.substring(i, i + 1).equals(letter)) {
newRevealed += letter;
found = true;
} else {
newRevealed += revealed.substring(i, i + 1);
}
}
revealed = newRevealed;
if (!found) {
guessesLeft--;
}
return found;
}
public boolean isWon() {
return revealed.equals(secretWord);
}
public boolean isLost() {
return guessesLeft <= 0 && !isWon();
}
public String getRevealed() { return revealed; }
public int getGuessesLeft() { return guessesLeft; }
}
AP Exam Tips
- •
- •
- •
- •
- •
Common Mistakes
- •
- •
- •
- •