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

TQC+ 物件導向程式語言 Java 6 602 電腦零件設計

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

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

題目一:

題目說明:

請開啟檔案 JPD06_1.java,設計「電腦零件設計」程式。為電腦零件設計一個類別架構,請依下列題意完成作答。將 JPD06_1.java 內的 class JPD06_1 修改為 class JPA06_1,將檔案另存為 JPA06_1.java 後編譯為 JPA06_1.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 請撰寫一個 LCD 類別。10 吋的成本是 2000 元,15 吋的成本是2500元,17 吋的成本是 3000 元。

(2) 請撰寫一個 CPU 類別。速度 1.66 的成本是 6000 元,速度 2.2 的成本是 8000元,速度 2.4 的成本是 11000 元。

(3) 請撰寫一個 HD 類別。120G 的成本是2400 元, 160G 的成本是 2800 元。

(4) 請使用以上的零件,撰寫以下的類別。

(5) 請撰寫一個 MiniNote 類別。MiniNote 類別的筆電有一個 10 吋的 LCD,一個速度 1.66 的 CPU 及一個 120G 的 HD。

(6) 請撰寫一個 Note15 類別。Note15 類別的筆電有一個 15 吋的 LCD, 一個速度 2.2 的 CPU 及一個 160G 的 HD。

(7) 這兩型筆電的成本是其零件成本的 1.4 倍。定價則是其零件成本的 2 倍。

(8) 請分別製造一個 MinitNote 筆電及一個 Note15 筆電,呼叫其 getCost 方法及 getPrice 方法,印出其成本及定價的傳回值。

執行結果參考畫面:



JPD06_1.java 檔案下載

程式碼:

class Part {
	int cost;
	
	int getCost() {
		return cost;
	}
}

class LCD extends Part {
	LCD(int size) {
		if(size == 10)
			cost = 2000;
		else if(size == 15)
			cost = 2500;
		else if(size == 17)
			cost = 3000;
	}
}

class CPU extends Part {
	CPU(double speed) {
		if(speed == 1.66)
			cost = 6000;
		else if(speed == 2.2)
			cost = 8000;
		else if(speed == 2.4)
			cost = 11000;
	}
}

class HD extends Part {
	HD(int size) {
		if(size == 120)
			cost = 2400;
		else if(size == 160)
			cost = 2800;
	}
}

class Note {
	LCD lcd;
	CPU cpu;
	HD hd;
	
	Note(LCD lcd, CPU cpu, HD hd) {
		this.lcd = lcd;
		this.cpu = cpu;
		this.hd = hd;
	}
	
	int getPartCost() {
		return lcd.getCost() + cpu.getCost() + hd.getCost();
	}
	
	double getCost() {
		return getPartCost() * 1.4;
	}
	
	double getPrice() {
		return getPartCost() * 2;
	}
}

class MiniNote extends Note {
	MiniNote() {
		super(new LCD(10), new CPU(1.66), new HD(120));
	}
}

class Note15 extends Note {
	Note15() {
		super(new LCD(15), new CPU(2.2), new HD(160));
	}
}

class JPA06_1 {
    public static void main(String args[]){
        MiniNote mininote = new MiniNote();
        System.out.println("MiniNote cost:"+mininote.getCost()+", price:"+mininote.getPrice());
        Note15 note15 = new Note15();
        System.out.println("Note15 cost:"+note15.getCost()+", price:"+note15.getPrice());        
    }
}

JPA06_1.java 檔案下載



題目二:

題目說明:

請開啟 JPD06_2.java,使用第一小題的零件組裝「桌上型電腦」。這家電腦公司想要計算電腦的成本與售價,請依下列題意完成作答。將 JPD06_2.java 内的 class JPD06_2 修改為 class JPA06_2,將檔案另存為 JPA06_2.java 後編譯為 JPA06_2.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 請寫一個 PC 類別,一個 PC 類別的桌上型電腦有一個速度 2.4 的 CPU 及一個 160G 的 HD。製造一個 PC 類別的桌上型電腦的成本是「零件成本」加 500,售價則為「零件成本」的 1.8 倍。

(2) 另請寫一個 MultiPC 類別,一個 MultiPC 的超級電腦,可以有多顆 2.4G 的CPU 及多顆 160G 的 HD。 MultiPC 的成本為其「零件成本」的 1.2 倍,售價為其「零件成本」的 1.8 倍。

(3) 請製造一個 PC 類別的桌上型電腦,一個 2 顆 CPU 及 4 顆 HD 的 MultiPC 及一個 4 顆 CPU 及 8 顆 HD 的 MultiPC。

(4) 分別呼叫其 getCost 方法及 getPrice 方法,印出其傳回值。

執行結果參考畫面:



JPD06_2.java 檔案下載

程式碼:

class Part {
	int cost;
	
	int getCost() {
		return cost;
	}
}

class LCD extends Part {
	LCD(int size) {
		if(size == 10)
			cost = 2000;
		else if(size == 15)
			cost = 2500;
		else if(size == 17)
			cost = 3000;
	}
}

class CPU extends Part {
	CPU(double speed) {
		if(speed == 1.66)
			cost = 6000;
		else if(speed == 2.2)
			cost = 8000;
		else if(speed == 2.4)
			cost = 11000;
	}
}

class HD extends Part {
	HD(int size) {
		if(size == 120)
			cost = 2400;
		else if(size == 160)
			cost = 2800;
	}
}

class Note {
	LCD lcd;
	CPU cpu;
	HD hd;
	
	Note(LCD lcd, CPU cpu, HD hd) {
		this.lcd = lcd;
		this.cpu = cpu;
		this.hd = hd;
	}
	
	int getPartCost() {
		return lcd.getCost() + cpu.getCost() + hd.getCost();
	}
	
	double getCost() {
		return getPartCost() * 1.4;
	}
	
	double getPrice() {
		return getPartCost() * 2;
	}
}

class MiniNote extends Note {
	MiniNote() {
		super(new LCD(10), new CPU(1.66), new HD(120));
	}
}

class Note15 extends Note {
	Note15() {
		super(new LCD(15), new CPU(2.2), new HD(160));
	}
}

abstract class PCandMultiPC {
	CPU cpu;
	HD hd;
	int cpuQuantity;	// CPU 的數量
	int hdQuantity;		// HD 的數量
	
	PCandMultiPC(CPU cpu, HD hd, int cpuQuantity, int hdQuantity) {
		this.cpu = cpu;
		this.hd = hd;
		this.cpuQuantity = cpuQuantity;
		this.hdQuantity = hdQuantity;
	}
	
	int getPartCost() {		// 零件成本
		return cpu.getCost() * cpuQuantity + hd.getCost() * hdQuantity;

	}
	
	abstract double getCost();
	
	double getPrice() {
		return getPartCost() * 1.8;
	}
}

class PC extends PCandMultiPC {
	PC() {
		super(new CPU(2.4), new HD(160), 1, 1);
	}
	
	double getCost() {
		return getPartCost() + 500;
	}
}


class MultiPC extends PCandMultiPC {
	MultiPC(int cpuQuantity, int hdQuantity) {
		super(new CPU(2.4), new HD(160), cpuQuantity, hdQuantity);
	}
	
	double getCost() {
		return getPartCost() * 1.2;
	}
}

class JPA06_2 {
    public static void main(String args[]){
        PC pc = new PC();
        System.out.println("PC cost:"+pc.getCost()+", price:"+pc.getPrice());
        MultiPC multipc1 = new MultiPC(2, 4);
        System.out.println("MultiPC: 2CPU, 4HD, cost:"+multipc1.getCost()+", price:"+multipc1.getPrice());
        MultiPC multipc2 = new MultiPC(4, 8);

        System.out.println("MultiPC: 4CPU, 8HD, cost:"+multipc2.getCost()+", price:"+multipc2.getPrice());
    }
}

JPA06_2.java 檔案下載



題目三:

題目說明:

