Question

In: Computer Science

This is in JAVA Bank Accounts 01: Child Classes Copy the following SimpleBankAccount class and use...

This is in JAVA

Bank Accounts 01: Child Classes

Copy the following SimpleBankAccount class and use it as a base class:

/**
 * Simple representation of a bank account
 *
 * @author Jo Belle
 * @version 0.5 (10/12/2020)
 */

import java.text.NumberFormat;

public class SimpleBankAccount{
    // fields (instance variables)
    private double balance;
    private String accountId;

    /**
     * Constructor for objects of class SimpleBankAccount
     */
    public SimpleBankAccount(){
        balance = 0.0;
        accountId = "";
    }

    /**
     * Constructor for objects of class SimpleBankAccount
     */
    public SimpleBankAccount( double bal, String id ){
        balance = bal;
         accountId = id;
    }

    /**
     * Add money to the balance
     *
     * @param  amount the amount to deposit
     * @return void
     */
    public void deposit( double amount ){
        balance += amount;
    }

    /**
     * Remove money from the balance
     *
     * @param  amount the amount to withdraw
     * @return true (success) or false (failure)
     */
    public boolean withdraw( double amount ){
        if( balance - amount >= 0 ){
            balance -= amount;
            return true;
        }else{
            return false;
        }
    }

    /**
     * Get the balance
     *
     * @return the balance
     */
    public double getBalance(){
         return balance;
    }

    /**
     * Set account ID
     *
     * @param the account ID
     */
    public void setAccountId(String id){
         accountId = id;
    }


    /**
     * Get the account ID
     *
     * @return the account ID
     */
    public String getAccountId(){
         return accountId;
    }

    /**
     * Produces a string represenation of the balance
     * @return The balance (with a label)
     */
    public String toString( ){
        // display balance as currency
        String balanceStr = NumberFormat.getCurrencyInstance().format( balance );
        return "Balance for account " + accountId + ": " + balanceStr + "\n";
    }
}

Include at least two classes: CheckingAccount and SavingsAccount. Save your CheckingAccount class in a file named CheckingAccount.java and your SavingsAccount class in a file named SavingsAccount.java. Your CheckingAccount class needs to add a field to track the last processed check number. Also include both a no-argument constructor and a parameterized constructor (that takes a double and a String). Furthermore, include the following method:

public boolean processCheck( int checkNum, double amount );

which returns false if checkNum has the same check number as the last check processed, otherwise it reduces the balance by amount and returns true.
Your SavingsAccount class needs to have a field for the interest rate. Also include both a constructor that just takes the interest rate (as a double) and a parameterized constructor (that takes a double, String and a double). Furthermore, include an applyInterest() method that multiples the current balance by the interest rate, and adds that to the balance.
The following code should work and produce the output below:

/** 
 * Exercises the basic functionality of a Checking and SavingsAccount
 *
 * @author Jo Belle
 * @version 0.3 (10/12/2020)
 */
public class AccountsDriver{
    final public static double INTEREST_RATE = 0.01;  // 1%

    public static void main( String[] args ){
        CheckingAccount checking = new CheckingAccount( 100.0, "checking123" );
        SavingsAccount savings = new SavingsAccount( 1000.0, "savings124", INTEREST_RATE );

        double monthlyExpenses = 756.34;
        int electricBillCheckNum = 2123;
        double electricBill = 60.34;
        int registationCheckNum = 2124;
        double registration = 50.00;
        double dinnerMoney = 55.32;
        double futureCar = 200.0;
        double textbook = 90.0;

        // checking account transactions
        checking.deposit( monthlyExpenses );
        checking.processCheck( electricBillCheckNum, electricBill );
        checking.withdraw( dinnerMoney );
        checking.processCheck( registationCheckNum, registration );
        System.out.print( checking.toString() );
        System.out.println( );

        // savings account transactions
        savings.deposit( futureCar );
        savings.applyInterest( );
        savings.withdraw( textbook );
        System.out.print( savings.toString() );
        System.out.println( );
    }
}

Output:

Checking Account:
Balance for account checking123: $690.68
Last processed check number: 2124

Savings Account:
Balance for account savings124: $1,122.00
APR: 1.0%

Make just the necessary changes to the code in SimpleBankAccount to complete the instructions.
Submit the following files:

  • CheckingAccount.java
  • SavingsAccount.java
  • SimpleBankAccount.java

Bank Accounts 02: Overriding 1

