SlideShare una empresa de Scribd logo
1 de 14
Descargar para leer sin conexión
ANSWER 12-13                                                      Computer Programming using Java             1   2     Computer Programming using Java                                                           ANSWER 12-13


  CHAPTER                                         คลาสและอ๊อบเจ็ค                                                     โจทย์ข้อที 3 [ระดับง่ าย]
 ANS-12
 ANS-                                            (Classes and Objects)                                                public class Course {
                                                                                                                          //แอตทริบิว 3 ตัว
โจทย์ข้อที 1 [ระดับง่ าย]                                                                                                  private int id;
                                                                                                                           public String title;
 1     public class Num {                                                                                                  protected double credit;
 2       private int x;
 3       private double y;
                                                      Attribute
 4       public Num() {                                                                                                   //ตัวสร้ างแบบไม่ มีพารามิเตอร์ ใดๆ
 5         x = 0; y = 0.0;
 6       }                                                                                                                 public Course() {
                                                      Constructor
 7       public Num(int m, double n) {                                                                                       id = 0;
 8         x = m; y = n;                                                                                                     title = "";
 9       }                                                                                                                   credit = 0.0;
 10      public int addNX(int n) {                                                                                         }
 11        return n + x;                                                            Class (ต้ นแบบ)
 12      }
 13      public double addNY(double n) {                                                                                  //ตัวสร้ างแบบมีพารามิเตอร์ 3 ตัว
 14        return n + y;                              Method                              ผลลัพธ์ ทีได้
 15      }                                                                                                                 public Course(int i, String t, double c) {
 16      public void showXY() {                                                      5                                       id = i;
 17        System.out.println(x);                                                    7.0                                     title = t;
 18        System.out.println(y);                                                    9                                       credit = c;
 19      }                                                                                                                 }
 20    }                                                                             20.0


 1     public class RunNum {
                                                                                                                          //เมท็อด setID(…)
 2       public static void main(String[] args){                                                                           public void setID(int i) {
 3         Num obj = new Num(5, 7.0);                           Object                                                       id = i;
 4         obj.showXY();                                                                                                   }
 5         int a = obj.addNX(4);
 6         double b = obj.addNY(13.0);
                                                                                    Class (ประมวลผล)
 7         System.out.println(a);          Main               Method                                                      //เมท็อด setTitle(…)
 8         System.out.println(b);
 9       }                                                                                                                 public void setTitle(String t) {
 10    }                                                                                                                     title = t;
                                                                                                                           }


                                                                                                                          //เมท็อด setCredit(…)
โจทย์ ข้อที 2 [ระดับง่ าย]
                                                                                                                           public void setCredit(double c) {
1)                                    6)                                    11)                                              credit = c;
                                                                                                                           }
2)                                    7)                                    12)
3)                                    8)                                    13)                                           //เมท็อด getLevel(…)
4)                                    9)                                    14)                                            public String getLevel() {
                                                                                                                             if (id % 1000 / 100 <= 4) {
5)                                    10)                                   15)                                                return "Undergraduate";
                                                                                                                             } else {
                                                                                                                               return "Graduate";
                                                                                                                             }
                                                                                                                           }




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             3   4        Computer Programming using Java                                                        ANSWER 12-13


    //เมท็อด getFaculty(…)                                                                                            โจทย์ข้อที 6 [ระดับง่ าย]
      public int getFaculty() {                                                                                       //ประกาศหัวคลาส TestNumber
        return id / 100000;
      }                                                                                                               import java.util.Scanner;
                                                                                                                       public class TestNumber {

    //เมท็อด getDepartment(…)                                                                                              public static void main(String[] args) {
      public int getDepartment() {                                                                                           Scanner kb = new Scanner(System.in);
        return id / 1000 % 100;
      }                                                                                                                       //อ๊ อบเจ็คชือ no1
                                                                                                                               Number no1 = new Number();

    //เมท็อด toString(…)
      public String toString() {                                                                                              //อ๊ อบเจ็คชือ no2
        return id + " " + title + " (" + credit + ")";
                                                                                                                               System.out.print("Enter x : ");
      }
                                                                                                                               double x = kb.nextDouble();
                                                                                                                               System.out.print("Enter y : ");
} //End of class                                                                                                               double y = kb.nextDouble();
                                                                                                                               Number no2 = new Number(x, y);
โจทย์ข้อที 4 [ระดับง่ าย]
                                                                                                                              //แสดงผลการบวก การลบ การคณ การหาร และการม็อด
                                                                                                                                                       ู
 9                               1) แอตทริบิว y หรื อตัวแปร y
 5                                                                                                                             System.out.println("Add:          "   +   no2.add());
 7                               2) ตัวแปร y (ในบรรทัดที 13)                                                                   System.out.println("Sub:          "   +   no2.sub());
 1                                                                                                                             System.out.println("Mul:          "   +   no2.mul());
 1                                                                                                                             System.out.println("Div:          "   +   no2.div());
                                                                                                                               System.out.println("Mod:          "   +   no2.mod());
 12
 11
 1                                                                                                                      } //End of main
 12                                                                                                                   } //End of class


                                                                                                                      โจทย์ข้อที 7 [ระดับง่ าย]
โจทย์ข้อที 5 [ระดับง่ าย]                                                                                             1) public double             data;
 0,
 101,Taksin                                                                                                           2)     private static int var = 10;
 103,Apisit
 Taksin:U                                                                                                             3)     protected static boolean check;
 Apisit:S
                                                                                                                      4)     private String stdName[] = new String[351];

                                                                                                                      5)     public static int m[][] = new int[8][5];

                                                                                                                      6)     private void show(String stdName, double grade)

                                                                                                                      7)     protected static int search(int num[], int x)

                                                                                                                      8)     public double[][] mulMatrix(double a[][], double b[][])


© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             5   6     Computer Programming using Java                                                           ANSWER 12-13


9)     public static int[] union(int x[], int y[])                                                                      //ตัวสร้ างแบบมีพารามิเตอร์ สองตัว
                                                                                                                          public Dice(int f, int v) {
                                                                                                                            face = f;
                                                                                                                            value = v;
โจทย์ ข้อที 8 [ระดับง่ าย]                                                                                                }
 ข้ อ คําตอบ                   ข้ อ คําตอบ                ข้ อ คําตอบ                  ข้ อ คําตอบ
  1.        B                   6.     A                  11.     C                    16.     D                        //เมท็อดประจําอ๊ อบเจ็คชือ roll
                                                                                                                          public void roll() {
  2.        D                   7.     D                  12.     B                    17.     C                            value = (int)(Math.random() * face) + 1;
                                                                                                                          }
  3.        B                   8.     D                  13.     D                    18.     D
  4.        C                   9.     A                  14.     A                    19.     B
                                                                                                                        //เมท็อดประจําอ๊ อบเจ็คชือ setValue
  5.        B                  10.     D                  15.     B                    20.     C                          public void setValue(int v) {
                                                                                                                            value = v;
                                                                                                                          }
โจทย์ข้อที 9 [ระดับปานกลาง]
 0,0                                                                                                                    //เมท็อดประจําอ๊ อบเจ็คชือ getFace
 6,10                                                                                                                     public int getFace() {
 Good By A                                                                                                                  return face;
 See You F                                                                                                                }
 0,0
 5,7
 5,7                                                                                                                    //เมท็อดประจําอ๊ อบเจ็คชือ getValue
 9,7                                                                                                                      public int getValue() {
 9,7                                                                                                                        return value;
 I Love Java                                                                                                              }


                                                                                                                        //เมท็อดประจําอ๊ อบเจ็คชือ show
โจทย์ข้อที 10 [ระดับปานกลาง]                                                                                              public void show() {
public class Dice {                                                                                                         System.out.println("Face: " + face);
                                                                                                                            System.out.println("Value: " + value);
     //แอตทริบิวประจําอ๊ อบเจ็คชือ face และ value                                                                         }
      public int face;
      public int value;                                                                                               } //End of class

     //ตัวสร้ างแบบไม่ มีพารามิเตอร์
      public Dice() {
        face = 2;
        roll();
      }


     //ตัวสร้ างแบบมีพารามิเตอร์ หนึงตัว
      public Dice(int f) {
        face = f;
        roll();
      }



© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             7   8     Computer Programming using Java                                                           ANSWER 12-13


public class TestDice {                                                                                               public class TestRealNumber {
  /* สร้ างลกเต๋ า 3 ลก ในรปของอ๊ อบเจ็คทีชือ d1, d2 และ d3 */
            ู         ู    ู                                                                                            /* สร้ างจํานวนจริ ง 1 จํานวนในรปของอ๊ อบเจ็ค r */
                                                                                                                                                        ู
    public static void main(String[] args) {                                                                              public static void main(String[] args) {
      Dice d1 = new Dice();                                                                                                 RealNumber r = new RealNumber();
      Dice d2 = new Dice(13);                                                                                               System.out.println(r.plus(15));
      Dice d3 = new Dice(30, 9);                                                                                            System.out.println(r.diff(50));
      d1.show();                                                                                                          }
      d2.show();
      d3.show();                                                                                                      } //End of class
      for (int i = 1; i <= 20; i++) {
        d1.roll();
        System.out.println(d1.getValue());
        d2.roll();                                                                                                    โจทย์ข้อที 12 [ระดับปานกลาง]
        System.out.println(d2.getValue());                                                                             public class Account {
        d3.roll();                                                                                                       private double balance;
        System.out.println(d3.getValue());                                                                               public Account() {
        System.out.println("-------");                                                                                     balance = 0.0;
      }                                                                                                                  }
    }                                                                                                                    public Account(double money) {
                                                                                                                           balance = money;
} //End of class                                                                                                         }
                                                                                                                         public void deposit(double money) {
                                                                                                                           balance += money;
โจทย์ข้อที 11 [ระดับปานกลาง]                                                                                             }
public class RealNumber {                                                                                                public double withdraw(double money) {
                                                                                                                           if (balance >= money) {
  //แอตทริบิวประจําอ๊ อบเจ็คแบบ public ชือ num                                                                               balance -= money;
    public double num;                                                                                                       return money;
                                                                                                                           } else {
                                                                                                                             return 0.0;
  //ตัวสร้ างแบบไม่ มีพารามิเตอร์                                                                                          }
    public RealNumber() {                                                                                                }
      num = 0.0;                                                                                                         public double getbalance() {
    }                                                                                                                      return balance;
                                                                                                                         }
                                                                                                                       } //End of Class
  //ตัวสร้ างแบบมีพารามิเตอร์
    public RealNumber(double n) {
      num = n;
    }                                                                                                                 สร้ างคลาส TestAccount
                                                                                                                       public class TestAccount {
  //เมท็อดประจําอ๊ อบเจ็คชือ plus                                                                                        public static void main(String[] args) {
                                                                                                                           Account acc = new Account(2000);
    public double plus(double n) {                                                                                         System.out.println("New Account");
      return num + n;                                                                                                      System.out.println("Balance : " + acc.getbalance());
    }                                                                                                                      double money = acc.withdraw(1500);
                                                                                                                           System.out.println("Withdraw : " + money);
  //เมท็อดประจําอ๊ อบเจ็คชือ diff                                                                                          System.out.println("Balance : " + acc.getbalance());
                                                                                                                           acc.deposit(800);
    public double diff(double n) {                                                                                         System.out.println("Deposit : " + 800);
      double x = num – n;                                                                                                  System.out.println("Balance : " + acc.getbalance());
      if (x >= 0) return x;                                                                                              }
      else return –x;                                                                                                  }
    }

} //End of class
© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             9   10     Computer Programming using Java                                                           ANSWER 12-13


โจทย์ข้อที 13 [ระดับปานกลาง]                                                                                                public void addFive(int c) {
public class PiggyBank {                                                                                                      if (getTotal() + 5 * c <= size) five = five + c;
   (1) แอตทริบิว one, two, five และ ten                                                                                     }
                                                                                                                              else System.out.println("Piggy Bank Full");

   (2) แอตทริบิว size
     public int one, two, five, ten;
     public int size;
                                                                                                                          (10) เมท็อดประจําอ๊ อบเจ็คชือ addTen(…)
                                                                                                                            public void addTen(int c) {
                                                                                                                              if (getTotal() + 10 * c <= size) ten = ten + c;
   (3) ตัวสร้ าง 2 แบบ                                                                                                        else System.out.println("Piggy Bank Full");
                                                                                                                            }
     PiggyBank() {
       clear();
       size = 100;                                                                                                     }//End of class
     }
     PiggyBank(int s) {
       clear();
                                                                                                                       สร้ างคลาส TestPiggyBank
       size = s;                                                                                                       public class TestPiggyBank {
     }                                                                                                                   public static void main(String[] args) {
                                                                                                                              PiggyBank pb = new PiggyBank(500);
                                                                                                                              System.out.println("Money Total : " + pb.getTotal());
   (4) เมท็อดประจําอ๊ อบเจ็คชือ clear(…)                                                                                      pb.addTwo(34);
     public void clear() {                                                                                                    System.out.println("Add 2 Baht Money : " + 34);
       one = two = five = ten = 0;                                                                                            System.out.println("Money Total : " + pb.getTotal());
     }                                                                                                                        pb.addTen(13);
                                                                                                                              System.out.println("Add 10 Baht Money : " + 13);
                                                                                                                              System.out.println("Money Total : " + pb.getTotal());
   (5) เมท็อดประจําอ๊ อบเจ็คชือ getTotal(…)
     public int getTotal() {                                                                                             }//End of main
       return one + (2 * two) + (5 * five) + (10 * ten);                                                               }//End of class
     }


   (6) เมท็อดประจําอ๊ อบเจ็คชือ full(…)
     public boolean full() {
       return getTotal() >= size;
     }
   (7) เมท็อดประจําอ๊ อบเจ็คชือ addOne(…)
     public void addOne(int c) {
       if (getTotal() + c <= size) one = one + c;
       else System.out.println("Piggy Bank Full");
     }


   (8) เมท็อดประจําอ๊ อบเจ็คชือ addTwo(…)
     public void addTwo(int c) {
       if (getTotal() + 2 * c <= size) two = two + c;
       else System.out.println("Piggy Bank Full");
     }


   (9) เมท็อดประจําอ๊ อบเจ็คชือ addFive(…)

© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)            © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             11   12     Computer Programming using Java                                                           ANSWER 12-13


  CHAPTER                                  คลาสและอ๊อบเจ็คแบบซับซ้อน                                                      //ตัวสร้ างแบบไม่ มีพารามิเตอร์
 ANS-13
 ANS-                                     (Advanced Classes and Objects)                                                    public Coin() {
                                                                                                                              side = "H";
                                                                                                                              value = 1;
โจทย์ข้อที 1 [ระดับปานกลาง]                                                                                                 }

  เรียกตัวสร้ างใด    ผลลัพธ์ ทีแสดง
  (ระบุหมายเลข)      ขึนบนจอภาพคือ                                                                                        //ตัวสร้ างแบบมีพารามิเตอร์ สองตัว
                                                                                                                            public Coin(String s, int v) {
                                                                                                                              side = s;
          1                0,0                                                                                                value = v;
                                                                                                                            }

          2               13,2                                                                                            //ตัวสร้ างแบบมีพารามิเตอร์ หนึงตัว
                                                                                                                            public Coin(Coin c) {
                                                                                                                              side = c.side;
          3               13,2                                                                                                value = c.value;
                                                                                                                            }


      2       3           7,11                                                                                            //เมท็อด flip(…)
                                                                                                                            public void flip() {
                                                                                                                              side = list[(int)(Math.random() * 2)];
          2               11,13                                                                                             }


                           0,0
                                                                                                                          //เมท็อด reverse(…)
      1       3
                                                                                                                            public void reverse() {
                                                                                                                              if (side.equals("H")) {
                                                                                                                                side = "T";
public class Num {                                                                                                            } else {
  ...                                                                                                                           side = "H";
  public boolean isEquals(int x, int y) {                                                                                     }
                                                                                                                            }
          return this.x == x && this.y == y;

   }                                                                                                                      //เมท็อด equals(…)
   public boolean isEquals(Num n) {
                                                                                                                            public boolean equals(Coin c) {
          return x == n.x && y == n.y;                                                                                        return side == c.side && value == c.value;
                                                                                                                            }
  }
} //End of class
                                                                                                                        } //End of class


โจทย์ข้อที 2 [ระดับยาก]
public class Coin {
   //แอตทริบิว side และ value
     public int value;
     public String side;




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)             © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             13   14     Computer Programming using Java                                                           ANSWER 12-13


/* คลาส Dem   oCoin   */                                                                                                    //เมท็อด rem    oveDuplicatedMem bers(…)
public class Dem oCoin {                                                                                                    public int[ ] rem oveDuplicatedMem bers(int x [ ] ) {
  public static void m ain(String[ ]              args) {                                                                     ...
                                                                                                                            } //E nd of m ethod
       Coin c1 = new Coin();
       Coin c2 = new Coin("H ", 1);
       Coin c3 = new Coin("T", 5);                                                                                          //เมท็อด eq    uals(…)
       Coin c4 = new Coin(c3 );                                                                                             public boolean eq uals(SetOfInteger x ) {
       for (int i = 1; i <= 20; i+ + ) {
         c1.flip();                                                                                                              if (x .iSet.length == iSet.length) {
         c2.reverse();                                                                                                             for (int i = 0; i < x .iSet.length; i+ + ) {
         c3 .flip();                                                                                                                  if (x .iSet[ i] ! = iSet[ i] ) return false;
         c4.reverse();                                                                                                             }
         Sy stem .out.println(c1.eq uals(c2));                                                                                     return true;
         Sy stem .out.println(c3 .eq uals(c4));                                                                                  } else {
       }                                                                                                                           return false;
                                                                                                                                 }

  } //E nd of m ain
                                                                                                                            } //E nd of m ethod
} //E nd of class

                                                                                                                            //เมท็อด toString(…)
