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

Polymorphism (test2 april 2010)

Given the following Curtain and Dress subclasses are inherited from TailorHouse
superclass.

Superclass : TailorHouse
Attributes:
String custNo // customer I/C number
String custName // customer Name
String date // date of order
char Cust_type // regular customer or new customer
// regular customer will be given 15% discount
// from total charges

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

Subclass : Dress
Attributes:
String dressType // Type of dress, Kebaya or Kurung or Jubah
boolean silk; // either Yes or No to silk

Details of charges are shown in the table below:

Dress Type Silk Non-


(RM) Silk
(RM)
Kebaya 80 40
Kurung 50 25
Jubah 100 50

Subclass : Curtain
Attributes:
int windows; // total number of windows
int doors; // total number of doors

final double windowPrice = 45.00; //cost per piece


final double doorPrice = 55.00; //cost per piece

a) Write the normal constructors for both sub classes (exclude the constants).
(4 marks)

b) Write an abstract methods of calCharges() to calculate the total charges for both
subclasses.
(7 marks)
c) Assume the following code has been defined :

TailorHouse [ ] T = new TailorHouse [25]; //consists of both subclasses


objects

Write a program fragment to determine and display the number of “kebaya “, “kurung”
and “jubah” made by the tailor at TailorHouse.
(5 marks)
d) Write a program fragment to calculate and display the total charges of all regular
customer who sent their curtain to the TailorHouse.
(4 marks)

e) Assume that all classes have toString() methods, write a program fragment to
display the details information of TailorHouse customers.
(2 marks)

//super class: TailorHouse


public abstract class TailorHouse
{
String custNo; // customer I/C number
String custName; // customer Name
String date; // date of order
char Cust_type; // regular customer or new customer
// regular customer will be given 15% discount
// from total charges

public TailorHouse()
{
custNo = null;
custName = null;
date = null;
Cust_type = '0';
}

public TailorHouse(String no,String name,String d,char type)


{
custNo = no;
custName = name;
date = d;
Cust_type = type;
}

public String getCustNo() {return custNo; }


public String getCustName() { return custName; }
public String getDate() { return date; }
public char getCust_type() { return Cust_type; }

public abstract double calCharges(); // method to calculate


the
// charges
public void Display()
{
System.out.println ("The customes id " + getCustNo());
System.out.println ("The customes name " + getCustName());
System.out.println ("The date " + getDate());
System.out.println ("The customer type - regular or else " +
getCust_type());
}
}

//sub class: Curtain


public class Curtain extends TailorHouse
{
int windows; // total number of windows
int doors; // total number of doors

final double windowPrice = 45.00; //cost per piece


final double doorPrice = 55.00; //cost per piece

public Curtain()
{
super();
windows = 0;
doors = 0;
}
public Curtain(String no,String name,String d,char type,int w,
int dr)
{
super(no, name, d, type);
windows = w;
doors = dr;
}
public int getWindows() { return windows; } // total
number of windows
public int getDoors() { return doors; }

public double calCharges()


{
if (super.getCust_type() == 'R')
return (0.85 * (windowPrice*getWindows()
+doorPrice*getDoors()));
else
return (windowPrice*getWindows()+doorPrice*getDoors());
}
public void Display ()
{
super.Display();
System.out.println ("The number of windows " +
getWindows());
System.out.println ("The number of doors " +
getDoors());
}
}
//sub class: Dress
public class Dress extends TailorHouse
{
String dressType; // Type of dress, Kebaya or Kurung or Jubah
boolean silk;

public Dress()
{
super();
dressType = null;
silk = true;
}

public Dress(String no,String name,String d,char type, String


dt, boolean sk)
{
super(no, name, d, type);
dressType =dt;
silk = sk;
}

public String getDressType()


{ return dressType; } // Type of dress, Kebaya or Kurung or
Jubah
public boolean getSilk()
{ return silk; }

public double calCharges()


{
double cost;
if (getSilk() == true)
{ if (getDressType().equalsIgnoreCase("Kebaya"))
cost = 80.0;
else if (getDressType().equalsIgnoreCase("Kurung"))
cost = 50.0;
else
cost = 100.0;
}
else
{
if (getDressType().equalsIgnoreCase("Kebaya"))
cost = 40.0;
else if (getDressType().equalsIgnoreCase("Kurung"))
cost = 25.0;
else
cost = 50.0;
}

if (super.getCust_type()== 'R')
return (0.85 * cost);
else
return cost;
}

public void Display ()


{
super.Display();
System.out.println ("The dress type " + getDressType());
System.out.println ("The silk " + getSilk());
}
}

//Sample of application
//Please write a complete task based on question
import java.util.*;
public class TailorHousApp
{
public static void main (String[] args)
{
String custNo, classType, dummy;
String custName, ct;
String date;
char Cust_type, select;
String dressType;
boolean silk;
int windows;
int doors;
Scanner s = new Scanner (System.in);
Scanner ss = new Scanner (System.in);

System.out.println ("Enter number of array ");


int n = s.nextInt();

TailorHouse[] T = new TailorHouse[n];


for (int i =0; i<T.length; i++)
{
// T[i] = new TailorHouse();
System.out.println ("Enter subclass D(ress) or
C(curtain)");
classType = ss.next();
select = classType.charAt(0);

if (select == 'D')
{
System.out.println ("Enter customer number ");
custNo = ss.next(); // customer I/C number
System.out.println ("Enter customer name ");
custName = ss.nextLine(); // customer Name
dummy =ss.nextLine();
System.out.println ("Enter date ");
date = ss.nextLine(); // date of order
System.out.println ("Enter customer type - R(egular)
");
ct = ss.next();
Cust_type = ct.charAt(0);
System.out.println ("Enter dress type ");
dressType = ss.next();
System.out.println ("Enter silk - true or false ");
silk = s.nextBoolean();
T[i] = new Dress(custNo, custName, date, Cust_type,
dressType, silk);
}
else
{
System.out.println ("Enter customer number ");
custNo = ss.next(); // customer I/C number
System.out.println ("Enter customer name ");
custName = ss.nextLine(); // customer Name
dummy = ss.nextLine();
System.out.println ("Enter date ");
date = ss.nextLine(); // date of order
System.out.println ("Enter customer type - R(egular)
");
ct = ss.next();
Cust_type = ct.charAt(0);
System.out.println ("Enter number of windows ");
windows = s.nextInt();
System.out.println ("Enter number of doors ");
doors = s.nextInt();

T[i] = new Curtain (custNo, custName, date,


Cust_type, windows, doors);
}
}
for (int j=0; j<T.length; j++)
System.out.println (T[j].calCharges());
}
}

Вам также может понравиться