1. SSK 3101 – LAB 4 Semester 1
2011/2012
____________________________________________________________________________
SSK 3101
COMPUTER PROGRAMMING II
LAB 4
NAME : SUGENTIRAN A/L MANE
LECTURER NAME : DR: MOHD TAUFIK ABDULLAH
1
2. SSK 3101 – LAB 4 Semester 1
2011/2012
____________________________________________________________________________
Fakulti Sains Komputer dan Teknologi Maklumat
SSK3101 (Programming Computer II)
Semester 1 2011/2012
October 21, 2010
Lab 4 (Individual)
Learning Objective:
The objective of this lab is to
1. Construct An Object-oriented Program Using Generic Programming (P4)
Instructions:
1. Date of submission is 28.10.2011 and submits to the lecturer the UML, source program and
the output. Good code development (which uses appropriate selection of variable name,
proper declaration, comments, indentation, and etc.) Your program must be successfully
demonstrates to the demonstrator.
2. Copy or other forms of cheating is forbidden. The faculty has very strong rules about this, and
the penalties may be severe. The standard penalty for the first offence is to award 0 to all
parties concerned.
2
3. SSK 3101 – LAB 4 Semester 1
2011/2012
____________________________________________________________________________
Question 1 [10M]
We had two inherited classes which are Person and Student from Lab 2 questions. Add class named
Employee. Make Faculty and Staff subclasses of Employee. An employee has an office, salary and
date-hired. Define a class named MyDate that contains the fields year, month and day. A faculty
member has office hours and a rank. A staff member has a title. Override the toString method in
each class to display the class name and the person’s name.
Draw the UML diagram for the classes. Implement the classes. Write a class to test program that
creates a Person, Student, Employee, Faculty and Staff and invokes their toString() methods.
Question 2 [10M]
In class (define on your own, example class name that contain main method can be
TestPolymorphismDemo) that contains main method, construct a method Print(Object O). By
using polymorphism feature, write the code which pass the instances of Person, Student, Employee,
Faculty and Staff to Print(Object O) method which invokes their respective toString() method.
Question 3 [10M]
Extend the Staff class to Admin class and Lecturer class. Lecturer has room_no and subject.
Variable room_no and subject should be declared by using a suitable modifier to restrict its
accessibility within the class itself. The variables can only be achieved by using method getRoom()
and getSubject().
Implement toString() methods in both Admin and Lecturer classes. For Admin class, toString()
method will print out the class name. For Lecturer, toString() method will print out the room_no
and subject. Just assign any random string value to room_no and subject.
Construct a method known as PrintDetails(Object O) in the Staff class. Pass both instances of
Admin and Lecturer to PrintDetails method and invoke their respective toString() method by using
the operator “instanceof” and appropriate casting might be required.
Question 4 [10M]
Rewrite the code in Question 2 without using polymorphism feature by using any programming
method, skill and creativity that you have to achieve the exact same output.
Explain the advantage of using polymorphism feature in the coding.
3
18. SSK 3101 – LAB 4 Semester 1
2011/2012
____________________________________________________________________________
Class Admin
public class Admin {
public Admin(){
}
public String toString(){
return "Class Admin";
}
}
-------------------------------------------------
Class Lecturer
public class Lecturer {
String room_no,subject;
public Lecturer(){
}
public Lecturer(String room_no,String subject){
this.room_no=room_no;
this.subject=subject;
}
public void setRoomNo(String newRoom_No){
this.room_no=newRoom_No;
}
public String getRoomNo(){
return room_no;
}
public void setSubject(String newSubject){
this.subject=newSubject;
}
public String getSubject(){
return subject;
}
public String toString(){
return "Room No : "+room_no+"nSubject : "+subject;
}
}
Class Staff
18
19. SSK 3101 – LAB 4 Semester 1
2011/2012
____________________________________________________________________________
public class Staff {
public static void PrintDetails(Object o){
if (o instanceof Admin){
System.out.println(((Admin)o).toString()+"n***************************");
}
else if (o instanceof Lecturer){
System.out.println(((Lecturer)o).toString()
+"n***************************");
}
}
}
----------------------------------------
Class TestStaff
import java.util.*;
public class TestStaff {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
Staff staff=new Staff();
//Class Admin
//Use casting
Object admin=new Admin();
System.out.println("***************************");
staff.PrintDetails(admin);
//Class Lecturer
String room_no,subject;
System.out.print("Room No : ");
room_no=scan.next();
System.out.print("Subject : ");
subject=scan.next();
System.out.println("***************************");
staff.PrintDetails(new Lecturer(room_no,subject));
}
}
Input:-
19
25. SSK 3101 – LAB 4 Semester 1
2011/2012
____________________________________________________________________________
Kelebihan polymorphism
1. Code that is more dynamic at runtime
2. Code is more flexible and reusable
3. Code re-usability: Using the code written somewhere in program
4. Multiple forms of one object are called in the same way
25