|
RHS |
Borrower Specialization
You are to develop a program, which can handle specializations of borrowers at the Fantasy Library. Due to the requirement specification the following specializations have been identified for the Borrower class: ChildBorrower and AdultBorrower, as shown below:
ChildBorrowerData fields:
Borrower parent (reference to one of the child's parents)Constructors:
ChildBorrower(String cprNo, String name, int year, String sex, Borrower parent):Methods operating on the data fields:
String toString() (overriding toString() from super class Borrower)
void setParent(Borrower parent)
Borrower getParent()AdultBorrower
Data fields:
String address
String phoneConstructors:
AdultBorrower(String cprNo, String name, int year, String sex, String address, String phone)Methods operating on the data fields:
String toString() (overriding toString() from the super class Borrower)
void setAddress(String address)
String getAddress()
void setPhone(String phone)
String getPhone()
Assignment 1: Specialization class:
AdultBorrower
Create a new folder on your network driver - call the
folder BorrowerSpecialization.
Copy your latest version Borrower.java to the folder or download my version from here: Borrower.java
First create a class AdultBorrower with the mentioned data fields, constructor and the methods:
- The constructor
must call super; e.g. super(cprNo, name, year, sex)
and initialize the data fields in the subclass.- String toString()
returns a String with all borrower data
must call super.toString() and add the data from the subclass- "getters and setters"
The usual getXxx() and setXxx(...) methods for the address and phone data fields.
Assignment 2: Application class:
BorrowerSpecializationApp
Now write an application class BorrowerSpecializationApp with the usual main method using
objects of the class AdultBorrower.
In main() you must show the construction of one ordinary borrower
(b1) and three adult borrowers (ab1, ab2, ab3).
Also show use of the method toString() to print out the data.
Compile and run!
Assignment 3: Application class:
Polymorphism
Try the following assignment:
b1 = ab1;
System.out.println(b1);
What was printed out - only borrower data or adult borrower data?
Note that b1 is declared as a reference to an ordinary borrower
object!
Assignment 4: Specialization
class: ChildBorrower
Now create a class ChildBorrower with the mentioned data fields, constructor and
the methods:
- The constructor
must call super; e.g. super(cprNo, name)
must initialize the data field parent- String toString()
returns a String with all borrower data
must call super.toString() and parent.toString()- "getters and setters"
The usual getXxx() and setXxx(...) methods for the parent data field.
Tip: Very similar to AdultBorrower class !!
Assignment 5: Application class:
BorrowerSpecializationApp
Extend main() with
construction of two child borrowers (cb1, cb2) having parents.
Also show use of the method toString() to print out these borrowers' data.
Compile and run!
Assignment 6: Application class:
array of borrowers
Define an array bTable of
the class Borrower:
Borrower[] bTable = new Borrower[5];
Initialize the 5 elements of the table, e.g. by means of constructions such as:
bTable[0] = new AdultBorrower("1111", "Michael", 1945, "Male", "Bredgade 7, Roskilde", "12 34 56 78");
bTable[1] = ab1;
bTable[2] = cb1;
... and so on
Produce a for-loop traversing the table bTable and printing all data by means of:
System.out.println(bTable[i].toString( ) )
Consider the above sentences carefully.
Notice how the right
toString() is called for the sub-classes
Assignment 7: Collection class:
BorrowerCollectionVector
In main(), define a
variable borrowers of the BorrowerCollectionVector (from a previous exercise) class.
Don't forget to copy BorrowerCollectionVector.java to your newly created folder.
Add all the borrowers
(b1, ab1, ab2, ab3, cb1,
cb2) to this collection (borrowers).
Use toString() to print
out all the data (borrowers.toString()).