JAVA Account class public class Account { protected int accountNumber; protected
ID: 3597422 • Letter: J
Question
JAVA
Account class
public class Account
{
protected int accountNumber;
protected double balance;
// Constructor
public Account(int a){
balance = 0.0;
accountNumber = a;
}
public void deposit(double amount){
if (amount > 0)
balance += amount;
else
System.err.println("Account.deposit(...): "
+"cannot deposit negative amount.");
}
public void withdraw(double amount){
if (amount > 0)
balance -= amount;
else
System.err.println("Account.withdraw(...): "
+"cannot withdraw negative amount.");
}
public double getbalance(){
return balance;
}
public double getAccountNumber(){
return accountNumber;
}
public void display(){
System.out.println("Account " + accountNumber + " : " + "Balance = " + balance);
}
}
Using the Account class as a base class, write two derived classes called SavingsAccount and CurrentAccount.
• A SavingsAccount class, in addition to the attributes of an Account class, should have an interest variable and a method which adds interest to the account.
• A CurrentAccount class, in addition to the attributes of an Account class, should have an overdraft limit variable. This should allow a user to withdraw more than the present balance of the account, up to some limit.
• Ensure that you have overridden methods of the Account class as necessary in both derived classes.
Explanation / Answer
Please do give this answer a thumbs up.Thank You.
public class Account
{
protected int accountNumber;
protected double balance;
// Constructor
public Account(int a){
balance = 0.0;
accountNumber = a;
}
public void deposit(double amount){
if (amount > 0)
balance += amount;
else
System.err.println("Account.deposit(...): "
+"cannot deposit negative amount.");
}
public void withdraw(double amount){
if (amount > 0)
balance -= amount;
else
System.err.println("Account.withdraw(...): "
+"cannot withdraw negative amount.");
}
public double getbalance(){
return balance;
}
public double getAccountNumber(){
return accountNumber;
}
public void display(){
System.out.println("Account " + accountNumber + " : " + "Balance = " + balance);
}
}
class SavingsAccount extends Account
{
private double interest;
//assuming interest to be in % i.e. 50%,58.5%
public SavingsAccount(int a,int interest)
{
super(a);
this.interest=interest;
}
public void addInterest()
{
balance+=balance*(interest/100);
}
public void withdraw(double amount){
if (amount>= 0 && balance>=amount)
balance -= amount;
else if (amount<0)
System.err.println("Account.withdraw(...): "
+"cannot withdraw negative amount.");
else
System.err.println("Account.withdraw(...): "
+"cannot withdraw more than balance.");
}
}
class CurrentAccount extends Account
{
private double overdraftLimit;
public CurrentAccount(int a,int overdraftLimit)
{
super(a);
this.overdraftLimit=overdraftLimit;
}
public void withdraw(double amount){
if (amount> 0 && (balance+overdraftLimit)>=amount)
balance -= amount;
else if(amount<0)
System.err.println("Account.withdraw(...): "
+"cannot withdraw negative amount.");
else
System.err.println("Account.withdraw(...): "
+"cannot withdraw more than balance+overdraft limit");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.