Question: Hey guys, I need some help for writing code in java Here are the instructions: Learning Objectives This assignment will help you understand how to
Hey guys, I need some help for writing code in java
Here are the instructions:
Learning Objectives This assignment will help you understand how to work with existing classes to an inheritance hierarchy. Add a CertificateDepositAccount (CDA) class to the bank account hierarchy. The CDA is just like a savings account, but you promise to leave the money in the account for a number of months, and there is a $20 penalty for early withdrawal. Construct the CDA with the interest rate and the number of months to maturity. In the addInterest method, you should decrement the count of months only for a CDA. If the count is positive during a CDA withdrawal, charge the withdrawal penalty.
These java classes are already premade and should work with the code I am trying to make:
SavingsAccount.java:
/** An account that earns interest at a fixed rate. */ public class SavingsAccount extends BankAccount { private double interestRate; /** Constructs a bank account with a given interest rate. @param rate the interest rate */ public SavingsAccount(double rate) { interestRate = rate; } /** Adds the earned interest to the account balance. */ public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } } CertificateDepositTester.java:
/** This program tests the BankAccount class and its subclasses. */ public class CertificateDepositTester { public static void main(String[] args) { SavingsAccount momsSavings = new SavingsAccount(5); CheckingAccount harrysChecking = new CheckingAccount(100); CertificateDepositAccount collegeFund = new CertificateDepositAccount(10, 3); momsSavings.deposit(10000); momsSavings.transfer(2000, harrysChecking); harrysChecking.withdraw(1500); harrysChecking.withdraw(80); momsSavings.transfer(1000, harrysChecking); harrysChecking.withdraw(400); momsSavings.transfer(3000, collegeFund); collegeFund.withdraw(800); // simulate end of month momsSavings.addInterest(); collegeFund.addInterest(); harrysChecking.deductFees(); System.out.println("Mom's savings balance: " + momsSavings.getBalance()); System.out.println("Expected: 4200"); System.out.println("Harry's checking balance: " + harrysChecking.getBalance()); System.out.println("Expected: 1116"); System.out.println("College fund's certificate deposit balance: " + collegeFund.getBalance()); System.out.println("Expected: 2398"); } } BankAccount.java:
** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount { private double balance; /** Constructs a bank account with a zero balance. */ public BankAccount() { balance = 0; } /** Constructs a bank account with a given balance. @param initialBalance the initial balance */ public BankAccount(double initialBalance) { balance = initialBalance; } /** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { balance = balance + amount; } /** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount) { balance = balance - amount; } /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; } /** Transfers money from the bank account to another account @param amount the amount to transfer @param other the other account */ public void transfer(double amount, BankAccount other) { withdraw(amount); other.deposit(amount); } } CheckingAccount.java:
/** A checking account that charges transaction fees. */ public class CheckingAccount extends BankAccount { private int transactionCount; private static final int FREE_TRANSACTIONS = 3; private static final double TRANSACTION_FEE = 2.0; /** Constructs a checking account with a given balance. @param initialBalance the initial balance */ public CheckingAccount(double initialBalance) { // Construct superclass super(initialBalance); // Initialize transaction count transactionCount = 0; } public void deposit(double amount) { transactionCount++; // Now add amount to balance super.deposit(amount); } public void withdraw(double amount) { transactionCount++; // Now subtract amount from balance super.withdraw(amount); } /** Deducts the accumulated fees and resets the transaction count. */ public void deductFees() { if (transactionCount > FREE_TRANSACTIONS) { double fees = TRANSACTION_FEE * (transactionCount - FREE_TRANSACTIONS); super.withdraw(fees); } transactionCount = 0; } }
This is the code I have so far:
public class CertificateDepositAccount extends SavingsAccount { private int months; public CertificateDepositAccount(double rate, int month) { super(rate); months=month; } public void addInterest() { months--; if(months>0) { } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
