TQC+ 物件導向程式語言 Java 6 610 員工薪資制度
第6類:物件導向程式設計與例外處理

TQC+ 物件導向程式語言 Java 6 610 員工薪資制度

來自的免費背景照片 zh.pngtree.com

題目前後相關,請閱讀完這個題組的五個題目之後再作答,每題答案皆能獨立執行。請將需要重複或共同使用的程式片段撰寫成函式,以供在類別中呼叫使用,避免在不同的類別中重複寫相同的程式碼,否則將酌量扣分。

題目一:

題目說明:

請開啟檔案 JPD06_1.java,設計「員工薪資制度」程式,請依下列題意完成作答。將 JPD06_1.java 內的 class JPD06_1 修改為 class JPA06_1,將檔案另存為 JPA06_1.java 後編譯為 JPA06_1.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 該工廠有三種類別的員工。

(2) SalaryWorker 以「年薪」(月新為年薪除以 12) 計算新水。

(3) HourlyWorker 以「時薪」計算薪水。

(4) Manager 必須是 SalaryWorker,但是除了年新之外,每月還有紅利。

(5) 每名員工都有員工編號。

(6) 請宣告一個 SalaryWorker 類別。其員工編號為 96001,年薪為 180000,撰寫 monthPay 方法計算這名員工該月的收入。

(7) 請宣告一個 HourlyWorker 類別。其員工編號為 96002,每月工時 160 小時,時新 100,撰寫 monthPay 方法計算這名員工該月的收入。

(8) 請宣告一個 Manager 類別。其員工編號為 97001,年新為 240000,每月的紅利為 5000,撰寫 monthPay 方法計算這名員工該月的收入。

(9) 請分別顯示三位員工的薪水,顯示如執行結果參考畫面。

類別員工編號年薪工時時薪每月紅利
SalaryWorker96001180000
HourlyWorker96002160100
Manager970012400005000

執行結果參考畫面:



JPD06_1.java 檔案下載

程式碼:

abstract class Employee {
	String id;
	
	Employee(String id) {
		this.id = id;
	}
	
	abstract double monthPay();
}

class SalaryWorker extends Employee {
	int annualSalary;
	
	SalaryWorker(String id, int annualSalary) {
		super(id);
		this.annualSalary = annualSalary;
	}
	
	double monthPay() {
		return annualSalary / 12;
	}
}

class HourlyWorker extends Employee {
	int monthlySalary, workingHours;
	
	HourlyWorker(String id, int monthlySalary, int workingHours) {
		super(id);
		this.monthlySalary = monthlySalary;
		this.workingHours = workingHours;
	}
	
	double monthPay() {
		return monthlySalary * workingHours;
	}
}

class Manager extends SalaryWorker {
	int bonus;
	
	Manager(String id, int annualSalary, int bonus) {
		super(id, annualSalary);
		this.bonus = bonus;
	}
	
	double monthPay() {
		return super.monthPay() + bonus;
	}
}

public class JPA06_1 {
   public static void main(String argv[]) {
        SalaryWorker sw1 = new SalaryWorker("96001",180000);
        System.out.println("SalaryWorker:" + sw1.monthPay());
        HourlyWorker hw1 = new HourlyWorker("96002", 100, 160);
        System.out.println("HourlyWorker:" + hw1.monthPay());
        Manager ma1 = new Manager("97001", 240000, 5000);
        System.out.println("Manager:" + ma1.monthPay());
    }
}

JPA06_1.java 檔案下載



題目二:

題目說明:

請開啟 JPD06_2.java,比較兩個員工的收入何者較高,請依下列題意完成作答。將 JPD06_2.java 内的 class JPD06_2 修改為 class JPA06_2,將檔案另存為 JPA06_2.java 後編譯為 JPA06_2.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 根據上題說明,依 SalaryWorker、HourlyWorker、Manager 順序先印出此三位的該月收入。

(2) 請寫一方法,首先比較 SalaryWorker 與 HourlyWork 兩者的該月收入,何者較高;再比較 HourlyWorker 與 Manager 的月收入何者較高,並接續列印新資較高的員工代號,顯示【員工代號 + 較高】。

(3) 再請計算每位員工該月的應繳稅額,税率均為薪水的 15%,接續 2.(2) 分別列出 SalaryWorker、HourlyWorker、Manager 的稅額。

執行結果參考畫面:



JPD06_2.java 檔案下載

程式碼:

abstract class Employee {
	String id;
	
	Employee(String id) {
		this.id = id;
	}
	
	abstract double monthPay();
	void payHigher(Employee employee) {
		if(this.monthPay() > employee.monthPay())
			System.out.println(this.id + "較高");
		else
			System.out.println(employee.id + "較高");
	}
	
	double monthTaxes() {
		return monthPay() * 0.15;
	}
}

class SalaryWorker extends Employee {
	int annualSalary;
	
	SalaryWorker(String id, int annualSalary) {
		super(id);
		this.annualSalary = annualSalary;
	}
	
	double monthPay() {
		return annualSalary / 12;
	}
}

class HourlyWorker extends Employee {
	int monthlySalary, workingHours;
	
	HourlyWorker(String id, int monthlySalary, int workingHours) {
		super(id);
		this.monthlySalary = monthlySalary;
		this.workingHours = workingHours;
	}
	
	double monthPay() {
		return monthlySalary * workingHours;
	}
}

class Manager extends SalaryWorker {
	int bonus;
	
	Manager(String id, int annualSalary, int bonus) {
		super(id, annualSalary);
		this.bonus = bonus;
	}
	
	double monthPay() {
		return super.monthPay() + bonus;
	}
}

public class JPA06_2 {
    public static void main(String argv[]) {
        
        SalaryWorker sw1 = new SalaryWorker("96001",180000);
        System.out.println("SalaryWorker:" + sw1.monthPay());
        HourlyWorker hw1 = new HourlyWorker("96002", 100, 160);
        System.out.println("HourlyWorker:" + hw1.monthPay());
        Manager ma1 = new Manager("97001", 240000, 5000);
        System.out.println("Manager:" + ma1.monthPay());

        sw1.payHigher(hw1);
        hw1.payHigher(ma1);

        System.out.println("SalaryWorker稅額:" + sw1.monthTaxes());
        System.out.println("HourlyWorker稅額:" + hw1.monthTaxes());
        System.out.println("Manager稅額:" + ma1.monthTaxes());    
    }
}

JPA06_2.java 檔案下載



題目三:

題目說明:

請開啟 JPD06_3.java,計算員工的總月繳稅額,請依下列題意完成作答。將 JPD06_3.java 内的 class JPD06_3 修改為 class JPA06_3,將檔案另存為 JPA06_3.java 後編譯為 JPA06_3.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 請以類別變數累計員工人數,並在每次計算某一員工的月繳稅額時,以類別變數累計總月繳稅額。

(2) 撰寫一個能夠傳回所有員工的平均繳稅金額的類別方法。

執行結果參考畫面:



JPD06_3.java 檔案下載

程式碼:

abstract class Employee {
	String id;
	static int people = 0;
	static double totalTaxes = 0.0;
	
	Employee(String id) {
		this.id = id;
		people++;
	}
	
	abstract double monthPay();
	void payHigher(Employee employee) {
		if(this.monthPay() > employee.monthPay())
			System.out.println(this.id + "較高");
		else
			System.out.println(employee.id + "較高");
	}
	
	double monthTaxes() {
		totalTaxes += monthPay() * 0.15;
		return monthPay() * 0.15;
	}
	
	static double getAverageTax() {
		return totalTaxes / people;
	}
}

class SalaryWorker extends Employee {
	int annualSalary;
	
