[Java] Please check your code at the link below before you submit your code here
ID: 3814532 • Letter: #
Question
[Java] Please check your code at the link below before you submit your code here.
http://www.codecheck.it/files/16113005213naf3u5yy42oz8y5dh7a695zb
In both the BankAccount and SavingsAccount provided, override the toStringand equals methods.
Use the following file:
AccountsOverrideTester.java
[Here's the code you need to modify]
/**
* A bank account has a balance that can be changed by
* deposits and withdrawals
*
*/
public class BankAccount
{
private double balance;
public final static double MINIMUM_BALANCE = 1500;
public final static double BELOW_MINIMUM_FEE = 10.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;
}
/**
* Do end of month processing for the account
*/
public void endOfMonth()
{
if (balance < MINIMUM_BALANCE)
{
balance = balance - BELOW_MINIMUM_FEE;
}
}
}
/**
* An account that earns interest every month
*/
public class SavingsAccount extends BankAccount
{
private double annualInterestRate;
/**
* Creates a InterestAccount object with a starting balance and account id
* @param initialBalance the starting balance
* @param rate the annual interest rate
*/
public SavingsAccount(double initialBalance, double rate)
{
super(initialBalance);
annualInterestRate = rate;
}
public double getInterestRate()
{
return annualInterestRate;
}
@Override
public void endOfMonth()
{
double interest = getBalance() * annualInterestRate / 100 /12;
deposit(interest);
}
}
Explanation / Answer
AccountsOverrideTester.java
public class AccountsOverrideTester
{
public static void main(String[] args)
{
BankAccount account1 = new BankAccount(1500);
BankAccount account2 = new BankAccount(1500);
BankAccount account3 = new BankAccount(5000);
SavingsAccount savings1 = new SavingsAccount(1500, .01);
SavingsAccount savings2 = new SavingsAccount(3000, .02);
SavingsAccount savings3 = new SavingsAccount(3000, .02);
System.out.println(account1);
System.out.println("Expected: BankAccount[balance=1500.0]");
System.out.println(savings3);
System.out.println("Expected: SavingsAccount[balance=3000.0][anualInterstRate=0.02]");
System.out.println(account1.equals(account2));
System.out.println("Expected: true");
System.out.println(account1.equals(account3));
System.out.println("Expected: false");
System.out.println("super equals sub: " + account1.equals(savings1));
System.out.println("Expected: false");
System.out.println(savings1.equals(savings2));
System.out.println("Expected: false");
System.out.println(savings3.equals(savings2));
System.out.println("Expected: true");
System.out.println("Different classes:" + savings1.equals("account"));
System.out.println("Expected: false");
System.out.println("null: " + savings1.equals(null));
System.out.println("Expected: false");
}
}
/**
* A bank account has a balance that can be changed by
* deposits and withdrawals
*
*/
class BankAccount
{
private double balance;
public final static double MINIMUM_BALANCE = 1500;
public final static double BELOW_MINIMUM_FEE = 10.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;
}
/**
* Do end of month processing for the account
*/
public void endOfMonth()
{
if (balance < MINIMUM_BALANCE)
{
balance = balance - BELOW_MINIMUM_FEE;
}
}
public String toString() {
return "BankAccount[balance="+balance+"]";
}
public boolean equals(BankAccount a){
if(a == null)
return false;
if(getClass() == a.getClass() && balance ==a.balance){
return true;
}
return false;
}
}
/**
* An account that earns interest every month
*/
class SavingsAccount extends BankAccount
{
private double annualInterestRate;
/**
* Creates a InterestAccount object with a starting balance and account id
* @param initialBalance the starting balance
* @param rate the annual interest rate
*/
public SavingsAccount(double initialBalance, double rate)
{
super(initialBalance);
annualInterestRate = rate;
}
public double getInterestRate()
{
return annualInterestRate;
}
@Override
public void endOfMonth()
{
double interest = getBalance() * annualInterestRate / 100 /12;
deposit(interest);
}
public String toString() {
return "SavingsAccount[balance="+getBalance()+"][anualInterstRate="+annualInterestRate+"]";
}
public boolean equals(SavingsAccount a){
if(a == null)
return false;
if(getBalance() ==a.getBalance()){
return true;
}
return false;
}
}
Output:
BankAccount[balance=1500.0]
Expected: BankAccount[balance=1500.0]
SavingsAccount[balance=3000.0][anualInterstRate=0.02]
Expected: SavingsAccount[balance=3000.0][anualInterstRate=0.02]
true
Expected: true
false
Expected: false
super equals sub: false
Expected: false
false
Expected: false
true
Expected: true
Different classes:false
Expected: false
null: false
Expected: false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.