Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote