Java Foundations Non-Prg Assignment 3(7.5 marks, 3% for the final grade)

Name: (print) ______/ Grade: ______

Questions(5 marks, 2% for the final grade)

1.How many children can any parent class have? How many parents can a child class have?How can you make sure a method is not overridden in a child class?How many interfaces can any one class implement?

2.What advantages does polymorphism provide?

3.If the class Utensil is declared as abstract, with Fork, Knife, and Spoon as its child classes, are the following declarations legal? Why or why not?

Utensil myFork;

Utensil mySpoon = new Utensil();

Knife myKnife = new Utensil();

Spoon mySpork = new Fork();

4.What exception will the following source code throw?

for(int i = 0; i <= myArray.length; i++)

System.out.println(myArray[i])

5.When writing custom exceptions, what Class must they eventually be derived from?

Programming question(2.5 marks, 1% for the final grade)

Bank Accounts

Design and write 3 classes – Account, CheckingAccount, and SavingsAccount. Have CheckingAccount and SavingsAccount inherit from Account. In Account, you should include an account number, an account balance, a deposit and toString method, and an abstract withdraw method. Since deposits work the same way in both child classes, make sure they cannot override it. The constructor should only take an initial deposit and generate a random 5 digit account number. The toString should display the account number and the balance. Use a NumberFormat object to format the balance.

In the CheckingAccount class add a minimum balance and overdraft fee. Implement the withdraw method so that overdrafts are allowed, but the overdraft fee is incurred if the balance drops below the minimum balance. Override the toString method to display everything the Account toString displays plus the minimum balance. Use the parent class toString to do most of the work. You should not need a NumberFormat object in this method.

In the SavingsAccount class add an annual interest rate and a method to recalculate the balance every month. Since the interest rate is annual, make sure to calculate the interest accordingly. Override the toString method to display everything the Account toString displays plus the interest rate. Like the CheckingAccount toString, you should use the parent class to do most of the work and should not need a NumberFormat object.

Create a driver class to instantiate and exercise each of the account types.

Resources:

  • The Java API documentation is located at:

Page 1 of 1