Question

In this lab, a piece of working software is given. You are asked to make changes...

In this lab, a piece of working software is given. You are asked to make changes to the software according to some requirement. The scenario in this lab mimics the situation that most old software applications are facing---need to update. When covering inheritance and polymorphism, we introduced a pattern that has three elements: instanceof operator, base pointers, and a collection data structure. In the given code, the collection used is a dynamic array (will change its length automatically) called Vector. Vector is an old data structure that is already deprecated in Java.

In this lab, the main task for you is to replace the Vector collection with the new collection data structure ArrayList. After the change, the program is supposed to produce the similar result in terms of functionality.

Given Code (You can cut and paste the code to Eclipse)

In this section, you are given a piece of working software that has 5 classes: the driver class TestLabVersion1, the Account class, the CheckingAccount, the SavingsAccount class, and the Bank class. Both the CheckingAccount and the SavingsAccount are children of Account. The Bank class contains different kinds of accounts.

In this lab, we assume that both CheckingAccount and SavinsAccount earn interest. The interest rate for checkingAccout is 0.05; the interest rate for SavingsAccount is 0.12. The getBalance() in the CheckingAccount will add 5% interest to the balance and return the total (do not change the balance in the parent class); the getBalance() in the SavingsAccount will add 12% interest to the balance and return the total (do not change the balance in the parent class). The class CheckingAccount and SavingsAccount are similar except the following two differences: 1) the interest rate is different. 2) the SavingsAccount has an extra method “getTax()” which returns the tax amount that is calculated according to the expression “tax = balance * 0.01.” The Bank class is the container that hosts different kinds of accounts. It has a method toString() that displays the content of accounts.

--------------------------------------------------------------------------------------Account begin

public class Account

{

private double balance;

private String name;

public Account() {}

public Account(String name, double openBalance)

{

this.name = name;

balance = openBalance;

}

public double getBalance()

{

return balance;

}

public String getName()

{

return name;

}

}

--------------------------------------------------------------------------------------Account end

--------------------------------------------------------------------------------------CheckingAccount begin

public class CheckingAccount extends Account

{

private final double INTERESTRATE = 0.05;

public CheckingAccount() {}

public CheckingAccount(String name, double openBalance)

{

super(name, openBalance);

}

public double getSupBalance()

{

return super.getBalance();

}

public double getBalance()

{

return super.getBalance() * (1+INTERESTRATE);

}

}

--------------------------------------------------------------------------------------CheckingAccount end

--------------------------------------------------------------------------------------SavingsAccount begin

public class SavingsAccount extends Account

{

private final double INTERESTRATE = 0.12;

private final double TAXRATE = 0.01;

public SavingsAccount() {}

public SavingsAccount(String name, double openBalance)

{

super(name, openBalance);

}

public double getSupBalance()

{

return super.getBalance();

}

public double getBalance()

{

return super.getBalance() * (1+INTERESTRATE);

}

public double getTax()

{

return super.getBalance() * TAXRATE;

}

}

--------------------------------------------------------------------------------------SavingsAccount end

--------------------------------------------------------------------------------------Bank begin

import java.util.Vector;

public class Bank {

int numCkAct=0;

int numSvAct=0;

Account act;

CheckingAccount ck1, ck2;

SavingsAccount sv;

Vector accounts = new Vector();

Bank() {

// create 2 CheckingAccount and 1 SavingsAccount

// and store them into the accounts collection.

ck1 = new CheckingAccount("kuodi", 100);

sv = new SavingsAccount("julie", 300);

ck2 = new CheckingAccount("jian", 200);

accounts.addElement(ck1);

accounts.addElement(sv);

accounts.addElement(ck2);

}

// output elements in the Vector collection.

public String toString() {

String str=” “;

for(int index = 0; index < accounts.size(); index++) {

act = (Account)accounts.elementAt(index);

if(act instanceof CheckingAccount) {

str = str + "\n\nBalance for " + act.getName() + " is: " +

((CheckingAccount)act).getSupBalance()

+ "\nThe balance with interest added is: " + act.getBalance();

numCkAct++;

}

else if(act instanceof SavingsAccount) {

str = str + "\n\nBalance for " + act.getName() + " is: " +

((SavingsAccount)act).getSupBalance()

+ "\nThe balance with interest added is: " + act.getBalance()+ "\nSavings account's tax: " + ((SavingsAccount)act).getTax();

n

Temporary reference points casting.

Examples can be found on week 5 teaching notes.

umSvAct++;

}

}

str = str + "\n\nTotal number of Checking account: " + numCkAct +

"\nTotal number of Savings account: " + numSvAct;

return str;

}

}

