Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

QUESTION 5 (APRIL 2008)

Given the following program segment:

public class Land


{ protected String owner;
protected double area;

abstract double calcTax();


abstract void display();
public Land( String own, double area)
{
//method definition
}
}

public class Housing extends Land


{
private char houseType;
private double tax_rate;

public Housing(String own,double area,char Hsetype)


{
//method definition
}
public double calcTax()
{
//method definition
}
public void display()
{
//method definition
}
}

public class Agriculture extends Land


{
private double fixed_tax_rate = 3.00;

public Agriculture(String own,double area)


{
//method definition
}
public double calcTax()
{
//method definition
}
public void display()
{
//method definition
} }
a) Define the method calcTax()of class Housing based on the following:

The tax for this type of land depends on its area and the type of house built on the land:

House Type Description Tax Rate (RM/m2)


T Terrace 10
S Semi-Detached 15
B Bungalow 20
C Condominium 5

(4 marks)
b) Define method calcTax()of class Agriculture. Agricultural lands are all charged a
fixed rate of RM 3 per meter square (m2).
(2 marks)

b) Write an application program that uses the concept of polymorphism to

i. store data on various types of land. The number of data to be stored and
information on each land are given by the user. The input process stops when
the users key-in a sentinel value.

ii. Display the total Tax for all lands.

iii. Display the Tax amount for Housing lands only.

iv. Determine and display the highest tax for Agriculture land.
(12 marks)

QUESTION 3 (APRIL 2009)

Given the following inheritance hierarchy

BankAccount

Saving Current

And the definition for the BankAccount class:

abstract class BankAccount


{
private String name;
private long accNum;
private double balance;

public BankAccount() {}
public BankAccount(String name, long accNum, double balance)
{ //method definition}

public String getName()


{ //method definition }

public long getAcc()


{ //method definition }

public double getBalance()


{ //method definition }

public String toString()


{ //method definition }

//method to update the balance for the account


public abstract double updateBalance();
}

a) Define the Saving class that inherits from the BankAccount class. The attributes and
methods are as follows:

Attributes: interest rate

Methods:
 Constructor with arguments
 toString(), to return the string representation of the object
 updateBalance(), given the formulae:
update = (interest rate X balance) + balance;
(10 marks)

b) Define the Current class that inherits from the BankAccount class. The attributes and
methods are as follows:

Attributes: interest rate and transaction fee


Methods:
 Constructor with arguments
 toString(), to return the string representation of the object
 updateBalance(), given the formulae:
update = (interest rate X balance) + balance – transaction fee;
(10 marks)

c) Write a Java application class called BankApp that uses the concept of polymorphism to
perform the following tasks:
 Declare an array of objects to store data on various types of BankAccount.
 Display the details for all bank customers.
 Display the details of customer whose balance exceeding RM 100,000
 Calculate and display the total balance of all Saving accounts and total balance of
all Current accounts.
.(15 marks)
QUESTION 3 (NOV 2009)

Given the following definition

public abstract class Student


{
private String name;
private double test1;
private double test2;
private double test3;
protected char grade;

public Student(String n, double tl, double t2, double t3)


{ . . . }
public abstract char computeGrade();

// Accessors
public String getName() {...}
public double getTest1() {...}
public double getTest2() {...}
public double getTest3() {...}
public char getGrade (){...}
public String toString (){...}
}

Suppose Undergraduate and Graduate extends Students, with no additional attributes. Using
polymorphism, write a Java application to

a. get the number of students and necessary data from the user. Store these data into an
array of students
(10 mark)

b. calculate and display the total number of students who get grade A and count how many of
these students from each subclass, Undergraduate and Graduate.
(6 mark)

c. Find and display the information of student whose name is given by the user
(4 mark)
QUESTION 4 (NOV 2009)

Given the following inheritance hierarchy

ThemePark

WaterPark WildlifePark

and the list of data members and methods for ThemePark, WaterPark, and WildlifePark classes:

Superclass : abstract class ThemePark

Data member(s):
String name; // customer's name
String icNo; // customer's identification card number
boolean member; // either member or not member of the park

Methods:
ThemePark();
ThemePark(String name, String icNo, boolean member);
String getName(); // return the customer's name
String getlc(); // return the ic number
boolean getMember(); // return the membership
String toString(); // return the details of objects
abstract double calCharges(); // calculate the charges

Subclass : class WaterPark

Data member(s):
boolean surfBeach; // either true or false to surf
boolean waterRides; // either true or false to ride

Methods:
boolean getSurf();// return the surfing status
boolean getRide();// return the riding status
double calCharges();// calculate the charges
String toString();// return the details of objects

Subclass : class WildlifePark

Data member(s):
String category; // category of the customers

Methods:
String getCategory();// return the category
double calCharges();// calculate the charges
a) Write the normal constructors for superclass and both subclasses.
(6 marks)

b) Given the details of ticket charges in table 5.1 and table 5.2 for both Water and Wildlife
Parks below:

Activity Cost(RM)
Surf Beach 25.00
Water Rides 20.00
Table 5.1 Details of ticket charges for Water Park

Category Cost(RM)
Adult 35.00
Child 20.00
Table 5.2 Details of ticket charges for Wildlife Park

Write abstract methods named calcharges() to calculate the charges of the activities for both
WaterPark and WildlifePark classes. The customers who are the members of the theme
park will be given a special discount of 25% for every total charges.

(9 marks)

You might also like