Advertisement
KuoHsiangYu

Box1_Box2_Box3_Java

Dec 18th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. /* 郭翔宇:使用Java物件導向概念實作能接收 1 2 3 號球的box1,只能接收 2 3 號球的box2,只能接收 3 號球的box3。 */
  2. /*
  3. 黃珍珍10
  4.  
  5. 黃珍珍‎Taiwan 程式語言讀書會 [JAVA,C,C++,C#,VB...等不拘]
  6. 35分鐘 ·
  7.  
  8. 想請問一個邏輯問題轉成程式 (程式我是用java寫)
  9. "這真的不是作業" 是我寫的程式裡有一小段這種邏輯 但我把它轉成箱子跟球的問題
  10.  
  11. 題目:
  12. 有編號123的箱子
  13. 1號可以放編號123的球
  14. 2號可以放編號23的球
  15. 3號只能放編號3
  16.  
  17. 想請問若今天有很多顆 可以重複的球
  18. 要如何擺才能讓球的數字加起來最大呢?
  19. https://www.facebook.com/photo.php?fbid=603186333433235&set=gm.2232585740289016&type=3&theater&ifg=1
  20.  */
  21.  /*
  22.  * To change this license header, choose License Headers in Project Properties.
  23.  * To change this template file, choose Tools | Templates
  24.  * and open the template in the editor.
  25.  */
  26.  
  27. /**
  28.  *
  29.  * @author Kuo, Hsiang-Yu
  30.  */
  31. class Three {
  32.  
  33.     int id = 3;
  34. }
  35.  
  36. class Two extends Three {
  37.  
  38.     int id = 2;
  39. }
  40.  
  41. class One extends Two {
  42.  
  43.     int id = 1;
  44. }
  45.  
  46. public class MainClass {
  47.  
  48.     /**
  49.      * @param args the command line arguments
  50.      */
  51.     public static void main(String[] args) {
  52.         // TODO code application logic here
  53.         int sum = 0;
  54.         One one = new One();
  55.         Two two = new Two();
  56.         Three three = new Three();
  57.  
  58.         System.out.println("box1(one) = " + box1(one));
  59.         System.out.println("box1(two) = " + box1(two));
  60.         System.out.println("box1(three) = " + box1(three));
  61.  
  62.         System.out.println("box2(one) = " + box2(one));
  63.         System.out.println("box2(two) = " + box2(two));
  64.         System.out.println("box2(three) = " + box2(three));
  65.  
  66.         System.out.println("box3(one) = " + box3(one));
  67.         System.out.println("box3(two) = " + box3(two));
  68.         System.out.println("box3(three) = " + box3(three));
  69.     }
  70.  
  71.     private static int box1(Three three) {
  72.         //throw new UnsupportedOperationException("Not supported yet.");
  73.         //To change body of generated methods, choose Tools | Templates.
  74.         /* 編號1的箱子,可以放編號 1 2 3 的球 */
  75.         if (three instanceof One) {
  76.             One one = (One) three;
  77.             return one.id;
  78.         } else if (three instanceof Two) {
  79.             Two two = (Two) three;
  80.             return two.id;
  81.         } else {
  82.             return three.id;
  83.         }
  84.     }
  85.  
  86.     private static int box2(Three three) {
  87.         //throw new UnsupportedOperationException("Not supported yet.");
  88.         //To change body of generated methods, choose Tools | Templates.
  89.         /* 編號2的箱子,可以放編號 2 3 的球 */
  90.         if (three instanceof One) {
  91.             /* 編號2的箱子拒絕接受 1號球,所以回傳0 */
  92.             System.out.println("box2 拒絕 1號球。");
  93.             return 0;
  94.         } else if (three instanceof Two) {
  95.             Two two = (Two) three;
  96.             return two.id;
  97.         } else {
  98.             return three.id;
  99.         }
  100.     }
  101.  
  102.     private static int box3(Three three) {
  103.         //throw new UnsupportedOperationException("Not supported yet.");
  104.         //To change body of generated methods, choose Tools | Templates.
  105.         /* 編號3的箱子,可以放編號 3 的球 */
  106.         if (three instanceof One) {
  107.             /* 編號3的箱子拒絕接受 1號球,所以回傳0 */
  108.             System.out.println("box3 拒絕 1號球。");
  109.             return 0;
  110.         } else if (three instanceof Two) {
  111.             /* 編號3的箱子拒絕接受 2號球,所以回傳0 */
  112.             System.out.println("box3 拒絕 2號球。");
  113.             return 0;
  114.         } else {
  115.             return three.id;
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement