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

Solutions

Expert Solution

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

import java.text.NumberFormat;

public class SimpleBankAccount{

   // make balance protected, so that it is directly accessible to the child classes
protected 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";
}
}

//end of SimpleBankAccount.java

// CheckingAccount.java

public class CheckingAccount extends SimpleBankAccount
{
   // field to track the last processed check number
   private int last_processed_check;
  
   // default constructor
   public CheckingAccount()
   {
       super();
       last_processed_check = -1; // set it to -1, since all check number >= 0
   }
  
   // parameterized constructor
   public CheckingAccount(double bal, String id)
   {
       super(bal, id);
       last_processed_check = -1; // set it to -1, since all check number >= 0
   }
  
   /*
   * returns false if checkNum has the same check number as the last check processed,
   * otherwise it reduces the balance by amount and returns true.
   */
   public boolean processCheck( int checkNum, double amount )
   {
       // checkNum is not same as last processed check
       if(checkNum != last_processed_check)
       {
           balance -= amount; // reduce balance by amount
           last_processed_check = checkNum; // set last_processed_check to checkNum
           return true;
       }
      
       return false; // checkNum and last_processed_check are equal
   }
  
   // override toString method to return string representation of CheckingAccount
   public String toString()
   {
       return "Checking Account:\n"+super.toString() + "Last processed check number: "+last_processed_check+"\n";
   }
  
}
//end of CheckingAccount.java

// SavingsAccount.java

public class SavingsAccount extends SimpleBankAccount
{
   // field for the interest rate
   private double interest_rate;
  
   // 1-argument constructor
   public SavingsAccount(double rate)
   {
       super();
       interest_rate = rate;
   }
  
   // 3-argument constructor
   public SavingsAccount(double bal, String id, double rate )
   {
       super(bal, id);
       interest_rate = rate;
   }
  
   // method that multiples the current balance by the interest rate, and adds that to the balance.
   public void applyInterest()
   {
       balance += (balance*interest_rate);
   }
  
   // return String representation of SavingsAccount
   public String toString()
   {
       return "Savings Account:\n"+super.toString()+"APR: "+(interest_rate*100)+"%";
   }
}
//end of SavingsAccount.java

// AccountsDriver.java
/**
* 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( );

   }

}

//end of AccountsDriver.java


Output:


Related Solutions

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...
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....
USE JAVA Develop the classes for the following requirements: 1. A class named Employee (general, for...
USE JAVA Develop the classes for the following requirements: 1. A class named Employee (general, for college) 2. A class named Instructor (more specific, for college) 3. A class named Staff (more specific, for college, HR officer, Marking staff) Tasks: 1. Figure out the relationships among the classes; 2. Determine the abstract class, and the child classes; 3. For the abstract class, determine at least one abstract method; 4. Each class should at least two data members and one extra...
//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?...
This is in JAVA Shapes2D Write the following four classes to practice using an abstract class...
This is in JAVA Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is...
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...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT