/****** 檔名:JPA06_1.java 題目:TQC+ JAVA6物件導向程式設計 參考答案 時間:2015/06 作者:fang shi mai ******/ 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()); } } abstract class Note { LCD lcd; CPU cpu; HD hd; double getCost() { return (lcd.getCost()+cpu.getCost()+hd.getCost())*1.4; } double getPrice() { return (lcd.getCost()+cpu.getCost()+hd.getCost())*2.0; } } class MiniNote extends Note { MiniNote() { lcd=new LCD(10); cpu=new CPU(1.66); hd=new HD("120G"); } } class Note15 extends Note { Note15() { lcd=new LCD(15); cpu=new CPU(2.2); hd=new HD("160G"); } } abstract class unit { int cost; double getCost() { return cost; } } class LCD extends unit { LCD(int a) { if(a==10) cost=2000; else if(a==15) cost=2500; else if(a==17) cost=3000; } } class CPU extends unit { CPU(double a) { if(a==1.66) cost=6000; else if(a==2.2) cost=8000; else if(a==2.4) cost=11000; } } class HD extends unit { HD(String a) { if(a.equals("120G")) cost=2400; else if(a.equals("160G")) cost=2800; } }
/****** 檔名:JPA06_2.java 題目:TQC+ JAVA6物件導向程式設計 參考答案 時間:2015/06 作者:fang shi mai ******/ 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()); } } abstract class AllPC { CPU cpu; HD hd; abstract double getPrice(); abstract double getCost(); } class PC extends AllPC { PC() { cpu=new CPU(2.4); hd=new HD("160G"); } double getPrice() { return (cpu.getCost()+hd.getCost())*1.8; } double getCost() { return (cpu.getCost()+hd.getCost())+500; } } class MultiPC extends AllPC { int countCPU; int countHD; MultiPC(int a,int b) { countCPU=a; countHD=b; cpu=new CPU(2.4); hd=new HD("160G"); } double getPrice() { return ((cpu.getCost()*countCPU)+(hd.getCost()*countHD))*1.8; } double getCost() { return ((cpu.getCost()*countCPU)+(hd.getCost()*countHD))*1.2; } } abstract class Note { LCD lcd; CPU cpu; HD hd; double getCost() { return (lcd.getCost()+cpu.getCost()+hd.getCost())*1.4; } double getPrice() { return (lcd.getCost()+cpu.getCost()+hd.getCost())*2.0; } } class MiniNote extends Note { MiniNote() { lcd=new LCD(10); cpu=new CPU(1.66); hd=new HD("120G"); } } class Note15 extends Note { Note15() { lcd=new LCD(15); cpu=new CPU(2.2); hd=new HD("160G"); } } abstract class unit { int cost; double getCost() { return cost; } } class LCD extends unit { LCD(int a) { if(a==10) cost=2000; else if(a==15) cost=2500; else if(a==17) cost=3000; } } class CPU extends unit { CPU(double a) { if(a==1.66) cost=6000; else if(a==2.2) cost=8000; else if(a==2.4) cost=11000; } } class HD extends unit { HD(String a) { if(a.equals("120G")) cost=2400; else if(a.equals("160G")) cost=2800; } }
/****** 檔名:JPA06_3.java 題目:TQC+ JAVA6物件導向程式設計 參考答案 時間:2015/06 作者:fang shi mai ******/ class JPA06_3 { public static void main(String args[]) { PC pc = new PC(); Note15 note15 = new Note15(); if(pc.isExpensive(note15)) System.out.println("PC is more expensive than Note15"); else System.out.println("Note15 is more expensive than PC"); } } abstract class AllPC { CPU cpu; HD hd; boolean isExpensive(Note a) { double x=this.getPrice(); double y=a.getPrice(); if(x>y) return true; else return false; } abstract double getPrice(); abstract double getCost(); } class PC extends AllPC { PC() { cpu=new CPU(2.4); hd=new HD("160G"); } double getPrice() { return (cpu.getCost()+hd.getCost())*1.8; } double getCost() { return (cpu.getCost()+hd.getCost())+500; } } class MultiPC extends AllPC { int countCPU; int countHD; MultiPC(int a,int b) { countCPU=a; countHD=b; cpu=new CPU(2.4); hd=new HD("160G"); } double getPrice() { return ((cpu.getCost()*countCPU)+(hd.getCost()*countHD))*1.8; } double getCost() { return ((cpu.getCost()*countCPU)+(hd.getCost()*countHD))*1.2; } } abstract class Note { LCD lcd; CPU cpu; HD hd; double getCost() { return (lcd.getCost()+cpu.getCost()+hd.getCost())*1.4; } double getPrice() { return (lcd.getCost()+cpu.getCost()+hd.getCost())*2.0; } } class MiniNote extends Note { MiniNote() { lcd=new LCD(10); cpu=new CPU(1.66); hd=new HD("120G"); } } class Note15 extends Note { Note15() { lcd=new LCD(15); cpu=new CPU(2.2); hd=new HD("160G"); } } abstract class unit { int cost; double getCost() { return cost; } } class LCD extends unit { LCD(int a) { if(a==10) cost=2000; else if(a==15) cost=2500; else if(a==17) cost=3000; } } class CPU extends unit { CPU(double a) { if(a==1.66) cost=6000; else if(a==2.2) cost=8000; else if(a==2.4) cost=11000; } } class HD extends unit { HD(String a) { if(a.equals("120G")) cost=2400; else if(a.equals("160G")) cost=2800; } }
/****** 檔名:JPA06_4.java 題目:TQC+ JAVA6物件導向程式設計 參考答案 時間:2015/06 作者:fang shi mai ******/ import java.util.LinkedList; 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()); } } class Order { LinkedList<All> ll=new LinkedList<All>(); void in(All a) { ll.add(a); } double revenue() { double sum=0; for(All i:ll) { sum+=i.getPrice(); } return sum; } } abstract class All { abstract double getPrice(); abstract double getCost(); } abstract class AllPC extends All { CPU cpu; HD hd; boolean isExpensive(Note a) { double x=this.getPrice(); double y=a.getPrice(); if(x>y) return true; else return false; } abstract double getPrice(); abstract double getCost(); } class PC extends AllPC { PC() { cpu=new CPU(2.4); hd=new HD("160G"); } double getPrice() { return (cpu.getCost()+hd.getCost())*1.8; } double getCost() { return (cpu.getCost()+hd.getCost())+500; } } class MultiPC extends AllPC { int countCPU; int countHD; MultiPC(int a,int b) { countCPU=a; countHD=b; cpu=new CPU(2.4); hd=new HD("160G"); } double getPrice() { return ((cpu.getCost()*countCPU)+(hd.getCost()*countHD))*1.8; } double getCost() { return ((cpu.getCost()*countCPU)+(hd.getCost()*countHD))*1.2; } } abstract class Note extends All { LCD lcd; CPU cpu; HD hd; double getCost() { return (lcd.getCost()+cpu.getCost()+hd.getCost())*1.4; } double getPrice() { return (lcd.getCost()+cpu.getCost()+hd.getCost())*2.0; } } class MiniNote extends Note { MiniNote() { lcd=new LCD(10); cpu=new CPU(1.66); hd=new HD("120G"); } } class Note15 extends Note { Note15() { lcd=new LCD(15); cpu=new CPU(2.2); hd=new HD("160G"); } } abstract class unit { int cost; double getCost() { return cost; } } class LCD extends unit { LCD(int a) { if(a==10) cost=2000; else if(a==15) cost=2500; else if(a==17) cost=3000; } } class CPU extends unit { CPU(double a) { if(a==1.66) cost=6000; else if(a==2.2) cost=8000; else if(a==2.4) cost=11000; } } class HD extends unit { HD(String a) { if(a.equals("120G")) cost=2400; else if(a.equals("160G")) cost=2800; } }
/****** 檔名:JPA06_5.java 題目:TQC+ JAVA6物件導向程式設計 參考答案 時間:2015/06 作者:fang shi mai ******/ import java.util.LinkedList; class JPA06_5 { public static void main(String args[]) { try { Order ord = new Order(); ord.in(new MiniNote()); ord.in(new Note15()); ord.in(new PC()); if(ord.selsey()>20000) throw new NewException("This order exceeds 20000:"+ord.selsey()); else System.out.println(ord.selsey()); } catch(NewException n) { System.out.println(n); } } } class NewException extends Exception { String exc; NewException(String a) { exc=a; } public String toString() { return exc; } } class Order { LinkedList<All> ll=new LinkedList<All>(); void in(All a) { ll.add(a); } double revenue() { double sum=0; for(All i:ll) { sum+=i.getPrice(); } return sum; } double selsey() { double sum=0; for(All i:ll) { sum+=(i.getPrice()-i.getCost()); } return sum; } } abstract class All { abstract double getPrice(); abstract double getCost(); } abstract class AllPC extends All { CPU cpu; HD hd; boolean isExpensive(Note a) { double x=this.getPrice(); double y=a.getPrice(); if(x>y) return true; else return false; } abstract double getPrice(); abstract double getCost(); } class PC extends AllPC { PC() { cpu=new CPU(2.4); hd=new HD("160G"); } double getPrice() { return (cpu.getCost()+hd.getCost())*1.8; } double getCost() { return (cpu.getCost()+hd.getCost())+500; } } class MultiPC extends AllPC { int countCPU; int countHD; MultiPC(int a,int b) { countCPU=a; countHD=b; cpu=new CPU(2.4); hd=new HD("160G"); } double getPrice() { return ((cpu.getCost()*countCPU)+(hd.getCost()*countHD))*1.8; } double getCost() { return ((cpu.getCost()*countCPU)+(hd.getCost()*countHD))*1.2; } } abstract class Note extends All { LCD lcd; CPU cpu; HD hd; double getCost() { return (lcd.getCost()+cpu.getCost()+hd.getCost())*1.4; } double getPrice() { return (lcd.getCost()+cpu.getCost()+hd.getCost())*2.0; } } class MiniNote extends Note { MiniNote() { lcd=new LCD(10); cpu=new CPU(1.66); hd=new HD("120G"); } } class Note15 extends Note { Note15() { lcd=new LCD(15); cpu=new CPU(2.2); hd=new HD("160G"); } } abstract class unit { int cost; double getCost() { return cost; } } class LCD extends unit { LCD(int a) { if(a==10) cost=2000; else if(a==15) cost=2500; else if(a==17) cost=3000; } } class CPU extends unit { CPU(double a) { if(a==1.66) cost=6000; else if(a==2.2) cost=8000; else if(a==2.4) cost=11000; } } class HD extends unit { HD(String a) { if(a.equals("120G")) cost=2400; else if(a.equals("160G")) cost=2800; } }
沒有留言:
張貼留言