Github 備份:LINK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 601_1 */ | |
//建立一個共有的方法 | |
class Unit{ | |
int cost ; | |
Unit(){ | |
cost = 0; | |
} | |
public int getCost(){ | |
return cost; | |
} | |
} | |
//引擎方法,繼承Unit,取得成本方法 | |
class Engine extends Unit { | |
Engine(int i){ | |
if(i==1600) | |
cost=20000; | |
else | |
cost= 25000; | |
} | |
} | |
//空調方法,繼承Unit,取得成本方法 | |
class Aircond extends Unit{ | |
Aircond(String s ) { | |
if(s.equals("auto")) | |
cost = 12000; | |
else | |
cost = 10000; | |
} | |
} | |
//音響方法,繼承Unit,取得成本方法 | |
class Sound extends Unit{ | |
Sound(){ | |
cost = 2000; | |
} | |
} | |
public class JPA06_1 { | |
public static void main(String args[]){ | |
//產生一個1600cc引擎物件 | |
Engine e1 = new Engine(1600); | |
System.out.println("1600 cost: " + e1.getCost()); | |
//產生一個2000cc引擎物件 | |
Engine e2 = new Engine(2000); | |
System.out.println("2000 cost: " + e2.getCost()); | |
//產生一個Auto空調物件 | |
Aircond a1 = new Aircond("auto"); | |
System.out.println("Auto: " + a1.getCost()); | |
//產生一個Manual空調物件 | |
Aircond a2 = new Aircond("manual"); | |
System.out.println("Manual: " + a2.getCost()); | |
//產生一個音響物件 | |
Sound s1 = new Sound(); | |
System.out.println("Stereo: " + s1.getCost()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 601_2 */ | |
class Unit{ | |
int cost ; | |
Unit(){cost = 0;} | |
public int getCost(){return cost;} | |
} | |
class Engine extends Unit { | |
Engine(int i){ | |
if(i==1600) | |
cost=20000; | |
else | |
cost= 25000; | |
} | |
} | |
class Aircond extends Unit{ | |
Aircond(String s ){ | |
if(s.equals("Auto")) | |
cost = 12000; | |
else | |
cost = 10000; | |
} | |
} | |
class Sound extends Unit{ | |
Sound(){ | |
cost = 2000; | |
} | |
} | |
//建立一個Car共有的方法 | |
abstract class Car{ | |
Engine e; | |
Aircond a; | |
Car(int i,String s){ | |
e = new Engine(i); | |
a = new Aircond(s); | |
} | |
public abstract double cost(); | |
public double price(){ | |
return cost() *1.2; | |
} | |
} | |
//建立一個基本型車種方法,繼承Car方法 | |
class BasicCar extends Car{ | |
public BasicCar(int i ,String s ){ | |
super(i,s); | |
} | |
public double cost(){ | |
return e.getCost()+a.getCost()+5000; | |
} | |
} | |
//建立一個豪華車種方法,繼承Car方法 | |
class LuxCar extends Car{ | |
public LuxCar(int i ,String s ){ | |
super(i,s); | |
} | |
public double cost(){ | |
return e.getCost()+a.getCost()+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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 601_3 */ | |
class Unit{ | |
int cost ; | |
Unit(){ | |
cost = 0; | |
} | |
public int getCost(){ | |
return cost; | |
} | |
} | |
class Engine extends Unit { | |
Engine(int i){ | |
if(i==1600) | |
cost=20000; | |
else | |
cost= 25000; | |
} | |
} | |
class Aircond extends Unit{ | |
Aircond(String s ){ | |
if(s.equals("Auto")) | |
cost = 12000; | |
else | |
cost = 10000; | |
} | |
} | |
class Sound extends Unit{ | |
Sound(){ | |
cost = 2000; | |
} | |
} | |
abstract class Car{ | |
Engine e; | |
Aircond a; | |
Car(int i,String s){ | |
e = new Engine(i); | |
a = new Aircond(s); | |
} | |
public abstract double cost(); | |
public double price(){ | |
return cost() *1.2; | |
} | |
} | |
class LuxCar extends Car{ | |
public LuxCar(int i ,String s ){ | |
super(i,s); | |
} | |
public double cost(){ | |
return e.getCost()+a.getCost()+10000; | |
} | |
} | |
//建立一個超級豪華車款 | |
class SLuxCar extends Car{ | |
public SLuxCar(int i ,String s ){//原本的引擎和空調 | |
super(i,s);} | |
Sound sc = new Sound(); | |
public double cost(){ | |
return e.getCost()+a.getCost()+10000+sc.getCost(); | |
} | |
public String expensive (LuxCar lc){ | |
if(lc.price()<price()) | |
return "YES!!"; | |
else | |
return "NO!!"; | |
} | |
} | |
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 SLuxCar more expensive than LuxCar? " + llc.expensive(lc)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 601_4 */ | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.Scanner; | |
class Unit{ | |
int cost ; | |
Unit(){cost = 0;} | |
public int getCost(){ | |
return cost; | |
} | |
} | |
class Engine extends Unit { | |
Engine(int i){ | |
if(i==1600) | |
cost=20000; | |
else | |
cost= 25000; | |
} | |
} | |
class Aircond extends Unit{ | |
Aircond(String s ){ | |
if(s.equals("auto")) | |
cost = 12000; | |
else | |
cost = 10000; | |
} | |
} | |
class Sound extends Unit{ | |
Sound(){ | |
cost = 2000; | |
} | |
} | |
abstract class Car{ | |
Engine e; | |
Aircond a; | |
Car(int i,String s){ | |
e = new Engine(i); | |
a = new Aircond(s); | |
} | |
public abstract double cost(); | |
public double price(){ | |
return cost() *1.2; | |
} | |
} | |
class BasicCar extends Car{ | |
public BasicCar(int i ,String s ){ | |
super(i,s); | |
} | |
public double cost(){ | |
return e.getCost()+a.getCost()+5000; | |
} | |
} | |
class LuxCar extends Car{ | |
public LuxCar(int i ,String s ){ | |
super(i,s); | |
} | |
public double cost(){ | |
return e.getCost()+a.getCost()+10000; | |
} | |
} | |
class SLuxCar extends Car{ | |
public SLuxCar(int i ,String s ){ | |
super(i,s); | |
} | |
Sound sc = new Sound(); | |
public double cost(){ | |
return e.getCost()+a.getCost()+10000+sc.getCost(); | |
} | |
} | |
//建立一個倉庫的方法 | |
class Warehouse{ | |
private ArrayList cars; | |
//建立一個加入ArrayList的方法 | |
public void add (Car car){ | |
cars.add(car); | |
} | |
//建構子,初始化ArrayList | |
public Warehouse(){ | |
cars = new ArrayList(); | |
} | |
public double TotalCost(){ | |
double i = 0.0; | |
//在List和Map系列裡面,都會有Iterator可搭配使用,檢視是否有下一個物件 | |
for(Iterator iterator = cars.iterator();iterator.hasNext();){ | |
Car car = (Car)iterator.next(); | |
i = i + car.cost(); | |
} | |
return i; | |
} | |
public double TotalPirce(){ | |
return TotalCost()*1.2; | |
} | |
} | |
public class JPA06_4 { | |
public static void main(String args[]) { | |
Scanner sc = null; | |
//使用try-catch來抓取讀檔失敗的Exception | |
try { | |
//檔案位置,請自行放置 | |
sc = new Scanner(new File("D:\\data.txt")); | |
} catch (FileNotFoundException e) { | |
System.out.println("File not found!"); | |
System.exit(0);//讀檔失敗便關閉 | |
} | |
Warehouse wh = new Warehouse(); | |
boolean si = true ; | |
//使用一個do-while檢視,是否可以繼續執行下去 | |
do{ | |
if(sc.hasNext()) { | |
String s = sc.next(); | |
int i = sc.nextInt(); | |
String s1 = sc.next(); | |
if(s.charAt(0)=='B') | |
//產生一個物件,並加入ArrayList中 | |
wh.add(new BasicCar(i,s1)); | |
if(s.charAt(0)=='S') | |
wh.add(new SLuxCar(i,s1)); | |
if(s.charAt(0)=='L') | |
wh.add(new LuxCar(i,s1)); | |
} else { | |
//如果讀取不到下一行資料,則進到這邊 | |
System.out.println("Total cost: "+(int)wh.TotalCost()); | |
System.out.println("Total cost: "+wh.TotalPirce()); | |
//結束do-while的動作 | |
si=false; | |
} | |
} while(si); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
B 1600 manual | |
L 2000 auto | |
S 2000 auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 601_5 */ | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.Scanner; | |
class Unit{ | |
int cost ; | |
Unit(){cost = 0;} | |
public int getCost(){return cost;} | |
} | |
class Engine extends Unit { | |
Engine(int i){ | |
if(i==1600) | |
cost=20000; | |
else | |
cost= 25000; | |
} | |
} | |
class Aircond extends Unit{ | |
Aircond(String s ) { | |
if(s.equals("auto")) | |
cost = 12000; | |
else | |
cost = 10000; | |
} | |
} | |
class Sound extends Unit{ | |
Sound(){ | |
cost = 2000; | |
} | |
} | |
abstract class Car{ | |
Engine e; | |
Aircond a; | |
Car(int i,String s){ | |
e = new Engine(i); | |
a = new Aircond(s); | |
} | |
public abstract double cost(); | |
public double price(){ | |
return cost() *1.2; | |
} | |
} | |
class BasicCar extends Car{ | |
public BasicCar(int i ,String s ){ | |
super(i,s); | |
} | |
public double cost(){ | |
return e.getCost()+a.getCost()+5000; | |
} | |
} | |
class LuxCar extends Car{ | |
public LuxCar(int i ,String s ){ | |
super(i,s); | |
} | |
public double cost(){ | |
return e.getCost()+a.getCost()+10000; | |
} | |
} | |
class SLuxCar extends Car{ | |
public SLuxCar(int i ,String s ){ | |
super(i,s); | |
} | |
Sound sc = new Sound(); | |
public double cost(){ | |
return e.getCost()+a.getCost()+10000+sc.getCost(); | |
} | |
} | |
class Warehouse{ | |
private ArrayList cars; | |
public void add (Car car){ | |
cars.add(car); | |
} | |
public Warehouse(){ | |
cars = new ArrayList(); | |
} | |
public double TotalCost(){ | |
double i = 0.0; | |
for(Iterator iterator = cars.iterator();iterator.hasNext();){ | |
Car car = (Car)iterator.next(); | |
i = i + car.cost(); | |
} | |
return i; | |
} | |
public double TotalPirce(){ | |
return TotalCost()*1.2; | |
} | |
} | |
public class JPA06_5 { | |
public static void main(String args[]) { | |
Scanner sc = null; | |
try { | |
sc = new Scanner(new File("D://wrongdata.txt")); | |
}catch (FileNotFoundException e) { | |
System.out.println("File not found!"); | |
System.exit(0); | |
} | |
Warehouse wh = new Warehouse(); | |
boolean si = true ; | |
do{ | |
if(sc.hasNext()) { | |
String s = sc.next(); | |
int i = sc.nextInt(); | |
String s1 = sc.next(); | |
//利用switch-case來抓取錯誤的資料 | |
switch(s.charAt(0)){ | |
case 'B': | |
wh.add(new BasicCar(i,s1)); | |
break; | |
case 'S': | |
wh.add(new SLuxCar(i,s1)); | |
break; | |
case 'L': | |
wh.add(new LuxCar(i,s1)); | |
break; | |
default://抓到錯誤便顯示出來,不加入倉庫中 | |
System.out.println("Incorect input data : "+s+" "+i+" "+s1); | |
} | |
} else { | |
System.out.println("Total cost: " + wh.TotalCost()); | |
System.out.println("Total price: " + wh.TotalPirce()); | |
si = false; | |
} | |
}while(si); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
B 1600 manual | |
L 2000 auto | |
S 2000 auto | |
X 2000 manual |
TQC+ JAVA6 試題總覽:LINK
Github 備份:LINK
本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~
請問 package那行是必要的嗎? 是考證照所要的檔案路徑? 還是只是個人用的?
回覆刪除請問一下這有pdf嗎?
回覆刪除