Lab 4 jawapan (sugentiran mane)

31
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

description

 

Transcript of Lab 4 jawapan (sugentiran mane)

Page 1: Lab 4 jawapan (sugentiran mane)

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

Page 2: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

Fakulti Sains Komputer dan Teknologi MaklumatSSK3101 (Programming Computer II)

Semester 1 2011/2012October 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

Page 3: Lab 4 jawapan (sugentiran mane)

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

Page 4: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

Question 1

UML:-

4

Person

+firstName:String+lastName:String+streetAddress:String+zipCode:int+phoneNumber:int

+Person()+Person(String,String,String,int,int)+setFirstName():void+getFirstName(String)+setLirstName():void+getLirstName(String)+setStreetAddress():void+getStreetAddress(String)+setZipCode():void+getZipCode(int)+setPhoneNumber():void+setPhoneNumber(int)+toString():String

Student

+majorField:String+gradePointAverage:String

+Student()+Student(String,String)+ setMajorField ():void+get MajorField (String)+setGradePointAverage ():void+getGradePointAverage (String)+toString():String

Page 5: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

5

Staff

+title:String

+ Staff()+ Staff(String)+setTitle():void+getTitle(String)+toString():String

Employee

+office:String+date:String+salary:double

+ Employee()+ Employee(String,String,double)+setOffice():void+getOffice(String)+setSalary():void+getSalary(double)+setDataHired():void+getDataHired(String)+toString():String

Faculty

+officeHours1:String+officeHours2:String+rank:String

+ Faculty()+ Faculty(int,int,String)+setOfficeHours1():void+getOfficeHours1(int)+setOfficeHours2():void+getOfficeHours2(int)+setRank():void+getRank(String)+toString():String

MyDate

+year:int+day:int+month:String

+ MyDate()+ MyDate(int,String,int)+toString():String

Page 6: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

Coding:-

Class Person

public class Person {

String firstName, lastName, streetAddress;int zipCode, phoneNumber;Person(){

}Person(String firstName,String lastName,String streetAddress,int zipCode,int phoneNumber){

this.firstName=firstName;this.lastName=lastName;this.streetAddress=streetAddress;this.zipCode=zipCode;this.phoneNumber=phoneNumber;

}

public void setFirstName(String firstName){this.firstName=firstName;

}

public String getFirstName(){return firstName;

}

public void setLastName(String lastName){this.lastName=lastName;

}

public String getLastName(){return lastName;

}

public void setStreetAddress(String streetAddress){this.streetAddress=streetAddress;

}

public String getStreetAddress(){return streetAddress;

}

public void setZipCode(int zipCode){this.zipCode=zipCode;

}

public int getZipCode(){return zipCode;

}

6

Page 7: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

public void setPhoneNumber(int phoneNumber){this.phoneNumber=phoneNumber;

}

public int getPhoneNumber(){return phoneNumber;

}

public String toString(){return "First Name : "+getFirstName()+"\nLast Name : "+getLastName()+"\nStreet Addrees : "+getStreetAddress()+"\nZip Code : "+getZipCode()+"\nPhone Number : "+getPhoneNumber();

}}

----------------------------------------------

Class Student

public class Student extends Person{String majorField,gradePointAverage;

Student(){

}

Student(String majorField, String gradePointAverage){//super(firstName,lastName,streetAddress,zipCode,phoneNumber);this.majorField=majorField;this.gradePointAverage=gradePointAverage;

}

public void setMajorField(String majorField){this.majorField=majorField;

}

public String getMajorField(){return majorField;

}

public void setGradePointAverage(String gradePointAverage){this.gradePointAverage=gradePointAverage;

}

public String getGradePointAverage(){return gradePointAverage;

}

public String toString(){return "Major Field : "+getMajorField()+"\nGrade Point Average : "+getGradePointAverage();

}}

7

Page 8: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

------------------------------------------

Class Employee

public class Employee {String office,date;double salary;

Employee(){}

Employee(String office, double salary, String date){this.office=office;this.salary=salary;this.date=date;

}

public void setOffice(String office){this.office=office;

}

public String getOffice(){return office;

}

public void setSalary(double salary){this.salary=salary;

}

public double getSalary(){return salary;

}

public void setDateHired(String date){this.date=date;

}

public String getDateHired(){return date;

}

public String toString(){return "Office : "+getOffice()+"\nSalary : Rm "+getSalary()+"\nDate Hired : "+getDateHired();

}}

Class Staff

public class Staff extends Employee{String title;

8

Page 9: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

Staff(){}

Staff(String title){this.title=title;

}

public void setTitle(String title){this.title=title;

}

public String getTitle(){return title;

}

public String toString(){return "Title : "+getTitle();

}}

---------------------------------------

Class Faculty

public class Faculty extends Employee{int officeHours1,officeHours2;String rank;

Faculty(){}

Faculty(int officeHours1,int officeHours2, String rank){//super(office,salary,date);this.officeHours1=officeHours1;this.officeHours2=officeHours2;this.rank=rank;

}

public void setOfficeHours1(int officeHours1){this.officeHours1=officeHours1;

}

public int getOfficeHours1(){return officeHours1;

}

public void setOfficeHours2(int officeHours2){this.officeHours2=officeHours2;

}

public int getOfficeHours2(){return officeHours2;

}

9

Page 10: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

public void setRank(String rank){this.rank=rank;

}

public String getRank(){return rank;

}

public String toString(){return "Office Hours : "+getOfficeHours1()+"am to "+getOfficeHours2()+"pm"+"\nRank : "+getRank();

}

}

---------------------------------------------------

Class MyDate

public class MyDate {int year,day;String month;

MyDate(){}

MyDate(int year,String month,int day){this.year=year;this.month=month;this.day=day;

}

public String toString(){return "Date Hired : "+day+"/"+month+"/"+year;

}}

-----------------------------------------------------

Class TestQ1

import java.util.*;public class TestQ1 {public static void main(String[] args) {

String ans;do{int a;Scanner scan=new Scanner(System.in);System.out.println("Enter 1 for Student and 2 for Staff : ");a=scan.nextInt();

if(a==1){

10

Page 11: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

//Class PersonString first,last,add;int zip,phone;System.out.print("First Name :");first=scan.next();System.out.print("Last Name : ");last=scan.next();System.out.print("Address : ");add=scan.next();System.out.print("Zip Code : ");zip=scan.nextInt();System.out.print("Phone No : ");phone=scan.nextInt();Person person=new Person(first,last,add,zip,phone);System.out.println("***************************"+"\nClass Person");System.out.println(person.toString());System.out.println("***************************");//Class StudentString major,point;System.out.print("Major Field : ");major=scan.next();System.out.print("Grade Point Average : ");point=scan.next();Student student=new Student(major,point);System.out.println("***************************"+"\nClass Student");System.out.println(student.toString());System.out.println("***************************");

}else if(a==2){

//Class EmployeeString office;double salary;int day,year;String month;System.out.println("Office Name : ");office=scan.next();System.out.println("Salary : ");salary=scan.nextDouble();System.out.println("Date Hired (mm/dd/yyyy) : ");System.out.println("Month");month=scan.next();System.out.println("Day");day=scan.nextInt();System.out.println("Year");year=scan.nextInt();MyDate date=new MyDate(year,month,day);date.toString();Employee employee=new Employee(office,salary,date.toString());System.out.println("**************************"+"\nClass Employee");System.out.println(employee.toString());System.out.println("***************************");//Class Facultyint officeHours1,officeHours2;String rank;

11

Page 12: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

System.out.println("Office Hours : ");officeHours1=scan.nextInt();System.out.println("to");officeHours2=scan.nextInt();System.out.println("Rank : ");rank=scan.next();Faculty faculty=new Faculty(officeHours1,officeHours2,rank);System.out.println("***************************"+"\nClass Faculty");System.out.println(faculty.toString());System.out.println("***************************");//Class StaffString title;System.out.println("Title : ");title=scan.next();Staff staff=new Staff(title);System.out.println("***************************"+"\nClass Staff");System.out.println(staff.toString());System.out.println("***************************");

}System.out.println("Do it again (Y/N)?");ans=scan.next();}while(ans.equalsIgnoreCase("Y"));

}}

Input:-

12

Page 13: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

Output:-

Question 2:-

13

Page 14: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

import java.util.*;public class TestPolymorphismDemo {

public static void main(String[] args) {String ans;

do {

int a;Scanner scan=new Scanner(System.in);System.out.println("Enter 1 for Student and 2 for Staff : ");a=scan.nextInt();

if(a==1){//Class PersonString first,last,add;int zip,phone;System.out.print("First Name :");first=scan.next();System.out.print("Last Name : ");last=scan.next();System.out.print("Address : ");add=scan.next();System.out.print("Zip Code : ");zip=scan.nextInt();System.out.print("Phone No : ");phone=scan.nextInt();System.out.println("***************************"+"\nClass Person");Print(new Person(first,last,add,zip,phone));System.out.println("***************************");//Class StudentString major,point;System.out.print("Major Field : ");major=scan.next();System.out.print("Grade Point Average : ");point=scan.next();System.out.println("***************************"+"\nClass Student");Print(new Student(major,point));System.out.println("***************************");}//Class Employeeelse if(a==2){String office;double salary;int day,year;String month;System.out.println("Office Name : ");office=scan.next();System.out.println("Salary : ");salary=scan.nextDouble();System.out.println("Date Hired (mm/dd/yyyy) : ");System.out.println("Month");month=scan.next();System.out.println("Day");day=scan.nextInt();

14

Page 15: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

System.out.println("Year");year=scan.nextInt();MyDate date=new MyDate(year,month,day);date.toString();System.out.println("**************************"+"\nClass Employee");Print(new Employee(office,salary,date.toString()));System.out.println("***************************");//Class Facultyint officeHours1,officeHours2;String rank;System.out.println("Office Hours : ");officeHours1=scan.nextInt();System.out.println("to");officeHours2=scan.nextInt();System.out.println("Rank : ");rank=scan.next();System.out.println("***************************"+"\nClass Faculty");Print(new Faculty(officeHours1,officeHours2,rank));System.out.println("***************************");//Class StaffString title;System.out.println("Title : ");title=scan.next();System.out.println("***************************"+"\nClass Staff");Print(new Staff(title));System.out.println("***************************");//Print(new Object());}System.out.println("Do it again (Y/N)?");ans=scan.next();

} while (ans.equalsIgnoreCase("Y"));}public static void Print(Object o){

System.out.println(o.toString());}

}

Input:-

Output:-

15

Page 16: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

Question 3:-

16

Page 17: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

UML:-

Coding:-

17

Admin

+Admin()+toString():String

Lecturer

+roon_no:String+subject:String

+ Lecturer()+ Lecturer(String,String)+setRoomNo():void+getRoomNo(String)+setSubject():void+getSubject(String)+toString():String

Staff

PrintDetails():void

Page 18: Lab 4 jawapan (sugentiran mane)

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

Page 19: Lab 4 jawapan (sugentiran mane)

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 castingObject admin=new Admin();System.out.println("***************************");staff.PrintDetails(admin);

//Class LecturerString 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

Page 20: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

Output:-

Question 4:-

20

Page 21: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

import java.util.*;public class TestQ4 {

public static void main(String[] args) {String ans;

do {int a;Scanner scan=new Scanner(System.in);System.out.println("Enter 1 for Student and 2 for Staff : ");a=scan.nextInt();

if(a==1){//Class PersonPerson person=new Person();String first,last,add;int zip,phone;System.out.print("First Name :");first=scan.next();System.out.print("Last Name : ");last=scan.next();System.out.print("Address : ");add=scan.next();System.out.print("Zip Code : ");zip=scan.nextInt();System.out.print("Phone No : ");phone=scan.nextInt();person.setFirstName(first);person.setLastName(last);person.setStreetAddress(add);person.setZipCode(zip);person.setPhoneNumber(phone);System.out.println("***************************"+"\nClass Person");System.out.println(person.toString());System.out.println("***************************");//Class StudentStudent student=new Student();String major,point;System.out.print("Major Field : ");major=scan.next();System.out.print("Grade Point Average : ");point=scan.next();student.setMajorField(major);student.setGradePointAverage(point);System.out.println("***************************"+"\nClass Student");System.out.println(student.toString());System.out.println("***************************");

}

else if(a==2){

//Class EmployeeEmployee employee=new Employee();String office;double salary;

21

Page 22: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

int day,year;String month;System.out.println("Office Name : ");office=scan.next();System.out.println("Salary : ");salary=scan.nextDouble();System.out.println("Date Hired (mm/dd/yyyy) : ");System.out.println("Month");month=scan.next();System.out.println("Day");day=scan.nextInt();System.out.println("Year");year=scan.nextInt();MyDate date=new MyDate(year,month,day);date.toString();employee.setOffice(office);employee.setSalary(salary);employee.setDateHired(date.toString());System.out.println("**************************"+"\nClass Employee");System.out.println(employee.toString());System.out.println("***************************");//Class FacultyFaculty faculty=new Faculty();int officeHours1,officeHours2;String rank;System.out.println("Office Hours : ");officeHours1=scan.nextInt();System.out.println("to");officeHours2=scan.nextInt();System.out.println("Rank : ");rank=scan.next();faculty.setOfficeHours1(officeHours1);faculty.setOfficeHours2(officeHours2);faculty.setRank(rank);System.out.println("***************************"+"\nClass Faculty");System.out.println(faculty.toString());System.out.println("***************************");//Class StaffStaff staff=new Staff();String title;System.out.println("Title : ");title=scan.next();staff.setTitle(title);System.out.println("***************************"+"\nClass Staff");System.out.println(staff.toString());System.out.println("***************************");//Print(new Object());

}System.out.println("Do it again (Y/N)?");ans=scan.next();} while (ans.equalsIgnoreCase("Y"));

}}

22

Page 23: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

Input:-

Output:-

23

Page 24: Lab 4 jawapan (sugentiran mane)

SSK 3101 – LAB 4 Semester 1 2011/2012 ____________________________________________________________________________

24

Page 25: Lab 4 jawapan (sugentiran mane)

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