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

TQC+ 物件導向程式語言 Java 6 601 汽車零件設計

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

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

題目一:

題目說明:

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

設計說明:

(1) 請撰寫一個 Engine (引擎) 類別。1600cc 引擎的成本是 20000 元,2000cc 引擎的成本是 25000 元。請製造一個 1600cc 的引擎及一個 2000cc 的引擎,呼叫其 getCost (成本) 方法,印出其傳回值。

(2) 請撰寫一個 Aircond (空調) 類別。Auto 空調的成本是 12000 元,Manual 空調的成本是 10000 元。請製造一個 Auto 的空調及一個 Manual 的空調,呼叫其 getCost (成本) 方法,印出其傳回值。

(3) 請撰寫一個 Sound (音響) 類別。一個音響的成本是 2000 元,請製造一個 Sound 物件,呼叫其 getCost (成本) 方法,印出其傳回值。

執行結果參考畫面:



JPD06_1.java 檔案下載

程式碼:

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

class Engine extends Part {
	Engine(int cc) {
		if(cc == 1600)
			cost = 20000;
		else if(cc == 2000)
			cost = 25000;
	}
}

class Aircond extends Part {
	Aircond(String aircond) {
		if(aircond == "Auto")
			cost = 12000;
		else if(aircond == "Manual")
			cost = 10000;
	}
}

class Sound extends Part {
	Sound() {
		cost = 2000;
	}
}

public class JPA06_1 {
    public static void main(String args[]){
        Engine e1 = new Engine(1600);
        System.out.println("1600 cost: " + e1.getCost());
        Engine e2 = new Engine(2000);
        System.out.println("2000 cost: " + e2.getCost());
     
        Aircond a1 = new Aircond("Auto");
        System.out.println("Auto: " + a1.getCost());
        Aircond a2 = new Aircond("Manual");
        System.out.println("Manual: " + a2.getCost());
    
        Sound s1 = new Sound();
        System.out.println("Stereo: " + s1.getCost());
    }
}

JPA06_1.java 檔案下載



題目二:

題目說明:

請開啟 JPD06_2.java,使用第一小題的零件開一家「汽車工廠」。請為這家汽車工廠設計一個 Car(汽車) 類別架構,以計算車子的成本與售價,請依下列題意完成作答。將 JPD06_2.java 内的 class JPD06_2 修改為 class JPA06_2,將檔案另存為 JPA06_2.java 後編譯為 JPA06_2.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 這家汽車工廠生產 BasicCar (基本型) 及 LuxCar (豪華車款) 兩種汽車。

(2) 基本型的成本是:1600cc 引擎成本 + Manual 空調成本 + 5000 元。

(3) 豪華車款的成本是:2000cc 引擎成本+ Auto 空調成本 + 10000 元。

(4) 這兩型的售價都是成本的 1.2 倍。

(5) 分別計算兩款車子的 cost (成本) 與 price (售價),顯示如執行結果參考畫面。

執行結果參考畫面:



JPD06_2.java 檔案下載

程式碼:

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

class Engine extends Part {
	Engine(int cc) {
		if(cc == 1600)
			cost = 20000;
		else if(cc == 2000)
			cost = 25000;
	}
}

class Aircond extends Part {
	Aircond(String aircond) {
		if(aircond == "Auto")
			cost = 12000;
		else if(aircond == "Manual")
			cost = 10000;
	}
}

class Sound extends Part {
	Sound() {
		cost = 2000;
	}
}

abstract class Car {
	Engine engine;
	Aircond aircond;
	
	Car(Engine engine, Aircond aircond) {
		this.engine = engine;
		this.aircond = aircond;
	}
	
	int getPartCost() {		// 取得引擎加空調的成本
		return engine.getCost() + aircond.getCost();
	}
	
	abstract double cost();
	
	double price() {
		return cost() * 1.2;
	}
}

class BasicCar extends Car {
	BasicCar(int cc, String aircond) {
		super(new Engine(cc), new Aircond(aircond));
	}
	
	double cost() {
		return getPartCost() + 5000;
	}
}

class LuxCar extends Car {
	LuxCar(int cc, String aircond) {
		super(new Engine(cc), new Aircond(aircond));
	}
	
	double cost() {
		return getPartCost() + 10000;
	}
}

public class JPA06_2 {
    public static void main(String args[]){
        BasicCar bc = new BasicCar(1600,"Manual");
        System.out.println("Basic cost: " + bc.cost());
        System.out.println("Basic price: " + bc.price());
    
        LuxCar lc = new LuxCar(2000,"Auto");
        System.out.println("Lux cost: " + lc.cost());
        System.out.println("Lux price: " + lc.price());
    }
}

JPA06_2.java 檔案下載



題目三:

題目說明:

請開啟 JPD06_3.java,這家工廠決定增加一「超級豪華型車款」,請依下列題意完成作答。將 JPD06_3.java 内的 class JPD06_3 修改為 class JPA06_3,將檔案另存為 JPA06_3.java 後編譯為 JPA06_3.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) SLuxCar (超級豪華型車款) 的成本 = 豪華車款的成本 + 音響成本。

(2) 這型車的售價也是成本的 1.2 倍。請為這型車設計一個 SLuxCar 類別,並計算超級豪華型的成本與售價。

(3) 也請為這家工廠增加一個方法,此方法能夠比較 SLuxCar (超級豪華型車款) 是否比 LuxCar (豪華型車款) 的售價較貴。

(4) 顯示如執行結果參考畫面。

執行結果參考畫面:



JPD06_3.java 檔案下載

程式碼:

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

class Engine extends Part {
	Engine(int cc) {
		if(cc == 1600)
			cost = 20000;
		else if(cc == 2000)
			cost = 25000;
	}
}


class Aircond extends Part {
	Aircond(String aircond) {
		if(aircond == "Auto")
			cost = 12000;
		else if(aircond == "Manual")
			cost = 10000;
	}
}

class Sound extends Part {
	Sound() {
		cost = 2000;
	}
}


abstract class Car {
	Engine engine;

	Aircond aircond;	
	
	Car(Engine engine, Aircond aircond) {
		this.engine = engine;
		this.aircond = aircond;		
	}
	
	int getPartCost() {		// 取得引擎加空調的成本
		return engine.getCost() + aircond.getCost();
	}
	
	abstract double cost();
	
	double price() {
		return cost() * 1.2;
	}
	
	String expensive(Car car) {
		return this.price() > car.price() ? "Yes!!" : "No!!";
	}
}


class BasicCar extends Car {
	BasicCar(int cc, String aircond) {
		super(new Engine(cc), new Aircond(aircond));
	}
	
	double cost() {
		return getPartCost() + 5000;
	}
}



class LuxCar extends Car {
	LuxCar(int cc, String aircond) {
		super(new Engine(cc), new Aircond(aircond));
	}
	
	double cost() {
		return getPartCost() + 10000;
	}
}

class SLuxCar extends Car {
	Sound sound = new Sound();

	
	SLuxCar(int cc, String aircond) {
		super(new Engine(cc), new Aircond(aircond));
	}
	
	double cost() {
		return getPartCost() + 10000 + sound.getCost();
	}
}


public class JPA06_3 {
    public static void main(String args[]) {
        SLuxCar llc = new SLuxCar(2000,"Auto");
        System.out.println("SLux cost: "  + llc.cost());
        System.out.println("SLux price: " + llc.price());
        LuxCar lc = new LuxCar(2000,"Auto");
        System.out.println("Is llc more expensive than lc? " + llc.expensive(lc));
   }
}

JPA06_3.java 檔案下載



題目四:

題目說明:

請開啟 JPD06_4.java,建立一個 Warehouse (倉庫) 類別,讀取 data.txt 内,將此三臺車放入此倉庫中,請依下列題意完成作答。將 JPD06_4.java 内的 class JPD06_4 修改為 class JPA06_4,將檔案另存為 JPA06_4.java 後編譯為 JPA06_4.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 請建立一個 Warehouse (倉庫) 類別,讀取 data.txt 内三台車子的資料,每一行即代表一台車,將此三台車放入此倉庫中。

(2) data.txt 内有三筆資料,分別代表 B (Basic 基本款)、L (LuxCar 豪華型車款)、S (SLuxCar 超級豪華型車款) 的成本,超級豪華車款的成本需再加上音響成本,内容如下:

車款引擎 (cc)空調
B1600Manual
L2000Auto
S2000Auto

(3) 一個倉庫物件内有一個 ArrayList 物件,這個 ArrayList 可以儲存車子。

(4) 請計算這個倉庫内,此三台車的總庫存成本及總售價 (成本的12倍),顯示如執行結果參考畫面。

(5) 若找不到 data.txt 檔案,則顯示【File not found!】。

執行結果參考畫面:



JPD06_4.java 檔案下載

data.txt 檔案下載

程式碼:

import java.util.*;
import java.io.*;

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

class Engine extends Part {
	Engine(int cc) {
		if(cc == 1600)
			cost = 20000;
		else if(cc == 2000)
			cost = 25000;
	}
}

class Aircond extends Part {
	Aircond(String aircond) {
		if(aircond.equals("Auto"))
			cost = 12000;
		else if(aircond.equals("Manual"))
			cost = 10000;
	}
}

class Sound extends Part {
	Sound() {
		cost = 2000;
	}
}

abstract class Car {
	Engine engine;
	Aircond aircond;	
	
	Car(Engine engine, Aircond aircond) {
		this.engine = engine;
		this.aircond = aircond;		
	}
	
	int getPartCost() {		// 取得引擎加空調的成本
		return engine.getCost() + aircond.getCost();
	}
	
	abstract double cost();
	
	double price() {
		return cost() * 1.2;
	}
	
	String expensive(Car car) {
		return this.price() > car.price() ? "Yes!!" : "No!!";
	}
}

class BasicCar extends Car {
	BasicCar(int cc, String aircond) {
		super(new Engine(cc), new Aircond(aircond));
	}
	
	double cost() {
		return getPartCost() + 5000;
	}
}

class LuxCar extends Car {
	LuxCar(int cc, String aircond) {
		super(new Engine(cc), new Aircond(aircond));
	}
	
	double cost() {
		return getPartCost() + 10000;
	}
}

class SLuxCar extends Car {
	Sound sound = new Sound();
	
	SLuxCar(int cc, String aircond) {
		super(new Engine(cc), new Aircond(aircond));
	}
	
	double cost() {
		return getPartCost() + 10000 + sound.getCost();
	}
}

class Warehouse {
	ArrayList cars;
	Car car;
	
	Warehouse() {
		cars = new ArrayList();
	}
	
	void add(Car car) {
		cars.add(car);
	}
	
	int TotalCost() {
		int totalcost = 0;
		
		for(int i = 0;i < cars.size();i++) {
			car = (Car) cars.get(i);
			totalcost += car.cost();			
		}
		// 第二種寫法
		/*for(Iterator iterator = cars.iterator();iterator.hasNext();) {
			car = (Car) iterator.next();			
			totalcost += car.cost();
		}*/
		
		return totalcost;
	}
	
	double TotalPirce() {
		return TotalCost() * 1.2;
	}
}

public class JPA06_4 {
    public static void main(String args[]) {
        Scanner sc = null;
        try {
            sc = new Scanner(new File("data.txt"));
        } catch(FileNotFoundException e) {
        	System.out.println("File not found!");
        	System.exit(0);
        }
        
        Warehouse wh = new Warehouse();
        
        boolean b = true;
        
        do {
        	if(sc.hasNext()) {
        		String model = sc.next();	// 車款
        		int cc = sc.nextInt();		// 引擎
        		String aircond = sc.next();	// 空調
        		
        		// 提供兩種判斷寫法;最上方的 Aircond 類別,要改成用 equals 判斷,否則不會計算到空調的成本。
        		if(model.equals("B"))
        			wh.add(new BasicCar(cc, aircond));
        		else if(model.charAt(0) == 'L')
        			wh.add(new LuxCar(cc, aircond));
        		else if(model.charAt(0) == 'S')
        			wh.add(new SLuxCar(cc, aircond));
        	}
        	else {
        		System.out.println("Total cost: " + wh.TotalCost());   
                System.out.println("Total price: " + wh.TotalPirce());
                b = false;
        	}
        } while(b);
    }	
}

JPA06_.java 檔案下載



題目五:

題目說明:

請開啟 JPD06_5.java,請由 wrongdata.txt 讀入車子的資料,然後放入一個由 Warehouse (倉庫) 類別製造的倉庫,請依下列題意完成作答。將 JPD06_5.java 内的 class JPD06_5 修改為 class JPA06_5,將檔案另存為 JPA06_5.java 後編譯為 JPA06_5.class,所有題目中有使用到的類別也請編譯後一併儲存。

設計說明:

(1) 請建立一個倉庫 (Warehouse) 類別,讀取 wrongdata.txt 内三台車子的資料,每一行即代表一台車,將此三台車放入此倉庫中。

(2) data.txt 内有四筆資料,分別代表 B (Basic 基本款)、L (Lux Car 豪華車款)、S ( SLuxCar 超級豪華車款) 的成本,超級豪華車款的成本需再加上音響成本,最後一筆為不正確的資料 (沒有 X 款的車型),內容如下:

車款引擎 (cc)空調
B1600Manual
L2000Auto
S2000Auto
X2000Manual

(3) 一個倉庫物件内有一個 ArrayList 物件,這個 ArrayList 可以儲存車子。

(4) wrongdata.txt 有不正確的資料 (沒有 X 款的車型),請使用自定例外類別處理,將不正確的資料印出。並計算這個倉庫内的總庫存成本及總售價 (成本的 1.2 倍),顯示如執行結果參考畫面。

(5) 若找不到 wrongdata.txt 檔案,則顯示【File not found!】。

執行結果參考畫面:



JPD06_5.java 檔案下載

wrongdata.txt 檔案下載

程式碼:

import java.util.*;
import java.io.*;

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

class Engine extends Part {
	Engine(int cc) {
		if(cc == 1600)
			cost = 20000;
		else if(cc == 2000)
			cost = 25000;
	}
}

class Aircond extends Part {
	Aircond(String aircond) {
		if(aircond.equals("Auto"))
			cost = 12000;
		else if(aircond.equals("Manual"))
			cost = 10000;
	}
}

class Sound extends Part {
	Sound() {
		cost = 2000;
	}
}


abstract class Car {
	Engine engine;
	Aircond aircond;	
	
	Car(Engine engine, Aircond aircond) {
		this.engine = engine;
		this.aircond = aircond;		
	}
	
	int getPartCost() {		// 取得引擎加空調的成本
		return engine.getCost() + aircond.getCost();
	}
	
	abstract double cost();
	
	double price() {
		return cost() * 1.2;
	}
	
	String expensive(Car car) {
		return this.price() > car.price() ? "Yes!!" : "No!!";
	}
}

class BasicCar extends Car {
	BasicCar(int cc, String aircond) {
		super(new Engine(cc), new Aircond(aircond));
	}
	
	double cost() {
		return getPartCost() + 5000;
	}
}

class LuxCar extends Car {
	LuxCar(int cc, String aircond) {
		super(new Engine(cc), new Aircond(aircond));
	}
	
	double cost() {
		return getPartCost() + 10000;
	}
}

class SLuxCar extends Car {
	Sound sound = new Sound();
	
	SLuxCar(int cc, String aircond) {
		super(new Engine(cc), new Aircond(aircond));
	}
	
	double cost() {
		return getPartCost() + 10000 + sound.getCost();
	}
}


class Warehouse {
	ArrayList cars;
	Car car;
	
	Warehouse() {
		cars = new ArrayList();
	}
	
	void add(Car car) {
		cars.add(car);
	}
	
	double TotalCost() {
		int totalcost = 0;
		
		for(int i = 0;i < cars.size();i++) {
			car = (Car) cars.get(i);
			totalcost += car.cost();			
		}
		// 第二種寫法
		/*for(Iterator iterator = cars.iterator();iterator.hasNext();) {
			car = (Car) iterator.next();			
			totalcost += car.cost();
		}*/
		
		return totalcost;
	}
	
	double TotalPirce() {
		return TotalCost() * 1.2;
	}
}

class ModelNotFoundException extends RuntimeException {		// 自訂的例外類別一定要是 Throwable 的衍生類別
	ModelNotFoundException(String model) {
		System.out.println("Incorrent input data: " + model + " 2000 manual");
	}
}


public class JPA06_5 {
    public static void main(String args[]) {
        Scanner sc = null;
        try {
            sc = new Scanner(new File("wrongdata.txt"));
        } catch (FileNotFoundException e) {
            System.out.println ("File not found!");
            // Stop program if no file found
            System.exit (0);
        }
        
        Warehouse wh = new Warehouse();
        
        boolean b = true;
        
        do {
        	if(sc.hasNext()) {
        		String model = sc.next();	// 車款
        		int cc = sc.nextInt();		// 引擎
        		String aircond = sc.next();	// 空調
        		
        		// 提供兩種判斷寫法;最上方的 Aircond 類別,要改成用 equals 判斷,否則不會計算到空調的成本。
        		try {
        			if(model.equals("B"))
        				wh.add(new BasicCar(cc, aircond));
        			else if(model.charAt(0) == 'L')
        				wh.add(new LuxCar(cc, aircond));
        			else if(model.charAt(0) == 'S')
        				wh.add(new SLuxCar(cc, aircond));
        			else
        				throw new ModelNotFoundException(model);
        		} catch(ModelNotFoundException e) {}
        	}
        	else {
        		System.out.println("Total cost: " + wh.TotalCost());   
                System.out.println("Total price: " + wh.TotalPirce());
                b = false;
        	}
        } while(b);
    }	
}

JPA06_5.java 檔案下載



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




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

可以多點幾次喔~~

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

發表迴響