Accessor methods (getters)
java
public class Student {
private String name;
private int grade;
private double gpa;
public Student(String name, int grade, double gpa) {
this.name = name;
this.grade = grade;
this.gpa = gpa;
}
// Accessors — read data
public String getName() {
return name;
}
public int getGrade() {
return grade;
}
public double getGpa() {
return gpa;
}
}
Naming convention
Properties of accessors
Mutator methods (setters)
java
public class Student {
private String name;
private int grade;
private double gpa;
// ... constructor and getters ...
// Mutators — change data
public void setName(String name) {
this.name = name;
}
public void setGrade(int grade) {
if (grade >= 9 && grade <= 12) {
this.grade = grade;
}
}
public void setGpa(double gpa) {
if (gpa >= 0.0 && gpa <= 4.0) {
this.gpa = gpa;
}
}
}
Properties of mutators
Validation in setters — why encapsulation works
java
public class BankAccount {
private double balance;
public void setBalance(double balance) {
if (balance >= 0) {
this.balance = balance; // only set if non-negative
}
// otherwise, do nothing — balance stays the same
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
}
return false; // withdrawal failed
}
}
Computed accessors
java
public class Rectangle {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
// Simple accessors
public double getWidth() { return width; }
public double getHeight() { return height; }
// Computed accessors — derived from instance variables
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public boolean isSquare() {
return width == height;
}
}
Accessor vs. mutator — how to tell
A complete class example
java
public class Thermostat {
private double temperature;
private boolean isCelsius;
public Thermostat(double temp, boolean celsius) {
this.temperature = temp;
this.isCelsius = celsius;
}
// Accessor
public double getTemperature() {
return temperature;
}
// Computed accessor
public double getTemperatureF() {
if (isCelsius) {
return temperature * 9.0 / 5.0 + 32;
}
return temperature;
}
// Accessor (boolean → use "is" prefix)
public boolean isCelsius() {
return isCelsius;
}
// Mutator
public void setTemperature(double temp) {
temperature = temp;
}
// Mutator — increase temperature
public void increaseBy(double degrees) {
if (degrees > 0) {
temperature += degrees;
}
}
// Mutator — switch between C and F
public void toggleScale() {
if (isCelsius) {
temperature = temperature * 9.0 / 5.0 + 32;
} else {
temperature = (temperature - 32) * 5.0 / 9.0;
}
isCelsius = !isCelsius;
}
}
Trace practice
java
Thermostat t = new Thermostat(100, true); // 100°C
t.increaseBy(5); // 105°C
System.out.println(t.getTemperatureF()); // 221.0°F
t.toggleScale(); // now 221.0°F
System.out.println(t.getTemperature()); // 221.0
t.toggleScale(); // back to 105.0°C
AP Exam Tips
- •
- •
- •
- •
- •
Common Mistakes
- •
- •
- •
- •