โจทย์ข้อที 3 [ระดับยาก]                                                                                                     public String toString() {
public class SetOfInteger {                                                                                                      String s = "";
    //แอตทริบิว iSet                                                                                                             for (int i = 0; i < iSet.length; i+ + )
                                                                                                                                   s = s + iSet[ i] + " ";
      public int iSet[ ] ;
                                                                                                                                 return s;

                                                                                                                            } //E nd of m ethod
    //ตัวสร้ างแบบทีรั บพารามิเตอร์ 1 ตัวทีเป็ นอาเรย์
    public SetOfInteger(int[ ]            x ) {
                                                                                                                            //เมท็อด addE    lem ent(…)
         x  = sort(x );                                                                                                     public void addE lem ent(int a) {
         x = rem oveDuplicatedMem bers(x );
          iSet = new int[ x .length] ;                                                                                           int x [ ] = new int[ iSet.length + 1] ;
          for (int i = 0; i < iSet.length; i+ + ) {                                                                              for (int i = 0; i < x .length - 1; i+ + )
            iSet[ i] = x [ i] ;                                                                                                       x [ i] = iSet[ i] ;
          }                                                                                                                      x [ x .length - 1] = a;
                                                                                                                                  x = sort(x );
                                                                                                                                   x = rem oveDuplicatedMem bers(x );
    } //E nd of constructor                                                                                                         iSet = x ;

    //ตัวสร้ างแบบทีรั บพารามิเตอร์ 1 ตัวทีเป็ นชนิด SetOfInteger                                                           } //E nd of m ethod
    public SetOfInteger(SetOfInteger s) {
         iSet = new int[ s.iSet.length] ;                                                                                   //เมท็อด findE    lem ent(…)
         for (int i = 0; i < iSet.length; i+ + ) {                                                                          public int findE lem ent(int a) {
           iSet[ i] = s.iSet[ i] ;
         }                                                                                                                       for (int i = 0; i < iSet.length; i+ + )
                                                                                                                                   if (iSet[ i] == a) return i;
                                                                                                                                 return - 1;
    } //E nd of constructor
                                                                                                                            } //E nd of m ethod
    //เมท็อด sort(…)
    public int[ ] sort(int x [ ] ) {
      ...
    } //E nd of m ethod

© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)             © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             15   16     Computer Programming using Java                                                           ANSWER 12-13


    //เมท็อด isSubset(…)                                                                                                โจทย์ข้อที 4 [ระดับยาก]
    public boolean isSubset(SetOfInteger s) {
                                                                                                                        public class Student {
         int count = 0;                                                                                                    //แอตทริบิวประจําอ๊ อบเจ็ค 3 ตัว
         for (int i = 0; i < s.iSet.length; i++) {
           for (int j = 0; j < iSet.length; j++) {                                                                           private int id;
             if (s.iSet[i] == iSet[j]) {                                                                                     private String name;
               count++;                                                                                                      private double score[];
               break;
             }
           }                                                                                                               //ตัวสร้ างแบบไม่ กาหนดพารามิเตอร์
                                                                                                                                              ํ
         }                                                                                                                   Student() {
         return s.iSet.length == count;                                                                                        id = 0;
                                                                                                                               name = "";
    } //End of method                                                                                                          score = new double[0];
                                                                                                                             }
    //เมท็อด union(…)
    public SetOfInteger union(SetOfInteger s) {                                                                            //ตัวสร้ างแบบ 3 พารามิเตอร์
         int x[] = new int[s.iSet.length + iSet.length];                                                                     Student(int i, String n, int x) {
         for (int i = 0; i < x.length; i++) {                                                                                  id = i;
           if (i < iSet.length) x[i] = iSet[i];                                                                                name = n;
           else x[i] = s.iSet[i - iSet.length];                                                                                score = new double[x];
         }                                                                                                                   }
         SetOfInteger un = new SetOfInteger(x);
         return un;
                                                                                                                           //ตัวสร้ างแบบ 1 พารามิเตอร์
    } //End of method                                                                                                        Student(Student s) {
                                                                                                                               id = s.getID();
    //เมท็อด intersect(…)                                                                                                      name = s.getName();
                                                                                                                               score = s.getScore();
    public SetOfInteger intersect(SetOfInteger s) {                                                                          }
         int x[] = new int[s.iSet.length];
         int len = 0;
         for (int i = 0; i < s.iSet.length; i++) {
                                                                                                                           //เมท็อดประจําอ๊ อบเจ็ค 4 เมท็อด getID(…), getName(…), getScore(…)และ setScore(…)
           for (int j = 0; j < this.iSet.length; j++) {                                                                      public int getID() {
             if (s.iSet[i] == this.iSet[j]) {                                                                                  return id;
               x[len] = s.iSet[i];                                                                                           }
               len++;                                                                                                        public String getName() {
               break;                                                                                                          return name;
             }                                                                                                               }
           }                                                                                                                 public double[] getScore() {
         }                                                                                                                     return score;
         int n[] = new int[len];                                                                                             }
         for (int i = 0; i < n.length; i++)                                                                                  public void setScore(double scr[]) {
           n[i] = x[i];                                                                                                        for (int i = 0; i < scr.length; i++) {
         SetOfInteger inter = new SetOfInteger(n);                                                                               score[i] = scr[i];
         return inter;                                                                                                         }
                                                                                                                             }
   } //End of method
} //End of class                                                                                                        } //End of class




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)             © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             17   18     Computer Programming using Java                                                           ANSWER 12-13


คลาส Freshmen                                                                                                           โจทย์ข้อที 5 [ระดับยาก]
import java.util.Scanner;
public class Freshmen {
                                                                                                                        1. คลาส Owner
  public static void main(String[] args) {                                                                              public class Owner {
    Scanner kb = new Scanner(System.in);                                                                                   //แอตทริบิว id และแอตทริบิว name
     //กําหนดให้ มีนิสิต 800 คน                                                                                              private int id;
       Student s[] = new Student[800];                                                                                       private String name;


     //กําหนดค่ าเริมต้ นให้ กับเลขประจําตัวของนิสิต ชือนิสิต จํานวนวิชา                                                   //ตัวสร้ างแบบไม่ มีพารามิเตอร์
       for (int i = 0; i < s.length; i++) {                                                                                  public Owner() {
         int id = (53001 + i) * 100 + 21;                                                                                      id = 0;
         System.out.print("Enter name : ");                                                                                    name = "";
         String name = kb.nextLine();                                                                                        }
         s[i] = new Student(id, name, 4);
       }
                                                                                                                           //ตัวสร้ างแบบทีมีพารามิเตอร์ 2 ตัว
     //เรี ยกใช้ งานเมท็อด setScore(…)                                                                                       public Owner(int i, String n)
                                                                                                                               id = i;
       for (int i = 0; i < s.length; i++) {                                                                                    name = n;
         double scr[] = new double[4];                                                                                       }
         for (int j = 0; j < scr.length; j++)
           scr[j] = Math.random() * 100;
         s[i].setScore(scr);                                                                                               //ตัวสร้ างแบบทีมีพารามิเตอร์ 1
       }
                                                                                                                             public Owner(Owner own) {
                                                                                                                               id = own.getID();
     //แสดงเลขประจําตัวนิสิต ชือนิสิต และคะแนนทัง 4 รายวิชาของนิสิตแต่ ละคน                                                    name = own.getName();
                                                                                                                             }
       for (int i = 0; i < s.length; i++) {
         System.out.print(s[i].getID() + "t" + s[i].getName() + "t");
         for (int j = 0; j < s[i].getScore().length; j++)                                                                  //เมท็อด getId(…)
           System.out.print(s[i].getScore()[j] + "t");
         System.out.println();                                                                                               public int getID() {
       }                                                                                                                       return id;
                                                                                                                             }

     //คํานวนหาคะแนนเฉลียของแต่ ละรายวิชา
                                                                                                                           //เมท็อด getName(…)
       for (int j = 0; i < s[0].getScore().length; j++) {
         double sum = 0.0;                                                                                                   public String getName() {
         for (int i = 0; i < s.length; i++) {                                                                                  return name;
           sum += s[i].getScore()[j];                                                                                        }
         }

       }
         System.out.println(sum / s.length);                                                                               //เมท็อด setID(…)
                                                                                                                             public void setID(int i) {
                                                                                                                               id = i;
  } //End of main                                                                                                            }
} //End of class

                                                                                                                           //เมท็อด setName(…)
                                                                                                                             public void setName(String n) {
                                                                                                                               name = n;
                                                                                                                             }


© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)             © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             19   20     Computer Programming using Java                                                           ANSWER 12-13


   //เมท็อด show(…)                                                                                                     3. คลาส City
     public void show() {                                                                                               import java.util.Scanner;
       System.out.println("ID : " + id);                                                                                public class City {
       System.out.println("Name : " + name);                                                                              public static void main(String[] args) {
     }                                                                                                                       //อาเรย์ ของอ๊ อบเจ็ค Land
                                                                                                                               Land a[][] = new Land[50][60];
} //End of class

2. คลาส Land                                                                                                                 //กําหนดค่ าเริมต้ นให้ กับทีดินทุกแปลง
public class Land {
                                                                                                                               for (int i = 0; i          < a.length; i++) {
   //แอตทริบิว n และแอตทริ บว price
                            ิ                                                                                                    for (int j = 0;          j < a[i].length; j++) {
     public Owner n;                                                                                                               Owner n = new          Owner();
     public double price;                                                                                                          a[i][j] = new          Land(n, (int)(Math.random() * 10000000) + 1);
                                                                                                                                 }
                                                                                                                               }
   //ตัวสร้ างแบบไม่ มีพารามิเตอร์
     public Land() {                                                                                                         //อ๊ อบเจ็คชือ ow1
       n = new Owner();
                                                                                                                               Owner ow1 = new Owner(1001, "Tukie");
       price = 0.0;
                                                                                                                               ow1.show();
     }


   //ตัวสร้ างแบบมีพารามิเตอร์ 2 ตัว                                                                                         //อ๊ อบเจ็คชือ ow2
     public Land(Owner w, double p) {                                                                                          Owner ow2 = new Owner(1002, "Annie");
       n = new Owner(w);                                                                                                       ow2.show();
       price = p;
     }
                                                                                                                             //ให้ ow1 ครอบครองทีดินในแปลงตําแหน่ งที (4, 9)
   //เมท็อด setOwner(…)                                                                                                        a[4][9].setOwner(ow1);
                                                                                                                               a[4][9].show();
     public void setOwner(Owner w) {
       n.setID(w.getID());
       n.setName(w.getName());                                                                                               //เปลียนราคาทีดินที ow1 ครองครองอย่ ู
     }
                                                                                                                               a[4][9].setPrice(30000000.0);
                                                                                                                               a[4][9].show();
   //เมท็อด setPrice(…)
     public void setPrice(double p) {                                                                                        //เปลียนให้ ow2 เข้ าไปครอบครองทีดินแปลงที ow1 ครอบครองอย่ ู
       price = p;
     }                                                                                                                         a[4][9].setOwner(ow2);
                                                                                                                               a[4][9].show();

   //เมท็อด show(…)
                                                                                                                             //กําหนดให้ ow1 เข้ าครอบครองทีดินแปลงใหม่ 50 แปลง
     public void show() {
       n.show();                                                                                                               for (int i = 0; i < a.length; i++) {
       System.out.println("Price : " + price);                                                                                   a[i][0].setOwner(ow1);
     }                                                                                                                           a[i][0].setPrice(50000000.0);
                                                                                                                                 a[i][0].show();
                                                                                                                               }
} //End of class

                                                                                                                          } //End of main
                                                                                                                        } //End of class

© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)             © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             21   22     Computer Programming using Java                                                           ANSWER 12-13


โจทย์ข้อที 6 [ระดับยาก]                                                                                                 โจทย์ข้อที 7 [ระดับเทพ]
คลาส SScanner                                                                                                           1. คลาส Fruit
import java.util.Scanner;                                                                                               public class Fruit {
import java.io.InputStream;
public class SScanner {
                                                                                                                           //แอตทริบิวประจําอ๊ อบเจ็คชือ name และ weigh
  //แอตทริบิว kb                                                                                                             public String name;
                                                                                                                             public double weigh;
    public Scanner kb;

                                                                                                                           //ตัวสร้ าง 4 แบบ ดังต่ อไปนี
  //ตัวสร้ าง                                                                                                                public Fruit() {
     public SScanner(InputStream in) {                                                                                         name = "";
       kb = new Scanner(in);                                                                                                   weigh = 0.0;
     }                                                                                                                       }

                                                                                                                             public Fruit(String n) {
  //เมท็อด nextInt(…)                                                                                                          name = n;
                                                                                                                               weigh = wh[(int)(Math.random() * 5)];
     public int nextInt() {                                                                                                  }
       return kb.nextInt();
     }                                                                                                                       public Fruit(String n, double w) {
                                                                                                                               name = n;
                                                                                                                               weigh = w;
  //เมท็อด nextLine(…)                                                                                                       }
     public String nextLine() {
                                                                                                                             public Fruit(Fruit f) {
       String s = "";
                                                                                                                               name = f.name;
       while (true) {
                                                                                                                               weigh = f.weigh;
         s = kb.nextLine();
                                                                                                                             }
         if(s.length() != 0) break;
       }
       return s;                                                                                                           //เมท็อดประจําอ๊ อบเจ็คชือ showFruit
     }
                                                                                                                             public void showFruit() {
                                                                                                                               System.out.println(name + " " + weigh + " kg.");
} //End of class                                                                                                             }

คลาส TestSScanner                                                                                                       } //End of class
public class TestSScanner {
  public static void main(String[] args) {
                                                                                                                        2. คลาส Basket
       SScanner kb = new SScanner(System.in);
                                                                                                                        public class Basket {
       System.out.print("Enter Int: ");
       int a = kb.nextInt();                                                                                               //แอตทริบิวประจําอ๊ อบเจ็คชือ fruit และ count
       System.out.print("Enter Str: ");                                                                                      public Fruit fruit[];
       String b = kb.nextLine();                                                                                             public int count;
       System.out.println(a + "," + b);

                                                                                                                           //ตัวสร้ างแบบไม่ มีพารามิเตอร์
  } //End of main
} //End of class                                                                                                             public Basket() {
                                                                                                                               fruit = new Fruit[10];
                                                                                                                               count = 0;
                                                                                                                             }




© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)             © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             23   24     Computer Programming using Java                                                           ANSWER 12-13


   //ตัวสร้ างแบบหนึงพารามิเตอร์ ชนิดจํานวนเต็ม                                                                            //เมท็อดประจําอ๊ อบเจ็คชือ getTotalWeigh
     public Basket(int n) {                                                                                                  public double getTotalWeigh() {
       fruit = new Fruit[n];                                                                                                   double weigh = 0.0;
       count = 0;                                                                                                              for (int i = 0; i < count; i++) {
     }                                                                                                                           weigh += fruit[i].weigh;
                                                                                                                               }
                                                                                                                               return weigh;
   //ตัวสร้ างแบบหนึงพารามิเตอร์ ชนิด Basket                                                                                 }
     public Basket(Basket b) {
       fruit = new Fruit[b.fruit.length];
       for (int i = 0; i < fruit.length; i++) {                                                                            //เมท็อดประจําอ๊ อบเจ็คชือ showBasket
         fruit[i] = b.fruit[i];                                                                                              public void showBasket() {
       }                                                                                                                       for (int i = 0; i < count; i++) {
       count = b.count;                                                                                                          fruit[i].showFruit();
     }                                                                                                                         }
                                                                                                                             }
   //เมท็อดประจําอ๊ อบเจ็คชือ isFull
                                                                                                                        } //End of class
     public boolean isFull() {
       return count == fruit.length;
     }                                                                                                                  3. คลาส FruitBasket
                                                                                                                        import java.util.Scanner;
                                                                                                                        public class FruitBasket {
   //เมท็อดประจําอ๊ อบเจ็คชือ isEmpty                                                                                      //เมท็อดประจําคลาสชือ getFruitBasket
     public boolean isEmpty() {                                                                                              public static Basket getFruitBasket(String f, double w, int n) {
       return count == 0;                                                                                                      Basket bsk = new Basket(n);
     }                                                                                                                         Fruit frt[] = new Fruit[n];
                                                                                                                               for (int i = 0; i < frt.length; i++) {
                                                                                                                                 if (w <= 0.0) frt[i] = new Fruit(f);
   //เมท็อดประจําอ๊ อบเจ็คชือ takeIn                                                                                             else frt[i] = new Fruit(f, w);
     public void takeIn(Fruit f) {                                                                                               bsk.takeIn(frt[i]);
       if (!isFull()) {                                                                                                        }
         fruit[count] = f;                                                                                                     return bsk;
         count++;                                                                                                            }
       } else {
         System.out.println("Basket Full");
       }                                                                                                                   //เมท็อดประจําคลาสชือ showFruitBasket
     }                                                                                                                       public static void showFruitBasket(Basket b) {
                                                                                                                               System.out.println("Total Weigh: " + b.getTotalWeigh());
                                                                                                                               System.out.println("Total Fruit: " + b.count);
   //เมท็อดประจําอ๊ อบเจ็คชือ takeOut                                                                                          System.out.println("List of Fruit: ");
     public Fruit takeOut() {                                                                                                  b.showBasket();
       if (!isEmpty()) {                                                                                                     }
         count--;
         Fruit f = fruit[count];
         fruit[count] = null;                                                                                             //เมท็อด main
         return f;                                                                                                        public static void main(String[] args) {
       } else {                                                                                                              //สร้ างกระเช้ าผลไม้ ขนมา 3 กระเช้ า
                                                                                                                                                    ึ
         System.out.println("Basket Empty");
         return null;                                                                                                          Basket     x[] = new Basket[3];
       }                                                                                                                       x[0] =     getFruitBasket("Apple", 0.12, 25);
     }                                                                                                                         x[1] =     getFruitBasket("Orange", 0.1, 30);
                                                                                                                               x[2] =     getFruitBasket("Melon", 0.45, 15);



© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)             © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             25   26     Computer Programming using Java                                                           ANSWER 12-13


     //สร้ างกระเช้ าผลไม้ เปล่ าๆ ขึนมาอีก 6 กระเช้า                                                                      //เมท็อด getID(…)
       Basket y[] = new Basket[6];                                                                                           public int getID() {
       for(int i = 0; i < y.length; i++) {                                                                                     return id;
         y[i] = new Basket();                                                                                                }
         int j = 0;
         while (!y[i].isFull()) {
           if (!x[j].isEmpty()) y[i].takeIn(x[j].takeOut());                                                               //เมท็อด getName(…)
           if (y[i].getTotalWeigh() > 2.0) {
             Fruit tmp = y[i].takeOut();                                                                                     public String getName() {
             break;                                                                                                            return name;
           }                                                                                                                 }
           j++;
           if (j > 2) j = 0;
         }                                                                                                                 //เมท็อด getNoob(…)
         System.out.println("-----Basket " + (i + 1) + "-----");                                                             public int getNoob() {
         showFruitBasket(y[i]);                                                                                                return noob;
       }                                                                                                                     }

  } //End of main
} //End of class                                                                                                           //เมท็อด set(…)
                                                                                                                             public    void set(Student s) {
                                                                                                                               id =    s.getID();
โจทย์ข้อที 8 [ระดับเทพ]                                                                                                        name    = s.getName();
                                                                                                                               noob    = s.getNoob();
คลาส Student                                                                                                                 }
public class Student {
   //แอตทริบิว id แอตทริบิว name และแอตทริ บว noob
                                            ิ
     private int id;                                                                                                       //เมท็อด show(…)
     private String name;                                                                                                    public void show() {
     private int noob;                                                                                                         String s = "";
                                                                                                                               if (noob == 1) s = "1-Noob";
                                                                                                                               else if (noob == 2) s = "2-Father Noob";
   //ตัวสร้ างแบบไม่ มีพารามิเตอร์                                                                                             else if (noob == 3) s = "3-God Noob";
     Student() {                                                                                                               else if (noob == 4) s = "4-Hof Noob";
       id = 0;                                                                                                                 else s = "Unknown";
       name = "";                                                                                                              System.out.println(id + " " + name + " [" + s + "]");
       noob = 0;                                                                                                             }
     }
                                                                                                                        } //End of class
   //ตัวสร้ างแบบทีรับพารามิเตอร์ 3 ตัว
     Student(int i, String n, int x) {                                                                                  คลาส Room
       id = i;                                                                                                          public class Room {
       name = n;
       noob = x;
                                                                                                                           //แอตทริบิว s ทีเป็ นอาเรย์ 2 มิติชนิด Student
     }                                                                                                                       public static Student s[][] = new Student[16][40];


   //ตัวสร้ างแบบทีรับพารามิเตอร์ เป็ นชนิด Student
     Student(Student s) {
       set(s);
     }


© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)             © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 12-13                                                      Computer Programming using Java             27   28     Computer Programming using Java                                                           ANSWER 12-13


   //เมท็อด setSeat(…)                                                                                                     //เมท็อด main(…)
     public static void setSeat(Student st) {                                                                                public static void main(String[] args) {
       int startRow = s.length - 1 - (s.length / 4) * (4 - st.getNoob());                                                      for (int i = 0; i < s.length; i++) {
       for (int i = startRow; i < startRow + 4; i++) {                                                                           for (int j = 0; j < s[i].length; j++) {
         for (int j = 0; j < s[i].length; j++) {                                                                                   s[i][j] = new Student();
           if (s[i][j].getName().equals("") &&                                                                                   }
               j != s[i].length / 2 &&                                                                                         }
               j != s[i].length / 2 - 1) {                                                                                     Student s1 = new Student(53300121, "Akeudom", 1); setSeat(s1);
             s[i][j].set(st);                                                                                                  Student s2 = new Student(53300221, "Phakhin", 1); setSeat(s2);
             return;                                                                                                           Student s3 = new Student(53300321, "Taparb", 2); setSeat(s3);
           }                                                                                                                   Student s4 = new Student(53300421, "Ong-ard", 3); setSeat(s4);
         }                                                                                                                     Student s5 = new Student(53300521, "Wongyos", 4); setSeat(s5);
       }                                                                                                                       showRoom();
       System.out.println("No Reserved Seat");                                                                                 showStudent();
     }                                                                                                                       }


   //เมท็อด showroom(…)                                                                                                 } //End of class

     public static void showRoom() {
       for (int i = 0; i < s.length; i++) {
         for (int j = 0; j < s[i].length; j++) {
           if (j == s[i].length / 2 || j == s[i].length / 2 - 1) {
             System.out.print(" ");
           } else {
             if (!s[i][j].getName().equals(""))
               System.out.print(s[i][j].getName().substring(0, 1) + " ");
             else
               System.out.print("# ");
           }
         }
         System.out.println();
       }
     }


   //เมท็อด showStudent(…)
     public static void showStudent(int r, int c) {
       System.out.print("Seat Row " + r + " Column " + c + "t");
       s[r + s.length - 2][c - 1].show();
     }


   //เมท็อด showStudent(…)
     public static void showStudent() {
       for (int i = s.length - 1; i >= 0; i--) {
         for (int j = 0; j < s[i].length; j++) {
           if (!s[i][j].getName().equals("")) {
             System.out.print("Seat Row " + (s.length - i) + " Column " +
                             (j + 1) + "t");
             s[i][j].show();
           }
         }
       }
     }


© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)             © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)

Más contenido relacionado

La actualidad más candente

Java-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 10-11 (For Print)Java-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 10-11 (For Print)Wongyos Keardsri
 
Java-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsJava-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsWongyos Keardsri
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 RecursionsWongyos Keardsri
 
Java-Chapter 01 Introduction to Java Programming
Java-Chapter 01 Introduction to Java ProgrammingJava-Chapter 01 Introduction to Java Programming
Java-Chapter 01 Introduction to Java ProgrammingWongyos Keardsri
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นThanachart Numnonda
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมThanachart Numnonda
 
Java Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionJava Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionIMC Institute
 
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญาสูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญาKanomwan Jeab
 
Java Programming: การจัดการกับข้อผิดพลาด
Java Programming: การจัดการกับข้อผิดพลาดJava Programming: การจัดการกับข้อผิดพลาด
Java Programming: การจัดการกับข้อผิดพลาดThanachart Numnonda
 
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชันค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชันsawed kodnara
 
2D Graphics and Animations in Java World
2D Graphics and Animations in Java World2D Graphics and Animations in Java World
2D Graphics and Animations in Java Worldkunemata
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowWongyos Keardsri
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysWongyos Keardsri
 
บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์Theeravaj Tum
 

La actualidad más candente (20)

Java-Chapter 08 Methods
Java-Chapter 08 MethodsJava-Chapter 08 Methods
Java-Chapter 08 Methods
 
Java-Answer Chapter 01-04
Java-Answer Chapter 01-04Java-Answer Chapter 01-04
Java-Answer Chapter 01-04
 
Java-Answer Chapter 08-09
Java-Answer Chapter 08-09Java-Answer Chapter 08-09
Java-Answer Chapter 08-09
 
Java-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 10-11 (For Print)Java-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 10-11 (For Print)
 
Java-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsJava-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and Objects
 
Java-Answer Chapter 07
Java-Answer Chapter 07Java-Answer Chapter 07
Java-Answer Chapter 07
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 Recursions
 
Java-Chapter 01 Introduction to Java Programming
Java-Chapter 01 Introduction to Java ProgrammingJava-Chapter 01 Introduction to Java Programming
Java-Chapter 01 Introduction to Java Programming
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่น
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุม
 
Java Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionJava Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and Collection
 
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญาสูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
 
Java Programming: การจัดการกับข้อผิดพลาด
Java Programming: การจัดการกับข้อผิดพลาดJava Programming: การจัดการกับข้อผิดพลาด
Java Programming: การจัดการกับข้อผิดพลาด
 
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชันค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
 
2D Graphics and Animations in Java World
2D Graphics and Animations in Java World2D Graphics and Animations in Java World
2D Graphics and Animations in Java World
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindow
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional Arrays
 
บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์
 
Expolog clipvidva
Expolog clipvidvaExpolog clipvidva
Expolog clipvidva
 

Destacado

How to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramHow to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramWongyos Keardsri
 
The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsWongyos Keardsri
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingWongyos Keardsri
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemWongyos Keardsri
 
SysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageSysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageWongyos Keardsri
 

Destacado (6)

How to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramHow to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master Program
 
IP address anonymization
IP address anonymizationIP address anonymization
IP address anonymization
 
The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applications
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script Programming
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating System
 
SysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageSysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming Language
 

Más de Wongyos Keardsri

Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IWongyos Keardsri
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsWongyos Keardsri
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsWongyos Keardsri
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityWongyos Keardsri
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 CountingWongyos Keardsri
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsWongyos Keardsri
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIWongyos Keardsri
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IWongyos Keardsri
 
Discrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesDiscrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesWongyos Keardsri
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesWongyos Keardsri
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationWongyos Keardsri
 
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsJava-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsWongyos Keardsri
 
Java-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysJava-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysWongyos Keardsri
 
Java-Chapter 06 File Operations
Java-Chapter 06 File OperationsJava-Chapter 06 File Operations
Java-Chapter 06 File OperationsWongyos Keardsri
 
Java-Chapter 05 String Operations
Java-Chapter 05 String OperationsJava-Chapter 05 String Operations
Java-Chapter 05 String OperationsWongyos Keardsri
 

Más de Wongyos Keardsri (19)

Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part III
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part II
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part I
 
Discrete-Chapter 10 Trees
Discrete-Chapter 10 TreesDiscrete-Chapter 10 Trees
Discrete-Chapter 10 Trees
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 Algorithms
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 Relations
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 Probability
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 Counting
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and Proofs
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part II
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part I
 
Discrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesDiscrete-Chapter 03 Matrices
Discrete-Chapter 03 Matrices
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and Sequences
 
Discrete-Chapter 01 Sets
Discrete-Chapter 01 SetsDiscrete-Chapter 01 Sets
Discrete-Chapter 01 Sets
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling Computation
 
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsJava-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and Applications
 
Java-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysJava-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional Arrays
 
Java-Chapter 06 File Operations
Java-Chapter 06 File OperationsJava-Chapter 06 File Operations
Java-Chapter 06 File Operations
 
Java-Chapter 05 String Operations
Java-Chapter 05 String OperationsJava-Chapter 05 String Operations
Java-Chapter 05 String Operations
 

