Java Programming Fundamentals Purpose Purpose is to practice using regular inher
ID: 3696424 • Letter: J
Question
Java Programming Fundamentals
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 double annualRate -annual interest rate e.g. 0.15 means 15% per
annum
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, starting balance (in dollars) and annual interest rate
-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 10 cents (in pennies) 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 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) to the monthly service charge:
-the first 2 withdrawals cost 25 cents each
-withdrawals after the first 2 cost 75 cents each
depositInterest()
-update the balance by calculating the monthly interest earned on the account, then add
this interest to the balance. In pseudocode:
monthly interest rate = annual interest rate / 12.0
monthly interest = balance * monthly interest rate
balance = balance + monthly interest
-(use Math.round() where necessary to prevent rounding errors)
printMonthEnd()
-run at the end of the month to first add interest, then deduct monthly service charges,
than summarize the account. In pseudocode (implement these actions in exactly this
order):
call the depositInterest() method
add a monthly fee of $1.00 to the monthly service charge
decrease the balance by the total 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 to zero
-an example of the printMonthEnd() output format:
Account ID 123
Balance is: $751.88
1 deposits, 3 withdrawals
Monthly service charge was: $2.35
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,
annualRate=0.1, serviceCharge=135]
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, the starting balance (in dollars) and annual interest rate
-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 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 add interest, 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 monthEnd() (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: $296.15
1 deposits, 1 withdrawals
Monthly service charge was: $6.35
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, annualRate=0.1, serviceCharge=35][active=false]
The Tester class
The Tester class tests your new Checking and MoneyMarket classes. Source code for
Tester is given below, and can be downloaded from Blackboard, Course Documents,
Week 12, Example programs.
import java.util.ArrayList;
/**
* Driver for Lab 5 Inheritance
*
* @version 5/31/2028
*/
public class Tester
{
public static void main(String arg)
{
// create some accounts
// $100.00, 10.0% annual interest
Checking check = new Checking("123", 100.0, 0.1);
// $1000.00, 10.0% annual interest
MoneyMarket money = new MoneyMarket("456", 1000.0, 0.1);
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("nPrint accounts");
for (Checking c : accounts)
System.out.println(c.toString());
// print month end report
System.out.println("nMonth 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 must have a clear, meaningful Javadoc comment
each toString() is required to use standardized formatting
when you have thoroughly tested your program and are certain it is correct, save your output into the output.txt file
Explanation / Answer
import java.io.*; class BankWork { final int max_limit=20; final int min_limit=1; final double min_bal=500; private String name[]=new String[20]; privateint accNo[]=newint[20]; private String accType[]=new String[20]; privatedouble balAmt[]=newdouble[20]; staticint totRec=0; //constructor BankWork() { for(int i=0;imax_limit) { System.out.println(" Sorry we cannot admit you in our bank... "); permit=false; } if(permit = true) //Allows to create new entry { totRec++; // Incrementing Total Record System.out.println(" =====RECORDING NEW ENTRY====="); try{ accNo[totRec]=totRec; //Created AutoNumber to accNo so no invalid id occurs System.out.println("Account Number : "+accNo[totRec]); BufferedReader obj = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Name : "); System.out.flush(); name[totRec]=obj.readLine(); System.out.print("Enter Account Type : "); System.out.flush(); accType[totRec]=obj.readLine(); do{ System.out.print("Enter Initial Amount to be deposited : "); System.out.flush(); str=obj.readLine(); balAmt[totRec]=Double.parseDouble(str); }while(balAmt[totRec]<min_bal); validation="" that="" minimun="" amount="" must="" be="" 500="" system.out.println(" ");="" }="" catch(exception="" e)="" {}="" to="" display="" details="" of="" recordpublicvoid="" display()="" {="" string="" str;="" int="" acno="0;" boolean="" valid="true;" system.out.println(" ="====DISPLAYING" customer="==== ");" try{="" bufferedreader="" obj="new" bufferedreader(new="" inputstreamreader(system.in));="" system.out.print("enter="" account="" number="" :="" ");="" system.out.flush();="" str="obj.readLine();" if="" (acnototrec)="" check="" whether="" accno="" is="" or="" not="" system.out.println(" invalid="" ");="" (valid="=true)" system.out.println(" account="" "+accno[acno]);="" system.out.println("name="" "+name[acno]);="" system.out.println("account="" type="" "+acctype[acno]);="" system.out.println("balance="" "+balamt[acno]+" ");="" deposit="" an="" amountpublicvoid="" deposit()="" double="" amt;="" acno;="" system.out.println(" ="====DEPOSIT" reading="" value="" no="" you="" want="" amt="Double.parseDouble(str);" balamt[acno]="balAmt[acno]+amt;" displaying="" depsit="" system.out.println(" after="" updation...");="" "+acno);="" withdraw="" balancepublicvoid="" withdraw()="" amt,checkamt;="" "+balamt[acno]);="" checkamt="balAmt[acno]-amt;" if(checkamt="" >="min_bal)" else="" system.out.println(" as="" per="" bank="" rule="" should="" maintain="" minimum="" balance="" rs="" 500 ");="" };="" class="" publicstaticvoid="" main(string="" args[])="" choice;="" choice="0;" bankwork="" bw_obj="new" bankwork();="" do="" system.out.println("choose="" your="" choices="" ...");="" system.out.println("1)="" new="" record="" entry="" system.out.println("2)="" system.out.println("3)="" deposit...");="" system.out.println("4)="" withdraw...");="" system.out.println("5)="" exit");="" switch(choice)="" case="" 1="" bw_obj.newentry();="" break;="" 2="" bw_obj.display();="" 3="" deposit...="" bw_obj.deposit();="" 4="" withdraw...="" bw_obj.withdraw();="" 5="" system.out.println(" .....thanks="" for="" visiting.....");="" default="" system.out.println(" invalid="" }while(choice!="5);" }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.