Consider the following class, representing a BankAccount and its transaction his
ID: 3595493 • Letter: C
Question
Consider the following class, representing a BankAccount and its transaction history.
Which of its methods are accessors and which are mutators?
public class Account {
public Account(long num) {
id = num;
balance = 0.0;
transactions = new ArrayList<Double>();
}
public void deposit(Double d) {
transactions.add(d);
balance = balance + d;
}
public void withdraw(Double d) {
transactions.add(-d);
balance = balance - d;
}
public Double getBalance() {
return balance;
}
public ArrayList<Double> getTransactionHistory() {
return transactions;
}
public long id;
private Double balance;
private ArrayList<Double> transactions;
}
Explanation / Answer
Mutators are setter methods, which are used to set values of private variables.
Accessors are getter methods, which return the private variable values.
Mutators:
deposit(Double d)
withdraw(Double d)
Accessors:
getBlanace()
getTransactionHistory()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.