網站聲明

本網站包含了各式各樣的資源,如果有侵占到您的著作權,請與本人通知,本人會立即改進。本站所有發表僅屬研究討論性質,如果有任何後果請自行負責。

2015-05-29

TQC+ Java6 基本認識 602-5

602-5.
設計說明:

1. 此筆訂單的資料如下:MineNote 1台、Note15 1台、PC 1台。
2. 利潤=總收入-總成本(非零件總成本)。
3. 如果此次收入淨額超過20000,則印出訊息[This order exceeds 20000:收入淨額]。

參考程式碼:
  1. import java.util.*;
  2. class Unit{
  3.  int cost;
  4.   Unit(){cost = 0;}
  5.   public int cost(){return cost;}
  6.  }
  7. class LCD extends Unit{
  8.  LCD(int i){
  9.   if (i==10) cost=2000;
  10.   else if (i==15) cost=2500;
  11.   else if (i==17) cost=3000;
  12.  }
  13. }
  14. class CPU extends Unit{
  15.  CPU(double i){
  16.    if (i==1.66) cost=6000;
  17.   else if (i==2.2) cost=8000;
  18.   else if (i==2.4) cost=11000;
  19.  }
  20. }
  21. class HD extends Unit{
  22.  HD(int i){
  23.   if (i==120) cost=2400;
  24.   else if (i==160) cost=2800;
  25.  }
  26. }
  27. class computer{
  28.  double getCost;
  29.  double getPrice;
  30.  computer(){getCost = 0;getPrice = 0;}
  31.  public double getCost(){return getCost;}
  32.  public double getPrice(){return getPrice;}
  33. }
  34. class PC extends computer{
  35.  PC(){
  36.   getCost=new CPU(2.4).cost() + new HD(160).cost()+500;
  37.   getPrice=(new CPU(2.4).cost() + new HD(160).cost())*1.8;
  38.  }
  39. }
  40. class MiniNote extends computer{
  41.  MiniNote(){
  42.   getCost = (new LCD(10).cost + new CPU(1.66).cost + new HD(120).cost)*1.4;
  43.   getPrice = (new LCD(10).cost + new CPU(1.66).cost + new HD(120).cost)*2;
  44.  }
  45. }
  46. class Note15 extends computer{
  47.  Note15(){
  48.   getCost = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*1.4;
  49.   getPrice = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*2;
  50.  }
  51. }
  52. class Order{
  53.  LinkedList<computer> pcs;
  54.  Order(){
  55.   pcs=new LinkedList<computer>();
  56.  }
  57.  public void in(computer i){
  58.   pcs.add(i);
  59.  }
  60.  public double revenue(){
  61.   double sum=0;
  62.   for(computer apc:pcs){
  63.    sum=sum+apc.getPrice()-apc.getCost();
  64.   }
  65.   return sum;
  66.  }
  67. }
  68. public class JPA602_5{
  69.  public static void main(String args[]){
  70.   Order ord = new Order();
  71.   ord.in(new MiniNote());
  72.   ord.in(new Note15());
  73.   ord.in(new PC());
  74.   if (ord.revenue()>20000) System.out.println("This order exceeds 20000:"+ord.revenue());
  75.   else System.out.println(ord.revenue());
  76.  }
  77. }

TQC+ Java 試題總整理

聲明:

這裡的範例程式碼皆由本人親自編輯,歡迎轉載本教學,但請註明本網站,尊重一下作者的心血

沒有留言:

張貼留言

歡迎留言,較舊文章需要留言審核看不到自己的留言是正常的。
若長時間無回應請使用以下聯絡方式:
填寫表單:https://forms.gle/hxxX9n4tATcFnhnk8
寄信到:happyplayblogs@gmail.com