---
# System prepended metadata

title: Java作業 - 10號
tags: [JAVA]

---

# Java作業 - 10號

###### tags: `JAVA`


## e4


```
package exam.e4;

public class Employee {
    private int empno;
    private String name;
    private long salary;
	
    public Employee(int empno, String name, long salary) {
		this.empno = empno;
		this.name = name;
		this.salary = salary;
	}
	public int getEmpno() {
		return empno;
	}
	public String getName() {
		return name;
	}
	public long getSalary() {
		return salary;
	}  
}

```
```
package exam.e4;

public class Manager extends Employee {

	private long bonus;
	
	public Manager(int empno, String name, long salary,long bonus) {
		super(empno, name, salary);
		
		this.bonus = bonus;	
	}

	public long getSalary() {
		
		return super.getSalary()+this.bonus;
	}
}

```


---

## e5

* e5只動到shoppingList.java 所以我就不放另外的class了
```
package exam.e5;

import java.util.ArrayList;
import java.util.List;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class ShoppingCart {
    public static void main(String[] args) {

        //實作1.加入二台Notebook("Asus",30000),Notebook("Acer",20000),Food("Cookie",200)到shoppingList中
        List<Product> shoppingList = new ArrayList<Product>();
        
        Notebook notebook1 = new Notebook("Asus", 30000);
        Notebook notebook2 = new Notebook("Acer", 20000);
        Food food = new Food("Cookie", 200);
        
        shoppingList.add(notebook1);
        shoppingList.add(notebook2);
        shoppingList.add(food);
        
        
        //實作2.利用for迴圈，計算shoppingList中的總金額,並印在Console中
        
        int total = 0;
        for (Product product : shoppingList) {
        	total = total + product.getPrice();
        }
        System.out.println(total);

        
        //實作3.利用for迴圈，計算shoppingList中是Notebook型態的總金額,並印在Console中
        
        int notebookTotal = 0;
        
        for (int i = 0; i < shoppingList.size(); i++) {
        	if(shoppingList.get(i) instanceof Notebook) {
        		notebookTotal = notebookTotal + shoppingList.get(i).getPrice();
        	}else {
        		continue;
        	}
		}
        System.out.println(notebookTotal);
    }
}

```


---
## e6
* 理由同e5，因為只動這個class，所以只放這個class
```
package exam.e6;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;


public class MapTest {
    public static void main(String[] args) {

         People p1 = new People("001",21);
         People p2 = new People("002",33);
         People p3 = new People("003",41);
         //實作1:請實作一個泛型的Map中的key值為people的id，value為people物件，將上述三個people放入Map中
         
         Map<String, People> map = new HashMap<>();
         map.put("001", p1);
         map.put("002", p2);
         map.put("003", p3);

         //實作2:請用for迴圈或者iterator，計算Map中三人的平均年齡，取到小數以下第一位
         
         double average = 0.0;
         
         Set<String> keys = map.keySet();
         
         for(String key : keys) {
        	 average = average + (map.get(key).getAge());
         }
         System.out.println(String.format("%.1f", (average/3)));

    }
}

```

## e7
```
package exam.e7;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class IOTest {
    public static void main(String[] args) {
    		/* 
    		 http://iosnetworkdemo.appspot.com/json.jsp?msg=helloWorld，
    		會回應{"msg":"helloWorld","狀 態":"成功"}，編碼為UTF8 
    	    請利用Java程式呼叫此網址，此將此網址回傳的結果示在Console中(IOTest.java)
    	    程式中的所有 Exception處理必須使用try catch不能直接由方法抛出。
           */

    	try {
			URL url = new URL("http://iosnetworkdemo.appspot.com/json.jsp?msg=helloWorld");
			try(InputStream is = url.openStream();
					InputStreamReader isr = new InputStreamReader(is,"UTF8");
					BufferedReader br = new BufferedReader(isr);){
		
				char[] buffer = new char[1024];
				
				while((br.read(buffer)) != -1) {
					br.read(buffer);
				}
				System.out.println(buffer);
								
			} catch (IOException e) {
				e.printStackTrace();
			}    	
    	} catch (MalformedURLException e) {
			e.printStackTrace();
		}
    	
    	
    }
}

```