--------------------------------------------------------------------------------------Bank end

--------------------------------------------------------------------------------------TestLabVersion1 begin

public class TestLabVersion1

{

public static void main(String[] s)

{

Bank bank = new Bank();

System.out.println(bank);

}

}

--------------------------------------------------------------------------------------TestLabVersion1 end

The program will dynamically count the number of CheckingAccount and the number of SavingsAccount. This is achieved by using the variables numSvAct and numCkAct. When an object is retrieved from a collection such Vector or ArrayList, its type is implicitly converted to Object. When applying the pattern through base pointer, we explicitly cast the returned object to the base pointer as in the line:

act = (Account)accounts.elementAt(index);

As you can see, getBalance() is polymorphic. The version to be called is the object that is pointed to at the time of invocation.

Using a base pointer to call a method that is implemented in a child class but not in its parent class is illustrated in the way that the getTax() in the SavingsAccount is used. We used the temporary reference pointer casting.

Required Work

The first required work is to study the code given above and try to understand how it works (recommend you to replicate the code and play with it). The second task is to change the collection data structure, recompile the program, and record what you have done (including outputs from program runs). The following section specifies what you need to do.

Change Java code as specified

You are mainly asked to supply the missing code for the Bank class (the shell of this class will be given to you). When re-implementing this class, you should use the ArrayList collection to replace the Vector. To help you, I give the basics of ArrayList. First, ArrayList supports dynamic arrays that can grow as needed. In essence, an ArrayList is a variable-length array of object references. Similar to Vector, ArrayList has methods to add items, remove items, and size() that will return the total number of items stored in the ArrayList. The following segment of code gives an example of putting items to and retrieve from an ArrayList.

--------------------------------------------------------------------------------------Sample code begin

import java.util.ArrayList;

public class ArrayListDemo

{

public static void main(String[] s)

{

ArrayList al = new ArrayList();

al.add(“item1”);

al.add(“item2”);

al.add(“item3”);

for (int i=0; i<al.size(); i++) {

System.out.println(al.get(i));

}

}

}

--------------------------------------------------------------------------------------Sample code end

SCORE YOUR POINTS BY FINISHING THE FOLLOWING

Important, to receive full points, you must following the requirement stated in this document. You can use the given code by cut and paste the code that contains four classes: TestLabVersion1, Account, CheckAccount, SavingsAccount to Eclipse. Then rewrite the Bank class using the shell given. Make sure that you use ArrayList collection and get the output similar to the working example given above.

Score your points by finishing the following sections:

1. (7 pts) Finish the Bank class whose skeleton is given below:

import java.util.ArrayList;

public class Bank

{

int numCkAct=0;

int numSvAct=0;

Account act;

CheckingAccount ck1, ck2;

SavingsAccount sv;

ArrayList accounts = new ArrayList();

Bank() {

System.out.println(“Welcome to the new Bank class”);

// add more code to finish this constructor.

}

// add more code to finish this class.

}

After the change, cut and paste your Java source code of Bank class in the space below (Note: you can only add code to the given shell, not allowed to delete code).

SUPPLY THE CODE FOR CLASS BANK IN THE SPCE BELOW TO SCORE YOUR POINTS:

Answer:

2. (8 pts) Do an output screen capture. After successfully wrote the required code, run the program in Eclipse. Then, either transcript the outputs from the Eclipse output console window or do a screen capture of the output window in the space below.

SUPPLY THE RUN OUTPUT IN THE SPCE BELOW TO SCORE YOUR POINTS:

Answer:

Hand to your instructor the following:

