Question

In: Computer Science

Java Programming Task #1 Extending the BankAccount Class Copy the files AccountDriver.java (Code Listing 10.1) and...

Java Programming

Task #1 Extending the BankAccount Class

  1. Copy the files AccountDriver.java (Code Listing 10.1) and BankAccount.java(Code Listing 10.2) from the Student CD or as directed by your instructor.BankAccount.java is complete and will not need to be modified.

  2. Create a new class called CheckingAccount that extends BankAccount.

  3. It should contain a static constant FEE that represents the cost of clearing one

    check. Set it equal to 15 cents.

  4. Write a constructor that takes a name and an initial amount as parameters. It

    should call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenated with –10 (All checking accounts at this bank are identified by the extension –10). There can be only one checking account for each account number. Remember since accountNumber is a private member in BankAccount, it must be changed through a mutator method.

  5. Write a new instance method, withdraw, that overrides the withdraw method in the superclass. This method should take the amount to withdraw, add to it the fee for check clearing, and call the withdraw method from the superclass. Remember that to override the method, it must have the same method heading. Notice that the withdraw method from the superclass returns true or falsedepending if it was able to complete the withdrawal or not. The method that overrides it must also return the same true or false that was returned from the call to the withdraw method from the superclass.

  6. Compile and debug this class.

Task #2 Creating a Second Subclass

  1. Create a new class called SavingsAccount that extends BankAccount.

  2. It should contain an instance variable called rate that represents the annual

    interest rate. Set it equal to 2.5%.

  3. It should also have an instance variable called savingsNumber, initialized to 0.

    In this bank, you have one account number, but can have several savings accounts with that same number. Each individual savings account is identified by the number following a dash. For example, 100001-0 is the first savings account you open, 100001-1 would be another savings account that is still part of your same account. This is so that you can keep some funds separate from the others, like a Christmas club account.

  4. An instance variable called accountNumber, that will hide theaccountNumber from the superclass, should also be in this class.

  5. Write a constructor that takes a name and an initial balance as parameters and calls the constructor for the superclass. It should initialize accountNumber to be the current value in the superclass accountNumber (the hidden instance variable) concatenated with a hyphen and then the savingsNumber.

Write a method called postInterest that has no parameters and returns no value. This method will calculate one month's worth of interest on the balance and deposit it into the account.

  1. Write a method that overrides the getAccountNumber method in the superclass.

  2. Write a copy constructor that creates another savings account for the same person. It should take the original savings account and an initial balance as parameters. It should call the copy constructor of the superclass, and assign thesavingsNumber to be one more than the savingsNumber of the original savings account. It should assign the accountNumber to be theaccountNumber of the superclass concatenated with the hyphen and thesavingsNumber of the new account.

  3. Compile and debug this class.

  4. Use the AccountDriver class to test out your classes. If you named and

    created your classes and methods correctly, it should not have any difficulties. If you have errors, do not edit the AccountDriver class. You must make your classes work with this program.

  5. Running the program should give the following output:

Account Number 100001-10 belonging to Benjamin Franklin
Initial balance = $1000.00
After deposit of $500.00, balance = $1500.00
After withdrawal of $1000.00, balance = $499.85
Account Number 100002-0 belonging to William Shakespeare
Initial balance = $400.00
After deposit of $500.00, balance = $900.00
Insufficient funds to withdraw $1000.00, balance = $900.00
After monthly interest has been posted, balance = $901.88
Account Number 100002-1 belonging to William Shakespeare
Initial balance = $5.00
After deposit of $500.00, balance = $505.00
Insufficient funds to withdraw $1000.00, balance = $505.00
Account Number 100003-10 belonging to Isaac Newton

Code Listing 10.1 (AccountDriver.java)

/**
   This program demonstrates the BankAccount and
   derived classes.

*/

public class AccountDriver
{
   public static void main(String[] args)
   {
      double put_in = 500;
      double take_out = 1000;
      String money;
      String money_in;
      String money_out;
      boolean completed;
      // Test the CheckingAccount class.
      CheckingAccount myCheckingAccount =
         new CheckingAccount("Benjamin Franklin", 1000);
      System.out.println("Account Number " +
                         myCheckingAccount.
                         getAccountNumber() +
                         " belonging to " +
                         myCheckingAccount.getOwner());
      money = String.format("%.2f",
                            myCheckingAccount.
                            getBalance());
      System.out.println("Initial balance = $" + money);
      myCheckingAccount.deposit(put_in);
      money_in = String.format("%.2f", put_in);
      money = String.format("%.2f",
                            myCheckingAccount.
                            getBalance());
      System.out.println("After deposit of $" +
                         money_in + ", balance = $" +
                         money);
      completed = myCheckingAccount.withdraw(take_out);
      money_out = String.format("%.2f", take_out);

Copyright © 2016 Pearson Education, Inc., Hoboken NJ

money = String.format("%.2f",
                      myCheckingAccount.
                      getBalance());
if (completed)
{
   System.out.println("After withdrawal of $" +
                      money_out + ", balance = $" +

money);

} else {

   System.out.println("Insuffient funds to " +
                      "withdraw $" + money_out +
                      ", balance = $" + money);
}
System.out.println();
// Test the SavingsAccount class.
SavingsAccount yourAccount =
   new SavingsAccount("William Shakespeare", 400);
System.out.println("Account Number " +
                   yourAccount.getAccountNumber() +
                   " belonging to " +
                   yourAccount.getOwner());
money = String.format("%.2f",
                      yourAccount.getBalance());
System.out.println("Initial balance = $" + money);
yourAccount.deposit(put_in);
money_in = String.format("%.2f", put_in);
money = String.format("%.2f",
                      yourAccount.getBalance());
System.out.println("After deposit of $" +
                   money_in + ", balance = $" +
                   money);
completed = yourAccount.withdraw(take_out);

Copyright © 2016 Pearson Education, Inc., Hoboken NJ

money_out = String.format("%.2f", take_out);
money = String.format("%.2f",
                      yourAccount.getBalance());
if (completed)
{
   System.out.println("After withdrawal of $" +
                      money_out + ", balance = $" +

money);

} else {

   System.out.println("Insuffient funds " +
                      "to withdraw $" + money_out +
                      ", balance = $" + money);
}
yourAccount.postInterest();
money = String.format("%.2f",
                      yourAccount.getBalance());
System.out.println("After monthly interest " +
                   "has been posted, " +
                   "balance = $" + money);
System.out.println();
// Test the copy constructor of the
// SavingsAccount class.
SavingsAccount secondAccount =
   new SavingsAccount(yourAccount, 5);
System.out.println("Account Number " +
                   secondAccount.
                   getAccountNumber() +
                   " belonging to " +
                   secondAccount.getOwner());
money = String.format("%.2f",
                      secondAccount.getBalance());
System.out.println("Initial balance = $" + money);
secondAccount.deposit(put_in);

} }

money_in = String.format("%.2f", put_in);
money = String.format("%.2f",
                      secondAccount.getBalance());
System.out.println("After deposit of $" + money_in +
                   ", balance = $" + money);
secondAccount.withdraw(take_out);
money_out = String.format("%.2f", take_out);
money  = String.format("%.2f",
                       secondAccount.getBalance());
if (completed)
{
   System.out.println("After withdrawal of $" +
                      money_out + ", balance = $" +

money);

} else {

   System.out.println("Insuffient funds " +
                      "to withdraw $" + money_out +
                      ", balance = $" + money);
}
System.out.println();
// Test to make sure new accounts are
// numbered correctly.
CheckingAccount yourCheckingAccount =
   new CheckingAccount("Issac Newton", 5000);
System.out.println("Account Number " +
                   yourCheckingAccount.
                   getAccountNumber() +
" belonging to " +
yourCheckingAccount.getOwner());

Code Listing 10.2 (BankAccount.java)

/**
   The BankAccount class is an abstract class that holds
   general data about a bank account. Classes representing
   specific types of bank accounts should inherit from
   this class.

*/

public abstract class BankAccount
{
   // Class variable so that each account
   // has a unique number
   protected static int numberOfAccounts = 100001;
   // Current balance in the account
   private double balance;
   // Name on the account
   private String owner;
   // Number bank uses to identify account
   private String accountNumber;
   /**
      Default constructor

*/

