Q1. Create a Java program with a class Account that has oneprivate instance variable Balance representing the account balance.The data type of the variable Balance is double. The classshould provide a parameterless constructor that initializes theinstance variable Balance to 0.0. The class should provide aconstructor that receives an initial balance via its parameter anduses it to initialize the instance variable Balance. The classshould have a public setter method and a public getter method toset or get the value of the variable Balance. When setting variableBalance, the setter should validate the balance to ensure that thevalue assigned to the variable should be greater than or equal to0.0. Otherwise, the setter should give an error message and set thevalue assigned to the variable Balance to 0.0. The getter methodshould return the current balance, i.e. the current value of thevariable Balance, The class should provide two publicmethods. A method named Credit should add an amount to the currentbalance. A method named Debit should withdraw money from theaccount and ensure that the debit amount does not exceed theaccount’s balance. If it does, the balance should be leftunchanged, and the method should display the message "Debit amountexceeded account balance!" to the user. After defining the classabove, create a class named AccountTest that creates an instance ofthe class Account and tests the methods of the class Account. Thatis, the instance will show the initial balance, deposit some money,show the updated balance, withdraw some money, and show the updatedbalance.
Q2. Create a derived class named SavingsAccount that inheritsfrom the class Account that was created in Q1. The derivedclass SavingsAccount should include an instance variable Rateindicating the interest rate (percentage) assigned to the account.The data type of the variable Rate should be double. Theclass SavingsAccount has two constructors. One is a parameterlessconstructor which always initializes the initial balance to 0.0,and the interest rate to 0.02. The other constructor of the classSavingsAccount should receive the initial balance, as well as aninitial value for the interest rate via its parameters, and theninitialize the corresponding variables. The classSavingsAccount should provide a public method namedCalculateInterest that returns a double indicating the amount ofinterest earned by an account. The method CalculateInterest shoulddetermine this amount by multiplying the interest rate by theaccount balance. [Note: SavingsAccount should inherit methodsCredit and Debit without redefining them.] After defining theclass, create a class named SavingsAccountTest that creates aninstance of the class SavingsAccount and tests the methods of thebase class and derived class. That is, the instance will show theinitial balance, deposit some money, show the updated balance,withdraw some money, and show the updated balance.
Q3. Create a derived class named CheckingAccount that inheritsfrom the base class Account created in Q1 and has an instancevariable named Fee. The variable Fee represents the fee charged pertransaction. The class CheckingAccount has two constructors.One is a parameterless constructor that initializes the balance andthe fee to 0.0. The other constructor should receive the initialbalance and fee amount via its parameters and then assign them tothe corresponding variables. The class CheckingAccount shouldredefine the methods Credit and Debit in the base class so thatthey can subtract the fee from the account balance whenever eithertransaction is performed successfully. However, theCheckingAccount’s Debit method should charge a fee only if themoney is actually withdrawn (i.e., the debit amount does not exceedthe account balance). After defining the class, create aclass named CheckingAccountTest that creates an instance of theclass CheckingAccount and tests the methods of the classCheckingAccount. That is, the instance will show the initialbalance, deposit some money, show the updated balance, withdrawsome money, and show the updated balance.
Q4. Modify the class Account in Q1 to an abstract class. Themethod Credit and the method Debit are redefined as two abstractmethods. Modify the derived class SavingsAccount in Q2 and thederived class CheckingAccount in Q3. [Hint: implement the methodCredit and the method Debit in each derived class.] After definingthe class, create a class named AccountTest. It creates an instanceof the class CheckingAccount and tests the methods of the classCheckingAccount. In addition, it also creates an instance of theclass SavingsAccount and tests the methods of the classSavingsAccount. That is, each instance will show the initialbalance, deposit some money, show the updated balance, withdrawsome money, and show the updated balance.
Q5. Rewrite the program in Q1 with an interface. That is, youuse an interface to replace the abstract class and, if needed,modify any other members. After defining the class, create a classnamed AccountTest. It creates an instance of the classCheckingAccount and tests the methods of the class CheckingAccount.In addition, it also creates an instance of the classSavingsAccount and tests the methods of the class SavingsAccount.That is, each instance will show the initial balance, deposit somemoney, show the updated balance, withdraw some money, and show theupdated balance. Note: we have discussed a lot of Java techniques.So when working on this problem, you can develop the programflexibly, such as adding additional fields and methods and/ormodifying any members.
Q1. Create a Java program with a class Account that has one private instance variable Balance representing the account b
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am