Account ac1 = new RegularAccount(500); System.out.println(ac1); ac1.applyInteres
ID: 3808096 • Letter: A
Question
Account ac1 = new RegularAccount(500);
System.out.println(ac1);
ac1.applyInterest();
System.out.println(ac1);
ac1.deposit(1200);
System.out.println(ac1);
ac1.applyInterest();
System.out.println(ac1);
ac1.withdraw(1000);
System.out.println(ac1);
ac1.applyInterest();
System.out.println(ac1);
PremiumAccount pacc1 = new PremiumAccount(500);
System.out.println(pacc1);
pacc1.applyInterest();
System.out.println(pacc1);
pacc1.deposit(1200);
System.out.println(pacc1);
pacc1.applyInterest();
System.out.println(pacc1);
--------------------------------------------
The code should output the following:
Regular Account: The balance is $500.00
Regular Account: The balance is $500.00
Regular Account: The balance is $1700.00
Regular Account: The balance is $1707.00
Regular Account: The balance is $707.00
Regular Account: The balance is $707.00
Premium Account: The balance is $500.00
Premium Account: The balance is $507.50
Premium Account: The balance is $1707.50
Premium Account: The balance is $1733.11
Explanation / Answer
Hi Please find my implementation.
Please let me know in case of any issue.
public abstract class Account{
protected double balance;
public void withdraw(double amount){
if(balance-amount < 0){
System.out.println("Balance is low");
}else
balance = balance - amount;
}
public void deposit(double amount){
balance = balance + amount;
}
public abstract void applyInterest();
@Override
public String toString() {
return "The balance is $"+String.format("%.2f", balance);
}
}
class PremiumAccount extends Account{
public PremiumAccount(double amount) {
balance = amount;
}
@Override
public void applyInterest() {
balance = balance + balance*0.015;
}
}
class RegularAccount extends Account{
public RegularAccount(double amount) {
balance = amount;
}
@Override
public void applyInterest() {
if(balance > 1000)
balance = balance + (balance - 1000)*0.01;
}
}
public class AccountTester {
public static void main(String[] args) {
Account ac1 = new RegularAccount(500);
System.out.println(ac1);
ac1.applyInterest();
System.out.println(ac1);
ac1.deposit(1200);
System.out.println(ac1);
ac1.applyInterest();
System.out.println(ac1);
ac1.withdraw(1000);
System.out.println(ac1);
ac1.applyInterest();
System.out.println(ac1);
PremiumAccount pacc1 = new PremiumAccount(500);
System.out.println(pacc1);
pacc1.applyInterest();
System.out.println(pacc1);
pacc1.deposit(1200);
System.out.println(pacc1);
pacc1.applyInterest();
System.out.println(pacc1);
}
}
/*
Sample run:
The balance is $500.00
The balance is $500.00
The balance is $1700.00
The balance is $1707.00
The balance is $707.00
The balance is $707.00
The balance is $500.00
The balance is $507.50
The balance is $1707.50
The balance is $1733.11
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.