TQC+ 物件導向程式語言 Java 6 606 薪資計算
題目前後相關,請閱讀完這個題組的五個題目之後再作答,每題答案皆能獨立執行。請將需要重複或共同使用的程式片段撰寫成函式,以供在類別中呼叫使用,避免在不同的類別中重複寫相同的程式碼,否則將酌量扣分。
題目一:
題目說明:
請開啟檔案 JPD06_1.java,為老師撰寫一個「薪資計算」程式,計算學生的選修課及平均成績,請依下列題意完成作答。將 JPD06_1.java 內的 class JPD06_1 修改為 class JPA06_1,將檔案另存為 JPA06_1.java 後編譯為 JPA06_1.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 某校有兩種類別的老師:FullTime (專任老師) 及 PartTime (兼任老師)。
(2) 專任老師有以下的資料:name、rate (每小時標準工資)、totalHours。
(3) 專任老師薪水的計算方式為:
salary = 9 * rate + ((totalHours – 9) * rate * 0.8)。
(4) 兼任老師有以下的資料:name、rate (每小時標準工資)、totalHours。
(5) 兼任老師新水的計算方式為:salary = totalHours * rate。
(6) 請為此學校新增兩名兼任老師及三名專任老師,並印出這五位教師的新資。
類別 | name | rate | totalHours |
---|---|---|---|
兼任 | John | 400 | 2 |
兼任 | Mary | 300 | 4 |
專任 | Peter | 400 | 9 |
專任 | Paul | 300 | 12 |
專任 | Eric | 350 | 15 |
執行結果參考畫面:
程式碼:
abstract class Teacher {
String name;
int rate, totalHours;
Teacher(String name, int rate, int totalHours) {
this.name = name;
this.rate = rate;
this.totalHours = totalHours;
}
abstract double salary();
}
class PartTimeTeacher extends Teacher {
PartTimeTeacher(String name, int rate, int totalHours) {
super(name, rate, totalHours);
}
double salary() {
return totalHours * rate;
}
}
class FullTimeTeacher extends Teacher {
FullTimeTeacher(String name, int rate, int totalHours) {
super(name, rate, totalHours);
}
double salary() {
return 9 * rate + ((totalHours - 9) * rate * 0.8);
}
}
public class JPA06_1 {
public static void main(String argv[]) {
PartTimeTeacher p1 = new PartTimeTeacher("John",400,2);
PartTimeTeacher p2 = new PartTimeTeacher("Mary",300,4);
FullTimeTeacher f1 = new FullTimeTeacher("Peter",400,9);
FullTimeTeacher f2 = new FullTimeTeacher("Paul",300,12);
FullTimeTeacher f3 = new FullTimeTeacher("Eric",350,15);
System.out.println("John-PartTime:" + p1.salary());
System.out.println("Mary-PartTime:" + p2.salary());
System.out.println("Peter-FulLTime:" + f1.salary());
System.out.println("Paul-FulLTime:" + f2.salary());
System.out.println("Eric-FulLTime:" + f3.salary());
}
}
題目二:
題目說明:
請開啟 JPD06_2.java,該校不論專、兼任教師都需要繳交健保費 100 元及稅金,請依下列題意完成作答。將 JPD06_2.java 内的 class JPD06_2 修改為 class JPA06_2,將檔案另存為 JPA06_2.java 後編譯為 JPA06_2.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 稅金 = salary * 10%。
(2) 請寫一個命名為 afterTaxIns 的方法,以算出五位教師在扣除健保費與稅金後的薪資。
(3) 該校增加一位 manager (行政主管) 的類別,行政主管必須是專任老師。
(4) 行政主管分三級:rank = l or 2 or 3。
其薪水的計算方式為:salary = 專任教師的薪水 + rank * 500。
(5) 請宣告一個名字是 Fang 的行政主管,其 rate 是 500,totalHours 是 12,rank 是 3。請計算 Fang 的「薪資」及 afterTaxins (扣除健保費與稅金後的薪資)。
類別 | name | rate | totalHours | rank |
---|---|---|---|---|
行政主管 | Fang | 500 | 12 | 3 |
執行結果參考畫面:
程式碼:
abstract class Teacher {
String name;
int rate, totalHours;
Teacher(String name, int rate, int totalHours) {
this.name = name;
this.rate = rate;
this.totalHours = totalHours;
}
abstract double salary();
double afterTaxIns() {
return salary() - 100 - salary() * 0.1;
}
}
class PartTimeTeacher extends Teacher {
PartTimeTeacher(String name, int rate, int totalHours) {
super(name, rate, totalHours);
}
double salary() {
return totalHours * rate;
}
}
class FullTimeTeacher extends Teacher {
FullTimeTeacher(String name, int rate, int totalHours) {
super(name, rate, totalHours);
}
double salary() {
return 9 * rate + ((totalHours - 9) * rate * 0.8);
}
}
class Manager extends FullTimeTeacher {
int rank;
Manager(String name, int rate, int totalHours, int rank) {
super(name, rate, totalHours);
this.rank = rank;
}
double salary() {
return super.salary() + rank * 500;
}
}
public class JPA06_2 {
public static void main(String argv[]) {
PartTimeTeacher p1 = new PartTimeTeacher("John",400,2);
PartTimeTeacher p2 = new PartTimeTeacher("Mary",300,4);
FullTimeTeacher f1 = new FullTimeTeacher("Peter",400,9);
FullTimeTeacher f2 = new FullTimeTeacher("Paul",300,12);
FullTimeTeacher f3 = new FullTimeTeacher("Eric",350,15);
System.out.println("John-afterTaxIns: " + p1.afterTaxIns());
System.out.println("Mary-afterTaxIns: " + p2.afterTaxIns());
System.out.println("Peter-afterTaxIns: " + f1.afterTaxIns());
System.out.println("Paul-afterTaxIns: " + f2.afterTaxIns());
System.out.println("Eric-afterTaxIns: " + f3.afterTaxIns());
Manager am1 = new Manager("Fang", 500, 12, 3);
System.out.println("Fang-Manager: " + am1.salary());
System.out.println("Fang-afterTax Ins: " + am1.afterTaxIns());
}
}
題目三:
題目說明:
請開啟 JPD06_3.java,請寫一個方法,此方法能比較兩位教師哪一位薪水較高,請依下列題意完成作答。將 JPD06_3.java 内的 class JPD06_3 修改為 class JPA06_3,將檔案另存為 JPA06_3.java 後編譯為 JPA06_3.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 請寫一個比較兩位教師,哪一位薪水較高的方法。
(2) 比較 Fang 與 Eric 哪一位薪水 (未扣除健保費及稅金) 較高?
(3) 比較 Eric 與John 哪一位薪水 (未扣除健保費及稅金) 較高?
執行結果參考畫面:
程式碼:
abstract class Teacher {
String name;
int rate, totalHours;
Teacher(String name, int rate, int totalHours) {
this.name = name;
this.rate = rate;
this.totalHours = totalHours;
}
abstract double salary();
double afterTaxIns() {
return salary() - 100 - salary() * 0.1;
}
void compare(Teacher teacher) {
if(this.salary() > teacher.salary())
System.out.printf("%s is higher than %s\n", this.name,teacher.name);
else
System.out.printf("%s is higher than %s\n", teacher.name,this.name);
}
}
class PartTimeTeacher extends Teacher {
PartTimeTeacher(String name, int rate, int totalHours) {
super(name, rate, totalHours);
}
double salary() {
return totalHours * rate;
}
}
class FullTimeTeacher extends Teacher {
FullTimeTeacher(String name, int rate, int totalHours) {
super(name, rate, totalHours);
}
double salary() {
return 9 * rate + ((totalHours - 9) * rate * 0.8);
}
}
class Manager extends FullTimeTeacher {
int rank;
Manager(String name, int rate, int totalHours, int rank) {
super(name, rate, totalHours);
this.rank = rank;
}
double salary() {
return super.salary() + rank * 500;
}
}
public class JPA06_3 {
public static void main(String argv[]) {
PartTimeTeacher p1 = new PartTimeTeacher("John",400,2);
PartTimeTeacher p2 = new PartTimeTeacher("Mary",300,4);
FullTimeTeacher f1 = new FullTimeTeacher("Peter",400,9);
FullTimeTeacher f2 = new FullTimeTeacher("Paul",300,12);
FullTimeTeacher f3 = new FullTimeTeacher("Eric",350,15);
Manager am1 = new Manager("Fang", 500, 12, 3);
am1.compare(f3);
p1.compare(f3);
}
}
題目四:
題目說明:
請開啟 JPD06_4.java,計算全部教師 afterTaxIns 的總和,請依下列題意完成作答。將 JPD06_4.java 内的 class JPD06_4 修改為 class JPA06_4,將檔案另存為 JPA06_4.java 後編譯為 JPA06_4.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 請使用 Java.util.HashMap 撰寫一個能夠存放教師資料的類別,HashMap 的 key 値為教師名稱 (String),value 值為教師物件。
(2) 將這些教師物件存入一個由這個類別所製造的實例中,然後撰寫一個 totalOfAll 方法,將所有老師個別的 afterTaxIns 新資相加,輸出總和。
執行結果參考畫面:
程式碼:
import java.util.HashMap;
abstract class Teacher {
String name;
int rate, totalHours;
Teacher(String name, int rate, int totalHours) {
this.name = name;
this.rate = rate;
this.totalHours = totalHours;
}
abstract double salary();
double afterTaxIns() {
return salary() - 100 - salary() * 0.1;
}
void compare(Teacher teacher) {
if(this.salary() > teacher.salary())
System.out.printf("%s is higher than %s\n", this.name,teacher.name);
else
System.out.printf("%s is higher than %s\n", teacher.name,this.name);
}
}
class PartTimeTeacher extends Teacher {
PartTimeTeacher(String name, int rate, int totalHours) {
super(name, rate, totalHours);
}
double salary() {
return totalHours * rate;
}
}
class FullTimeTeacher extends Teacher {
FullTimeTeacher(String name, int rate, int totalHours) {
super(name, rate, totalHours);
}
double salary() {
return 9 * rate + ((totalHours - 9) * rate * 0.8);
}
}
class Manager extends FullTimeTeacher {
int rank;
Manager(String name, int rate, int totalHours, int rank) {
super(name, rate, totalHours);
this.rank = rank;
}
double salary() {
return super.salary() + rank * 500;
}
}
class TeacherDB {
HashMap<String, Teacher> hashmap = new HashMap();
void store(String name, Teacher teacher) {
hashmap.put(name, teacher);
}
double totalOfAll() {
double total = 0.0;
for(String key : hashmap.keySet())
total += hashmap.get(key).afterTaxIns();
return total;
}
}
public class JPA06_4 {
public static void main(String argv[]) {
PartTimeTeacher p1 = new PartTimeTeacher("John",400,2);
PartTimeTeacher p2 = new PartTimeTeacher("Mary",300,4);
FullTimeTeacher f1 = new FullTimeTeacher("Peter",400,9);
FullTimeTeacher f2 = new FullTimeTeacher("Paul",300,12);
FullTimeTeacher f3 = new FullTimeTeacher("Eric",350,15);
Manager am1 = new Manager("Fang", 500, 12, 3);
TeacherDB school = new TeacherDB();
school.store("John", p1);
school.store("Mary", p2);
school.store("Peter", f1);
school.store("Paul", f2);
school.store("Eric", f3);
school.store("Fang", am1);
System.out.println("Total salary: " + school.totalOfAll());
}
}
題目五:
題目說明:
請開啟 JPD06_5.java,接續題目四的薪資比較,請依下列題意完成作答。將 JPD06_5.java 内的 class JPD06_5 修改為 class JPA06_5,將檔案另存為 JPA06_5.java 後編譯為 JPA06_5.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 撰寫一個能夠印出五位教師 (不含行政主管 Fang) 的姓名及薪資 (不扣稅及不扣健保費) 的方法。這個方法利用例外處理將薪水少於 1500 的教師姓名前印出兩個星號 (**)。
執行結果參考畫面:
程式碼:
import java.util.HashMap;
abstract class Teacher {
String name;
int rate, totalHours;
Teacher(String name, int rate, int totalHours) {
this.name = name;
this.rate = rate;
this.totalHours = totalHours;
}
abstract double salary();
double afterTaxIns() {
return salary() - 100 - salary() * 0.1;
}
void compare(Teacher teacher) {
if(this.salary() > teacher.salary())
System.out.printf("%s is higher than %s\n", this.name,teacher.name);
else
System.out.printf("%s is higher than %s\n", teacher.name,this.name);
}
}
class PartTimeTeacher extends Teacher {
PartTimeTeacher(String name, int rate, int totalHours) {
super(name, rate, totalHours);
}
double salary() {
return totalHours * rate;
}
}
class FullTimeTeacher extends Teacher {
FullTimeTeacher(String name, int rate, int totalHours) {
super(name, rate, totalHours);
}
double salary() {
return 9 * rate + ((totalHours - 9) * rate * 0.8);
}
}
class Manager extends FullTimeTeacher {
int rank;
Manager(String name, int rate, int totalHours, int rank) {
super(name, rate, totalHours);
this.rank = rank;
}
double salary() {
return super.salary() + rank * 500;
}
}
class TeacherDB {
HashMap<String, Teacher> hashmap = new HashMap();
void store(String name, Teacher teacher) {
hashmap.put(name, teacher);
}
double totalOfAll() {
double total = 0.0;
for(String key : hashmap.keySet())
total += hashmap.get(key).afterTaxIns();
return total;
}
void printAllTeacher() {
for(String key : hashmap.keySet()) {
try {
if(hashmap.get(key).salary() < 1500)
throw new Exception();
System.out.printf("%s %.1f\n", hashmap.get(key).name,hashmap.get(key).salary());
} catch(Exception e) {
System.out.printf("**%s %.1f\n", hashmap.get(key).name,hashmap.get(key).salary());
}
}
}
}
public class JPA06_5 {
public static void main(String argv[]) {
PartTimeTeacher p1 = new PartTimeTeacher("John",400,2);
PartTimeTeacher p2 = new PartTimeTeacher("Mary",300,4);
FullTimeTeacher f1 = new FullTimeTeacher("Peter",400,9);
FullTimeTeacher f2 = new FullTimeTeacher("Paul",300,12);
FullTimeTeacher f3 = new FullTimeTeacher("Eric",350,15);
TeacherDB school = new TeacherDB();
school.store("John", p1);
school.store("Mary", p2);
school.store("Peter", f1);
school.store("Paul", f2);
school.store("Eric", f3);
school.printAllTeacher();
}
}