   public BankAccount()
   {
      balance = 0;
      accountNumber = numberOfAccounts + "";
      numberOfAccounts++;

}

/**
   Standard constructor
   @param name The owner of the account.
   @param amount The beginning balance.

*/

public BankAccount(String name, double amount)
{
   owner = name;
   balance = amount;
   accountNumber = numberOfAccounts + "";
   numberOfAccounts++;

}

/**
   Copy constructor creates another account
   for the same owner.
   @param oldAccount The account with information
          to copy.
   @param amount The beginning balance of the

new account.

*/

public BankAccount(BankAccount oldAccount,
                   double amount)
{
   owner = oldAccount.owner;
   balance = amount;
   accountNumber = oldAccount.accountNumber;

}

/**
   Allows you to add money to the account.
   @param amount The amount to deposit in the account.

*/

public void deposit(double amount)
{
   balance = balance + amount;
}

Copyright © 2016 Pearson Education, Inc., Hoboken NJ

/**
   Allows you to remove money from the account if
   enough money is available, returns true if the
   transaction was completed, returns false if
   there was not enough money.
   @param amount The amount to withdraw from
          the account.
   @return True if there was sufficient funds to
           complete the transaction, false otherwise.

*/

public boolean withdraw(double amount)
{
   boolean completed = true;
   if (amount <= balance)
   {
      balance = balance - amount;
   }

else {

      completed = false;
   }
   return completed;
}
/**
   Accessor method to balance
   @return The balance of the account.

*/

public double getBalance()
{
   return balance;
}
/**
   accessor method to owner
   @return The owner of the account.

*/

public String getOwner()
{
   return owner;
}

/** Accessor method to account number @return The account number.

*/

   public String getAccountNumber()
   {
      return accountNumber;
   }
   /**
      Mutator method to change the balance
      @param newBalance The new balance for the account.

*/

   public void setBalance(double newBalance)
   {
      balance = newBalance;
   }
   /**
      Mutator method to change the account number
      @param newAccountNumber The new account number.

*/

   public void setAccountNumber(String newAccountNumber)
   {
      accountNumber = newAccountNumber;
   }

}

Solutions

Expert Solution

//BankAccount.java
public abstract class BankAccount {

   // Class variable so that each account
   // has a unique number
   protected static int numberOfAccounts = 100001;
   // Current balance in the account
   private double balance;
   // Name on the account
   private String owner;
   // Number bank uses to identify account
   private String accountNumber;
   /**
   Default constructor
   */

   public BankAccount()
   {
   balance = 0;
   accountNumber = numberOfAccounts + "";
   numberOfAccounts++;
   }

   /**
   Standard constructor
   @param name The owner of the account.
   @param amount The beginning balance.
   */

   public BankAccount(String name, double amount)
   {
   owner = name;
   balance = amount;
   accountNumber = numberOfAccounts + "";
   numberOfAccounts++;
   }

   /**
   Copy constructor creates another account
   for the same owner.
   @param oldAccount The account with information
   to copy.
   @param amount The beginning balance of the
   new account.

   */

   public BankAccount(BankAccount oldAccount,
   double amount)
   {
   owner = oldAccount.owner;
   balance = amount;
   accountNumber = oldAccount.accountNumber;
   }

   /**
   Allows you to add money to the account.
   @param amount The amount to deposit in the account.
   */

   public void deposit(double amount)
   {
   balance = balance + amount;
   }

   /**
   Allows you to remove money from the account if
   enough money is available, returns true if the
   transaction was completed, returns false if
   there was not enough money.
   @param amount The amount to withdraw from
   the account.
   @return True if there was sufficient funds to
   complete the transaction, false otherwise.
   */

   public boolean withdraw(double amount)
   {
   boolean completed = true;
   if (amount <= balance)
   {
   balance = balance - amount;
   }
   else {

   completed = false;
   }
   return completed;
   }
   /**
   Accessor method to balance
   @return The balance of the account.
   */

   public double getBalance()
   {
   return balance;
   }
   /**
   accessor method to owner
   @return The owner of the account.
   */

   public String getOwner()
   {
   return owner;
   }
   /** Accessor method to account number @return The account number.

   */

   public String getAccountNumber()
   {
   return accountNumber;
   }
   /**
   Mutator method to change the balance
   @param newBalance The new balance for the account.
   */

   public void setBalance(double newBalance)
   {
   balance = newBalance;
   }
   /**
   Mutator method to change the account number
   @param newAccountNumber The new account number.
   */

   public void setAccountNumber(String newAccountNumber)
   {
   accountNumber = newAccountNumber;
   }
}

//end of BankAccount.java

// CheckingAccount.java
public class CheckingAccount extends BankAccount{
  
   private static final double FEE = 0.15; // constant class variable FEE
  
   // constructor
   public CheckingAccount(String name, double initialAmount)
   {
       super(name,initialAmount);
       setAccountNumber(getAccountNumber()+"-10");
   }
  