請開啟 JPD06_3.java,這家工廠為了計算成本,必須比較兩款電腦何者較貴,請依下列題意完成作答。將 JPD06_3.java 内的 class JPD06_3 修改為 class JPA06_3,將檔案另存為 JPA06_3.java 後編譯為 JPA06_3.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 請比較一台 Note15 的筆電和一個 PC 類別的桌上型電腦何者售價較貴。

(2) 請新增一個名為 AIIPC 的類別,於此類別中撰寫 isExpensive 方法,此方法能夠比較何者的售價較貴,並傳回一個布林値,顯示如執行結果參考畫面。

執行結果參考畫面:



JPD06_3.java 檔案下載

程式碼:

class Part {
	int cost;
	
	int getCost() {
		return cost;
	}
}

class LCD extends Part {
	LCD(int size) {
		if(size == 10)
			cost = 2000;
		else if(size == 15)
			cost = 2500;
		else if(size == 17)
			cost = 3000;
	}
}

class CPU extends Part {
	CPU(double speed) {
		if(speed == 1.66)
			cost = 6000;
		else if(speed == 2.2)
			cost = 8000;
		else if(speed == 2.4)
			cost = 11000;
	}
}

class HD extends Part {
	HD(int size) {
		if(size == 120)
			cost = 2400;
		else if(size == 160)
			cost = 2800;
	}
}

abstract class AllPC {
	abstract double getCost();
	abstract double getPrice();
	
	boolean isExpensive(AllPC allpc) {
		return this.getPrice() > allpc.getPrice();
	}
}

class Note extends AllPC {
	LCD lcd;
	CPU cpu;
	HD hd;
	
	Note(LCD lcd, CPU cpu, HD hd) {
		this.lcd = lcd;
		this.cpu = cpu;
		this.hd = hd;
	}
	
	int getPartCost() {
		return lcd.getCost() + cpu.getCost() + hd.getCost();
	}
	
	double getCost() {
		return getPartCost() * 1.4;
	}
	
	double getPrice() {
		return getPartCost() * 2;
	}
}

class MiniNote extends Note {
	MiniNote() {
		super(new LCD(10), new CPU(1.66), new HD(120));
	}
}

class Note15 extends Note {
	Note15() {
		super(new LCD(15), new CPU(2.2), new HD(160));
	}
}

abstract class PCandMultiPC extends AllPC {
	CPU cpu;
	HD hd;
	int cpuQuantity;	// CPU 的數量
	int hdQuantity;		// HD 的數量
	
	PCandMultiPC(CPU cpu, HD hd, int cpuQuantity, int hdQuantity) {
		this.cpu = cpu;
		this.hd = hd;
		this.cpuQuantity = cpuQuantity;
		this.hdQuantity = hdQuantity;
	}
	
	int getPartCost() {		// 零件成本
		return cpu.getCost() * cpuQuantity + hd.getCost() * hdQuantity;
	}
	
	abstract double getCost();
	
	double getPrice() {
		return getPartCost() * 1.8;
	}
}

class PC extends PCandMultiPC {
	PC() {
		super(new CPU(2.4), new HD(160), 1, 1);
	}
	
	double getCost() {
		return getPartCost() + 500;
	}
}

class MultiPC extends PCandMultiPC {
	MultiPC(int cpuQuantity, int hdQuantity) {
		super(new CPU(2.4), new HD(160), cpuQuantity, hdQuantity);
	}
	
	double getCost() {
		return getPartCost() * 1.2;
	}
}

class JPA06_3 {
    public static void main(String args[]) {
        PC pc = new PC();
        Note15 note15 = new Note15();
        
        if(note15.isExpensive(pc))
        	System.out.println("Note15 is more expensive than PC");
        else
        	System.out.println("PC is more expensive than Note15");
    }
}

JPA06_3.java 檔案下載



題目四:

題目說明:

請開啟 JPD06_4.java,電腦公司接到一筆訂單,需計算這筆訂單的總收入,請依下列題意完成作答。將 JPD06_4.java 内的 class JPD06_4 修改為 class JPA06_4,將檔案另存為 JPA06_4.java 後編譯為 JPA06_4.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 請使用 LinkedList 為這家工廠寫一個 Order 類別。

