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 - 602_1 */ | |
//建立一個共有的方法,以供其他方法繼承用 | |
class Unit { | |
int cost; | |
Unit(){cost = 0;} | |
public int getcost(){ | |
return cost; | |
} | |
} | |
//建立LCD的方法,內有初始化cost | |
class LCD extends Unit{ | |
LCD(int i){ | |
if(i==10) | |
cost=2000; | |
else if(i==15) | |
cost=2500; | |
else | |
cost=3000; | |
} | |
} | |
//建立CPU的方法,內有初始化cost | |
class CPU extends Unit{ | |
CPU(double d ){ | |
if(d==1.66) | |
cost=6000; | |
if(d==2.2) | |
cost=8000; | |
if(d==2.4) | |
cost=11000; | |
} | |
} | |
//建立HD的方法,內有初始化cost | |
class HD extends Unit { | |
HD(String s){ | |
if(s=="120G") | |
cost = 2400; | |
else | |
cost = 2800; | |
} | |
} | |
//建立一個Note抽象類別 | |
abstract class Note{ | |
double cost; | |
LCD l; | |
CPU c; | |
HD dd; | |
Note(int i,double d,String s){ | |
l = new LCD(i); | |
c = new CPU(d); | |
dd = new HD(s); | |
} | |
abstract public double getCost(); | |
abstract public double getPrice(); | |
} | |
//建立一個小筆電的方法 | |
class MiniNote extends Note{ | |
//建構子,將值傳遞給父親 | |
MiniNote(){ | |
super(10,1.66,"120G"); | |
//計算cost值,其中使用的物件由父親繼承下來 | |
cost = l.getcost()+c.getcost()+dd.getcost(); | |
} | |
public double getCost(){ | |
return cost*1.4; | |
} | |
public double getPrice(){ | |
return cost*2.0; | |
} | |
} | |
//建立一個15吋筆電的方法 | |
class Note15 extends Note{ | |
Note15(){ | |
super(15,2.2,"160G"); | |
cost = l.getcost()+c.getcost()+dd.getcost(); | |
} | |
public double getCost(){ | |
return cost*1.4; | |
} | |
public double getPrice(){ | |
return cost*2.0; | |
} | |
} | |
class JPA06_1 { | |
public static void main(String args[]){ | |
//產生小筆電的物件 | |
MiniNote mininote = new MiniNote(); | |
System.out.println("MiniNote cost:"+mininote.getCost()+", price:"+mininote.getPrice()); | |
//產生15吋筆電的物件 | |
Note15 note15 = new Note15(); | |
System.out.println("Note15 cost:"+note15.getCost()+", price:"+note15.getPrice()); | |
} | |
} |
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 - 602_2 */ | |
class Unit{ | |
int cost; | |
Unit(){ | |
cost = 0; | |
} | |
public int getcost(){ | |
return cost; | |
} | |
} | |
class LCD extends Unit{ | |
LCD(int i){ | |
if(i==10) | |
cost=2000; | |
else if(i==15) | |
cost=2500; | |
else | |
cost=3000; | |
} | |
} | |
class CPU extends Unit{ | |
CPU(double d ){ | |
if(d==1.66) | |
cost=6000; | |
if(d==2.2) | |
cost=8000; | |
if(d==2.4) | |
cost=11000; | |
} | |
} | |
class HD extends Unit{ | |
HD(String s){ | |
if(s=="120G") | |
cost = 2400; | |
else | |
cost = 2800; | |
} | |
} | |
//建立公用的方法,以供繼承 | |
abstract class PCandMultiPC{ | |
double cost; | |
CPU l; | |
HD c; | |
PCandMultiPC() { | |
l = new CPU(2.4); | |
c = new HD("160G"); | |
} | |
abstract public double getCost(); | |
abstract public double getPrice(); | |
} | |
class PC extends PCandMultiPC{ | |
public double getCost(){ | |
return (l.getcost()+c.getcost()+500); | |
} | |
public double getPrice(){ | |
return (l.getcost()+c.getcost())*1.8; | |
} | |
} | |
class MultiPC extends PCandMultiPC{ | |
double toa; | |
MultiPC(int a,int b) { | |
toa = a*l.getcost()+b*c.getcost(); | |
} | |
public double getCost(){ | |
return (toa*1.2); | |
} | |
public double getPrice(){ | |
return (toa*1.8); | |
} | |
} | |
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()); | |
} | |
} |
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 - 602_3 */ | |
class Unit{ | |
int cost; | |
Unit(){ | |
cost = 0; | |
} | |
public int getcost(){ | |
return cost; | |
} | |
} | |
class LCD extends Unit{ | |
LCD(int i){ | |
if(i==10) | |
cost=2000; | |
else if(i==15) | |
cost=2500; | |
else | |
cost=3000; | |
} | |
} | |
class CPU extends Unit{ | |
CPU(double d ){ | |
if(d==1.66) | |
cost=6000; | |
if(d==2.2) | |
cost=8000; | |
if(d==2.4) | |
cost=11000; | |
} | |
} | |
class HD extends Unit{ | |
HD(String s){ | |
if(s=="120G") | |
cost = 2400; | |
else | |
cost = 2800; | |
} | |
} | |
abstract class Note{ | |
double cost; | |
LCD l; | |
CPU c; | |
HD dd; | |
Note(int i,double d,String s){ | |
l = new LCD(i); | |
c = new CPU(d); | |
dd = new HD(s); | |
} | |
abstract public double getCost(); | |
abstract public double getPrice(); | |
} | |
class MiniNote extends Note{ | |
MiniNote(){ | |
super(10,1.66,"120G"); | |
cost = l.getcost()+c.getcost()+dd.getcost(); | |
} | |
public double getCost(){ | |
return cost*1.4; | |
} | |
public double getPrice(){ | |
return cost*2.0; | |
} | |
} | |
class Note15 extends Note{ | |
Note15(){ | |
super(15,2.2,"160G"); | |
cost = l.getcost()+c.getcost()+dd.getcost(); | |
} | |
public double getCost(){ | |
return cost*1.4; | |
} | |
public double getPrice(){ | |
return cost*2.0; | |
} | |
} | |
abstract class PCandMultiPC{ | |
double cost; | |
CPU l; | |
HD c; | |
PCandMultiPC(){ | |
l = new CPU(2.4); | |
c = new HD("160G"); | |
} | |
abstract public double getCost(); | |
abstract public double getPrice(); | |
} | |
class PC extends PCandMultiPC{ | |
public double getCost(){ | |
return (l.getcost()+c.getcost()+500); | |
} | |
public double getPrice(){ | |
return (l.getcost()+c.getcost())*1.8; | |
} | |
} | |
class MultiPC extends PCandMultiPC{ | |
double total; | |
MultiPC(int a,int b){ | |
total = a*l.getcost()+b*c.getcost(); | |
} | |
public double getCost(){ | |
return (total*1.2); | |
} | |
public double getPrice(){ | |
return (total*1.8); | |
} | |
} | |
//建立一個AllPC的方法 | |
class AllPC{ | |
//將傳進來的物件,取得其成本的方法 | |
double a1,a2; | |
AllPC(PCandMultiPC p,Note n){ | |
a1=p.getCost(); | |
a2=n.getCost(); | |
} | |
//設計一個比較方法,回傳布林值 | |
public boolean isExpensive(){ | |
if(a1>a2) | |
return true; | |
else | |
return false; | |
} | |
} | |
class JPA06_3 { | |
public static void main(String args[]) { | |
PC pc = new PC(); | |
Note15 note15 = new Note15(); | |
//產生一個物件,放入兩個物件去比較 | |
AllPC app = new AllPC(pc, note15); | |
if(app.isExpensive()) | |
System.out.println("PC is more expensive then Note15 "); | |
else | |
System.out.println("Note15 is more expensive then PC "); | |
} | |
} |
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 - 602_4 */ | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
class Unit{ | |
int cost; | |
Unit(){ | |
cost = 0; | |
} | |
public int getcost(){ | |
return cost; | |
} | |
} | |
class LCD extends Unit{ | |
LCD(int i){ | |
if(i==10) | |
cost=2000; | |
else if(i==15) | |
cost=2500; | |
else | |
cost=3000; | |
} | |
} | |
class CPU extends Unit{ | |
CPU(double d ){ | |
if(d==1.66) | |
cost=6000; | |
if(d==2.2) | |
cost=8000; | |
if(d==2.4) | |
cost=11000; | |
} | |
} | |
class HD extends Unit{ | |
HD(String s){ | |
if(s=="120G") | |
cost = 2400; | |
else | |
cost = 2800; | |
} | |
} | |
abstract class Note extends AllPC{ | |
double cost; | |
LCD l; | |
CPU c; | |
HD dd; | |
Note(int i,double d,String s){ | |
l = new LCD(i); | |
c = new CPU(d); | |
dd = new HD(s); | |
} | |
abstract public double getCost(); | |
abstract public double getPrice(); | |
} | |
class MiniNote extends Note{ | |
MiniNote(){ | |
super(10,1.66,"120G"); | |
cost = l.getcost()+c.getcost()+dd.getcost(); | |
} | |
public double getCost(){ | |
return cost*1.4; | |
} | |
public double getPrice(){ | |
return cost*2.0; | |
} | |
} | |
class Note15 extends Note{ | |
Note15(){ | |
super(15,2.2,"160G"); | |
cost = l.getcost()+c.getcost()+dd.getcost(); | |
} | |
public double getCost(){ | |
return cost*1.4; | |
} | |
public double getPrice(){ | |
return cost*2.0; | |
} | |
} | |
abstract class PCandMultiPC extends AllPC{ | |
double cost; | |
CPU l; | |
HD c; | |
PCandMultiPC(){ | |
l = new CPU(2.4); | |
c = new HD("160G"); | |
} | |
abstract public double getCost(); | |
abstract public double getPrice(); | |
} | |
class PC extends PCandMultiPC{ | |
public double getCost(){ | |
return (l.getcost()+c.getcost()+500); | |
} | |
public double getPrice(){ | |
return (l.getcost()+c.getcost())*1.8; | |
} | |
} | |
class MultiPC extends PCandMultiPC{ | |
double total; | |
MultiPC(int a,int b){ | |
total = a*l.getcost()+b*c.getcost(); | |
} | |
public double getCost(){ | |
return (total*1.2); | |
} | |
public double getPrice(){ | |
return (total*1.8); | |
} | |
} | |
abstract class AllPC{ | |
AllPC(){} | |
abstract public double getCost(); | |
abstract public double getPrice(); | |
} | |
//建立一個訂單的方法 | |
class Order{ | |
//使用LinkedList來儲存每筆電腦物件 | |
LinkedList pcs; | |
//利用建構子來建立LinkedList物件 | |
Order(){ | |
pcs = new LinkedList(); | |
} | |
void in(AllPC allpc){ | |
pcs.add(allpc); | |
} | |
public double revenue(){ | |
double d =0.0; | |
//利用iterator來依序讀取LinkedList內的物件 | |
for(Iterator iterator = pcs.iterator();iterator.hasNext();){ | |
//將下一個物件讀入 | |
AllPC allpc = (AllPC)iterator.next(); | |
d=d+allpc.getPrice(); | |
} | |
return d; | |
} | |
} | |
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()); | |
} | |
} |
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 - 602_5 */ | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
class Unit{ | |
int cost; | |
Unit(){ | |
cost = 0; | |
} | |
public int getcost(){ | |
return cost; | |
} | |
} | |
class LCD extends Unit{ | |
LCD(int i){ | |
if(i==10) | |
cost=2000; | |
else if(i==15) | |
cost=2500; | |
else | |
cost=3000; | |
} | |
} | |
class CPU extends Unit{ | |
CPU(double d ){ | |
if(d==1.66) | |
cost=6000; | |
if(d==2.2) | |
cost=8000; | |
if(d==2.4) | |
cost=11000; | |
} | |
} | |
class HD extends Unit{ | |
HD(String s){ | |
if(s=="120G") | |
cost = 2400; | |
else | |
cost = 2800; | |
} | |
} | |
abstract class Note extends AllPC{ | |
double cost; | |
LCD l; | |
CPU c; | |
HD dd; | |
Note(int i,double d,String s){ | |
l = new LCD(i); | |
c = new CPU(d); | |
dd = new HD(s); | |
} | |
abstract public double getCost(); | |
abstract public double getPrice(); | |
} | |
class MiniNote extends Note{ | |
MiniNote(){ | |
super(10,1.66,"120G"); | |
cost = l.getcost()+c.getcost()+dd.getcost(); | |
} | |
public double getCost(){ | |
return cost*1.4; | |
} | |
public double getPrice(){ | |
return cost*2.0; | |
} | |
} | |
class Note15 extends Note{ | |
Note15(){ | |
super(15,2.2,"160G"); | |
cost = l.getcost()+c.getcost()+dd.getcost(); | |
} | |
public double getCost(){return cost*1.4;} | |
public double getPrice(){return cost*2.0;} | |
} | |
abstract class PCandMultiPC extends AllPC{ | |
double cost; | |
CPU l; | |
HD c; | |
PCandMultiPC() { | |
l = new CPU(2.4); | |
c = new HD("160G"); | |
} | |
abstract public double getCost(); | |
abstract public double getPrice(); | |
} | |
class PC extends PCandMultiPC{ | |
public double getCost(){ | |
return (l.getcost()+c.getcost()+500); | |
} | |
public double getPrice(){ | |
return (l.getcost()+c.getcost())*1.8; | |
} | |
} | |
class MultiPC extends PCandMultiPC{ | |
double toa; | |
MultiPC(int a,int b){ | |
toa = a*l.getcost()+b*c.getcost(); | |
} | |
public double getCost(){ | |
return (toa*1.2); | |
} | |
public double getPrice(){ | |
return (toa*1.8); | |
} | |
} | |
abstract class AllPC{ | |
AllPC(){} | |
abstract public double getCost(); | |
abstract public double getPrice(); | |
} | |
class Order{ | |
LinkedList pcs; | |
Order(){ | |
pcs = new LinkedList(); | |
} | |
void in(AllPC allpc){ | |
pcs.add(allpc); | |
} | |
public double revenue(){ | |
double d =0.0; | |
for(Iterator iterator = pcs.iterator();iterator.hasNext();){ | |
AllPC allpc = (AllPC)iterator.next(); | |
d=d+allpc.getPrice(); | |
} | |
return d; | |
} | |
} | |
class JPA06_5 { | |
public static void main(String args[]){ | |
//產生物件 | |
Order ord = new Order(); | |
MiniNote m =new MiniNote(); | |
Note15 n =new Note15(); | |
PC p =new PC(); | |
//加入訂單中 | |
ord.in(m); | |
ord.in(n); | |
ord.in(p); | |
//計算總成本 | |
double totalcost = m.getCost()+n.getCost()+p.getCost(); | |
//價算利潤 | |
double profit = ord.revenue() - totalcost; | |
if(profit>20000) | |
System.out.printf("This order exceeds 20000:%.1f",profit); | |
} | |
} |
TQC+ JAVA6 試題總覽:LINK
Github 備份:LINK
本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~
抱歉再度提問@@
回覆刪除第五題的要求
(3)如果此次總利潤超過 20000,則使用Exception 印出信息【This order exceeds 20000:xx】,將總利潤帶入xx。 直接用if 真的OK嗎@@ 雖然這有點太深 讓我不懂到底該怎寫
謝謝您的提醒~當初考的時候還沒有這個勘誤~所以就用一般的方法帶過了~
刪除有更新在上方的程式碼了~
如果有更新錯的話,在這附上原始碼:http://goo.gl/2GpwGi
也謝謝您找出了許多的錯誤^^"
筆者原本有想過要整個code翻新,但有兩個點卡了我~"~
1.JAVA6出來也有段時間了(太舊),TQC+也差不多要出JAVA8的考題了吧xD
2.我現在主要在寫C/C++,沒有一個較長的時間回去寫JAVA QQ