JAVA Define a class variable in each sub class (SavingsAccount and CurrentAccoun
ID: 3602375 • Letter: J
Question
JAVA
Define a class variable in each sub class (SavingsAccount and CurrentAccount), which will keep a track of how many objects have been created from that class.
public class SavingAccount extends Account {
private double interestRate;
public SavingAccount(int accountNumber, double interestRate) {
super(accountNumber);
this.interestRate = interestRate;
}
public void display() {
super.display();
System.out.println("Interest rate: " + interestRate);
}
}
public class CurrentAccount extends Account {
private double overdraftLimit;
public CurrentAccount(int accountNumber, double overdraftLimit) {
super(accountNumber);
this.overdraftLimit = overdraftLimit;
}
public void withdraw(double amount) {
if (amount > 0)
if ((balance + overdraftLimit) - amount >= 0)
balance -= amount;
else
System.err.println("Account.withdraw(...): " + "cannot withdraw more than your balance.");
else
System.err.println("Account.withdraw(...): " + "cannot withdraw negative amount.");
}
}
Explanation / Answer
Answer:
public class SavingAccount extends Account {
private double interestRate;
private static int countObjects = 0;
public SavingAccount(int accountNumber, double interestRate) {
super(accountNumber);
this.interestRate = interestRate;
countObjects++;
}
public void display() {
super.display();
System.out.println("Interest rate: " + interestRate);
}
}
public class CurrentAccount extends Account {
private double overdraftLimit;
private static int countObjects = 0;
public CurrentAccount(int accountNumber, double overdraftLimit) {
super(accountNumber);
this.overdraftLimit = overdraftLimit;
countObjects++;
}
public void withdraw(double amount) {
if (amount > 0)
if ((balance + overdraftLimit) - amount >= 0)
balance -= amount;
else
System.err.println("Account.withdraw(...): " + "cannot withdraw more than your balance.");
else
System.err.println("Account.withdraw(...): " + "cannot withdraw negative amount.");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.