設計說明:
1. 請比較一台Note 15的筆電和一個PC類別的桌上型電腦何者售價較貴。
2. 請新增一個名為AllPC的類別,於此類別中撰寫isExpensive方法,此方法龍夠比較何者的售價較貴,並傳回一個布林值,顯示如執行結果參考畫面。
參考程式碼:
TQC+ Java 試題總整理
- class Unit{
- int cost;
- Unit(){cost = 0;}
- public int cost(){return cost;}
- }
- class LCD extends Unit{
- LCD(int i){
- if (i==10) cost=2000;
- else if (i==15) cost=2500;
- else if (i==17) cost=3000;
- }
- }
- class CPU extends Unit{
- CPU(double i){
- if (i==1.66) cost=6000;
- else if (i==2.2) cost=8000;
- else if (i==2.4) cost=11000;
- }
- }
- class HD extends Unit{
- HD(int i){
- if (i==120) cost=2400;
- else if (i==160) cost=2800;
- }
- }
- class computer{
- double getCost;
- double getPrice;
- computer(){getCost = 0;getPrice = 0;}
- public double getCost(){return getCost;}
- public double getPrice(){return getPrice;}
- }
- class PC extends computer{
- PC(){
- getCost=new CPU(2.4).cost() + new HD(160).cost()+500;
- getPrice=(new CPU(2.4).cost() + new HD(160).cost())*1.8;
- }
- }
- class Note15 extends computer{
- Note15(){
- getCost = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*1.4;
- getPrice = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*2;
- }
- }
- class AllPC{
- double pc, note15;
- AllPC(double a,double b){
- pc=a;
- note15=b;
- }
- public boolean isExpensive(){
- if (pc>note15) return true;
- else return false;
- }
- }
- public class JPA602_3{
- public static void main(String args[]){
- PC pc = new PC();
- Note15 note15 = new Note15();
- AllPC allpc =new AllPC(pc.getPrice(),note15.getPrice());
- if (allpc.isExpensive())System.out.println("PC is more expensive than Note15");
- else System.out.println("Note15 is more expensive than PC");
- }
- }
TQC+ Java 試題總整理
聲明:
這裡的範例程式碼皆由本人親自編輯,歡迎轉載本教學,但請註明本網站,尊重一下作者的心血
沒有留言:
張貼留言
歡迎留言,較舊文章需要留言審核看不到自己的留言是正常的。
若長時間無回應請使用以下聯絡方式:
填寫表單:https://forms.gle/hxxX9n4tATcFnhnk8
寄信到:happyplayblogs@gmail.com