Question
Part II: Design, implement, and use a simple class In this task you will design a class applying the concept of information hiding. The fields
Part II: Design, implement, and use a simple class In this task you will design a class applying the concept of information hiding. The fields of the class should be private and only accessible from outside world using getter and setter methods as discussed in class. You do not necessarily create a getter or setter for fields that do not have to be read or written by a client of the class. In addition to these basic access methods, a class also has convenience method(s) that do basic computations using the values of fields. For the class, you will write a main program that tests the functionality of the class by creating an instance of the class as described below and by retrieving the values from this object. Also implement a toString method for the class you design. Bank account (10 Points) Implement a class BankAccount in package com.labs.lab2 that models a bank account. For a bank account you should store the balance , the owners name , whether this is a checking or saving account , and the overdraw limit which is the maximum amount the account can fall below zero, e.g., if the overdraw limit is 30.0 then the account balance is not allowed to drop below -30.0. When the balance is set, then make sure that it is greater than or equal to -withdrawal limit. It should be possible to withdraw money from an account using a method double withDraw(double amount) that you have to implement. This method has to check whether the there is sufficient balance such that after the withdrawal the account balance is still above the overdraw limit . In case the withdrawal would cause the balance to drop below the limit, no money should be withdrawn. The return value of the method is the amount of money that was withdrawn. Do not forget to update the balance if the withdrawal is successful. If the account is a savings account then the bank charges a 3 dollar fee for each withdrawal. Note that the fee has to be taken into account when calculating the new account balance and when checking the overdraft limit Similarly, it should be possible to deposit money into the account using a method void deposit(double amount) . This increases balance by amount . It should also be possible to transfer money from one account to another. That should be implemented as a method boolean transfer(BankAccount to, double amount) . This method transfers amount from the bank account on which this method is called to bank account to . If this would cause the balance of the source account to drop below the overdraft limit, then the transfer should be abandoned (no balances are changed). The method returns true if the transfer is successful and false otherwise. To test your Bank account class, write a class BankAccountTest in package com.labs.lab1 which contains a main method that implements the following test case: Create a savings account for customer Alice with a withdrawal limit of $50.0. The initial balance of this account should be $100.0. Then create a checking account for Bob with a withdrawal limit of $30.0 and an initial balance of $350.0. Then execute the following operations and print their return values to the standard output: Withdraw $200 from Bobs account Deposit $50 into Alices account Withdraw $1000 from Bobs account (note that this would drop Bobs balance below his withdrawal limit) Transfer $75 from Bob to Alices account Print the account information for Bobs and Alices account using the toString method. 4 Use of control constructs and expressions In this task you will use control structures and expressions extensively. We will be extending the BankAccount class from the previous task. Interest calculation (3 Points) Create a class InterestCalculator in package com.labs.lab2 which has a single static method double applyInterest(BankAccount a, int years, double interestRate) which applies interestRate percent interest to the balance of bank account a for years years and returns the final balance of a after the interest has been applied. Recall that for x% interest the account balance is updated like this: balance = balance (1 + x 100 ) Write a main method that tests applyInterest using the following test case: Create a saving bank account for Alice with $110 balance. Apply 5% interest for 8 years and print the final account balance.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started