   // overridden withdraw method
   public boolean withdraw(double amount)
   {
       return super.withdraw(amount+FEE);
   }
}

//end of CheckingAccount.java

//SavingsAccount.java
public class SavingsAccount extends BankAccount{
  
   // class instance variables
   private double rate = 0.025;
   private int savingsNumber = 0;
  
   // constructor
   public SavingsAccount(String name, double initialBalance)
   {
       super(name,initialBalance);
       setAccountNumber(super.getAccountNumber()+"-"+savingsNumber);
   }
  
   // copy constructor
   public SavingsAccount(SavingsAccount other, double initialBalance)
   {
       super(other,initialBalance);

       savingsNumber = other.savingsNumber+1;
       setAccountNumber(super.getAccountNumber().substring(0,super.getAccountNumber().indexOf("-")+1)+savingsNumber);

}
  
   // method to add monthly interest to account
   public void postInterest()
   {
       setBalance(getBalance()+getBalance()*(rate/12));
   }
  
   // overridden getAccountNumber
   public String getAccountNumber()
   {
       return super.getAccountNumber();
   }
}

//end of SavingsAccount.java

//AccountDriver.java
/**
This program demonstrates the BankAccount and
derived classes.
*/

public class AccountDriver {

   public static void main(String[] args)
   {
   double put_in = 500;
   double take_out = 1000;
   String money;
   String money_in;
   String money_out;
   boolean completed;
   // Test the CheckingAccount class.
   CheckingAccount myCheckingAccount =
   new CheckingAccount("Benjamin Franklin", 1000);
   System.out.println("Account Number " +
   myCheckingAccount.
   getAccountNumber() +
   " belonging to " +
   myCheckingAccount.getOwner());
   money = String.format("%.2f",
   myCheckingAccount.
   getBalance());
   System.out.println("Initial balance = $" + money);
   myCheckingAccount.deposit(put_in);
   money_in = String.format("%.2f", put_in);
   money = String.format("%.2f",
   myCheckingAccount.
   getBalance());
   System.out.println("After deposit of $" +
   money_in + ", balance = $" +
   money);
   completed = myCheckingAccount.withdraw(take_out);
   money_out = String.format("%.2f", take_out);
   money = String.format("%.2f",
myCheckingAccount.
getBalance());
       if (completed)
       {
       System.out.println("After withdrawal of $" +
       money_out + ", balance = $" +
       money);
      
       } else {
      
       System.out.println("Insuffient funds to " +
       "withdraw $" + money_out +
       ", balance = $" + money);
       }
       System.out.println();
       //Test the SavingsAccount class.
       SavingsAccount yourAccount =
       new SavingsAccount("William Shakespeare", 400);
       System.out.println("Account Number " +
       yourAccount.getAccountNumber() +
       " belonging to " +
       yourAccount.getOwner());
       money = String.format("%.2f",
       yourAccount.getBalance());
       System.out.println("Initial balance = $" + money);
       yourAccount.deposit(put_in);
       money_in = String.format("%.2f", put_in);
       money = String.format("%.2f",
       yourAccount.getBalance());
       System.out.println("After deposit of $" +
       money_in + ", balance = $" +
       money);
       completed = yourAccount.withdraw(take_out);
      
       money_out = String.format("%.2f", take_out);
       money = String.format("%.2f",
       yourAccount.getBalance());
       if (completed)
       {
       System.out.println("After withdrawal of $" +
       money_out + ", balance = $" +
       money);
      
       } else {
      
       System.out.println("Insuffient funds " +
       "to withdraw $" + money_out +
       ", balance = $" + money);
       }
       yourAccount.postInterest();
       money = String.format("%.2f",
       yourAccount.getBalance());
       System.out.println("After monthly interest " +
       "has been posted, " +
       "balance = $" + money);
       System.out.println();
       // Test the copy constructor of the
       // SavingsAccount class.
       SavingsAccount secondAccount =
       new SavingsAccount(yourAccount, 5);
       System.out.println("Account Number " +
       secondAccount.
       getAccountNumber() +
       " belonging to " +
       secondAccount.getOwner());
       money = String.format("%.2f",
       secondAccount.getBalance());
       System.out.println("Initial balance = $" + money);
       secondAccount.deposit(put_in);
      
       money_in = String.format("%.2f", put_in);
       money = String.format("%.2f",
       secondAccount.getBalance());
       System.out.println("After deposit of $" + money_in +
       ", balance = $" + money);
       secondAccount.withdraw(take_out);
       money_out = String.format("%.2f", take_out);
       money = String.format("%.2f",
       secondAccount.getBalance());
       if (completed)
       {
       System.out.println("After withdrawal of $" +
       money_out + ", balance = $" +
       money);
      
       } else {
      
       System.out.println("Insuffient funds " +
       "to withdraw $" + money_out +
       ", balance = $" + money);
       }
       System.out.println();
       // Test to make sure new accounts are
       // numbered correctly.
       CheckingAccount yourCheckingAccount =
       new CheckingAccount("Issac Newton", 5000);
       System.out.println("Account Number " +
       yourCheckingAccount.
       getAccountNumber() +
       " belonging to " +
       yourCheckingAccount.getOwner());
       }
}

