Page 1 of 1

My books My folder Career Life Expert Q&A Find solutions to your homework Search Question Q1. Create a Java program with

Posted: Sun Jul 10, 2022 11:26 am
by answerhappygod
My books
My folder
Career
Life
Expert Q&A
Find solutions to your homework
Search
Question
Q1. Create a Java program with a class Account that has one private instance variable Balance representing the account balance. The data type of the variable Balance is double. The class should provide a parameterless constructor that initializes the instance variable Balance to 0.0. The class should provide a constructor that receives an initial balance via its parameter and uses it to initialize the instance variable Balance. The class should have a public setter method and a public getter method to set or get the value of the variable Balance. When setting variable Balance, the setter should validate the balance to ensure that the value assigned to the variable should be greater than or equal to 0.0. Otherwise, the setter should give an error message and set the value assigned to the variable Balance to 0.0. The getter method should return the current balance, i.e. the current value of the variable Balance, The class should provide two public methods. A method named Credit should add an amount to the current balance. A method named Debit should withdraw money from the account and ensure that the debit amount does not exceed the account’s balance. If it does, the balance should be left unchanged, and the method should display the message "Debit amount exceeded account balance!" to the user. After defining the class above, create a class named AccountTest that creates an instance of the class Account and tests the methods of the class Account. That is, the instance will show the initial balance, deposit some money, show the updated balance, withdraw some money, and show the updated balance.
Q2. Create a derived class named SavingsAccount that inherits from the class Account that was created in Q1. The derived class SavingsAccount should include an instance variable Rate indicating the interest rate (percentage) assigned to the account. The data type of the variable Rate should be double. The class SavingsAccount has two constructors. One is a parameterless constructor which always initializes the initial balance to 0.0, and the interest rate to 0.02. The other constructor of the class SavingsAccount should receive the initial balance, as well as an initial value for the interest rate via its parameters, and then initialize the corresponding variables. The class SavingsAccount should provide a public method named CalculateInterest that returns a double indicating the amount of interest earned by an account. The method CalculateInterest should determine this amount by multiplying the interest rate by the account balance. [Note: SavingsAccount should inherit methods Credit and Debit without redefining them.] After defining the class, create a class named SavingsAccountTest that creates an instance of the class SavingsAccount and tests the methods of the base class and derived class. That is, the instance will show the initial balance, deposit some money, show the updated balance, withdraw some money, and show the updated balance.
Q3. Create a derived class named CheckingAccount that inherits from the base class Account created in Q1 and has an instance variable named Fee. The variable Fee represents the fee charged per transaction. The class CheckingAccount has two constructors. One is a parameterless constructor that initializes the balance and the fee to 0.0. The other constructor should receive the initial balance and fee amount via its parameters and then assign them to the corresponding variables. The class CheckingAccount should redefine the methods Credit and Debit in the base class so that they can subtract the fee from the account balance whenever either transaction is performed successfully. However, the CheckingAccount’s Debit method should charge a fee only if the money is actually withdrawn (i.e., the debit amount does not exceed the account balance). After defining the class, create a class named CheckingAccountTest that creates an instance of the class CheckingAccount and tests the methods of the class CheckingAccount. That is, the instance will show the initial balance, deposit some money, show the updated balance, withdraw some money, and show the updated balance.
*** The code to this point has been answered***
Q4. Modify the class Account in Q1 to an abstract class. The method Credit and the method Debit are redefined as two abstract methods. Modify the derived class SavingsAccount in Q2 and the derived class CheckingAccount in Q3. [Hint: implement the method Credit and the method Debit in each derived class.] After defining the class, create a class named AccountTest. It creates an instance of the class CheckingAccount and tests the methods of the class CheckingAccount. In addition, it also creates an instance of the class SavingsAccount and tests the methods of the class SavingsAccount. That is, each instance will show the initial balance, deposit some money, show the updated balance, withdraw some money, and show the updated balance.
Q5. Rewrite the program in Q1 with an interface. That is, you use an interface to replace the abstract class and, if needed, modify any other members. After defining the class, create a class named AccountTest. It creates an instance of the class CheckingAccount and tests the methods of the class CheckingAccount. In addition, it also creates an instance of the class SavingsAccount and tests the methods of the class SavingsAccount. That is, each instance will show the initial balance, deposit some money, show the updated balance, withdraw some money, and show the updated balance. Note: we have discussed a lot of Java techniques. So when working on this problem, you can develop the program flexibly, such as adding additional fields and methods and/or modifying any members.