In this exercise, you will be creating new classes that are derived from a class
ID: 3750408 • Letter: I
Question
In this exercise, you will be creating new classes that are derived from a class called BankAccount. A checking account is a bank account and a savings account is a bank account as well. This sets up a relationship called inheritance, where BankAccount is the superclass and CheckingAccount and SavingsAccount are subclasses. This relationship allows CheckingAccount to inherit attributes from BankAccount (like owner, balance, and accountNumber, but it can have new attributes that are specific to a checking account, like a fee for clearing a check. It also allows CheckingAccount to inherit methods from BankAccount, like deposit, that are universal for all bank accounts. You will write a withdraw method in CheckingAccount that overrides the withdraw method in BankAccount, in order to do something slightly different than the original withdraw method. You will use an instance variable called accountNumber in SavingsAccount to hide the accountNumber variable inherited from BankAccount. The UML diagram for the inheritance relationship is as follows: . . . BankAccount attributes balance:double -ow ner: String -accountNumber: String #numberOfAccounts : int operations +BankAccount) +BankAccount( name String, amount double) +BankAccount( oldAccount BankAccount, amount: double) +deposit( amount: double) void +w ithdraw ( amount: double) boolean +getBalance() double +getOw ner) String +getAccountNumber() String +setBalance( amount double) void +setAccountNumber( new AccountNumber: String): voidExplanation / Answer
Note : Getting Some Error I will rectify.Thank You
__________________
package org.students;
import java.util.Date;
public class BankAccount {
// Declaring instance variables
private String owner;
private String accountNumber;
private double balance;
private static int numberOfAccounts=100000;
BankAccount()
{
}
// Parameterized constructor
public BankAccount(String name,double amount) {
this.owner=name;
this.balance = amount;
numberOfAccounts++;
this.accountNumber=String.valueOf(numberOfAccounts);
}
// Setters and getters
// This method will deduct the amount from the balance
public boolean withdraw(double amount) {
if(balance>=amount)
{
balance = balance - amount;
return true;
}
else
{
return false;
}
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void deposit(double amount) {
balance = balance + amount;
System.out.printf("After deposit of $%.2f, balance = $%.2f ",amount,getBalance());
}
@Override
public String toString() {
System.out.printf("AccountNumber %s has been created for %s Initial balance = $%.2f",accountNumber,owner,balance);
return "";
}
}
_______________
package org.students;
public class CheckingAccount extends BankAccount {
//Declaring instance variables
private double fee;
//Parameterized constructor
public CheckingAccount(String name,double amount, double fee) {
super(name,amount);
super.setAccountNumber(getAccountNumber()+"-CA");
this.fee = fee;
}
//This method will deduct the Monthly fee
public boolean withdraw(double amount)
{
boolean b=false;
if(amount>100 && getBalance()+fee>=amount)
{
super.withdraw(amount+fee);
System.out.printf("After withdrawl of $%.2f, balance = %.2f ",amount,getBalance());
b=true;
}
else if(getBalance()>=amount)
{
super.withdraw(amount);
System.out.printf("After withdrawl of $%.2f, balance = %.2f ",amount,getBalance());
b=true;
}
else
{
b=false;
System.out.printf("Insufficient funds to withdraw $%.2f, balance = %.2f ",amount,getBalance());
}
return b;
}
}
________________
package org.students;
public class SavingsAccount extends BankAccount {
// Declaring instance variables
private double rate;
private int savingsNumber=0;
private String accountNumber;
// Parameterized constructor
public SavingsAccount(String name,double amount, double rate) {
super(name,amount);
this.rate = rate;
this.accountNumber=getAccountNumber()+"-S"+savingsNumber;
setAccountNumber(accountNumber);
savingsNumber++;
}
public SavingsAccount(SavingsAccount oldAccount,double amount)
{
super(oldAccount.getOwner(),amount);
this.rate = oldAccount.getInterestRate();
this.accountNumber=getAccountNumber()+"-S"+savingsNumber;
savingsNumber++;
}
// Sets the interest rate for this account.
// @param rate the monthly interest rate in percent
public void setInterestRate(double rate) {
rate = rate;
}
// getters
public double getInterestRate() {
return rate;
}
// This method will calculate the Interest
public void postInterest() {
deposit(getBalance()+(getBalance() * getInterestRate()));
System.out.printf("After Monthly ineterst has been posted, balance =$%.2f ",getBalance());
}
public String getAccountNumber()
{
return accountNumber;
}
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
System.out.printf("AccountNumber %s has been created for %s Initial balance = $%.2f",accountNumber,getOwner(),getBalance());
return "";
}
}
__________________
package org.students;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
CheckingAccount c=new CheckingAccount("Mike Smith",5000.00,0.25);
System.out.println(c);
c.deposit(1000);
c.withdraw(500);
c.withdraw(99);
System.out.println();
SavingsAccount sa=new SavingsAccount("Alex Hunter",400.00,0.02);
System.out.println(sa);
sa.deposit(1000.00);
sa.withdraw(1000.00);
sa.postInterest();
System.out.println();
SavingsAccount sa1=new SavingsAccount(sa,5.00);
System.out.println(sa1);
sa1.deposit(500.00);
sa1.withdraw(1000.00);
System.out.println();
CheckingAccount c2=new CheckingAccount("Mike Smith",5000.00,0.25);
System.out.println(c2);
}
}
___________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.