Java-Answer Chapter 12-13 (For Print)

  • 1. ANSWER 12-13 Computer Programming using Java 1 2 Computer Programming using Java ANSWER 12-13 CHAPTER คลาสและอ๊อบเจ็ค โจทย์ข้อที 3 [ระดับง่ าย] ANS-12 ANS- (Classes and Objects) public class Course { //แอตทริบิว 3 ตัว โจทย์ข้อที 1 [ระดับง่ าย] private int id; public String title; 1 public class Num { protected double credit; 2 private int x; 3 private double y; Attribute 4 public Num() { //ตัวสร้ างแบบไม่ มีพารามิเตอร์ ใดๆ 5 x = 0; y = 0.0; 6 } public Course() { Constructor 7 public Num(int m, double n) { id = 0; 8 x = m; y = n; title = ""; 9 } credit = 0.0; 10 public int addNX(int n) { } 11 return n + x; Class (ต้ นแบบ) 12 } 13 public double addNY(double n) { //ตัวสร้ างแบบมีพารามิเตอร์ 3 ตัว 14 return n + y; Method ผลลัพธ์ ทีได้ 15 } public Course(int i, String t, double c) { 16 public void showXY() { 5 id = i; 17 System.out.println(x); 7.0 title = t; 18 System.out.println(y); 9 credit = c; 19 } } 20 } 20.0 1 public class RunNum { //เมท็อด setID(…) 2 public static void main(String[] args){ public void setID(int i) { 3 Num obj = new Num(5, 7.0); Object id = i; 4 obj.showXY(); } 5 int a = obj.addNX(4); 6 double b = obj.addNY(13.0); Class (ประมวลผล) 7 System.out.println(a); Main Method //เมท็อด setTitle(…) 8 System.out.println(b); 9 } public void setTitle(String t) { 10 } title = t; } //เมท็อด setCredit(…) โจทย์ ข้อที 2 [ระดับง่ าย] public void setCredit(double c) { 1) 6) 11) credit = c; } 2) 7) 12) 3) 8) 13) //เมท็อด getLevel(…) 4) 9) 14) public String getLevel() { if (id % 1000 / 100 <= 4) { 5) 10) 15) return "Undergraduate"; } else { return "Graduate"; } } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 2. ANSWER 12-13 Computer Programming using Java 3 4 Computer Programming using Java ANSWER 12-13 //เมท็อด getFaculty(…) โจทย์ข้อที 6 [ระดับง่ าย] public int getFaculty() { //ประกาศหัวคลาส TestNumber return id / 100000; } import java.util.Scanner; public class TestNumber { //เมท็อด getDepartment(…) public static void main(String[] args) { public int getDepartment() { Scanner kb = new Scanner(System.in); return id / 1000 % 100; } //อ๊ อบเจ็คชือ no1 Number no1 = new Number(); //เมท็อด toString(…) public String toString() { //อ๊ อบเจ็คชือ no2 return id + " " + title + " (" + credit + ")"; System.out.print("Enter x : "); } double x = kb.nextDouble(); System.out.print("Enter y : "); } //End of class double y = kb.nextDouble(); Number no2 = new Number(x, y); โจทย์ข้อที 4 [ระดับง่ าย] //แสดงผลการบวก การลบ การคณ การหาร และการม็อด ู 9 1) แอตทริบิว y หรื อตัวแปร y 5 System.out.println("Add: " + no2.add()); 7 2) ตัวแปร y (ในบรรทัดที 13) System.out.println("Sub: " + no2.sub()); 1 System.out.println("Mul: " + no2.mul()); 1 System.out.println("Div: " + no2.div()); System.out.println("Mod: " + no2.mod()); 12 11 1 } //End of main 12 } //End of class โจทย์ข้อที 7 [ระดับง่ าย] โจทย์ข้อที 5 [ระดับง่ าย] 1) public double data; 0, 101,Taksin 2) private static int var = 10; 103,Apisit Taksin:U 3) protected static boolean check; Apisit:S 4) private String stdName[] = new String[351]; 5) public static int m[][] = new int[8][5]; 6) private void show(String stdName, double grade) 7) protected static int search(int num[], int x) 8) public double[][] mulMatrix(double a[][], double b[][]) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 3. ANSWER 12-13 Computer Programming using Java 5 6 Computer Programming using Java ANSWER 12-13 9) public static int[] union(int x[], int y[]) //ตัวสร้ างแบบมีพารามิเตอร์ สองตัว public Dice(int f, int v) { face = f; value = v; โจทย์ ข้อที 8 [ระดับง่ าย] } ข้ อ คําตอบ ข้ อ คําตอบ ข้ อ คําตอบ ข้ อ คําตอบ 1. B 6. A 11. C 16. D //เมท็อดประจําอ๊ อบเจ็คชือ roll public void roll() { 2. D 7. D 12. B 17. C value = (int)(Math.random() * face) + 1; } 3. B 8. D 13. D 18. D 4. C 9. A 14. A 19. B //เมท็อดประจําอ๊ อบเจ็คชือ setValue 5. B 10. D 15. B 20. C public void setValue(int v) { value = v; } โจทย์ข้อที 9 [ระดับปานกลาง] 0,0 //เมท็อดประจําอ๊ อบเจ็คชือ getFace 6,10 public int getFace() { Good By A return face; See You F } 0,0 5,7 5,7 //เมท็อดประจําอ๊ อบเจ็คชือ getValue 9,7 public int getValue() { 9,7 return value; I Love Java } //เมท็อดประจําอ๊ อบเจ็คชือ show โจทย์ข้อที 10 [ระดับปานกลาง] public void show() { public class Dice { System.out.println("Face: " + face); System.out.println("Value: " + value); //แอตทริบิวประจําอ๊ อบเจ็คชือ face และ value } public int face; public int value; } //End of class //ตัวสร้ างแบบไม่ มีพารามิเตอร์ public Dice() { face = 2; roll(); } //ตัวสร้ างแบบมีพารามิเตอร์ หนึงตัว public Dice(int f) { face = f; roll(); } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 4. ANSWER 12-13 Computer Programming using Java 7 8 Computer Programming using Java ANSWER 12-13 public class TestDice { public class TestRealNumber { /* สร้ างลกเต๋ า 3 ลก ในรปของอ๊ อบเจ็คทีชือ d1, d2 และ d3 */ ู ู ู /* สร้ างจํานวนจริ ง 1 จํานวนในรปของอ๊ อบเจ็ค r */ ู public static void main(String[] args) { public static void main(String[] args) { Dice d1 = new Dice(); RealNumber r = new RealNumber(); Dice d2 = new Dice(13); System.out.println(r.plus(15)); Dice d3 = new Dice(30, 9); System.out.println(r.diff(50)); d1.show(); } d2.show(); d3.show(); } //End of class for (int i = 1; i <= 20; i++) { d1.roll(); System.out.println(d1.getValue()); d2.roll(); โจทย์ข้อที 12 [ระดับปานกลาง] System.out.println(d2.getValue()); public class Account { d3.roll(); private double balance; System.out.println(d3.getValue()); public Account() { System.out.println("-------"); balance = 0.0; } } } public Account(double money) { balance = money; } //End of class } public void deposit(double money) { balance += money; โจทย์ข้อที 11 [ระดับปานกลาง] } public class RealNumber { public double withdraw(double money) { if (balance >= money) { //แอตทริบิวประจําอ๊ อบเจ็คแบบ public ชือ num balance -= money; public double num; return money; } else { return 0.0; //ตัวสร้ างแบบไม่ มีพารามิเตอร์ } public RealNumber() { } num = 0.0; public double getbalance() { } return balance; } } //End of Class //ตัวสร้ างแบบมีพารามิเตอร์ public RealNumber(double n) { num = n; } สร้ างคลาส TestAccount public class TestAccount { //เมท็อดประจําอ๊ อบเจ็คชือ plus public static void main(String[] args) { Account acc = new Account(2000); public double plus(double n) { System.out.println("New Account"); return num + n; System.out.println("Balance : " + acc.getbalance()); } double money = acc.withdraw(1500); System.out.println("Withdraw : " + money); //เมท็อดประจําอ๊ อบเจ็คชือ diff System.out.println("Balance : " + acc.getbalance()); acc.deposit(800); public double diff(double n) { System.out.println("Deposit : " + 800); double x = num – n; System.out.println("Balance : " + acc.getbalance()); if (x >= 0) return x; } else return –x; } } } //End of class © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 5. ANSWER 12-13 Computer Programming using Java 9 10 Computer Programming using Java ANSWER 12-13 โจทย์ข้อที 13 [ระดับปานกลาง] public void addFive(int c) { public class PiggyBank { if (getTotal() + 5 * c <= size) five = five + c; (1) แอตทริบิว one, two, five และ ten } else System.out.println("Piggy Bank Full"); (2) แอตทริบิว size public int one, two, five, ten; public int size; (10) เมท็อดประจําอ๊ อบเจ็คชือ addTen(…) public void addTen(int c) { if (getTotal() + 10 * c <= size) ten = ten + c; (3) ตัวสร้ าง 2 แบบ else System.out.println("Piggy Bank Full"); } PiggyBank() { clear(); size = 100; }//End of class } PiggyBank(int s) { clear(); สร้ างคลาส TestPiggyBank size = s; public class TestPiggyBank { } public static void main(String[] args) { PiggyBank pb = new PiggyBank(500); System.out.println("Money Total : " + pb.getTotal()); (4) เมท็อดประจําอ๊ อบเจ็คชือ clear(…) pb.addTwo(34); public void clear() { System.out.println("Add 2 Baht Money : " + 34); one = two = five = ten = 0; System.out.println("Money Total : " + pb.getTotal()); } pb.addTen(13); System.out.println("Add 10 Baht Money : " + 13); System.out.println("Money Total : " + pb.getTotal()); (5) เมท็อดประจําอ๊ อบเจ็คชือ getTotal(…) public int getTotal() { }//End of main return one + (2 * two) + (5 * five) + (10 * ten); }//End of class } (6) เมท็อดประจําอ๊ อบเจ็คชือ full(…) public boolean full() { return getTotal() >= size; } (7) เมท็อดประจําอ๊ อบเจ็คชือ addOne(…) public void addOne(int c) { if (getTotal() + c <= size) one = one + c; else System.out.println("Piggy Bank Full"); } (8) เมท็อดประจําอ๊ อบเจ็คชือ addTwo(…) public void addTwo(int c) { if (getTotal() + 2 * c <= size) two = two + c; else System.out.println("Piggy Bank Full"); } (9) เมท็อดประจําอ๊ อบเจ็คชือ addFive(…) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 6. ANSWER 12-13 Computer Programming using Java 11 12 Computer Programming using Java ANSWER 12-13 CHAPTER คลาสและอ๊อบเจ็คแบบซับซ้อน //ตัวสร้ างแบบไม่ มีพารามิเตอร์ ANS-13 ANS- (Advanced Classes and Objects) public Coin() { side = "H"; value = 1; โจทย์ข้อที 1 [ระดับปานกลาง] } เรียกตัวสร้ างใด ผลลัพธ์ ทีแสดง (ระบุหมายเลข) ขึนบนจอภาพคือ //ตัวสร้ างแบบมีพารามิเตอร์ สองตัว public Coin(String s, int v) { side = s; 1 0,0 value = v; } 2 13,2 //ตัวสร้ างแบบมีพารามิเตอร์ หนึงตัว public Coin(Coin c) { side = c.side; 3 13,2 value = c.value; } 2 3 7,11 //เมท็อด flip(…) public void flip() { side = list[(int)(Math.random() * 2)]; 2 11,13 } 0,0 //เมท็อด reverse(…) 1 3 public void reverse() { if (side.equals("H")) { side = "T"; public class Num { } else { ... side = "H"; public boolean isEquals(int x, int y) { } } return this.x == x && this.y == y; } //เมท็อด equals(…) public boolean isEquals(Num n) { public boolean equals(Coin c) { return x == n.x && y == n.y; return side == c.side && value == c.value; } } } //End of class } //End of class โจทย์ข้อที 2 [ระดับยาก] public class Coin { //แอตทริบิว side และ value public int value; public String side; © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 7. ANSWER 12-13 Computer Programming using Java 13 14 Computer Programming using Java ANSWER 12-13 /* คลาส Dem oCoin */ //เมท็อด rem oveDuplicatedMem bers(…) public class Dem oCoin { public int[ ] rem oveDuplicatedMem bers(int x [ ] ) { public static void m ain(String[ ] args) { ... } //E nd of m ethod Coin c1 = new Coin(); Coin c2 = new Coin("H ", 1); Coin c3 = new Coin("T", 5); //เมท็อด eq uals(…) Coin c4 = new Coin(c3 ); public boolean eq uals(SetOfInteger x ) { for (int i = 1; i <= 20; i+ + ) { c1.flip(); if (x .iSet.length == iSet.length) { c2.reverse(); for (int i = 0; i < x .iSet.length; i+ + ) { c3 .flip(); if (x .iSet[ i] ! = iSet[ i] ) return false; c4.reverse(); } Sy stem .out.println(c1.eq uals(c2)); return true; Sy stem .out.println(c3 .eq uals(c4)); } else { } return false; } } //E nd of m ain } //E nd of m ethod } //E nd of class //เมท็อด toString(…) โจทย์ข้อที 3 [ระดับยาก] public String toString() { public class SetOfInteger { String s = ""; //แอตทริบิว iSet for (int i = 0; i < iSet.length; i+ + ) s = s + iSet[ i] + " "; public int iSet[ ] ; return s; } //E nd of m ethod //ตัวสร้ างแบบทีรั บพารามิเตอร์ 1 ตัวทีเป็ นอาเรย์ public SetOfInteger(int[ ] x ) { //เมท็อด addE lem ent(…) x = sort(x ); public void addE lem ent(int a) { x = rem oveDuplicatedMem bers(x ); iSet = new int[ x .length] ; int x [ ] = new int[ iSet.length + 1] ; for (int i = 0; i < iSet.length; i+ + ) { for (int i = 0; i < x .length - 1; i+ + ) iSet[ i] = x [ i] ; x [ i] = iSet[ i] ; } x [ x .length - 1] = a; x = sort(x ); x = rem oveDuplicatedMem bers(x ); } //E nd of constructor iSet = x ; //ตัวสร้ างแบบทีรั บพารามิเตอร์ 1 ตัวทีเป็ นชนิด SetOfInteger } //E nd of m ethod public SetOfInteger(SetOfInteger s) { iSet = new int[ s.iSet.length] ; //เมท็อด findE lem ent(…) for (int i = 0; i < iSet.length; i+ + ) { public int findE lem ent(int a) { iSet[ i] = s.iSet[ i] ; } for (int i = 0; i < iSet.length; i+ + ) if (iSet[ i] == a) return i; return - 1; } //E nd of constructor } //E nd of m ethod //เมท็อด sort(…) public int[ ] sort(int x [ ] ) { ... } //E nd of m ethod © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 8. ANSWER 12-13 Computer Programming using Java 15 16 Computer Programming using Java ANSWER 12-13 //เมท็อด isSubset(…) โจทย์ข้อที 4 [ระดับยาก] public boolean isSubset(SetOfInteger s) { public class Student { int count = 0; //แอตทริบิวประจําอ๊ อบเจ็ค 3 ตัว for (int i = 0; i < s.iSet.length; i++) { for (int j = 0; j < iSet.length; j++) { private int id; if (s.iSet[i] == iSet[j]) { private String name; count++; private double score[]; break; } } //ตัวสร้ างแบบไม่ กาหนดพารามิเตอร์ ํ } Student() { return s.iSet.length == count; id = 0; name = ""; } //End of method score = new double[0]; } //เมท็อด union(…) public SetOfInteger union(SetOfInteger s) { //ตัวสร้ างแบบ 3 พารามิเตอร์ int x[] = new int[s.iSet.length + iSet.length]; Student(int i, String n, int x) { for (int i = 0; i < x.length; i++) { id = i; if (i < iSet.length) x[i] = iSet[i]; name = n; else x[i] = s.iSet[i - iSet.length]; score = new double[x]; } } SetOfInteger un = new SetOfInteger(x); return un; //ตัวสร้ างแบบ 1 พารามิเตอร์ } //End of method Student(Student s) { id = s.getID(); //เมท็อด intersect(…) name = s.getName(); score = s.getScore(); public SetOfInteger intersect(SetOfInteger s) { } int x[] = new int[s.iSet.length]; int len = 0; for (int i = 0; i < s.iSet.length; i++) { //เมท็อดประจําอ๊ อบเจ็ค 4 เมท็อด getID(…), getName(…), getScore(…)และ setScore(…) for (int j = 0; j < this.iSet.length; j++) { public int getID() { if (s.iSet[i] == this.iSet[j]) { return id; x[len] = s.iSet[i]; } len++; public String getName() { break; return name; } } } public double[] getScore() { } return score; int n[] = new int[len]; } for (int i = 0; i < n.length; i++) public void setScore(double scr[]) { n[i] = x[i]; for (int i = 0; i < scr.length; i++) { SetOfInteger inter = new SetOfInteger(n); score[i] = scr[i]; return inter; } } } //End of method } //End of class } //End of class © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 9. ANSWER 12-13 Computer Programming using Java 17 18 Computer Programming using Java ANSWER 12-13 คลาส Freshmen โจทย์ข้อที 5 [ระดับยาก] import java.util.Scanner; public class Freshmen { 1. คลาส Owner public static void main(String[] args) { public class Owner { Scanner kb = new Scanner(System.in); //แอตทริบิว id และแอตทริบิว name //กําหนดให้ มีนิสิต 800 คน private int id; Student s[] = new Student[800]; private String name; //กําหนดค่ าเริมต้ นให้ กับเลขประจําตัวของนิสิต ชือนิสิต จํานวนวิชา //ตัวสร้ างแบบไม่ มีพารามิเตอร์ for (int i = 0; i < s.length; i++) { public Owner() { int id = (53001 + i) * 100 + 21; id = 0; System.out.print("Enter name : "); name = ""; String name = kb.nextLine(); } s[i] = new Student(id, name, 4); } //ตัวสร้ างแบบทีมีพารามิเตอร์ 2 ตัว //เรี ยกใช้ งานเมท็อด setScore(…) public Owner(int i, String n) id = i; for (int i = 0; i < s.length; i++) { name = n; double scr[] = new double[4]; } for (int j = 0; j < scr.length; j++) scr[j] = Math.random() * 100; s[i].setScore(scr); //ตัวสร้ างแบบทีมีพารามิเตอร์ 1 } public Owner(Owner own) { id = own.getID(); //แสดงเลขประจําตัวนิสิต ชือนิสิต และคะแนนทัง 4 รายวิชาของนิสิตแต่ ละคน name = own.getName(); } for (int i = 0; i < s.length; i++) { System.out.print(s[i].getID() + "t" + s[i].getName() + "t"); for (int j = 0; j < s[i].getScore().length; j++) //เมท็อด getId(…) System.out.print(s[i].getScore()[j] + "t"); System.out.println(); public int getID() { } return id; } //คํานวนหาคะแนนเฉลียของแต่ ละรายวิชา //เมท็อด getName(…) for (int j = 0; i < s[0].getScore().length; j++) { double sum = 0.0; public String getName() { for (int i = 0; i < s.length; i++) { return name; sum += s[i].getScore()[j]; } } } System.out.println(sum / s.length); //เมท็อด setID(…) public void setID(int i) { id = i; } //End of main } } //End of class //เมท็อด setName(…) public void setName(String n) { name = n; } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 10. ANSWER 12-13 Computer Programming using Java 19 20 Computer Programming using Java ANSWER 12-13 //เมท็อด show(…) 3. คลาส City public void show() { import java.util.Scanner; System.out.println("ID : " + id); public class City { System.out.println("Name : " + name); public static void main(String[] args) { } //อาเรย์ ของอ๊ อบเจ็ค Land Land a[][] = new Land[50][60]; } //End of class 2. คลาส Land //กําหนดค่ าเริมต้ นให้ กับทีดินทุกแปลง public class Land { for (int i = 0; i < a.length; i++) { //แอตทริบิว n และแอตทริ บว price ิ for (int j = 0; j < a[i].length; j++) { public Owner n; Owner n = new Owner(); public double price; a[i][j] = new Land(n, (int)(Math.random() * 10000000) + 1); } } //ตัวสร้ างแบบไม่ มีพารามิเตอร์ public Land() { //อ๊ อบเจ็คชือ ow1 n = new Owner(); Owner ow1 = new Owner(1001, "Tukie"); price = 0.0; ow1.show(); } //ตัวสร้ างแบบมีพารามิเตอร์ 2 ตัว //อ๊ อบเจ็คชือ ow2 public Land(Owner w, double p) { Owner ow2 = new Owner(1002, "Annie"); n = new Owner(w); ow2.show(); price = p; } //ให้ ow1 ครอบครองทีดินในแปลงตําแหน่ งที (4, 9) //เมท็อด setOwner(…) a[4][9].setOwner(ow1); a[4][9].show(); public void setOwner(Owner w) { n.setID(w.getID()); n.setName(w.getName()); //เปลียนราคาทีดินที ow1 ครองครองอย่ ู } a[4][9].setPrice(30000000.0); a[4][9].show(); //เมท็อด setPrice(…) public void setPrice(double p) { //เปลียนให้ ow2 เข้ าไปครอบครองทีดินแปลงที ow1 ครอบครองอย่ ู price = p; } a[4][9].setOwner(ow2); a[4][9].show(); //เมท็อด show(…) //กําหนดให้ ow1 เข้ าครอบครองทีดินแปลงใหม่ 50 แปลง public void show() { n.show(); for (int i = 0; i < a.length; i++) { System.out.println("Price : " + price); a[i][0].setOwner(ow1); } a[i][0].setPrice(50000000.0); a[i][0].show(); } } //End of class } //End of main } //End of class © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 11. ANSWER 12-13 Computer Programming using Java 21 22 Computer Programming using Java ANSWER 12-13 โจทย์ข้อที 6 [ระดับยาก] โจทย์ข้อที 7 [ระดับเทพ] คลาส SScanner 1. คลาส Fruit import java.util.Scanner; public class Fruit { import java.io.InputStream; public class SScanner { //แอตทริบิวประจําอ๊ อบเจ็คชือ name และ weigh //แอตทริบิว kb public String name; public double weigh; public Scanner kb; //ตัวสร้ าง 4 แบบ ดังต่ อไปนี //ตัวสร้ าง public Fruit() { public SScanner(InputStream in) { name = ""; kb = new Scanner(in); weigh = 0.0; } } public Fruit(String n) { //เมท็อด nextInt(…) name = n; weigh = wh[(int)(Math.random() * 5)]; public int nextInt() { } return kb.nextInt(); } public Fruit(String n, double w) { name = n; weigh = w; //เมท็อด nextLine(…) } public String nextLine() { public Fruit(Fruit f) { String s = ""; name = f.name; while (true) { weigh = f.weigh; s = kb.nextLine(); } if(s.length() != 0) break; } return s; //เมท็อดประจําอ๊ อบเจ็คชือ showFruit } public void showFruit() { System.out.println(name + " " + weigh + " kg."); } //End of class } คลาส TestSScanner } //End of class public class TestSScanner { public static void main(String[] args) { 2. คลาส Basket SScanner kb = new SScanner(System.in); public class Basket { System.out.print("Enter Int: "); int a = kb.nextInt(); //แอตทริบิวประจําอ๊ อบเจ็คชือ fruit และ count System.out.print("Enter Str: "); public Fruit fruit[]; String b = kb.nextLine(); public int count; System.out.println(a + "," + b); //ตัวสร้ างแบบไม่ มีพารามิเตอร์ } //End of main } //End of class public Basket() { fruit = new Fruit[10]; count = 0; } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 12. ANSWER 12-13 Computer Programming using Java 23 24 Computer Programming using Java ANSWER 12-13 //ตัวสร้ างแบบหนึงพารามิเตอร์ ชนิดจํานวนเต็ม //เมท็อดประจําอ๊ อบเจ็คชือ getTotalWeigh public Basket(int n) { public double getTotalWeigh() { fruit = new Fruit[n]; double weigh = 0.0; count = 0; for (int i = 0; i < count; i++) { } weigh += fruit[i].weigh; } return weigh; //ตัวสร้ างแบบหนึงพารามิเตอร์ ชนิด Basket } public Basket(Basket b) { fruit = new Fruit[b.fruit.length]; for (int i = 0; i < fruit.length; i++) { //เมท็อดประจําอ๊ อบเจ็คชือ showBasket fruit[i] = b.fruit[i]; public void showBasket() { } for (int i = 0; i < count; i++) { count = b.count; fruit[i].showFruit(); } } } //เมท็อดประจําอ๊ อบเจ็คชือ isFull } //End of class public boolean isFull() { return count == fruit.length; } 3. คลาส FruitBasket import java.util.Scanner; public class FruitBasket { //เมท็อดประจําอ๊ อบเจ็คชือ isEmpty //เมท็อดประจําคลาสชือ getFruitBasket public boolean isEmpty() { public static Basket getFruitBasket(String f, double w, int n) { return count == 0; Basket bsk = new Basket(n); } Fruit frt[] = new Fruit[n]; for (int i = 0; i < frt.length; i++) { if (w <= 0.0) frt[i] = new Fruit(f); //เมท็อดประจําอ๊ อบเจ็คชือ takeIn else frt[i] = new Fruit(f, w); public void takeIn(Fruit f) { bsk.takeIn(frt[i]); if (!isFull()) { } fruit[count] = f; return bsk; count++; } } else { System.out.println("Basket Full"); } //เมท็อดประจําคลาสชือ showFruitBasket } public static void showFruitBasket(Basket b) { System.out.println("Total Weigh: " + b.getTotalWeigh()); System.out.println("Total Fruit: " + b.count); //เมท็อดประจําอ๊ อบเจ็คชือ takeOut System.out.println("List of Fruit: "); public Fruit takeOut() { b.showBasket(); if (!isEmpty()) { } count--; Fruit f = fruit[count]; fruit[count] = null; //เมท็อด main return f; public static void main(String[] args) { } else { //สร้ างกระเช้ าผลไม้ ขนมา 3 กระเช้ า ึ System.out.println("Basket Empty"); return null; Basket x[] = new Basket[3]; } x[0] = getFruitBasket("Apple", 0.12, 25); } x[1] = getFruitBasket("Orange", 0.1, 30); x[2] = getFruitBasket("Melon", 0.45, 15); © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 13. ANSWER 12-13 Computer Programming using Java 25 26 Computer Programming using Java ANSWER 12-13 //สร้ างกระเช้ าผลไม้ เปล่ าๆ ขึนมาอีก 6 กระเช้า //เมท็อด getID(…) Basket y[] = new Basket[6]; public int getID() { for(int i = 0; i < y.length; i++) { return id; y[i] = new Basket(); } int j = 0; while (!y[i].isFull()) { if (!x[j].isEmpty()) y[i].takeIn(x[j].takeOut()); //เมท็อด getName(…) if (y[i].getTotalWeigh() > 2.0) { Fruit tmp = y[i].takeOut(); public String getName() { break; return name; } } j++; if (j > 2) j = 0; } //เมท็อด getNoob(…) System.out.println("-----Basket " + (i + 1) + "-----"); public int getNoob() { showFruitBasket(y[i]); return noob; } } } //End of main } //End of class //เมท็อด set(…) public void set(Student s) { id = s.getID(); โจทย์ข้อที 8 [ระดับเทพ] name = s.getName(); noob = s.getNoob(); คลาส Student } public class Student { //แอตทริบิว id แอตทริบิว name และแอตทริ บว noob ิ private int id; //เมท็อด show(…) private String name; public void show() { private int noob; String s = ""; if (noob == 1) s = "1-Noob"; else if (noob == 2) s = "2-Father Noob"; //ตัวสร้ างแบบไม่ มีพารามิเตอร์ else if (noob == 3) s = "3-God Noob"; Student() { else if (noob == 4) s = "4-Hof Noob"; id = 0; else s = "Unknown"; name = ""; System.out.println(id + " " + name + " [" + s + "]"); noob = 0; } } } //End of class //ตัวสร้ างแบบทีรับพารามิเตอร์ 3 ตัว Student(int i, String n, int x) { คลาส Room id = i; public class Room { name = n; noob = x; //แอตทริบิว s ทีเป็ นอาเรย์ 2 มิติชนิด Student } public static Student s[][] = new Student[16][40]; //ตัวสร้ างแบบทีรับพารามิเตอร์ เป็ นชนิด Student Student(Student s) { set(s); } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 14. ANSWER 12-13 Computer Programming using Java 27 28 Computer Programming using Java ANSWER 12-13 //เมท็อด setSeat(…) //เมท็อด main(…) public static void setSeat(Student st) { public static void main(String[] args) { int startRow = s.length - 1 - (s.length / 4) * (4 - st.getNoob()); for (int i = 0; i < s.length; i++) { for (int i = startRow; i < startRow + 4; i++) { for (int j = 0; j < s[i].length; j++) { for (int j = 0; j < s[i].length; j++) { s[i][j] = new Student(); if (s[i][j].getName().equals("") && } j != s[i].length / 2 && } j != s[i].length / 2 - 1) { Student s1 = new Student(53300121, "Akeudom", 1); setSeat(s1); s[i][j].set(st); Student s2 = new Student(53300221, "Phakhin", 1); setSeat(s2); return; Student s3 = new Student(53300321, "Taparb", 2); setSeat(s3); } Student s4 = new Student(53300421, "Ong-ard", 3); setSeat(s4); } Student s5 = new Student(53300521, "Wongyos", 4); setSeat(s5); } showRoom(); System.out.println("No Reserved Seat"); showStudent(); } } //เมท็อด showroom(…) } //End of class public static void showRoom() { for (int i = 0; i < s.length; i++) { for (int j = 0; j < s[i].length; j++) { if (j == s[i].length / 2 || j == s[i].length / 2 - 1) { System.out.print(" "); } else { if (!s[i][j].getName().equals("")) System.out.print(s[i][j].getName().substring(0, 1) + " "); else System.out.print("# "); } } System.out.println(); } } //เมท็อด showStudent(…) public static void showStudent(int r, int c) { System.out.print("Seat Row " + r + " Column " + c + "t"); s[r + s.length - 2][c - 1].show(); } //เมท็อด showStudent(…) public static void showStudent() { for (int i = s.length - 1; i >= 0; i--) { for (int j = 0; j < s[i].length; j++) { if (!s[i][j].getName().equals("")) { System.out.print("Seat Row " + (s.length - i) + " Column " + (j + 1) + "t"); s[i][j].show(); } } } } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)