(2) 此筆訂單的資料如下:
MiniNote1 :1 台
Note15:1 台
PC:1 台

(3) 請寫一個 revenue 方法,輸出此訂單的總收入。

執行結果參考畫面:



JPD06_4.java 檔案下載

程式碼:

import java.util.LinkedList;

class Part {
	int cost;
	
	int getCost() {
		return cost;
	}
}

class LCD extends Part {
	LCD(int size) {
		if(size == 10)
			cost = 2000;
		else if(size == 15)
			cost = 2500;
		else if(size == 17)
			cost = 3000;
	}
}

class CPU extends Part {
	CPU(double speed) {
		if(speed == 1.66)
			cost = 6000;
		else if(speed == 2.2)
			cost = 8000;
		else if(speed == 2.4)
			cost = 11000;
	}
}

class HD extends Part {
	HD(int size) {
		if(size == 120)
			cost = 2400;
		else if(size == 160)
			cost = 2800;
	}
}

abstract class AllPC {
	abstract double getCost();
	abstract double getPrice();
	
	boolean isExpensive(AllPC allpc) {
		return this.getPrice() > allpc.getPrice();
	}
}

class Note extends AllPC {
	LCD lcd;
	CPU cpu;
	HD hd;
	
	Note(LCD lcd, CPU cpu, HD hd) {
		this.lcd = lcd;
		this.cpu = cpu;
		this.hd = hd;
	}
	
	int getPartCost() {
		return lcd.getCost() + cpu.getCost() + hd.getCost();
	}
	
	double getCost() {
		return getPartCost() * 1.4;
	}
	
	double getPrice() {
		return getPartCost() * 2;
	}
}

class MiniNote extends Note {
	MiniNote() {
		super(new LCD(10), new CPU(1.66), new HD(120));
	}
}

class Note15 extends Note {
	Note15() {
		super(new LCD(15), new CPU(2.2), new HD(160));
	}
}

abstract class PCandMultiPC extends AllPC {
	CPU cpu;
	HD hd;
	int cpuQuantity;	// CPU 的數量
	int hdQuantity;		// HD 的數量
	
	PCandMultiPC(CPU cpu, HD hd, int cpuQuantity, int hdQuantity) {
		this.cpu = cpu;
		this.hd = hd;
		this.cpuQuantity = cpuQuantity;
		this.hdQuantity = hdQuantity;
	}
	
	int getPartCost() {		// 零件成本
		return cpu.getCost() * cpuQuantity + hd.getCost() * hdQuantity;
	}
	

	abstract double getCost();
	
	double getPrice() {
		return getPartCost() * 1.8;
	}
}

class PC extends PCandMultiPC {
	PC() {
		super(new CPU(2.4), new HD(160), 1, 1);
	}
	
	double getCost() {
		return getPartCost() + 500;
	}
}

class MultiPC extends PCandMultiPC {
	MultiPC(int cpuQuantity, int hdQuantity) {
		super(new CPU(2.4), new HD(160), cpuQuantity, hdQuantity);
	}
	
	double getCost() {
		return getPartCost() * 1.2;
	}
}

class Order {
	LinkedList<AllPC> list;
	
	Order() {
		list = new LinkedList<AllPC>();
	}
	
	void in(AllPC allpc) {
		list.add(allpc);
	}
	
	double revenue() {
		double sum = 0;
		
		for(AllPC allpc : list)
			sum += allpc.getPrice();
		
		return sum;
	}
}

class JPA06_4 {
    public static void main(String args[]){
        Order ord = new Order();
        ord.in(new MiniNote());
        ord.in(new Note15());
        ord.in(new PC());
        System.out.println(ord.revenue());
    }
}

JPA06_4.java 檔案下載



題目五:

題目說明:

請開啟 JPD06_5.java,接續題目四的利潤計算,請使用 Exception 並依下列題意完成作答。將 JPD06_5.java 内的 class JPD06_5 修改為 class JPA06_5,將檔案另存為 JPA06_5.java 後編譯為 JPA06_5.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 此筆訂單的資料如下:
MiniNote1 :1 台
Note15:1 台
PC:1 台

