1. Create a new class called SavingsAccount that extends BankAccount. 2. It shou
ID: 3529010 • Letter: 1
Question
1. Create a new class called SavingsAccount that extends BankAccount.
2. It should contain an instance variable called rate that represents the annual
interest rate. Set it equal to 2.5%.
3. It should also have an instance variable called savingsNumber, initialized to
0. In this bank, you have one account number, but can have several savings
accounts with that same number. Each individual savings account is identified
by the number following a dash. For example, 100001-0 is the first savings
account you open, 100001-1 would be another savings account that is still part
of your same account. This is so that you can keep some funds separate from
the others, like a Christmas club account.
4. An instance variable called accountNumber that will hide the
accountNumber from the superclass, should also be in this class.
5. Write a constructor that takes a name and an initial balance as parameters and
calls the constructor for the superclass. It should initialize accountNumber
to be the current value in the superclass accountNumber (the hidden
instance variable) concatenated with a hyphen and then the savingsNumber.
6. Write a method called postInterest that has no parameters and returns no
value. This method will calculate one month
Explanation / Answer
Hey please check it out..
BankAccount Class
package cramsterhelp;
public abstract class BankAccount
{
// class variable so that each account has a unique number
protected static int numberOfAccounts = 100001;
// current balance in the account
private double balance;
// name on the account
private String owner;
// number bank uses to identify account
private String accountNumber;
//default no-arg constructor
public BankAccount()
{
balance = 0;
accountNumber = numberOfAccounts + "";
numberOfAccounts++;
}
//standard constructor
public BankAccount(String name, double amount)
{
owner = name;
balance = amount;
accountNumber = numberOfAccounts + "";
numberOfAccounts++;
}
//copy constructor
public BankAccount(BankAccount oldAccount, double amount)
{
owner = oldAccount.owner;
balance = amount;
accountNumber = oldAccount.accountNumber;
}
//Method "deposit" allows you to add money to the account
public void deposit(double amount)
{
balance = balance + amount;
}
//Method "withdraw" allows you to remove money from the account if enough money
//returns true if the transaction was completed
//returns false if the there was not enough money
public boolean withdraw(double amount)
{
boolean completed = true;
if (amount <= balance)
{
balance = balance - amount;
}
else
{
completed = false;
}
return completed;
}
//accessor method to get balance
public double getBalance()
{
return balance;
}
//accessor method to get owner
public String getOwner()
{
return owner;
}
//accessor method to get account number
public String getAccountNumber()
{
return accountNumber;
}
//mutator method to change the balance
public void setBalance(double newBalance)
{
balance = newBalance;
}
//mutator method to change the account number
public void setAccountNumber(String newAccountNumber)
{
accountNumber = newAccountNumber;
}
}
SavingsAccount Class
package cramsterhelp;
public class SavingsAccount extends BankAccount{
private double rate = .025;
private int savingsNumber =0;
private String accountNumber;
public SavingsAccount(String name,int balance) {
super(name,balance);
this.accountNumber = super.getAccountNumber()+"-"+savingsNumber;
}
public SavingsAccount(SavingsAccount s,int balance)
{
super(s,balance);
this.setSavingsNumber(s.getSavingsNumber()+1);
this.setAccountNumber(super.getAccountNumber()+"-"+this.getSavingsNumber());
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public int getSavingsNumber() {
return savingsNumber;
}
public void setSavingsNumber(int savingsNumber) {
this.savingsNumber = savingsNumber;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public void postInterest()
{
double intrestPerMonth=(this.getBalance() * .025 )/12;
this.setBalance(this.getBalance()+intrestPerMonth);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.