	SalaryWorker(String id, int annualSalary) {
		super(id);
		this.annualSalary = annualSalary;
	}
	
	double monthPay() {
		return annualSalary / 12;
	}
}

class HourlyWorker extends Employee {
	int monthlySalary, workingHours;
	
	HourlyWorker(String id, int monthlySalary, int workingHours) {
		super(id);
		this.monthlySalary = monthlySalary;
		this.workingHours = workingHours;
	}
	
	double monthPay() {
		return monthlySalary * workingHours;
	}
}

class Manager extends SalaryWorker {
	int bonus;
	
	Manager(String id, int annualSalary, int bonus) {
		super(id, annualSalary);
		this.bonus = bonus;
	}
	
	double monthPay() {
		return super.monthPay() + bonus;
	}
}

public class JPA06_3 {
    public static void main(String argv[]) {
      SalaryWorker sw1 = new SalaryWorker("96001",180000);        
        HourlyWorker hw1 = new HourlyWorker("96002", 100, 160);        
        Manager ma1 = new Manager("97001", 240000, 5000);
       
        System.out.println("SalaryWorker稅額:" + sw1.monthTaxes());
        System.out.println("HourlyWorker稅額:" + hw1.monthTaxes());
        System.out.println("Manager稅額:" + ma1.monthTaxes()); 
        System.out.println("平均稅額:" + Employee.getAverageTax());
    }
}

JPA06_3.java 檔案下載



題目四:

題目說明:

請開啟 JPD06_4.java,計算員工稅後薪資,請依下列題意完成作答。將 JPD06_4.java 内的 class JPD06_4 修改為 class JPA06_4,將檔案另存為 JPA06_4.java 後編譯為 JPA06_4.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 請使用 HashMap 寫一個 Management 人事管理類別,Management 提供一個能夠傳入員工編號,然後傳回該員工稅後薪資的方法 afterTax。

(2) 請查詢 97001 的稅後薪資。

執行結果參考畫面:



JPD06_4.java 檔案下載

程式碼:

import java.util.HashMap;

abstract class Employee {
	String id;
	static int people = 0;
	static double totalTaxes = 0.0;
	
	Employee(String id) {
		this.id = id;
		people++;
	}
	
	abstract double monthPay();
	void payHigher(Employee employee) {
		if(this.monthPay() > employee.monthPay())
			System.out.println(this.id + "較高");
		else
			System.out.println(employee.id + "較高");
	}
	
	double monthTaxes() {
		totalTaxes += monthPay() * 0.15;
		return monthPay() * 0.15;
	}
	
	static double getAverageTax() {
		return totalTaxes / people;
	}
}

class SalaryWorker extends Employee {
	int annualSalary;
	
	SalaryWorker(String id, int annualSalary) {
		super(id);
		this.annualSalary = annualSalary;
	}
	
	double monthPay() {
		return annualSalary / 12;
	}
}

class HourlyWorker extends Employee {
	int monthlySalary, workingHours;
	
	HourlyWorker(String id, int monthlySalary, int workingHours) {
		super(id);
		this.monthlySalary = monthlySalary;
		this.workingHours = workingHours;
	}
	
	double monthPay() {
		return monthlySalary * workingHours;
	}
}

class Manager extends SalaryWorker {
	int bonus;
	
	Manager(String id, int annualSalary, int bonus) {
		super(id, annualSalary);
		this.bonus = bonus;
	}
	
	double monthPay() {
		return super.monthPay() + bonus;
	}
}

class Management {
	HashMap<String, Employee> hashmap = new HashMap();
	Employee employee;
	
	void put(String id, Employee employee) {
		hashmap.put(id, employee);
	}
	
	double afterTax(String id) {
		employee = hashmap.get(id);
		return employee.monthPay() - employee.monthTaxes();
	}
}