(2) 利潤 = 總收入 – 總成本 (非零件總成本)。

(3) 如果此次總利潤超過 20000,則使用 Exception 印出信息【This order exceeds 20000:xx】,將總利潤代入 xx。

執行結果參考畫面:



JPD06_5.java 檔案下載

程式碼:

import java.util.LinkedList;

class Part {
	int cost;
	
	int getCost() {
		return cost;
	}
}

class LCD extends Part {
	LCD(int size) {
		if(size == 10)
			cost = 2000;
		else if(size == 15)
			cost = 2500;
		else if(size == 17)
			cost = 3000;
	}
}

class CPU extends Part {
	CPU(double speed) {
		if(speed == 1.66)
			cost = 6000;
		else if(speed == 2.2)
			cost = 8000;
		else if(speed == 2.4)
			cost = 11000;
	}
}

class HD extends Part {
	HD(int size) {
		if(size == 120)
			cost = 2400;
		else if(size == 160)
			cost = 2800;
	}
}

abstract class AllPC {
	abstract double getCost();
	abstract double getPrice();
	
	boolean isExpensive(AllPC allpc) {
		return this.getPrice() > allpc.getPrice();
	}
}

class Note extends AllPC {
	LCD lcd;
	CPU cpu;
	HD hd;
	
	Note(LCD lcd, CPU cpu, HD hd) {
		this.lcd = lcd;
		this.cpu = cpu;
		this.hd = hd;
	}
	
	int getPartCost() {
		return lcd.getCost() + cpu.getCost() + hd.getCost();
	}
	
	double getCost() {
		return getPartCost() * 1.4;
	}
	
	double getPrice() {
		return getPartCost() * 2;
	}
}


class MiniNote extends Note {
	MiniNote() {
		super(new LCD(10), new CPU(1.66), new HD(120));
	}
}

class Note15 extends Note {
	Note15() {
		super(new LCD(15), new CPU(2.2), new HD(160));
	}
}

abstract class PCandMultiPC extends AllPC {
	CPU cpu;
	HD hd;
	int cpuQuantity;	// CPU 的數量
	int hdQuantity;		// HD 的數量
	
	PCandMultiPC(CPU cpu, HD hd, int cpuQuantity, int hdQuantity) {
		this.cpu = cpu;
		this.hd = hd;
		this.cpuQuantity = cpuQuantity;
		this.hdQuantity = hdQuantity;
	}
	
	int getPartCost() {		// 零件成本
		return cpu.getCost() * cpuQuantity + hd.getCost() * hdQuantity;
	}
	
	abstract double getCost();
	
	double getPrice() {
		return getPartCost() * 1.8;
	}
}

class PC extends PCandMultiPC {
	PC() {
		super(new CPU(2.4), new HD(160), 1, 1);
	}
	
	double getCost() {
		return getPartCost() + 500;
	}
}

class MultiPC extends PCandMultiPC {
	MultiPC(int cpuQuantity, int hdQuantity) {
		super(new CPU(2.4), new HD(160), cpuQuantity, hdQuantity);
	}
	
	double getCost() {
		return getPartCost() * 1.2;
	}
}


class Order {
	LinkedList<AllPC> list;
	
	Order() {
		list = new LinkedList<AllPC>();
	}
	
	void in(AllPC allpc) {
		list.add(allpc);
	}
	
	double revenue() {
		double sum = 0;
		
		for(AllPC allpc : list)
			sum += allpc.getPrice() - allpc.getCost();
		
		return sum;
	}
}

class JPA06_5 {
    public static void main(String args[]){
    	Order ord = new Order();
    	
    	try {
            ord.in(new MiniNote());
            ord.in(new Note15());
            ord.in(new PC());
            
            if(ord.revenue() > 20000)
            	throw new Exception();
    	} catch(Exception e) {
    		System.out.println("This order exceeds 20000:" + ord.revenue());
    	}
    }
}

JPA06_.java 檔案下載



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




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

可以多點幾次喔~~

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

發表迴響