---
# System prepended metadata

title: TQC java 練習題
tags: [複習用, 證照考試, JAVA課堂學習, 高科大]

---

# TQC java 練習題
###### tags: `JAVA課堂學習` `複習用` `高科大` `證照考試`

## <font color="FFD306">💡</font>注意事項:
1. 通常**題組一**都會**改變題型**，不會和平常練習的參考答案一樣。
2. 答案沒有唯一解，但需要==依照題目敘述==，將程式碼製作出來。

## 題目102
![](https://hackmd.io/_uploads/r1a5lC3qt.png =850x500)

:::spoiler {state="open"} **自行速解**
```java
import java.util.*;
public class JPA01 
{
    	static Scanner kb=new Scanner(System.in);
	public static void main(String args[])
	{
		double kgn=2.20462;

		System.out.print("Please input: ");
		double kg=kb.nextDouble();

		System.out.printf("%.6f kg = %.6f ponds",kg,kg*kgn);
	}
}
```
:::



## 題目108
![](https://hackmd.io/_uploads/Syxi7Cnqt.png =850x500)

:::success
```java=1
public static int add(int a,int b)
{
	System.out.println("Adding two integers:"+a+","+b);
	return a+b;
}
public static double add(double a,double b)
{
	System.out.println("Adding two doubles:"+a+","+b);
	return a+b;
}
public static String add(String a,String b)
{
	System.out.println("Adding two Strings:"+a+","+b);
	return a+b;
}
```
:::

:::spoiler {state="open"} **自行速解**
```java
class JPA01 
{
  	public static int add(int a,int b)
	{
		System.out.println("Adding two integers:"+a+","+b);
		return a+b;
	}
	public static double add(double a,double b)
	{
		System.out.println("Adding two doubles:"+a+","+b);
		return a+b;
	}
  	public static String add(String a,String b)
	{
		System.out.println("Adding two Strings:"+a+","+b);
		return a+b;
	}
  
  	public static void main (String[] args) 
	{
    		int i = add(2, 3);
    		double d = add(5.2, 4.3);
    		String s = add("I love ", "Java!!");
    		System.out.printf("%d %f %s \n", i, d, s);
  	}
}
```
:::
## 題目110
![](https://hackmd.io/_uploads/BJdTQ025F.png =850x500)

:::spoiler {state="open"} **自行速解**
```java
import java.util.Scanner;
public class JPA01 
{
    	public static void main(String args[]) 
	{
        	double totalarea;
        	System.out.printf("圓形面積為：%f \n",calCircle(5));
        	System.out.printf("三角形面積為：%f \n",calTriangle(10,5));
        	System.out.printf("方形面積為：%f \n",calRectangle(5,10));
        	totalarea = calCircle(5)+calTriangle(10,5)+calRectangle(5,10);
        	System.out.printf("此圖形面積為：%f \n",totalarea);
    	}
   	
	static double calCircle(int r) 
	{
		double PI=3.1415926;
        	return Math.pow(r,2)*PI;
    	}
    	static double calTriangle(int b,int h) 
	{
        	return b*h/2.0;
    	}
    	static double calRectangle(int m,int n) 
	{
        	return (double)m*n;
    	}
}

```
:::

---
## 題目202
![](https://hackmd.io/_uploads/rk7LN0n9Y.png =850x500)

:::spoiler {state="open"} **自行速解**
```java
import java.util.*;
class JPA02 
{
    	static Scanner keyboard = new Scanner(System.in);
    	public static void main(String[] args) 
	{
        	test();
        	test();
    	}
    
    	public static void test() 
	{
        	System.out.println("Input:");
		int a=keyboard.nextInt();
		int b=keyboard.nextInt();

		if(a>b)
		{
			System.out.println(a+" is larger than "+b);
		}
		else
		{
			System.out.println(b+" is larger than "+a);
		}
    	}
}

```
:::

## 題目206
![](https://hackmd.io/_uploads/SymXBC39F.png =850x500)

:::spoiler {state="open"} **自行速解**
```java
import java.util.*;
public class JPA02 
{
	static Scanner keyboard = new Scanner(System.in);
    	public static void main(String[] args) 
	{
        	test();        
        	test();
        	test();
        	test();
    	}
    
    	static void test() 
	{
        	int chi, eng, math, avg;
        	System.out.print("Input Chinese score:");
        	chi = keyboard.nextInt();
        	System.out.print("Input English score:");
        	eng = keyboard.nextInt();
        	System.out.print("Input Math score:");
        	math = keyboard.nextInt();
        
        	if(chi<60)
		{
			System.out.println("Chinese failed.");
		}
		
		if(eng<60)
		{
			System.out.println("English failed.");
		}

		if(math<60)
		{
			System.out.println("Math failed.");
		}

		if(chi>=60&&eng>=60&&math>=60)
		{
			System.out.println("All Pass.");
		}
    	}
}

```
:::
## 題目208
![](https://hackmd.io/_uploads/rJR4S0hcK.png =850x500)

:::spoiler {state="open"} **自行速解**
```java
import java.util.*;
class JPA02 
{
    	static Scanner keyboard = new Scanner(System.in);
    	public static void main(String[] args) 
	{
        	test();
        	test();
        	test();
        	test();
        	test();
    	}
  
    	public static void test()
	{
        	System.out.println("Input:");
		int score=keyboard.nextInt();

		if(score>=90)
		{
			System.out.println("Your grade is A.");
		}
		else
		{
			if(score>=80)
			{
				System.out.println("Your grade is B.");
			}
			else
			{
				if(score>=70)
				{
					System.out.println("Your grade is C.");
				}
				else
				{
					if(score>=60)
					{
						System.out.println("Your grade is D.");
					}
					else
					{
						System.out.println("Your grade is F.");
					}
				}
			}
		}
    	}
}

```
:::

---
## 題目304
![](https://hackmd.io/_uploads/HJIvS0h9Y.png =850x500)

:::spoiler {state="open"} **自行速解**
```java
import java.util.Scanner;
public class JPA03 
{
   	static Scanner kb=new Scanner(System.in);
    	public static void main(String[] args) 
	{
        	int total = 0;
        	int s = 0;
        	int count = 0;
        	double average;
        
		do
		{
			System.out.print("Please enter meal dollars or enter -1 to stop: ");
			s=kb.nextInt();
	
			if(s!=-1)
			{
				count++;
				total+=s;
			}
				
		}while(s!=-1);

		average=(double)total/count;

        	System.out.println("餐點總費用:"+total);
        	System.out.printf(" %d 道餐點平均費用為: %.2f",count,average);
    	}
}
```
:::

## 題目308
![](https://hackmd.io/_uploads/Hyz2S0hqt.png =850x500)

![](https://hackmd.io/_uploads/S16Rr029Y.png =850x500)

:::spoiler {state="open"} **自行速解**
```java
import java.util.Scanner;
public class JPA03 
{
    	static Scanner keyboard = new Scanner(System.in);
    	static int i = -1;
    	public static void main(String[] args) 
	{
        	int total = 0, s = 0;

		do
		{
			System.out.print("請輸入消費金額，或輸入-1結束:");
			s=keyboard.nextInt();
	
			if(s!=-1)
			{
				total+=s;
			}
		}while(s!=-1);

		System.out.println("電腦週邊總費用："+total);
    	}
}
```
:::

## 題目310
![](https://hackmd.io/_uploads/rk8QLA35Y.png =850x500)

:::spoiler {state="open"} **自行速解**
```java
import java.util.Scanner;
public class JPA03 
{
    	static Scanner keyboard = new Scanner(System.in);
    	public static void main(String[] args) 
	{
		int n;
        	do
		{
			System.out.print("請輸入n的值(n > 0，且為偶數):");
			n=keyboard.nextInt();

		}while(n<=0||n%2!=0);


		int i=2,sum=0;
		
		do
		{
			sum+=i;
			i+=2;
		
		}while(i<=n);
		System.out.println("2+4+...+"+n+"="+sum);
    	}
}
```
:::

---
## 題目402
![](https://hackmd.io/_uploads/rksUIA35t.png =850x500)

![](https://hackmd.io/_uploads/Bk95U025Y.png =850x500)
:::success
**main檔**
```java=1
FT(n,1)
F(n,1)
```

**尾端遞迴FT()**
```java=1
static int FT(int n,int r)
	{
		if(n==0)
		{
			return r;
		}
		else
		{
			return FT(n-1,r*n);
		}
	}
```
**迴圈F()**
```java=1
static int F(int n,int r)
	{
		if(n==0)
		{
			return 1;
		}
		else
		{
			for(int i=1;i<=n;i++)
			{
				r*=i;
			}
			return r;
		}
	}
```
:::
:::spoiler {state="open"} **自行速解**
```java
import java.util.Scanner;
public class JPA04
{
    	static Scanner keyboard = new Scanner(System.in);
    	public static void main(String args[])
	{
		int n=0;
        	do
		{
			System.out.print("Input n(0 <= n <= 16):");
			n=keyboard.nextInt();

			if(n>=0&&n<=16)
			{
				System.out.println(n+" 的階乘(尾端遞迴) = "+FT(n,1));
				System.out.println(n+" 的階乘(迴圈) = "+F(n,1));
			}

		}while(n!=999);
    	}
    	
	static int FT(int n,int r)
	{
		if(n==0)
		{
			return r;
		}
		else
		{
			return FT(n-1,r*n);
		}
	}
	static int F(int n,int r)
	{
		if(n==0)
		{
			return 1;
		}
		else
		{
			for(int i=1;i<=n;i++)
			{
				r*=i;
			}
			return r;
		}
	}
}
```
:::

## 題目406
![](https://hackmd.io/_uploads/ry9aIC2cF.png =850x500)

![](https://hackmd.io/_uploads/SJt0IAncK.png =850x500)

:::warning
**遞迴函式解法 countA()**

```java=1
public static int countA(String str) 
{
	if(str.equals(""))
	{
		return 0;
	}
	else
	{
		if(str.charAt(0)=='A')
		{
			return 1+countA(str.substring(1));
		}
		else
		{
			return countA(str.substring(1));
		}
	}
}
```
:::

:::spoiler {state="open"} **自行速解**
```java
import java.util.Scanner;
public class JPA04 
{
    	static Scanner keyboard = new Scanner(System.in);
    	public static void main(String args[]) 
	{
		for(int i=0;i<2;i++)
		{
      			System.out.print("Input a string: ");
			String s=keyboard.nextLine();
			System.out.println(s+" has "+countA(s)+" As");
		}
    	}
    
    	public static int countA(String str) 
	{
		if(str.equals(""))
		{
			return 0;
		}
		else
		{
			if(str.charAt(0)=='A')
			{
				return 1+countA(str.substring(1));
			}
			else
			{
				return countA(str.substring(1));
			}
		}
      	}
}
```
:::

## 題目408
![](https://hackmd.io/_uploads/SJAxvCh5K.png =850x500)

:::danger
**字串遞迴函式 reverse()**

```java=1
static String reverse(String str)
{
	if(str.equals(""))
	{
		return "";
	}
	else
	{
		return reverse(str.substring(1))+str.substring(0,1);
	}
}
```
:::

:::spoiler {state="open"} **自行速解**
```java
import java.util.Scanner;
public class JPA04 
{
    	static Scanner keyboard = new Scanner(System.in);
    	public static void main(String args[]) 
	{
        	String s, c; 
        	System.out.print("Input a string: ");
        	s = keyboard.nextLine();
        	System.out.printf("%s\n", reverse(s));
        	System.out.print("Input a string: ");
        	s = keyboard.nextLine();
        	System.out.printf("%s\n", reverse(s));
    	}
    
   	static String reverse(String str)
	{
		if(str.equals(""))
		{
			return "";
		}
		else
		{
			return reverse(str.substring(1))+str.substring(0,1);
		}
	}
}
```
:::

---
## 題目504
![](https://hackmd.io/_uploads/Hk37wChcK.png =850x500)

:::spoiler {state="open"} **自行速解**
```java
import java.util.Scanner;
public class JPA05 
{
    	public static Scanner keyboard = new Scanner(System.in);
	public static void main(String[] argv)
	{    
		int[] F=new int[10];

		F[0]=0;
		F[1]=1;
		
		for(int i=2;i<F.length;i++)
		{
			F[i]=F[i-1]+F[i-2];
		}
	
		for(int i=0;i<F.length;i++)
		{
			System.out.println(F[i]);
		}
    	}
}
```
:::

## 題目506
![](https://hackmd.io/_uploads/rJUvwCnqt.png =850x500)

:::spoiler {state="open"} **自行速解**
```java
public class JPA05 
{
    	public static void main(String[] argv) 
	{
       		int sum =0;
       		int A[][][] = {{{1,2,3},{4,5,6}},
                       {{7,8,9},{10,11,12}},
                       {{13,14,15},{16,17,18}},
                       {{19,20,21},{22,23,24}}};
                               
       		for(int i=0;i<A.length;i++)
		{
			for(int j=0;j<A[i].length;j++)
			{
				for(int k=0;k<A[i][j].length;k++)
				{
       					sum+=A[i][j][k];
				}
			}
		}
		System.out.printf("sum = %d\n", sum);
    	}
}
```
:::

## 題目508
![](https://hackmd.io/_uploads/B1puDA3cF.png =850x500)

:::spoiler {state="open"} **自行速解**
```java
public class JPA05 
{
    	public static void main(String[] argv) 
	{
        	int[] data = {2, 4, 3, 5, 7, 6, 9, 1};  // 為排序的資料
        	
		int temp;

		for(int i=0;i<data.length-1;i++)
		{
			for(int j=0;j<data.length-1;j++)
			{
				if(data[j]>data[j+1])
				{
					temp=data[j];
					data[j]=data[j+1];
					data[j+1]=temp;
				}
			}
			
			for(int k=0;k<data.length;k++)
			{
				System.out.print(data[k]+" ");
			}
			System.out.println("");
		}	
    	}
}
```
:::

---
## 題目606
### 606_1
![](https://hackmd.io/_uploads/HyDpcVejt.png =800x500)

**執行結果**
![](https://hackmd.io/_uploads/r1GlZHgst.png)

:::warning
記得要<font color="#EA000">**繼承**</font>，`extends`常忘記打。
:::

==父class==
```java=1
class Teacher
{
	String name;
	int rate;
	int totalHours;

	Teacher(String name,int rate,int totalHours)
	{
		this.name=name;
		this.rate=rate;
		this.totalHours=totalHours;
	}
}
```
==子class==
1. FullTimeTeacher
```java=1
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);
	}
}
```
2. PartTimeTeacher
```java=1
class PartTimeTeacher extends Teacher
{
	PartTimeTeacher(String name,int rate,int totalHours)
	{
		super(name,rate,totalHours);
	}
	double salary()
	{
		return totalHours*rate;
	}
}
```

:::spoiler **整合版**
```java
class Teacher
{
	String name;
	int rate;
	int totalHours;

	Teacher(String name,int rate,int totalHours)
	{
		this.name=name;
		this.rate=rate;
		this.totalHours=totalHours;
	}
}
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 PartTimeTeacher extends Teacher
{
	PartTimeTeacher(String name,int rate,int totalHours)
	{
		super(name,rate,totalHours);
	}
	double salary()
	{
		return totalHours*rate;
	}
}
```
:::

### 606_2
![](https://hackmd.io/_uploads/B1q_mHlsF.png =800x500)

**執行結果**
![](https://hackmd.io/_uploads/BJTsOBgoF.png)
:::danger
:warning: 注意!!!
<font color="EA000">父 class 的 **屬性** 必須 **先宣告**</font>
```java=1
class Manager extends FullTimeTeacher
{
	int rank;    //多一個宣告rank
	
	Manager(String name,int rate,int totalHours,int rank)
	{
		super(name,rate,totalHours); //父class的屬性必須先宣告
		this.rank=rank;             //子class多加的rank，才能宣告
	}
	double salary()
	{
		return super.salary()+rank*500; //salary()+rank*rate
	}
	double getTotalSalary()
	{
		return salary();
	}
}
```
:::
==父class==
```java=1
abstract class Teacher
{
	String name;
	int rate;
	int totalHours;

	Teacher(String name,int rate,int totalHours)
	{
		this.name=name;
		this.rate=rate;
		this.totalHours=totalHours;
	}
	abstract double salary();
	double afterTaxIns()
	{
		return salary()*0.9-100;
	}
}
```

==子class==
3. Manager
```java=1
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; //salary()+rank*rate
	}
	double getTotalSalary()
	{
		return salary();
	}
}
```

:::spoiler **整合版**
```java
abstract class Teacher
{
	String name;
	int rate;
	int totalHours;

	Teacher(String name,int rate,int totalHours)
	{
		this.name=name;
		this.rate=rate;
		this.totalHours=totalHours;
	}
	abstract double salary();
	double afterTaxIns()
	{
		return salary()*0.9-100;
	}
}
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 PartTimeTeacher extends Teacher
{
	PartTimeTeacher(String name,int rate,int totalHours)
	{
		super(name,rate,totalHours);
	}
	double salary()
	{
		return totalHours*rate;
	}
}
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;
	}
	double getTotalSalary()
	{
		return salary();
	}
}
```
:::

### 606_3
![](https://hackmd.io/_uploads/HyW1eUlst.png)

**執行結果**
![](https://hackmd.io/_uploads/BkZMeUxjK.png)

:::warning
**記得 main檔要打**
```java=1
am1.compare(f3);
p1.compare(f3);
```
:::

==父class==
```java=1
abstract class Teacher
{
	String name;
	int rate;
	int totalHours;

	Teacher(String name,int rate,int totalHours)
	{
		this.name=name;
		this.rate=rate;
		this.totalHours=totalHours;
	}
	abstract double salary();
	double afterTaxIns()
	{
		return salary()*0.9-100;
	}

	void compare(Teacher t)
	{
		if(this.salary()>t.salary())
		{
			System.out.println(this.name+" is higher than "+t.name);
		}
		else
		{
			System.out.println(t.name+" is higher than "+this.name);
		}
	}
}
```

:::spoiler **整合版**
```java
abstract class Teacher
{
	String name;
	int rate;
	int totalHours;

	Teacher(String name,int rate,int totalHours)
	{
		this.name=name;
		this.rate=rate;
		this.totalHours=totalHours;
	}
	abstract double salary();
	double afterTaxIns()
	{
		return salary()*0.9-100;
	}

	void compare(Teacher t)
	{
		if(this.salary()>t.salary())
		{
			System.out.println(this.name+" is higher than "+t.name);
		}
		else
		{
			System.out.println(t.name+" is higher than "+this.name);
		}
	}
}
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 PartTimeTeacher extends Teacher
{
	PartTimeTeacher(String name,int rate,int totalHours)
	{
		super(name,rate,totalHours);
	}
	double salary()
	{
		return totalHours*rate;
	}
}
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;
	}
	double getTotalSalary()
	{
		return salary();
	}
}
```
:::

### 606_4
![](https://hackmd.io/_uploads/r1tJLIgiF.png)

**執行結果**
![](https://hackmd.io/_uploads/Sya-8IxjF.png)

:::info
<font color="EA000">**易錯部分**</font>
1. `import java.util.*;`忘記打，因為要用HashMap函式，所以要記得。
2. class Manger的部分，要注意因為afterTaxlns，不是用繼承，而是使用泛型Teacher t，透過t.afterTaxIns()，t會隨main檔變化，舉例來說，當HashMap隨意抓到p1的值，要算出p1.afterTaxIns()，所以會隨著前面new過的值，開始跑父子class。

[參考資料: 一文搞懂泛型 | IT人](https://iter01.com/597570.html)

:::

==子class==
4. TeacherDB

```java=1
class TeacherDB
{
	HashMap<String,Teacher> T =new HashMap<String,Teacher>();

	void store(String name,Teacher t)
	{
		T.put(name,t);
	}

	double totalOfAll()
	{
		double sum=0;
		for(Teacher t : T.values())
		{
			sum+=t.afterTaxIns();
		}
		return sum;
	}
}
```

:::spoiler **整合版**
```java
import java.util.*;
abstract class Teacher
{
	String name;
	int rate;
	int totalHours;

	Teacher(String name,int rate,int totalHours)
	{
		this.name=name;
		this.rate=rate;
		this.totalHours=totalHours;
	}
	abstract double salary();
	double afterTaxIns()
	{
		return salary()*0.9-100;
	}

	void compare(Teacher t)
	{
		if(this.salary()>t.salary())
		{
			System.out.println(this.name+" is higher than "+t.name);
		}
		else
		{
			System.out.println(t.name+" is higher than "+this.name);
		}
	}
}
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 PartTimeTeacher extends Teacher
{
	PartTimeTeacher(String name,int rate,int totalHours)
	{
		super(name,rate,totalHours);
	}
	double salary()
	{
		return totalHours*rate;
	}
}
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;
	}
	double getTotalSalary()
	{
		return salary();
	}
}
class TeacherDB
{
	HashMap<String,Teacher> T =new HashMap<String,Teacher>();

	void store(String name,Teacher t)
	{
		T.put(name,t);
	}

	double totalOfAll()
	{
		double sum=0;
		for(Teacher t : T.values())
		{
			sum+=t.afterTaxIns();
		}
		return sum;
	}
}
```
:::

### 606_5
![](https://hackmd.io/_uploads/Bk7Gb9-sF.png)

**執行結果**
![](https://hackmd.io/_uploads/rJ7E-qbsF.png)
:::success
此題**輸出結果**的**排序不影響**，但需要**名稱**、**薪水**和 ** 完全**符合**。
:::
==子class==
4. TeacherDB **printAllTeacher()**
```java=1
class TeacherDB
{
	HashMap<String,Teacher> T =new HashMap<String,Teacher>();

	void store(String name,Teacher t)
	{
		T.put(name,t);
	}

	double totalOfAll()
	{
		double sum=0;
		for(Teacher t : T.values())
		{
			sum+=t.afterTaxIns();
		}
		return sum;
	}

	void printAllTeacher()
	{
		for(Teacher t:T.values())
		{
			try
			{
				if(t.salary()<1500)
				{
					throw new Exception("**"+t.name+" "+t.salary());
				}
				else
				{
					System.out.println(t.name+" "+t.salary());
				}
			}
			catch(Exception e)
			{
				System.out.println(e.getMessage());
			}
		}
	}
}
```
:::spoiler **整合版**
```java
import java.util.*;
abstract class Teacher
{
	String name;
	int rate;
	int totalHours;

	Teacher(String name,int rate,int totalHours)
	{
		this.name=name;
		this.rate=rate;
		this.totalHours=totalHours;
	}
	abstract double salary();
	double afterTaxIns()
	{
		return salary()*0.9-100;
	}

	void compare(Teacher t)
	{
		if(this.salary()>t.salary())
		{
			System.out.println(this.name+" is higher than "+t.name);
		}
		else
		{
			System.out.println(t.name+" is higher than "+this.name);
		}
	}
}
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 PartTimeTeacher extends Teacher
{
	PartTimeTeacher(String name,int rate,int totalHours)
	{
		super(name,rate,totalHours);
	}
	double salary()
	{
		return totalHours*rate;
	}
}
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;
	}
	double getTotalSalary()
	{
		return salary();
	}
}
class TeacherDB
{
	HashMap<String,Teacher> T =new HashMap<String,Teacher>();

	void store(String name,Teacher t)
	{
		T.put(name,t);
	}

	double totalOfAll()
	{
		double sum=0;
		for(Teacher t : T.values())
		{
			sum+=t.afterTaxIns();
		}
		return sum;
	}

	void printAllTeacher()
	{
		for(Teacher t:T.values())
		{
			try
			{
				if(t.salary()<1500)
				{
					throw new Exception("**"+t.name+" "+t.salary());
				}
				else
				{
					System.out.println(t.name+" "+t.salary());
				}
			}
			catch(Exception e)
			{
				System.out.println(e.getMessage());
			}
		}
	}
}
```
:::

## 題目610
### 610_1
![](https://hackmd.io/_uploads/Skbrzi-jK.png)

**執行結果**
![](https://hackmd.io/_uploads/HJ6Ifj-jt.png)

==父class==
```java=1
class Employee
{
	String id;
	Employee(String id)
	{
		this.id=id;
	}
}
```
==子class==
1. SalaryWorker
```java=1
class SalaryWorker extends Employee
{
	int yearPay;
	SalaryWorker(String id,int yearPay)
	{
		super(id);
		this.yearPay=yearPay;
	}

	double monthPay()
	{
		return yearPay/12.0;
	}	
}
```
2. HourlyWorker
```java=1
class HourlyWorker extends Employee
{
	int hour,hourPay;
	HourlyWorker(String id,int hour,int hourPay)
	{
		super(id);
		this.hour=hour;
		this.hourPay=hourPay;
	}

	double monthPay()
	{
		return hour*hourPay;
	}	
}
```
3. Manager
```java=1
class Manager extends SalaryWorker
{
	int bonus;
	Manager(String id,int yearPay,int bonus)
	{
		super(id,yearPay);
		this.bonus=bonus;
	}
	
	double monthPay()
	{
		return super.monthPay()+bonus;
	}
}
```

:::spoiler **整合版**
```java
class Employee
{
	String id;
	Employee(String id)
	{
		this.id=id;
	}
}
class SalaryWorker extends Employee
{
	int yearPay;
	SalaryWorker(String id,int yearPay)
	{
		super(id);
		this.yearPay=yearPay;
	}

	double monthPay()
	{
		return yearPay/12.0;
	}	
}
class HourlyWorker extends Employee
{
	int hour,hourPay;
	HourlyWorker(String id,int hour,int hourPay)
	{
		super(id);
		this.hour=hour;
		this.hourPay=hourPay;
	}

	double monthPay()
	{
		return hour*hourPay;
	}	
}

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

### 610_2
![](https://hackmd.io/_uploads/rJL1Nn-jY.png)

**執行結果**
![](https://hackmd.io/_uploads/H1cgNhbsF.png)
:::danger
main檔有多compare的部分要處理。
```java=1
sw1.compare(hw1);
hw1.compare(ma1);
```
:::
==父class==
```java=1
abstract class Employee
{
	String id;
	Employee(String id)
	{
		this.id=id;
	}
	
	void compare(Employee e)
	{
		if(this.monthPay()>e.monthPay())
		{
			System.out.println(this.id+"較高");
		}
		else
		{
			System.out.println(e.id+"較高");
		}
	}
	
	abstract double monthPay();
	double monthTaxes()
	{
		return monthPay()*0.15;
	}
}
```
==main檔==
```java=1
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.compare(hw1);
	hw1.compare(ma1);

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

:::spoiler **整合版**
```java
abstract class Employee
{
	String id;
	Employee(String id)
	{
		this.id=id;
	}
	
	void compare(Employee e)
	{
		if(this.monthPay()>e.monthPay())
		{
			System.out.println(this.id+"較高");
		}
		else
		{
			System.out.println(e.id+"較高");
		}
	}
	
	abstract double monthPay();
	double monthTaxes()
	{
		return monthPay()*0.15;
	}
}
class SalaryWorker extends Employee
{
	int yearPay;
	SalaryWorker(String id,int yearPay)
	{
		super(id);
		this.yearPay=yearPay;
	}

	double monthPay()
	{
		return yearPay/12.0;
	}	
}
class HourlyWorker extends Employee
{
	int hour,hourPay;
	HourlyWorker(String id,int hour,int hourPay)
	{
		super(id);
		this.hour=hour;
		this.hourPay=hourPay;
	}

	double monthPay()
	{
		return hour*hourPay;
	}	
}

class Manager extends SalaryWorker
{
	int bonus;
	Manager(String id,int yearPay,int bonus)
	{
		super(id,yearPay);
		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.compare(hw1);
	hw1.compare(ma1);

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

### 610_3
![](https://hackmd.io/_uploads/ry7Lw2WoF.png)

**執行結果**
![](https://hackmd.io/_uploads/rk6PPhbot.png)

==父class==
```java=1
abstract class Employee
{
	String id;
	static int count=0;
	static double totalTax=0;
	
	Employee(String id)
	{
		this.id=id;
		count++;
	}
	
	void compare(Employee e)
	{
		if(this.monthPay()>e.monthPay())
		{
			System.out.println(this.id+"較高");
		}
		else
		{
			System.out.println(e.id+"較高");
		}
	}
	
	abstract double monthPay();
	double monthTaxes()
	{
		totalTax+=monthPay()*0.15;
		return monthPay()*0.15;
	}

	static double getAverageTax()
	{
		return totalTax/count;
	}
}
```
:::spoiler **整合版**
```java
abstract class Employee
{
	String id;
	static int count=0;
	static double totalTax=0;
	
	Employee(String id)
	{
		this.id=id;
		count++;
	}
	
	void compare(Employee e)
	{
		if(this.monthPay()>e.monthPay())
		{
			System.out.println(this.id+"較高");
		}
		else
		{
			System.out.println(e.id+"較高");
		}
	}
	
	abstract double monthPay();
	double monthTaxes()
	{
		totalTax+=monthPay()*0.15;
		return monthPay()*0.15;
	}

	static double getAverageTax()
	{
		return totalTax/count;
	}
}
class SalaryWorker extends Employee
{
	int yearPay;
	SalaryWorker(String id,int yearPay)
	{
		super(id);
		this.yearPay=yearPay;
	}

	double monthPay()
	{
		return yearPay/12.0;
	}	
}
class HourlyWorker extends Employee
{
	int hour,hourPay;
	HourlyWorker(String id,int hour,int hourPay)
	{
		super(id);
		this.hour=hour;
		this.hourPay=hourPay;
	}

	double monthPay()
	{
		return hour*hourPay;
	}	
}

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

### 610_4
![](https://hackmd.io/_uploads/SJJpj2ZiY.png)

**執行結果**
![](https://hackmd.io/_uploads/HydCjhZot.png)

==子class==
4. Management
```java=1
class Management
{
	HashMap<String,Employee> E=new HashMap<String,Employee>();

	void put(String id,Employee e)
	{
		E.put(id,e);
	}

	double afterTax(String id)
	{
		return E.get(id).monthPay()-E.get(id).monthTaxes();
	}
}
```
:::spoiler **整合版**
```java
import java.util.*;
abstract class Employee
{
	String id;
	static int count=0;
	static double totalTax=0;
	
	Employee(String id)
	{
		this.id=id;
		count++;
	}
	
	void compare(Employee e)
	{
		if(this.monthPay()>e.monthPay())
		{
			System.out.println(this.id+"較高");
		}
		else
		{
			System.out.println(e.id+"較高");
		}
	}
	
	abstract double monthPay();
	double monthTaxes()
	{
		totalTax+=monthPay()*0.15;
		return monthPay()*0.15;
	}

	static double getAverageTax()
	{
		return totalTax/count;
	}
}
class SalaryWorker extends Employee
{
	int yearPay;
	SalaryWorker(String id,int yearPay)
	{
		super(id);
		this.yearPay=yearPay;
	}

	double monthPay()
	{
		return yearPay/12.0;
	}	
}
class HourlyWorker extends Employee
{
	int hour,hourPay;
	HourlyWorker(String id,int hour,int hourPay)
	{
		super(id);
		this.hour=hour;
		this.hourPay=hourPay;
	}

	double monthPay()
	{
		return hour*hourPay;
	}	
}

class Manager extends SalaryWorker
{
	int bonus;
	Manager(String id,int yearPay,int bonus)
	{
		super(id,yearPay);
		this.bonus=bonus;
	}
	
	double monthPay()
	{
		return super.monthPay()+bonus;
	}
}
class Management
{
	HashMap<String,Employee> E=new HashMap<String,Employee>();

	void put(String id,Employee e)
	{
		E.put(id,e);
	}

	double afterTax(String id)
	{
		return E.get(id).monthPay()-E.get(id).monthTaxes();
	}
}
```
:::


### 610_5
![](https://hackmd.io/_uploads/S1a1JpZjY.png)

**執行結果**
![](https://hackmd.io/_uploads/rJPbk6-sY.png)

:::danger
main檔記得要填寫**自訂的方法呼叫**。
```java=1
m.totalSalary();
```
:::
==子class==
4. Management
```java=1
class Management
{
	HashMap<String,Employee> E=new HashMap<String,Employee>();

	void put(String id,Employee e)
	{
		E.put(id,e);
	}

	double afterTax(String id)
	{
		return E.get(id).monthPay()-E.get(id).monthTaxes();
	}
	
	void totalSalary()
	{
		double totalSalary=0;

		for(Employee e:E.values())
		{
			totalSalary+=e.monthPay();
		}

		try
		{
			
			if(totalSalary>50000)
			{
				throw new Exception("Total salary exceed limit: "+totalSalary);
			}
			else
			{
				System.out.println("Total salary: "+totalSalary);
			}
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}
}
```
==main 檔==
```java=1
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);
        
	m.totalSalary();
    }
}
```

:::spoiler **整合版**
```java
import java.util.*;
abstract class Employee
{
	String id;
	static int count=0;
	static double totalTax=0;
	
	Employee(String id)
	{
		this.id=id;
		count++;
	}
	
	void compare(Employee e)
	{
		if(this.monthPay()>e.monthPay())
		{
			System.out.println(this.id+"較高");
		}
		else
		{
			System.out.println(e.id+"較高");
		}
	}
	
	abstract double monthPay();
	double monthTaxes()
	{
		totalTax+=monthPay()*0.15;
		return monthPay()*0.15;
	}

	static double getAverageTax()
	{
		return totalTax/count;
	}
}
class SalaryWorker extends Employee
{
	int yearPay;
	SalaryWorker(String id,int yearPay)
	{
		super(id);
		this.yearPay=yearPay;
	}

	double monthPay()
	{
		return yearPay/12.0;
	}	
}
class HourlyWorker extends Employee
{
	int hour,hourPay;
	HourlyWorker(String id,int hour,int hourPay)
	{
		super(id);
		this.hour=hour;
		this.hourPay=hourPay;
	}

	double monthPay()
	{
		return hour*hourPay;
	}	
}

class Manager extends SalaryWorker
{
	int bonus;
	Manager(String id,int yearPay,int bonus)
	{
		super(id,yearPay);
		this.bonus=bonus;
	}
	
	double monthPay()
	{
		return super.monthPay()+bonus;
	}
}
class Management
{
	HashMap<String,Employee> E=new HashMap<String,Employee>();

	void put(String id,Employee e)
	{
		E.put(id,e);
	}

	double afterTax(String id)
	{
		return E.get(id).monthPay()-E.get(id).monthTaxes();
	}
	
	void totalSalary()
	{
		double totalSalary=0;

		for(Employee e:E.values())
		{
			totalSalary+=e.monthPay();
		}

		try
		{
			
			if(totalSalary>50000)
			{
				throw new Exception("Total salary exceed limit: "+totalSalary);
			}
			else
			{
				System.out.println("Total salary: "+totalSalary);
			}
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}
}
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);
        
	m.totalSalary();
    }
}
```
:::

---
:::spoiler 最後更新日期 
>==第一版==[time=2021 12 20 , 12:04 AM][color=#786ff7]

>第二版[time=2021 12 22 , 5:20 PM][color=#ce770c]

>第三版[time=2021 12 23 , 5:54 PM][color=#ce770c]

>**最後版[time=2021 12 25 , 12:38 AM]**[color=#EA0000]
:::