Building off of the Bank Accounts 01 practice assignment above, in your CheckingAccount and SavingsAccount classes, override the toString() method. Additionally include a call to SimpleBankAccount's toString() method. Use the appropriate annotation to designate that you're expecting this method to override another method.
Submit the following files:

  • CheckingAccount.java
  • SavingsAccount.java
  • SimpleBankAccount.java

Bank Accounts 03: Overriding 2

Building off of the Bank Accounts 01 practice assignment above, add an equals() method that returns true if all of the fields match and false otherwise. The Object class has the following method:

public boolean equals( Object obj )

To override this method, you must have the same method header. Additionally, to use the fields of the class that overrides the method, you need to cast the parameter to the current class.
Your equals methods should work so that the following code will execute, but not display anything:

/** 
 * Exercises equals()
 *
 * @author Jo Belle
 * @version 0.1 (10/12/2020)
 */
public class BankAccounts03{
    final public static double INTEREST_RATE = 0.01;  // 1%

    public static void main( String[] args ){
        CheckingAccount checking = new CheckingAccount( 100.0, "checking123" );
        SavingsAccount savings = new SavingsAccount( 1000.0, "savings124", INTEREST_RATE );

        CheckingAccount checkingCopy = new CheckingAccount( 100.0, "checking123" );
        SavingsAccount savingsCopy = new SavingsAccount( 1000.0, "savings124", INTEREST_RATE );

        if( checking.equals( checkingCopy ) == false ){
            System.err.println("ERROR: The following objects are equal:");
            System.err.println( checking );
            System.err.println( checkingCopy );
        }

        if( savings.equals( savingsCopy ) == false ){
            System.err.println("ERROR: The following objects are equal:");
            System.err.println( savings );
            System.err.println( savingsCopy );
        }

        int electricBillCheckNum = 2123;
        double electricBill = 60.34;
        double futureCar = 200.0;

        checking.processCheck( electricBillCheckNum, electricBill );

        savings.deposit( futureCar );
        savings.applyInterest( );

        if( checking.equals( checkingCopy ) == true ){
            System.err.println("ERROR: The following objects are NOT equal:");
            System.err.println( checking );
            System.err.println( checkingCopy );
        }

        if( savings.equals( savingsCopy ) == true ){
            System.err.println("ERROR: The following objects are NOT equal:");
            System.err.println( savings );
            System.err.println( savingsCopy );
        }
    }
}


Submit the following files:

  • CheckingAccount.java
  • SavingsAccount.java
  • SimpleBankAccount.java

Solutions

Expert Solution

Solution to the above problem is given below.

SimpleBankAccount.java


/**
 * Simple representation of a bank account
 *
 * @author Jo Belle
 * @version 0.5 (10/12/2020)
 */
import java.text.NumberFormat;

public class SimpleBankAccount{
    // fields (instance variables)
    private double balance;
    private String accountId;

    /**
     * Constructor for objects of class SimpleBankAccount
     */
    public SimpleBankAccount(){
        balance = 0.0;
        accountId = "";
    }

    /**
     * Constructor for objects of class SimpleBankAccount
     */
    public SimpleBankAccount( double bal, String id ){
        balance = bal;
         accountId = id;
    }

    /**
     * Add money to the balance
     *
     * @param  amount the amount to deposit
     * @return void
     */
    public void deposit( double amount ){
        balance += amount;
    }

    /**
     * Remove money from the balance
     *
     * @param  amount the amount to withdraw
     * @return true (success) or false (failure)
     */
    public boolean withdraw( double amount ){
        if( balance - amount >= 0 ){
            balance -= amount;
            return true;
        }else{
            return false;
        }
    }

    /**
     * Get the balance
     *
     * @return the balance
     */
    public double getBalance(){
         return balance;
    }

    /**
     * Set account ID
     *
     * @param the account ID
     */
    public void setAccountId(String id){
         accountId = id;
    }


    /**
     * Get the account ID
     *
     * @return the account ID
     */
    public String getAccountId(){
         return accountId;
    }

    /**
     * Produces a string represenation of the balance
     * @return The balance (with a label)
     */
    public String toString( ){
        // display balance as currency
        String balanceStr = NumberFormat.getCurrencyInstance().format( balance );
        return "Balance for account " + accountId + ": " + balanceStr + "\n";
    }
}

CheckingAccount.java


public class CheckingAccount extends SimpleBankAccount{
        
        private int lastProcessedCheckNumber;

        /**
         * 
         */
        public CheckingAccount() {
                super();
                // TODO Auto-generated constructor stub
        }

