Purpose Purpose is to practice using regular inheritance. The Checking superclas
ID: 3778506 • Letter: P
Question
Purpose
Purpose is to practice using regular inheritance.
The Checking superclass
First design, code and test a class called Checking. This will be your superclass. Use exactly and only these instance variables:
private String accountID ---an identifying account number e.g. “123”
private int balance ---balance of the account in pennies, to avoid floating point round-off errors e.g. 100 means $1.00
private int numDeposits ---number of deposits made this month
private int numWithdrawals ---number of withdrawals made this month
private int serviceCharge ---maintain the total monthly service charges incurred here, in pennies
Checking will have the following methods. Be careful to implement the steps of each method in exactly the order given below:
constructor
-has parameters for the account ID and starting balance (in dollars) -must initialize all instance variables -use the following to convert the dollar starting balance parameter bal to cents, avoiding round-off error:
balance = (int) Math.round(bal * 100.0);
appropriate get and set methods
-you have to decide during design what get and set methods are required
deposit()
-has a parameter for the deposit amount in dollars -make the necessary conversion then add the amount to the balance -increment the number of deposits -add a deposit fee of 50 cents (in pennies) per deposit to the monthly service charge
-note that the deposit fee is NOT deducted from balance at the time of the deposit transaction -(use static final constants for all the ‘magic number’ fee amounts)
withdraw()
-has a parameter for the withdrawal amount in dollars -make the necessary conversion then subtract the amount from the balance -increment the number of withdrawals -add a withdrawal fee (in pennies) per withdrawal to the monthly service charge: -the first 2 withdrawals cost 25 cents each -withdrawals after the first 2 cost 100 cents each
printMonthEnd()
-run at the end of the month to first deduct monthly service charges than summarize the account. In pseudocode (implement these actions in exactly this order):
decrease the balance by the monthly service charge output the account ID, resulting balance, number of deposits and withdrawals and the monthly service charge (use printf() to output monetary amounts in dollars, with 2 decimal places) set the number of deposits and withdrawals and monthly service charge back to zero -an example of the printMonthEnd() output format:
Account ID 123
Balance is: $746.00
1 deposits, 3 withdrawals
Monthly service charge was: $2.00
toString()
-return a String that represents the Checking object, must be in standardized formatting e.g.
Checking[accountID=123, balance=74800, numDeposits=1, numWithdrawals=3, serviceCharge=200]
The MoneyMarket subclass
Next design, code and test a MoneyMarket subclass that inherits from Checking. MoneyMarket instance variable:
active --- a boolean variable that maintains the status of the account. Status is defined as active if the balance is greater than a minimum balance of $500.00, otherwise is not active. Account status must be updated every time the account balance is changed. Withdrawals are not allowed if the account is not active, until deposits make the account active again.
MoneyMarket will have the following methods Be careful to implement the steps of each method in exactly the order given below:
constructor
-has parameters for account ID and the starting balance (in dollars) -use the superclass constructor to set these -then set whether the account is active or not active
withdraw()
-output a message in the following format if the account is not active, and the method is done: MoneyMarket 456: Withdrawal not allowed
-otherwise call Checking withdraw() (use super to do this), then must update the status of the account
deposit()
-call Checking deposit() (use super to do this), then must update the status of the account
printMonthEnd()
-run at the end of the month to deduct monthly service charges and summarize the account. In pseudocode (implement these actions in exactly this order): add a minimum balance fee of $5.00 to the monthly service fee if the account is not active call Checking printMonthEnd() (use super to do this) update status of the account output the status of the account e.g. "Money market account is: active" or "Money market account is: inactive" -an example of the printMonthEnd() output format for the MoneyMarket class:
Account ID 456
Balance is: $294.25
1 deposits, 1 withdrawals
Monthly service charge was: $5.75
Money market account is: inactive
toString()
-return a String that represents the MoneyMarket object, must be in standardized formatting e.g.
MoneyMarket[accountID=456, balance=30000, numDeposits=1, numWithdrawals=1, serviceCharge=75][active=false]
The Tester class The Tester class tests your new Checking and MoneyMarket classes. Source code for Tester is given below.
import java.util.ArrayList;
/** * Driver for Lab 5 Inheritance
*
* @author
* @version
*/
public class Tester
{
public static void main(String arg[])
{
// create some accounts
// $100.00 balance
Checking check = new Checking("123", 100.0);
// $1000.00 balance
MoneyMarket money = new MoneyMarket("456", 1000.0);
ArrayList<Checking> accounts = new ArrayList<>();
accounts.add(check);
accounts.add(money);
// do some transactions
System.out.println("Transactions");
// on Checking object
check.deposit(750.0);
check.withdraw(12.0);
check.withdraw(34.0);
check.withdraw(56.0);
// on MoneyMarket object
money.withdraw(750.0);
money.withdraw(100.0);
money.deposit(50.0);
// print accounts
System.out.println(" Print accounts");
for (Checking c : accounts)
System.out.println(c.toString());
// print month end report
System.out.println(" Month end report");
for (Checking c : accounts) {
c.printMonthEnd();
System.out.println();
}
}
}
Hints
first, take a calculator and work carefully through Tester line by line, writing down on a piece of paper the values of the instance variables that should be produced...
this makes sure you understand what every method does, and gives you the expected output you need to test your program
now design your new Checking and MoneyMarket classes on a piece of paper
use inheritance – a MoneyMarket account is-a Checking account, with an account status added
design algorithms, think about parameters and return values
then use BlueJ as usual to code and test each method in turn. Comment out in Tester the methods you have not yet implemented
Required
Tester in your final submission must not be changed in any way
every method is required to have a clear, meaningful Javadoc comment. You will lose points otherwise
each toString() is required to use the standardized formatting for inheritance. You will lose points otherwise
Explanation / Answer
Code:
// class Checking
package com.chegg;
public class Checking {
private String accountID;
private int balance;
private int numDeposits;
private int numWithdrawals;
private int serviceCharge;
private static final int MAGIC_NUMBER_DEPOSIT = 50;
private static final int MAGIC_NUMBER_WITHDRAWAL1 = 25;
private static final int MAGIC_NUMBER_WITHDRAWAL2 = 100;
public Checking(String id, double d) {
setAccountID(id);
setBalance(d);
}
// setter methods
private void setBalance(double d) {
this.balance = (int) Math.round(d * 100.0);
}
private void setAccountID(String id) {
this.accountID = id;
}
public void setServiceCharge(float amount) {
int cents = (int) Math.round(amount * 100.0);
serviceCharge = serviceCharge + cents;
}
// getter methods
public int getNumDeposits() {
return numDeposits;
}
public String getAccountID() {
return accountID;
}
public int getNumWithdrawals() {
return numWithdrawals;
}
public int getservicecharge() {
return serviceCharge;
}
public int getBalance() {
return balance;
}
// deposit
public void deposit(double d) {
int cents = (int) Math.round(d * 100.0);
balance = balance + cents;
numDeposits++;
serviceCharge = serviceCharge + MAGIC_NUMBER_DEPOSIT;
}
// withdrawal
public void withdrawal(double d) {
int cents = (int) Math.round(d * 100.0);
balance = balance - cents;
numWithdrawals++;
if (numWithdrawals < 3)
serviceCharge = serviceCharge + MAGIC_NUMBER_WITHDRAWAL1;
else
serviceCharge = serviceCharge + MAGIC_NUMBER_WITHDRAWAL2;
}
// printMonthEnd
public void printMonthEnd() {
balance = balance - serviceCharge;
System.out.println("Account ID : " + accountID);
System.out.println("Balance is : $" + (int) Math.round(balance / 100.0) + ".00");
System.out.println(numDeposits + " deposits, " + numWithdrawals + " withdrawals");
System.out.println("Monthly service charge was: $" + (int) Math.round(serviceCharge / 100.0) + ".00");
numDeposits = numWithdrawals = serviceCharge = 0;
}
public String toString() {
String result = "Checking[accountID=" + accountID + ", balance=" + balance + ", numDeposits=" + numDeposits
+ ", numWithdrawals=" + numWithdrawals + ", serviceCharge=" + serviceCharge + "]";
return result;
}
}
// class MonthlyMarket
package com.chegg;
public class MoneyMarket extends Checking {
private boolean active;
private static final int MINIMUM_BALANCE = 50000;
private static final float MINIMUM_SERVICE_FEE = 5.0f;
public MoneyMarket(String id, double d) {
super(id, d);
updateStatus();
}
public void withdrawal(float amount) {
if (!active) {
System.out.println("MoneyMarket " + super.getAccountID() + ": Withdrawal not allowed.");
} else {
super.withdrawal(amount);
updateStatus();
}
}
public void deposit(float amount) {
super.deposit(amount);
updateStatus();
}
public void printMonthEnd() {
if (!active) {
super.setServiceCharge(MINIMUM_SERVICE_FEE);
}
super.printMonthEnd();
System.out.println("Money market account is: " + active);
}
public String toString() {
String result = "MoneyMarket[accountID=" + super.getAccountID() + ", balance=" + super.getBalance()
+ ", numDeposits=" + super.getNumDeposits() + ", numWithdrawals=" + super.getNumWithdrawals()
+ ", serviceCharge=" + super.getservicecharge() + "][active=" + active + "]";
return result;
}
private void updateStatus() {
if (super.getBalance() > MINIMUM_BALANCE)
active = true;
else
active = false;
}
}
// class Tester
package com.chegg;
import java.util.ArrayList;
/**
* * Driver for Lab 5 Inheritance
*
* @author
* @version
*/
public class Tester {
public static void main(String arg[]) {
// create some accounts
// $100.00 balance
Checking check = new Checking("123", 100.0);
// $1000.00 balance
MoneyMarket money = new MoneyMarket("456", 1000.0);
ArrayList<Checking> accounts = new ArrayList<>();
accounts.add(check);
accounts.add(money);
// do some transactions
System.out.println("Transactions");
// on Checking object
check.deposit(750.0);
check.withdrawal(12.0);
check.withdrawal(34.0);
check.withdrawal(56.0);
// on MoneyMarket object
money.withdrawal(750.0);
money.withdrawal(100.0);
money.deposit(50.0);
// print accounts
System.out.println(" Print accounts");
for (Checking c : accounts)
System.out.println(c.toString());
// print month end report
System.out.println(" Month end report");
for (Checking c : accounts) {
c.printMonthEnd();
System.out.println();
}
}
}
Output:
Transactions
Print accounts
Checking[accountID=123, balance=74800, numDeposits=1, numWithdrawals=3, serviceCharge=200]
MoneyMarket[accountID=456, balance=20000, numDeposits=1, numWithdrawals=2, serviceCharge=100][active=true]
Month end report
Account ID : 123
Balance is : $746.00
1 deposits, 3 withdrawals
Monthly service charge was: $2.00
Account ID : 456
Balance is : $199.00
1 deposits, 2 withdrawals
Monthly service charge was: $1.00
Money market account is: true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.