1) this document (with all the bold faced sections finished), with output results.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*************************Account.java*****************/

public class Account

{

   private double balance;

   private String name;

   public Account() {
   }

   public Account(String name, double openBalance)

   {

       this.name = name;

       balance = openBalance;

   }

   public double getBalance()

   {

       return balance;

   }

   public String getName()

   {

       return name;

   }

}

/************************************CheckingAccount.java************************/

public class CheckingAccount extends Account

{

   private final double INTERESTRATE = 0.05;

   public CheckingAccount() {
   }

   public CheckingAccount(String name, double openBalance)

   {

       super(name, openBalance);

   }

   public double getSupBalance()

   {

       return super.getBalance();

   }

   public double getBalance()

   {

       return super.getBalance() * (1 + INTERESTRATE);

   }

}

/******************************Savings

public class SavingsAccount extends Account

{

   private final double INTERESTRATE = 0.12;

   private final double TAXRATE = 0.01;

   public SavingsAccount() {
   }

   public SavingsAccount(String name, double openBalance)

   {

       super(name, openBalance);

   }

   public double getSupBalance()

   {

       return super.getBalance();

   }

   public double getBalance()

   {

       return super.getBalance() * (1 + INTERESTRATE);

   }

   public double getTax()

   {

       return super.getBalance() * TAXRATE;

   }

}

/**********************************Bank.java*****************************/

import java.util.ArrayList;

public class Bank {

   int numCkAct = 0;

   int numSvAct = 0;

   Account act;

   CheckingAccount ck1, ck2;

   SavingsAccount sv;

   ArrayList<Account> accounts = new ArrayList<>();

   Bank() {

       // create 2 CheckingAccount and 1 SavingsAccount

       // and store them into the accounts collection.

       ck1 = new CheckingAccount("kuodi", 100);

       sv = new SavingsAccount("julie", 300);

       ck2 = new CheckingAccount("jian", 200);

       accounts.add(ck1);

       accounts.add(sv);

       accounts.add(ck2);

   }

   // output elements in the Vector collection.

   public String toString() {

       String str = "";

       for (int index = 0; index < accounts.size(); index++) {

           act = (Account) accounts.get(index);

           if (act instanceof CheckingAccount) {

               str = str + "\n\nBalance for " + act.getName() + " is: " +

                       ((CheckingAccount) act).getSupBalance()

                       + "\nThe balance with interest added is: " + act.getBalance();

               numCkAct++;

           }

           else if (act instanceof SavingsAccount) {

               str = str + "\n\nBalance for " + act.getName() + " is: " +

                       ((SavingsAccount) act).getSupBalance()

                       + "\nThe balance with interest added is: " + act.getBalance() + "\nSavings account's tax: "
                       + ((SavingsAccount) act).getTax();

               numSvAct++;

               // Temporary reference points casting.

               // Examples can be found on week 5 teaching notes.

           }

       }

       str = str + "\n\nTotal number of Checking account: " + numCkAct +

               "\nTotal number of Savings account: " + numSvAct;

       return str;

   }

/******************************TestBank.java********************/

package account3;

public class TestBank {

   public static void main(String[] args) {
      
       Bank bank = new Bank();
      
       System.out.println(bank.toString());
   }
}
/************************output***************************/

Balance for kuodi is: 100.0
The balance with interest added is: 105.0

Balance for julie is: 300.0
The balance with interest added is: 336.00000000000006
Savings account's tax: 3.0

Balance for jian is: 200.0
The balance with interest added is: 210.0

Total number of Checking account: 2
Total number of Savings account: 1

Console X <terminated> TestBank [Java Application] C:\Program Files\Java jre1.8.0_211\bin\ja Balance for kuodi is: 100.0 The

Please let me know if you have any doubt or modify the answer, Thanks :)