public class JPA06_4 {
    public static void main(String argv[]) {
        SalaryWorker sw1 = new SalaryWorker("96001",180000);
        HourlyWorker hw1 = new HourlyWorker("96002", 100, 160);        
        Manager ma1 = new Manager("97001", 240000, 5000);        
        Management m = new Management();
        m.put("96001", sw1);
        m.put("96002", hw1);
        m.put("97001", ma1);
        System.out.println("97001 的稅後薪資: " + m.afterTax("97001"));
    }
}

JPA06_4.java 檔案下載



題目五:

題目說明:

請開啟 JPD06_5.java,為題目四增加例外處理的功能,請依下列題意完成作答。將 JPD06_5.java 内的 class JPD06_5 修改為 class JPA06_5,將檔案另存為 JPA06_5.java 後編譯為 JPA06_5.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 請為 Management 寫一個計算員工總月薪的方法。如果總月薪超過 50000,則以 exception 印出警告訊息。

執行結果參考畫面:



JPD06_5.java 檔案下載

程式碼:

import java.util.HashMap;

abstract class Employee {
	String id;
	static int people = 0;
	static double totalTaxes = 0.0;
	
	Employee(String id) {
		this.id = id;
		people++;
	}
	
	abstract double monthPay();
	void payHigher(Employee employee) {
		if(this.monthPay() > employee.monthPay())
			System.out.println(this.id + "較高");
		else
			System.out.println(employee.id + "較高");
	}
	
	double monthTaxes() {
		totalTaxes += monthPay() * 0.15;
		return monthPay() * 0.15;
	}
	
	static double getAverageTax() {
		return totalTaxes / people;
	}
}

class SalaryWorker extends Employee {
	int annualSalary;
	
	SalaryWorker(String id, int annualSalary) {
		super(id);
		this.annualSalary = annualSalary;
	}
	
	double monthPay() {
		return annualSalary / 12;
	}
}

class HourlyWorker extends Employee {
	int monthlySalary, workingHours;
	
	HourlyWorker(String id, int monthlySalary, int workingHours) {
		super(id);
		this.monthlySalary = monthlySalary;
		this.workingHours = workingHours;
	}
	
	double monthPay() {
		return monthlySalary * workingHours;
	}
}

class Manager extends SalaryWorker {
	int bonus;
	
	Manager(String id, int annualSalary, int bonus) {
		super(id, annualSalary);
		this.bonus = bonus;
	}
	
	double monthPay() {
		return super.monthPay() + bonus;
	}
}

class Management {
	HashMap<String, Employee> hashmap = new HashMap();
	Employee employee;
	
	void put(String id, Employee employee) {
		hashmap.put(id, employee);
	}
	
	double afterTax(String id) {
		employee = hashmap.get(id);
		return employee.monthPay() - employee.monthTaxes();
	}
	
	void totalMonthPay() throws Exception {
		double totalmonthpay = 0.0;
		
		for(Employee employee : hashmap.values())
			totalmonthpay += employee.monthPay();
		if(totalmonthpay > 50000)
			throw new Exception("Total salary exceed limit: " + totalmonthpay);
	}
}

public class JPA06_5 {
    public static void main(String argv[]) {
        SalaryWorker sw1 = new SalaryWorker("96001",180000);
        HourlyWorker hw1 = new HourlyWorker("96002", 100, 160);        
        Manager ma1 = new Manager("97001", 240000, 5000);        
        Management m = new Management();
        m.put("96001", sw1);
        m.put("96002", hw1);
        m.put("97001", ma1);
        try {
        	m.totalMonthPay();
        } catch(Exception e) {
        	System.out.println(e.getMessage());
        }
    }
}

JPA06_5.java 檔案下載



TQC+ 物件導向程式語言 Java 6 第六類:物件導向程式設計與例外處理




如果覺得文章內容還不錯的話,麻煩請幫我點個讚!感謝

可以多點幾次喔~~

第一次點讚需使用 Google 或 Facebook 帳號註冊
Exit mobile version