Вы находитесь на странице: 1из 4

# OBJECT ORIENTED PROGRAMMING CSC 238

## LAB EXERCISE: POLYMORPHISM

1 Given the following class:

a) Based on class Cake and the following table, define TWO (2) subclasses named as

Additional attribute weight(kg) quantity
Price calculation rate*weight rate*quantity

b) By using classes definition from a), write an application program that will:
i) declare an array of 20 cake objects;
ii) input data for cake objects and store them into the array;
iii) display the total price for all types of cakes;
v) display the total price and the quantity sold for ready made cakes;
v) display the information for the cake that has been sold for the highest price.

public abstract class Cake
{
protected String name;
protected double rate;
public Cake (String n, double r)
{
name = n;
rate = r;
}
public abstract double calcPrice();
public String toString()
{
return name + \t + rate;
}
}

OBJECT ORIENTED PROGRAMMING CSC 238

2 Given the following cyclingGlass and swimmingGlass subclass are inherited from sportVissionShoppe
superclass

Superclass : sportVisionShoppe

Attributes:
String custOrderNo // customer order number
String custOrderDate // date of order
float depositPymt

Abstract Method:
public abstract double calCharges() //method to calculate the price

Subclass : cyclingGlass

Attributes:
char type_glass // T Transition Glass, P Polarized glass, I Iridium glass
boolean uvProtection; // with UV protection extra RM 30 will be charge to the total price
Float depositPymt

Method:
public double calCharges() //method to calculate the price
public String toString() // method to display the data members

Details of charges are shown in table 4.1 below:

Glass Type Price
Transition RM 300.00
Polarized RM 350.00
Iridium RM 400.00
Table 4.1

Subclass : swimmingGlass

Attributes:
char Type; // A Anti-fogging google, S short-sighted googlr
String style; // Retro, Warrior or Modern

Method:
public double calCharges() //method to calculate the price
public String toString() // method to display the data members

OBJECT ORIENTED PROGRAMMING CSC 238

Details of charges are shown in table 4.2 below:

Glass Type Price
Style and Discount
Retro Warrior Modern
Anti-Fogging google RM 150.00 7% 30% 10%
Short-sighted google RM 250.00 5% 20% 7%
Table 4.2

a) Write an abstract method of calCharges() to calculate the charges for both subclasses
b) Assume bellow coding being defined :
//consist of both subclasses objects
sportVisionShope[] S = new sportVisionShope[10]

Write a program to determine and display the number of customer who buy cycling and
swimming glass from the sportVisionShoppe
c) Write a program to calculate the total charges for all customers who bpught Retro goggle at
the sportVisionShoppe outlet.
Write a program to list the deposit paid by the customer who bought transition cycling glass from this
shop,

3

The following are the superclass Bank and its subclasses Saving and Current.

Super class : Bank
public class Bank {
String accNo; //customer account number with Bank
String custName; //customer name
int custGender; //customer gender 1 = Male, 2 =Female
String custJob; //customer job position
souble curBal; //customer balance in the bank account

public String toString();
public abstract double calcBalance();

Subclass : Saving
public class Saving {
double savRate; //percent interest rate per year

}

Subclass : Current
public class Current{
boolean fixedDep; //whether the customer keeps the fixed
// deposit with the bank of not
double curRate; //percent interest rate per year
}

The above classes show that Bank can have two different types of account which are
OBJECT ORIENTED PROGRAMMING CSC 238

Saving and Current account. The balance amount in the bank for each account is based
on the following calculation :

Saving :
Balance = current balance + (savRate * current balance)

Current :
Balance = current balance + (curRate * current balance)

If the customer has a fixed deposit with the bank, then the bank will charge RM150
for the service fee. The amount will be deducted automatically yearly.

a) Write a method of calcBalance() for both subclasses.
(5 marks)
b) Search a customer based on the account number entered then display detail
information of him/her. If the customer account number is not found, display an
appropriate message.
(5 marks)
c) Count how many customers that have the current account with the bank and the total
balance. (5 marks)