設計說明:
1. SLuxCar(超級豪華型車款)的成本=豪華東款的成本+音響成本。
2. 這型重的售價也是成本的1.2倍。請為這型重設計一個SLuxCar類別,並計算超級豪華型的成本與售價。
3. 也請為這家工廠增加一個方法、此方法能夠比較SLuxCar(超級豪華型單款)是否比LuxCar(豪華型車款)的售價較貴。
4. 顯示如執行結果參考畫面。
參考程式碼:
TQC+ Java 試題總整理
- class Unit{
- double cost,price;
- Unit(){cost = 0; price=0;}
- public double cost(){return cost;}
- public double price(){return price;}
- }
- class BasicCar extends Unit {
- BasicCar(int i,String t){
- cost = new Engine(i).cost+new Aircond(t).cost+5000;
- price =cost*1.2;
- }
- }
- class LuxCar extends Unit {
- LuxCar(int i,String t){
- cost = new Engine(i).cost+new Aircond(t).cost+10000;
- price = cost*1.2;
- }
- }
- class SLuxCar extends Unit {
- public double price(){return price;}
- SLuxCar(int i,String t){
- cost = new Engine(i).cost+new Aircond(t).cost+new Sound().cost+10000;
- price = cost*1.2;
- }
- public String expensive(LuxCar lc){
- if (price()>lc.price) return "Yes!!";
- else return "No!!";
- }
- }
- class Engine extends Unit {
- Engine(int i){
- if (i==1600) cost=20000;
- else if(i==2000) cost=25000;
- }
- }
- class Aircond extends Unit{
- Aircond(String t){
- if (t.equals("Auto")) cost=12000;
- else if (t.equals("Manual")) cost=10000;
- }
- }
- class Sound extends Unit{
- Sound(){
- cost=2000;
- }
- }
- public class JPA601_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 llc more expensive than lc? " + llc.expensive(lc));
- }
- }
TQC+ Java 試題總整理
聲明:
這裡的範例程式碼皆由本人親自編輯,歡迎轉載本教學,但請註明本網站,尊重一下作者的心血
沒有留言:
張貼留言
歡迎留言,較舊文章需要留言審核看不到自己的留言是正常的。
若長時間無回應請使用以下聯絡方式:
填寫表單:https://forms.gle/hxxX9n4tATcFnhnk8
寄信到:happyplayblogs@gmail.com