        /**
         * @param bal
         * @param id
         */
        public CheckingAccount(double bal, String id) {
                super(bal, id);
                // TODO Auto-generated constructor stub
        }
        
        
        
        /**
         * @return the lastProcessedCheckNumber
         */
        public int getLastProcessedCheckNumber() {
                return lastProcessedCheckNumber;
        }

        /**
         * @param lastProcessedCheckNumber the lastProcessedCheckNumber to set
         */
        public void setLastProcessedCheckNumber(int lastProcessedCheckNumber) {
                this.lastProcessedCheckNumber = lastProcessedCheckNumber;
        }

        public boolean processCheck( int checkNum, double amount ) {
                
                if(checkNum == this.lastProcessedCheckNumber) {
                        return false;
                }
                this.lastProcessedCheckNumber = checkNum;
                return true;
        }

        @Override
        public String toString() {
                return "CheckingAccount [lastProcessedCheckNumber=" + lastProcessedCheckNumber + "]";
        }


        
}

SavingsAccount.java


public class SavingsAccount extends SimpleBankAccount{
        private double interestRate;

        /**
         * 
         */
        public SavingsAccount() {
                super();
                // TODO Auto-generated constructor stub
        }

        /**
         * @return the interestRate
         */
        public double getInterestRate() {
                return interestRate;
        }

        /**
         * @param interestRate the interestRate to set
         */
        public void setInterestRate(double interestRate) {
                this.interestRate = interestRate;
        }

        /**
         * @param bal
         * @param id
         */
        public SavingsAccount(double bal, String id, double interestRate) {
                super(bal, id);
                this.interestRate = interestRate;
                
        }
        // method to apply interest rate
        public double applyInterest() {
                
                return super.getBalance() + super.getBalance()*interestRate;
        }

        @Override
        public String toString() {
                return "SavingsAccount [interestRate=" + interestRate + "]";
        }
        

}

After making BankAccount02 modifications:

SimpleBankAccount.java


/**
 * Simple representation of a bank account
 *
 * @author Jo Belle
 * @version 0.5 (10/12/2020)
 */
import java.text.NumberFormat;

public class SimpleBankAccount{
    // fields (instance variables)
    private double balance;
    private String accountId;

    /**
     * Constructor for objects of class SimpleBankAccount
     */
    public SimpleBankAccount(){
        balance = 0.0;
        accountId = "";
    }

    /**
     * Constructor for objects of class SimpleBankAccount
     */
    public SimpleBankAccount( double bal, String id ){
        balance = bal;
         accountId = id;
    }

    /**
     * Add money to the balance
     *
     * @param  amount the amount to deposit
     * @return void
     */
    public void deposit( double amount ){
        balance += amount;
    }

    /**
     * Remove money from the balance
     *
     * @param  amount the amount to withdraw
     * @return true (success) or false (failure)
     */
    public boolean withdraw( double amount ){
        if( balance - amount >= 0 ){
            balance -= amount;
            return true;
        }else{
            return false;
        }
    }

    /**
     * Get the balance
     *
     * @return the balance
     */
    public double getBalance(){
         return balance;
    }

    /**
     * Set account ID
     *
     * @param the account ID
     */
    public void setAccountId(String id){
         accountId = id;
    }


    /**
     * Get the account ID
     *
     * @return the account ID
     */
    public String getAccountId(){
         return accountId;
    }

    /**
     * Produces a string represenation of the balance
     * @return The balance (with a label)
     */
    public String toString( ){
        // display balance as currency
        String balanceStr = NumberFormat.getCurrencyInstance().format( balance );
        return "Balance for account " + accountId + ": " + balanceStr + "\n";
    }
}

CheckingAccount.java


public class CheckingAccount extends SimpleBankAccount{
        
        private int lastProcessedCheckNumber;

        /**
         * 
         */
        public CheckingAccount() {
                super();
                // TODO Auto-generated constructor stub
        }

        /**
         * @param bal
         * @param id
         */
        public CheckingAccount(double bal, String id) {
                super(bal, id);
                // TODO Auto-generated constructor stub
        }
        
        
        
        /**
         * @return the lastProcessedCheckNumber
         */
        public int getLastProcessedCheckNumber() {
                return lastProcessedCheckNumber;
        }

        /**
         * @param lastProcessedCheckNumber the lastProcessedCheckNumber to set
         */
        public void setLastProcessedCheckNumber(int lastProcessedCheckNumber) {
                this.lastProcessedCheckNumber = lastProcessedCheckNumber;
        }