Add a comment
Know the answer?
Add Answer to:
In this lab, a piece of working software is given. You are asked to make changes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture...

    MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture an output. polymorphism. Write an application that creates a vector of Account pointers to two SavingsAccount and two CheckingAccountobjects. For each Account in the vector, allow the user to specify an amount of money to withdraw from the Account using member function debit and an amount of money to deposit into the Account using member function credit. As you process each Account, determine its...

  • Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes....

    Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes. Do not worry about rounding in classes that are instantiated as integer classes, you may just use the default rounding. You will add an additional data member, method, and bank account type that inherits SavingsAc-count ("CD", or certicate of deposit). Deliverables A driver program (driver.cpp) An implementation of Account class (account.h) An implementation of SavingsAccount (savingsaccount.h) An implementation of CheckingAccount (checkingaccount.h) An implementation of...

  • For this lab you must write a complete class in C++. This will be a class...

    For this lab you must write a complete class in C++. This will be a class named BankAccount. This class must have the following private variables: 1. accountHolderName : string 2. balance : double 3. interestRate: double This class must have the following public constructor: 1. BancAccount(name : string, balance : double, rate : double) This class must have the following public member functions: 1. getAccountHolderName() : string a. returns the account holders name 2. getBalance() : double a. returns...

  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

  • [JAVA] < Instruction> You are given two classes, BankAccount and ManageAccounts ManageAccounts is called a Driver...

    [JAVA] < Instruction> You are given two classes, BankAccount and ManageAccounts ManageAccounts is called a Driver class that uses BankAccount. associate a member to BankAcount that shows the Date and Time that Accounts is created. You may use a class Date. To find API on class Date, search for Date.java. Make sure to include date information to the method toString(). you must follow the instruction and Upload two java files. BankAccount.java and ManageAccounts.java. -------------------------------------------------------------------------- A Bank Account Class File Account.java...

  • WHERE in this code should you put the main method so that the code will compile...

    WHERE in this code should you put the main method so that the code will compile WITHOUT changing anything else in the code? Simply adding the main method only? public class Account { private String name; private double balance;    public Account(String name, double balance) { this.name = name; if (balance > 0.0) this.balance = balance; } public void deposit(double depositAmount) { if (depositAmount > 0.0) balance += depositAmount; }    public double getBalance() { return balance; } public String...

  • *Step1: -Create UML of data type classes: reuse from lab3 for class Account, CheckingAccount and SavingAccount...

    *Step1: -Create UML of data type classes: reuse from lab3 for class Account, CheckingAccount and SavingAccount -Read the requirement of each part; write the pseudo-code in a word document by listing the step by step what you suppose to do in main() and then save it with the name as Lab4_pseudoCode_yourLastName *Step2: -start editor eClipse, create the project→project name: FA2019_LAB4PART1_yourLastName(part1) OR FA2019_LAB4PART2_yourLastName (part2) -add data type classes (You can use these classes from lab3) Account_yourLastName.java CheckingAccount_yourLastName.java SavingAccount_yourLastName.java -Add data structure...

  • Instructions Using the BCException.java and the BankCustomer.java from the first part of the assignment you will...

    Instructions Using the BCException.java and the BankCustomer.java from the first part of the assignment you will implement a driver class called Bank.java to manipulate an ArrayList of BankCustomer objects. Your code should read bank customers’ information from the user. It will then create a bank customer objects and add them to an arraylist in a sorted fashion (sorted by account number). The arraylist should be sorted in ascending order at all times. The arraylist should not contain null objects at...

  • Im having trouble with this C++ program. Lab 10/object/test files provided LAB 10 1 //Savings.cpp - displays the account balance at 2 //the end of 1 through 3 years 3 //Created/revised by <your nam...

    Im having trouble with this C++ program. Lab 10/object/test files provided LAB 10 1 //Savings.cpp - displays the account balance at 2 //the end of 1 through 3 years 3 //Created/revised by <your name> on <current date> 4 5 #include <iostream> 6 #include <iomanip> 7 #include <cmath> 8 using namespace std; 9 10 //function prototype 11 double getBalance(int amount, double rate, int y); 12 13 int main() 14 { 15 int deposit = 0; 16 double interestRate = 0.0; 17...

  • The code is attached below has the Account class that was designed to model a bank account.

    IN JAVAThe code is attached below has the Account class that was designed to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds. I need creat program using the 2 java programs.Create two subclasses for checking and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.I need to write a test program that creates objects of Account,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT