The document describes an object-oriented programming lab exercise on polymorphism. It involves:
1) Defining subclasses orderCake and readymadeCake that inherit from an abstract Cake class. The subclasses have additional attributes and methods to calculate price differently for each type of cake.
2) Writing a program to store cake objects in an array, calculate total prices, and display information about readymade cakes and the highest priced cake.
3) Defining subclasses cyclingGlass and swimmingGlass that inherit from an abstract sportVisionShoppe class. The subclasses have different attributes and methods to calculate charges.
4) Writing programs to count customers for each glass type, calculate total charges for
Download as DOCX, PDF, TXT or read online on Scribd
80%(5)80% found this document useful (5 votes)
5K views
Lab Exercise JAVA (Polymorphism)
The document describes an object-oriented programming lab exercise on polymorphism. It involves:
1) Defining subclasses orderCake and readymadeCake that inherit from an abstract Cake class. The subclasses have additional attributes and methods to calculate price differently for each type of cake.
2) Writing a program to store cake objects in an array, calculate total prices, and display information about readymade cakes and the highest priced cake.
3) Defining subclasses cyclingGlass and swimmingGlass that inherit from an abstract sportVisionShoppe class. The subclasses have different attributes and methods to calculate charges.
4) Writing programs to count customers for each glass type, calculate total charges for
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
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)