Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We allowed 60 mins only to solve this question! So it should take less than 60 mins! Goals : To learn the basics of inheritance,

We allowed 60 mins only to solve this question! So it should take less than 60 mins!

Goals: To learn the basics of inheritance, polymorphism, the usage of super, different ways to call polymorphic and non-polymorphic methods, the software pattern (instanceof+base pointers+a collection), and the dynamic structure ArrayList.

Problem Statement

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 TestLab3Version1, the Account class, the CheckingAccount, the SavingsAccount class, and the Bank class. Both the CheckingAccount and the SavingsAccount are children of Account (Note: here the Account is defined as regular class, not an abstract class). 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 + " Balance for " + act.getName() + " is: " +

((CheckingAccount)act).getSupBalance()

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

numCkAct++;

}

else if(act instanceof SavingsAccount) {

str = str + " Balance for " + act.getName() + " is: " +

((SavingsAccount)act).getSupBalance()

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

numSvAct++;

}

}

str = str + " Total number of Checking account: " + numCkAct +

" Total number of Savings account: " + numSvAct;

return str;

}

}

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

--------------------------------------------------------------------------------------TestLab3Version1 begin

public class TestLab3Version1

{

public static void main(String[] s)

{

Bank bank = new Bank();

System.out.println(bank);

}

}

--------------------------------------------------------------------------------------TestLab3Version1 end

The program will dynamically count the number of CheckingAccount and the number of SavingsAccount. This is achieved by using the operator instanceof. 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

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: TestLab3Version1, 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. 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 debugging, 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):

Answer:

2. 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.

Answer:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions

Question

c. Acafeteriawhere healthy, nutritionally balanced foods are served

Answered: 1 week ago

Question

c. What steps can you take to help eliminate the stress?

Answered: 1 week ago