---
# System prepended metadata

title: 'Java第四週[JPA109、107、107_2、110 函數、副程式的運用、物件導向]'
tags: [複習用, JAVA課堂學習, 高科大]

---

# Java第四週[JPA109、107、107_2、110 函數、副程式的運用、物件導向]


#### 複習用

## 開始解題(JPA09、7、7_2、10):

### 第一題(JPA09)-變數範圍[函數、副程式的運用]:
#### 函數寫法(副程式)
```java=1
public static int fun()        //int 有回傳值(要寫return)
public static void test()      //void 無回傳值
```
![](https://i.imgur.com/6Zjcvwq.png)
JPD0範本:
```java
public class JPD01
{   
	public static int adder (__________________)
	{ 
        	return _______________;                                    
    	} 
   
	public static int gameRating (int s, int a, int e) 
	{ 
        	return adder(__________________);                  
    	} 
  
	public static void main (String argv[]) 
	{ 
        	int skill = 6, action = 9, excitment = 8, result; 
        	result = gameRating(skill, action, excitment); 
        	System.out.print("The rating of the game is "); 
        	System.out.println(result);
    	} 
}
```
解答:
```java
public class JPA09
{   
	public static int adder (int skill,int action,int excitement) //解
	{ 
        	return skill+action+excitement; //解                        
    	} 
   
	public static int gameRating (int s, int a, int e)
	{ 
        	return adder(s,a,e); //解                           
    	} 
  
	public static void main (String argv[]) 
	{ 
        	int skill = 6, action = 9, excitment = 8, result; 
        	result = gameRating(skill, action, excitment); 
        	System.out.print("The rating of the game is "); 
        	System.out.println(result);
    	} 
}
```
---
### 第二題(JPA07)-運動成績[物件導向]:
#### 函數寫法(副程式)
```java=1
public static int fun()        //int 有回傳值(要寫return)
public static void test()      //void 無回傳值
```
#### 物件導向

[參考資料1](http://kaiching.org/pydoing/java-guide/unit-12-static.html)
[參考資料2](http://in.ncu.edu.tw/w3meng/train96/mengtrain96/lecture/TextBook_JAVAProgram.pdf)
#### class用法
記得class要在外面宣告，而非放在JPA07的class裡

![](https://i.imgur.com/yG7EIMn.png)
JPD0範本:
```java
public class JPD01
{
	public static void main(String argv[]) 
	{
		int action = 1, skill = 2, teamgame = 3;

               System.out.println("The basketball grade is " + Basketball.calGrade(_____________________));
               System.out.println("The baseball grade is " + _______________(________________________));
    	}
}

class Basketball 
{
    	......   
	{
        	return a + s + t;
    	}
}

class Baseball 
{
    	......   
	{
        	return 10 + s + t;
  	}
}
```
解答:
```java
public class JPA07     //JPA07 class類別
{
	public static void main(String argv[]) 
	{
        	int action = 1, skill = 2, teamgame = 3;

		System.out.println("The basketball grade is " + Basketball.calGrade(action,skill,teamgame));
        	System.out.println("The baseball grade is " + Baseball.calGrade(skill,teamgame));
    	}
}

class Basketball  //建立Basketball類別   //建立類別，並宣告型態
{
	static int calGrade(int a,int s,int t) //設函數
	{
        	return a + s + t;
    	}
}

class Baseball  //建立Baseball類別
{
	static int calGrade(int s,int t)    //設函數
	{
   	     return 10 + s + t;
    	}
}
```
---
### 第三題(JPA07_2)-運動成績[物件導向的運用]:
#### 若要輸出abc的結果?要如何更改程式
![](https://i.imgur.com/iTv70ne.png)
解答:
```java
public class JPA072
{
	public static void main(String argv[]) 
	{
    		int action = 1, skill = 2, teamgame = 3;

   		System.out.println("The basketball grade is " + Basketball.calGrade(action,skill,teamgame));

		Basketball.abc=3;                    //主程式設abc=3
		System.out.println(Basketball.abc); //輸出abc=3
    		
		System.out.println("The baseball grade is " + Baseball.calGrade(skill,teamgame));

		System.out.println(Basketball.abc);//再次輸出abc的結果
   	}
}

class Basketball 
{	
	static int abc;	//在籃球類別中宣告一個整數abc
	static int calGrade(int a,int s,int t)
	{
	        return a + s + t;
	}
}

class Baseball 
{
    	static int calGrade(int s,int t)   
	{	
		Basketball.abc=7;  //在baseball類別中的副程式設abc=7
        	return 10 + s + t;
    	}
}
```
---
### 第四題(JPA010)-圖形面積[函數(副程式)運用]:
#### 函數寫法(副程式)
```java=1
public static int fun()        //int 有回傳值(要寫return)
public static void test()      //void 無回傳值
```
#### 面積公式
1. 圓形 [ $r^2*pi$ ] (pi=3.1415926)
2. 三角形 [ 底*高/2.0 ]
3. 方形 [ 長*寬 ]
4. 梯形 [ (上底+下底)*高/2.0 ]
* 總面積相加即可
* double 要記得/2.0

![](https://i.imgur.com/1d7XzZO.png)
![](https://i.imgur.com/Igd3Nww.png)
JPD0範本:
```java
import java.util.Scanner;
public class JPD01 
{
	public static void main(String args[]) 
	{
        	double totalarea;
        	System.out.printf("圓形面積為：%f \n",calCircle(5));
        	System.out.printf("三角形面積為：%f \n",________________);
        	System.out.printf("方形面積為：%f \n",_______________);
        	totalarea = __________________________________________;
        	System.out.printf("此圖形面積為：%f \n",totalarea);
    	}
	_______ ______ calCircle(_____) 
	{
        
   	}
	______ ____ calTriangle(_____________) 
	{
        
    	}
    	______ ____ calRectangle(______________) 
	{
        
    	}
}
```
解答:
```java
import java.util.Scanner;
public class JPA10 
{
	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);

                //老師額外出題
		System.out.printf("梯形面積為：%f \n",calTrapezoid(5,10,4));
    	}
	static double calCircle(int r)     //圓形
	{	final double pi=3.1415926;
        	return Math.pow(r,2)*pi;
   	}
	static double calTriangle(int u,int h)	//三角形
	{
        	return u*h/2.0;
    	}
    	static double calRectangle(int m,int n)	//方形
	{
        	return (double)m*n;  //前面加(double)，可以強制切換型態
    	}
	static double calTrapezoid(int u1,int u2,int h) //額外出題-梯形
	{
        	return (u1+u2)*h/2.0;
    	}
}
```
---
最後編輯時間:2021/4/7 11:16pm.
###### tags: `JAVA課堂學習` `複習用` `高科大`