        public boolean processCheck( int checkNum, double amount ) {
                
                if(checkNum == this.lastProcessedCheckNumber) {
                        return false;
                }
                this.lastProcessedCheckNumber = checkNum;
                return true;
        }

        @Override
        public String toString() {
                return super.toString()+"CheckingAccount [lastProcessedCheckNumber=" + lastProcessedCheckNumber + "]";
        }


        
}

SavingsAccount.java


public class SavingsAccount extends SimpleBankAccount{
        private double interestRate;

        /**
         * 
         */
        public SavingsAccount() {
                super();
                // TODO Auto-generated constructor stub
        }

        /**
         * @return the interestRate
         */
        public double getInterestRate() {
                return interestRate;
        }

        /**
         * @param interestRate the interestRate to set
         */
        public void setInterestRate(double interestRate) {
                this.interestRate = interestRate;
        }

        /**
         * @param bal
         * @param id
         */
        public SavingsAccount(double bal, String id, double interestRate) {
                super(bal, id);
                this.interestRate = interestRate;
                
        }
        // method to apply interest rate
        public double applyInterest() {
                
                return super.getBalance() + super.getBalance()*interestRate;
        }

        @Override
        public String toString() {
                return super.toString()+ "SavingsAccount [interestRate=" + interestRate + "]";
        }
        

}

Output BankAccount02:

Output for AccountsDriver class:

Output for BankAccount03:

End of Solution


Related Solutions

Write a java program with the following classes: Class Player Method Explanation: play : will use...
Write a java program with the following classes: Class Player Method Explanation: play : will use a loop to generate a series of random numbers and add them to a total, which will be assigned to the variable score. decideRank: will set the instance variable rank to “Level 1”, “Level 2”, “Level 3”, “Level 4” based on the value of score, and return that string. getScore : will return score. toString: will return a string of name, score and rank....
//Using Java language Write a copy instructor for the following class. Be as efficient as possible....
//Using Java language Write a copy instructor for the following class. Be as efficient as possible. import java.util.Random; class Saw {    private int x;    private Integer p; //------------------------------------------ public Saw() { Random r = new Random();        x = r.nextInt();        p = new Integer(r.nextInt()); } }
Design You will need to have at least four classes: a parent class, a child class,...
Design You will need to have at least four classes: a parent class, a child class, a component class, and an unrelated class. The component object can be included as a field in any of the other three classes. Think about what each of the classes will represent. What added or modified methods will the child class have? What added fields will the child class have? Where does the component belong? How will the unrelated class interact with the others?...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads in the principle 2) reads in additional money deposited each year (treat this as a constant) 3) reads in years to grow, and 4) reads in interest rate And then finally prints out how much money they would have each year. See below for formatting. Enter the principle: XX Enter the annual addition: XX Enter the number of years to grow: XX Enter the...
In Java, using the code provided for Class Candle, create a child class that meets the...
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output ------------------------------------------------------------------------ 1. The child class will be named  ScentedCandle 2. The data field for the ScentedCandle class is:    scent 3. It will also have getter and setter methods 4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint:   price = height * PER_INCH) CODE...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the...
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the Java ArrayList. This class is not complete and must be modified as such: Write the method body for the default constructor Write the method body for the methods: arraySize(), elements(), grow(), shrink(). The incomplete code is provided here: public class DynArray { private double[] array; private int size; private int nextIndex;    public int arraySize() { }    public int elements() { } public...
Create two child classes, UnderGraduateStudent and GraduateStudent that will extend from the Student class. Override the...
Create two child classes, UnderGraduateStudent and GraduateStudent that will extend from the Student class. Override the char getLetterGrade() method in each of the child classes. Use Student.java class defined below: (complete and compile) class Student {    private int id;    private int midtermExam;    private int finalExam;    public double calcAvg() {       double avg;       avg = (midtermExam + finalExam) / 2.0;       return avg;    }    public char getLetterGrade() {       char letterGrade = ‘ ‘;...
JAVA -The next three questions use the following class: class Identification { private int idNum;   ...
JAVA -The next three questions use the following class: class Identification { private int idNum;    public Identification() { this(0); }    public Identification(int startingIdNum) { idNum = startingIdNum; }    public int getIdNum() { return idNum; }    public void setIdNum(int idNum) { this.idNum = idNum; } } Here is one program using the above class: public class Main {    public static void main(String[] args) {        Identification i1 = new Identification();        Identification i2 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT