TUGAS 1. Pelajari program berikut,kemudian modifikasi program dengan ketentuan berikut 1. 2. 3.
Rubah ke bentuk GUI Interface program tentukan sendiri. Data staff dibuat dalam bentuk entrian ( lihat pada class staff ).
// Firm. // Demonstrates polymorphic processing. class Firm { //----------------------------------------------------------------// Creates a staff of employees for a firm and pays them. //----------------------------------------------------------------public static void main (String[] args) { Staff personnel = new Staff(); personnel.payday(); } }
// Staff.java // Represents the personnel staff of a particular business. class Staff { StaffMember[] staffList; //----------------------------------------------------------------// Sets up the list of staff members. //----------------------------------------------------------------public Staff () { staffList = new StaffMember[6]; staffList[0] = new Executive ("Sam", "123 Main Line", "555-0469", "123-45-6789", 1923.07); staffList[1] = new Employee ("Carla", "456 Off Line", "555-0101", "987-65-4321", 846.15); staffList[2] = new Employee ("Woody", "789 Off Rocker", "555-0000", "010-20-3040", 769.23);
‘13
1
Pemrograman Berorientasi Objek Syukri Nazar
Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id
staffList[3] = new Hourly ("Diane", "678 Fifth Ave.", "555-0690", "958-47-3625", 8.55); staffList[4] = new Volunteer ("Norm", "987 Suds Blvd.", "555-8374"); staffList[5] = new Volunteer ("Cliff", "321 Duds Lane", "555-7282"); ((Executive)staffList[0]).awardBonus (5000.00); ((Hourly)staffList[3]).addHours (40); } //----------------------------------------------------------------// Pays all staff members. //----------------------------------------------------------------public void payday () { double amount; for (int count=0; count < staffList.length; count++) { System.out.println (staffList[count]); amount = staffList[count].pay(); // polymorphic if (amount == 0.0) System.out.println ("Thanks!"); else System.out.println ("Paid: " + amount); System.out.println ("-----------------------------------"); } } }
// StaffMember.java // Represents a generic staff member. abstract class StaffMember { protected String name; protected String address; protected String phone; //----------------------------------------------------------------‘13
2
Pemrograman Berorientasi Objek Syukri Nazar
Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id
// Sets up a staff member using the specified information. //----------------------------------------------------------------public StaffMember (String name, String address, String phone) { this.name = name; this.address = address; this.phone = phone; } //----------------------------------------------------------------// Returns a string including the basic employee information. //----------------------------------------------------------------public String toString () { String result = "Name: " + name + "\n"; result += "Address: " + address + "\n"; result += "Phone: " + phone; return result; } //----------------------------------------------------------------// Derived classes must define the pay method for each employee // type. //----------------------------------------------------------------public abstract double pay(); } // Volunteer.java // Represents a staff member that works as a volunteer. class Volunteer extends StaffMember { //----------------------------------------------------------------// Sets up a volunteer using the specified information. //----------------------------------------------------------------public Volunteer (String name, String address, String phone) { super (name, address, phone); } //----------------------------------------------------------------// Returns a zero pay value for this volunteer. //----------------------------------------------------------------public double pay() { return 0.0; ‘13
3
Pemrograman Berorientasi Objek Syukri Nazar
Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id
} }
// Employee.java // Represents a general paid employee. class Employee extends StaffMember { protected String socialSecurityNumber; protected double payRate; //----------------------------------------------------------------// Sets up an employee with the specified information. //----------------------------------------------------------------public Employee (String name, String address, String phone, String socialSecurityNumber, double payRate) { super (name, address, phone); this.socialSecurityNumber = socialSecurityNumber; this.payRate = payRate; } //----------------------------------------------------------------// Returns information about an employee as a string. //----------------------------------------------------------------public String toString () { String result = super.toString(); result += "\nSocial Security Number: " + socialSecurityNumber; return result; } //----------------------------------------------------------------// Returns the pay rate for this employee. //----------------------------------------------------------------public double pay () { return payRate; } }
// Executive.java // Represents an executive staff member, who can earn a bonus. ‘13
4
Pemrograman Berorientasi Objek Syukri Nazar
Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id
class Executive extends Employee { private double bonus; //----------------------------------------------------------------// Sets up an executive with the specified information. //----------------------------------------------------------------public Executive (String name, String address, String phone, String socialSecurityNumber, double payRate) { super (name, address, phone, socialSecurityNumber, payRate); bonus = 0; // bonus has yet to be awarded } //----------------------------------------------------------------// Awards the specified bonus to this executive. //----------------------------------------------------------------public void awardBonus (double execBonus) { bonus = execBonus; } //----------------------------------------------------------------// Computes and returns the pay for an executive, which is the // regular employee payment plus a one-time bonus. //----------------------------------------------------------------public double pay () { double payment = super.pay() + bonus; bonus = 0; return payment; } }
// Hourly.java // Represents an employee that gets paid by the hour. class Hourly extends Employee { private int hoursWorked; //----------------------------------------------------------------// Sets up this hourly employee using the specified information. ‘13
5
Pemrograman Berorientasi Objek Syukri Nazar
Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id
//----------------------------------------------------------------public Hourly (String name, String address, String phone, String socialSecurityNumber, double payRate) { super (name, address, phone, socialSecurityNumber, payRate); hoursWorked = 0; } //----------------------------------------------------------------// Adds the specified number of hours to this employee's // accumulated hours. //----------------------------------------------------------------public void addHours (int moreHours) { hoursWorked += moreHours; } //----------------------------------------------------------------// Computes and returns the pay for this hourly employee. //----------------------------------------------------------------public double pay () { double payment = payRate * hoursWorked; hoursWorked = 0; return payment; } //----------------------------------------------------------------// Returns information about this hourly employee as a string. //----------------------------------------------------------------public String toString () { String result = super.toString(); result += "\nCurrent hours: " + hoursWorked; return result; } } 2. Perhatikan diagram berikut Diagram berikut mengilustrasikan konsef pewarisan, yang dimodelkan dalam diagram UML berikut:
‘13
6
Pemrograman Berorientasi Objek Syukri Nazar
Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id
Keterangan Status : Tetap (untuk dosen tetap) dan Tak Tetap (untuk dosen tak tetap) Tunjangan Fungsional : Jika dosen tetap maka tunjangan fungsioanl = 300000 Jika dosen tak tetap maka tunjangan fungsional = 0 Transfort : Jika dosen tatak tetap maka transfort = 250000 Jika dosen tetap maka transfort = 0 Total Dibayar : Jumlah SKS * Honor Per SKS PPh : 2% * TotalDibayar Total Pendapatan : Total Diabyar – PPh Berdasarkan keterangan diatas buat program dengan interface berikut
‘13
7
Pemrograman Berorientasi Objek Syukri Nazar
Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id
3. Suatu perusahaan mempunyai karyawan tetap dan karyawan harian. Karywan tetap mempunyai gaji bulana dan tunjangan jabatan bagi yang memiliki jabatan struktural. Tunjangan jabatan untuk manager 5000 dan kepala bagian 200. Gaji pokok 500 bagi yang masa kerjanya 1 hinggai 3 tahun dan 750 bagi masa kerja lebih dari 3 tahun. Karywan harian mempunyai gaji 20 perhari. Semua karyawan mendapat uang makan sebesar 5 jika masuk kerja dan dibayar ahir bulan. Berdasarkan keterangan diatas buatlah. a. Buat struktur class karyawan diatas dalam bentuk diagram uml b. buat program untuk menghitung total pendapatan karyawan perbulan , jika perhitungan total pendapatan = gaji+tunjangan + jumlah_hari_kerja*uangmakan
Note : Jawaban di imel dan paling lambat diterima hari sabtu tanggal 12-01-13 jam 17.00
‘13
8
Pemrograman Berorientasi Objek Syukri Nazar
Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id