Consider the following class, representing a BankAccount and its transaction his
ID: 3595590 • Letter: C
Question
Consider the following class, representing a BankAccount and its transaction history.
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;
}
1.Which of its methods are accessors? Which of its methods are mutators?
2. What do you like about the class design?
e. The class is immutable
3. What do you dislike about the class design?
a. It clearly distinguishes between mutator and accessor methodsExplanation / Answer
1. getBalance() and getTransactionHistory() methods are accessors. deposit() and withdraw() methods are mutators
2. The data is encapsulated (b)
3. Returning the arrayList breaks the encapsulation (d)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.