//end of AccountDriver.java

Output:


Related Solutions

Task #1 Writing a Copy Constructor 1.Copy the files Address.java(Code Listing 8.1), Person.java(Code Listing 8.2),Money.java(Code Listing...
Task #1 Writing a Copy Constructor 1.Copy the files Address.java(Code Listing 8.1), Person.java(Code Listing 8.2),Money.java(Code Listing 8.3), MoneyDemo.java(Code Listing 8.4), andCreditCardDemo.java(Code Listing 8.5) from the Student CD or as directed by your instructor. Address.java, Person.java, MoneyDemo.java, and CreditCardDemo.java are complete and will not need to be modified. We will start by modifying Money.java. 2.Overload the constructor. The constructor that you will write will be a copy constructor. It should use the parameter Money object to make a duplicate Moneyobject, by...
I need this code in java. Task (1) Create two files to submit: ItemToPurchase.java - Class...
I need this code in java. Task (1) Create two files to submit: ItemToPurchase.java - Class definition ShoppingCartPrinter.java - Contains main() method Build the ItemToPurchase class with the following specifications: Private fields String itemName - Initialized in default constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 Default constructor Public member methods (mutators & accessors) setName() & getName() (2 pts) setPrice() & getPrice() (2 pts) setQuantity() & getQuantity()...
1.Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the...
1.Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the following method: public ExtLinkedList <E> mergeThreeLists (ExtLinkedList<E> list1, ExtLinkedList<E> list2) { } that returns an ExtLinkedList<E> which is the merged version of values from this list, followed by values from list1, and then followed by values from list2. For example, if E is Integer type and this list has (5,3,1), list1 has (8, 10,12,14), and list2 has (22,23,24,25,26) in that order, then the returned...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the following method: public ExtLinkedList <E> mergeThreeLists (ExtLinkedList<E> list1, ExtLinkedList<E> list2) { } that returns an ExtLinkedList<E> which is the merged version of values from thislist, followed by values from list1, and then followed by values from list2.    For example, if E is Integer type and this listhas (5,3,1), list1has (8, 10,12,14), and list2has (22,23,24,25,26) in that order, then the returned list from a call to...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data:  Account holder’s name (string)  Account number (int)  Account type (string, check/savings/business)  Balance (double)  Interest rate (double) – store interest rate as a decimal number.  Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. 1.2 Implement...
Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is...
Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is to fill in the code marked as "...your code here...". A detailed explanation follows the code. import java.util.*; public class OddManOut {    public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("How many random Integers to produce?"); int num = sc.nextInt();    ArrayList<Integer> randomInts = createRandomList(num); System.out.println("The random list is: "); System.out.println(randomInts);    removeOdds( randomInts ); System.out.println("The random list with only...
The following code is included for the java programming problem: public class Bunny {        private...
The following code is included for the java programming problem: public class Bunny {        private int bunnyNum;        public Bunny(int i) {               bunnyNum = i;        }        public void hop() {               System.out.println("Bunny " + bunnyNum + " hops");        } } Create an ArrayList <????> with Bunny as the generic type. Use an index for-loop to build (use .add(….) ) the Bunny ArrayList. From the output below, you need to have 5. Use an index for-loop...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
c++ code please show differint h files and cpp files 2.3 Task 1 You are working...
c++ code please show differint h files and cpp files 2.3 Task 1 You are working as a programmer designing a vehicular system for the newly forced Edison Arms Company. The weapon system is a generic weapon housing that can fit a number of different weapons that are all controlled by a tank in the same way. Most importantly, is the emphasis on safety. During combat, the weapon systems cannot become unreliable or fail lest the pilots be put in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT