View stunning SlideShares in full-screen with the new iOS app!Introducing SlideShare for AndroidExplore all your favorite topics in the SlideShare appGet the SlideShare app to Save for Later — even offline
View stunning SlideShares in full-screen with the new Android app!View stunning SlideShares in full-screen with the new iOS app!
SSK 3101 – LAB 4 Semester 12011/2012____________________________________________________________________________ SSK 3101 COMPUTER PROGRAMMING II LAB 4 NAME : SUGENTIRAN A/L MANE LECTURER NAME : DR: MOHD TAUFIK ABDULLAH 1
SSK 3101 – LAB 4 Semester 12011/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
SSK 3101 – LAB 4 Semester 12011/2012____________________________________________________________________________Question 1 [10M]We had two inherited classes which are Person and Student from Lab 2 questions. Add class namedEmployee. Make Faculty and Staff subclasses of Employee. An employee has an office, salary anddate-hired. Define a class named MyDate that contains the fields year, month and day. A facultymember has office hours and a rank. A staff member has a title. Override the toString method ineach 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 thatcreates 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 beTestPolymorphismDemo) that contains main method, construct a method Print(Object O). Byusing 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 itsaccessibility 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_noand 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 ofAdmin and Lecturer to PrintDetails method and invoke their respective toString() method by usingthe operator “instanceof” and appropriate casting might be required.Question 4 [10M]Rewrite the code in Question 2 without using polymorphism feature by using any programmingmethod, skill and creativity that you have to achieve the exact same output.Explain the advantage of using polymorphism feature in the coding. 3
SSK 3101 – LAB 4 Semester 12011/2012____________________________________________________________________________ Kelebihan polymorphism1. Code that is more dynamic at runtime2. Code is more flexible and reusable3. Code re-usability: Using the code written somewhere in program4. Multiple forms of one object are called in the same way 25