SlideShare a Scribd company logo
ANSWER 10-11                                                          Computer Programming using Java           1
     CHAPTER                                         อาเรย์สองมิติ
 ANS-10                                         (Two Dimensional Arrays)
โจทย์ ข้อที่ 1 [ระดับง่ าย]
1) int a[][] = {{0,0,0},{0,0,0}};
2)    int b[][] = {{1,1},{1,1},{1,1},{1,1},{1,1}};

3)    String s[][] = {{"Java"},{"Java"},{"Java"}};

4)    String t[][] = {{"Java","Java","Java"}};

5)    double em[][] = {{}};


โจทย์ ข้อที่ 2 [ระดับง่ าย]
       ข้ อที่ 1 อาเรย์   m[][]            ข้ อที่ 2 อาเรย์   n[][]            ข้ อที่ 3 อาเรย์   p[][]


       1       2          3   4            1                                   1       2          3
       2       3          4   5            2       3                           2       3
       3       4          5   6            3       4          5                3       4          5   6
       4       5          6   7            4       5          6   7            4
                                                                               5       6



โจทย์ ข้อที่ 3 [ระดับง่ าย]
1) boolean matrix[][]               = new boolean[5][8];

2)    String chess[][] = new String[8][8];

3)    int tranMatrix[][] = new int[4][9];

4)    double data[][] = new double[300][3];

5)    n[7] = new int[4];

6)    n[4] = new int[x * x + 1];




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


    โจทย์ ข้อที่ 4 [ระดับปานกลาง]
    public class RainStatistic {
           public static double[][] getTable() {
             double rain[][] = new double[12][];
             for (int i = 0; i < 12; i++) {
               if (i == 1) {
                 rain[i] = new double[28];
                 // rain[i] = new double[29];
               } else if (i == 3 || i == 5 || i == 8 || i == 10) {
                 rain[i] = new double[30];
               } else {
                 rain[i] = new double[31];
               }
             }
             return rain;
           }

         public static void main(String[] args) {
             double x[][] = getTable();

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



    โจทย์ ข้อที่ 5 [ระดับง่ าย]
    1) int x = num[51][49];
    2)      char c = code[9][60];

    3)      int var1 = bank[0][1];

    4)      double var2 = bank[1][bank[1].length - 1];

    5)      code[6][4] = x;

    6)      sName[0][9] = s1;

    7)      sName[1][2] = s2;

    8)     a[a.length - 1][a[a.length - 1].length - 1] = y;




    © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
                                                ้
ANSWER 10-11                                                       Computer Programming using Java              3
โจทย์ ข้อที่ 6 [ระดับง่ าย]
public class The2DArray {
     public static void setArray(int n[][]) {
       for (int i = 0; i < n.length; i++) {
         for (int j = 0; j < n[i].length; j++) {
           n[i][j] = (i + 1) * (j + 1);
         }
       }
     }

   public static void main(String[] args) {
        int x[][] = new int[5][6];
        setArray(x);

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


โจทย์ ข้อที่ 7 [ระดับปานกลาง]
public class EqualityOf2DArray {
     public static boolean isArrayEquals(int x[][], int y[][]) {
       if (x.length == y.length) {
         for (int i = 0; i < x.length; i++) {
           if (x[i].length != y[i].length) return false;
         }
         for (int i = 0; i < x.length; i++) {
           for (int j = 0; j < x[i].length; j++) {
             if (x[i][j] != y[i][j]) return false;
           }
         }
         return true;
       } else {
         return false;
       }
     }

   public static void main(String[] args) {
        int m[][] = {{1},{2,3},{3,4,5,6},{4,5,6,7}};
        int n[][] = {{1},{2,3},{3,4,5},{4,5,6,7}};
        boolean ch = isArrayEquals(m, n);

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




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


    โจทย์ ข้อที่ 8 [ระดับปานกลาง - ระดับยาก]
    import java.util.Scanner;
    public class DataExperiment {
      public static double max(double t[][]) {
           double maxData = t[0][0];
           for (int i = 0; i < t.length; i++) {
             for (int j = 0; j < t[i].length; j++) {
               if (t[i][j] > maxData) maxData = t[i][j];
             }
           }
           return maxData;

      } //End of max(…)
       public static double min(double t[][]) {
           double minData = t[0][0];
           for (int i = 0; i < t.length; i++) {
             for (int j = 0; j < t[i].length; j++) {
               if (t[i][j] < minData) minData = t[i][j];
             }
           }
           return minData;

       } //End of min(…)


      public static double middleRange(double t[][]) {
           return (max(t) + min(t)) / 2.0;

       } //End of avgRange(…)
      public static double meanOfMonth(double t[][], int m) {
           double sum = 0.0;
           int i = (m + 3) % 12;
           for (int j = 0; j < t[i].length; j++) {
             sum += t[i][j];
           }
           return sum / t[i].length;

       } //End of meanOfMonth(…)
      public static double dataOfDay(double t[][], int d, int m) {
           return t[(m + 3) % 12][d - 1];

      } //End of dataOfDay(…)




    © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
                                                ้
ANSWER 10-11                                                       Computer Programming using Java              5
  public static void main(String[] args) {
       Scanner kb = new Scanner(System.in);
       double test[][] = new double[12][];
       for (int i = 0; i < test.length; i++) {
         if (i == 5) {
           test[i] = new double[28];
         } else if (i == 0 || i == 2 || i == 7 || i == 9) {
           test[i] = new double[30];
         } else {
           test[i] = new double[31];
         for (int j = 0; j < test[i].length; j++) {
           System.out.print((j + 1) + "/" + ((i + 9) % 12) + " : ");
           test[i][j] = kb.nextDouble();
         }//End of for j
       }//End of for i

       //========= Method Call =========
       System.out.println(max(test));
       System.out.println(min(test));
       System.out.println(middleRange(test));
       System.out.println(meanOfMonth(test, 10));
       System.out.println(dataOfDay(test, 13, 6));

  } //End of main(…)
} //End of Class


โจทย์ ข้อที่ 9 [ระดับง่ าย – ระดับยาก]
import java.util.Scanner;
public class MatrixOperation {
    //[ระดับปานกลาง] เขียนเมท็อด addMatrix(…)
     public static double[][] addMatrix(double x[][], double y[][]) {
       double add[][] = new double[x.length][x[0].length];
       for (int i = 0; i < x.length; i++) {
         for (int j = 0; j < x[i].length; j++) {
           add[i][j] = x[i][j] + y[i][j];
         }
       }
       return add;
     }


    //[ระดับปานกลาง] เขียนเมท็อด mulScalarMatrix(…)
     public static double[][] mulScalarMatrix(double x[][], double n) {
       double mulS[][] = new double[x.length][x[0].length];
       for (int i = 0; i < x.length; i++) {
         for (int j = 0; j < x[i].length; j++) {
           mulS[i][j] = n * x[i][j];
         }
       }
       return mulS;
     }




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


        //[ระดับยาก] เขียนเมท็อด transposeMatrix(…)
         public static double[][]transposeMatrix(double x[][]) {
           double at[][] = new double[x[0].length][x.length];
           for (int i = 0; i < x.length; i++) {
             for (int j = 0; j < x[i].length; j++) {
               at[j][i] = x[i][j];
             }
           }
           return at;
         }


        //[ระดับยาก] เขียนเมท็อด mulMatrix(…)
         public static double[][] mulMatrix(double x[][], double y[][]) {
           double mul[][] = new double[x.length][y[0].length];
           for (int i = 0; i < mul.length; i++) {
             for (int j = 0; j < mul[i].length; j++) {
               for (int k = 0; k < y.length; k++) {
                 mul[i][j] += x[i][k] * y[k][j];
               }
             }
           }
           return mul;
         }


        //[ระดับง่ าย] เขียนเมท็อด printMatrix(…)
         public static void printMatrix(double x[][]) {
           for (int i = 0; i < x.length; i++) {
             for (int j = 0; j < x[i].length; j++) {
               System.out.print(x[i][j] + "t");
             }
             System.out.println();
           }
         }


        //[ระดับง่ าย] เขียนเมท็อด main(…)
        public static void main(String[] args) {
             //Define and        creat matrix
             double a[][]        = {{1,3,5,9},{2,6,4,3}};
             double b[][]        = {{3,6},{4,8},{1,0}};
             double c[][]        = {{0,2,4},{5,3,1},{7,11,2},{6,6,9}};

             //Calculate matrix m
             double t1[][] = transposeMatrix(mulScalarMatrix(a, 2.0));
             double t2[][] = transposeMatrix(mulScalarMatrix(b, 0.25));
             double t3[][] = mulMatrix(t1, t2);
             double t4[][] = mulScalarMatrix(c, -1.0);
             double m[][] = addMatrix(t3, t4);




    © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
                                                ้
ANSWER 10-11                                                       Computer Programming using Java              7
         //Print matrix
         System.out.println("-----              Matrix a -----");
         printMatrix(a);
         System.out.println("-----              Matrix b -----");
         printMatrix(b);
         System.out.println("-----              Matrix c -----");
         printMatrix(c);
         System.out.println("-----              Matrix m -----");
         printMatrix(m);

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


โจทย์ ข้อที่ 10 [ระดับยาก]
 public static int[] splitRowOfArray(int a[][], int row) {
   int sp[];
   if (row > 0 && row <= a.length) {
     sp = new int[a[row - 1].length];
     for (int i = 0; i < sp.length; i++) {
       sp[i] = a[row - 1][i];
     }
   } else {
     sp = new int[0];
   }
   return sp;
 }


โจทย์ ข้อที่ 11 [ระดับยาก]
 public static int[] splitColumnOfArray(int a[][], int col) {
   int sp[];
   if (col > 0 && col <= a[0].length) {
     sp = new int[a.length];
     for (int i = 0; i < sp.length; i++) {
       sp[i] = a[i][col - 1];
     }
   } else {
     sp = new int[0];
   }
   return sp;
 }




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


       CHAPTER                                               การเวียนเกิด
     ANS-11                                                   (Recursion)
    โจทย์ ข้อที่ 1 [ระดับง่ าย]
         เรี ยก           เรี ยก         เรี ยก          เรี ยก            เรี ยก              เรี ยก
        f(5)             f(4)           f(3)            f(2)              f(1)                f(0)


       ผลลัพธ์          ผลลัพธ์        ผลลัพธ์         ผลลัพธ์            ผลลัพธ์             ผลลัพธ์
                                                                                                             เวียนเข้ า
                                                                                                 0
                                                                           1   +               1  +
                                                         2   +             2   +               2  +
                                        3 +              3   +             3   +               3  +
                          4 +           4 +              4   +             4   +               4  +
         5 +              5 +           5 +              5   +             5   +               5  +

       รอบที่ 1          รอบที่ 2      รอบที่ 3        รอบที่ 4           รอบที่ 5            รอบที่ 6
        คืนค่ า           คืนค่ า       คืนค่ า         คืนค่ า            คืนค่ า             คืนค่ า
         15                10             6               3                  1                   0


       ผลลัพธ์          ผลลัพธ์        ผลลัพธ์         ผลลัพธ์            ผลลัพธ์             ผลลัพธ์

                                                                                                 0
                                                                           1   +               1  +
                                                         2   +             2   +               2  +          เวียนออก
                                        3 +              3   +             3   +               3  +
                          4 +           4 +              4   +             4   +               4  +
         5 +              5 +           5 +              5   +             5   +               5  +

       รอบที่ 1          รอบที่ 2      รอบที่ 3        รอบที่ 4           รอบที่ 5            รอบที่ 6


    โจทย์ ข้อที่ 2 [ระดับง่ าย]         โจทย์ ข้อที่ 3 [ระดับง่ าย]              โจทย์ ข้อที่ 4 [ระดับง่ าย]
       การเรียกใช้           คําตอบ        การเรียกใช้           คําตอบ             การเรียกใช้           คําตอบ
     mul(4, 3)                    12      gcd(28, 16)             4                 expo(4)                 16
     mul(5, 7)                    35      gcd(9, 14)              1                 expo(7)                128
     mul(20, 10)               200        gcd(75, 30)             15                expo(11)              2048




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

       1                                 2                                              4
                      expo(0)                      expo(0)
                                                                      expo(1)                      expo(1)
                           1                        1
                                                                           2                          2
                                                              expo(0)           expo(0) expo(0)                  expo(0)

                                                                  1              1             1                 1

                                                    expo(3)

                                                        8
                          expo(2)                                                    expo(2)

                            4                                                          4
           expo(1)                       expo(1)                      expo(1)                      expo(1)

               2                             2                             2                         2
 expo(0)             expo(0) expo(0)               expo(0) expo(0)              expo(0) expo(0)              expo(0)

       1              1              1              1             1              1             1                 1



โจทย์ ข้อที่ 5 [ระดับง่ าย]
   การเรียกใช้ และคําตอบ
 printX(4)           printX(7)
 1234                1234567
 123                 123456
 12                  12345
 1                   1234
                     123
                     12
                     1


โจทย์ ข้อที่ 6 [ระดับปานกลาง]
                                                              พารามิเตอร์ n
  พารามิเตอร์ m
                                0                  1                  2                    3                 4

           0                    1                  2                  3                    4                 5
           1                    2                  3                  4                  5                   6
           2                    3                  5                  7                  9                11
           3                    5                13                   29                61                125


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


     โจทย์ ข้อที่ 7 [ระดับง่ าย]
                 n                            กรณีฐาน         f n) = 1
                                                               (                                 n = 1
                ∑ i3
                      1
       f n) =
        (                   n ≥ 1
                i=1
                                              กรณีเวียนเกิด   f n) = f n − 1) + 1 / n 3
                                                               (      (                           n > 1


      public static double f(int n) {
        if (n <= 1) {
          return 1.0;
        } else {
          return f(n - 1) + 1.0 / (n * n * n);
        }
      }


     โจทย์ ข้อที่ 8 [ระดับง่ าย]
      public static int f(int n) {
        if (n <= 0) {
          return 0;
        } else {
          return f(n - 1) +
          (int) Math.pow(n, n) + (2 * n);
        }
      }


     โจทย์ ข้อที่ 9 [ระดับง่ าย]
      public static double g(int n) {
        if (n <= 0) {
          return 0.0;
        } else if (n == 1) {
          return 1.0;
        } else {
          return g(n - 1) * (1.0 + 1.0 / n);
        }
      }


     โจทย์ ข้อที่ 10 [ระดับง่ าย]
      public static int fac(int n) {
        if (n < 2) {
          return 1;
        } else {
          return fac(n - 1) * n;
        }
      }




     © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
                                                 ้
ANSWER 10-11                                                                Computer Programming using Java      11
โจทย์ ข้อที่ 11 [ระดับง่ าย]
 public static int fibo(int n) {
   if (n < 2) {
     return n;
   } else {
     return fibo(n - 1) + fibo(n - 2);
   }
 }


โจทย์ ข้อที่ 12 [ระดับง่ าย]                                      fibo(5)

                                     fibo(4)                           5                     fibo(3)

                                         3                                                     2
                     fibo(3)                           fibo(2)                     fibo(2)             fibo(1)

                       2                                 1                            1                  1
           fibo(2)             fibo(1)       fibo(1)             fibo(0)    fibo(1)          fibo(0)

              1                  1              1                  0           1               0
  fibo(1)             fibo(0)

       1               0


โจทย์ ข้อที่ 13 [ระดับปานกลาง]
 public static double pow(double a, int b) {
   if (b < 0) {
     return pow(a, b + 1) * 1.0 / a;
   } else if (b == 0) {
     return 1.0;
   } else {
     return pow(a, b - 1) * a;
   }
 }


โจทย์ ข้อที่ 14 [ระดับยาก]
 public static double f(int n) {
   if (n <= 1) {
     return 1.0;
   } else {
     return 1.0 + (n / f(n - 1));
   }
 }




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


     โจทย์ ข้อที่ 15 [ระดับยาก]
     1) public static double       g(int n, int m) {
                if (n <= 1) {
                  return 1;
                } else {
                  if (n % 2 == 0) {
                    return g(n – 1, m) - Math.pow(n, m);
                  } else {
                    return g(n – 1, m) + Math.pow(n, m);
                  }
                }
            }

     2)     public static double h(int n) {
              if (n <= 1) {
                return 1;
              } else {
                if (n % 2 == 0) {
                  return h(n - 1) + 1.0 / n;
                } else {
                  return h(n - 1) - 1.0 / n;
                }
              }
            }



     โจทย์ ข้อที่ 16 [ระดับปานกลาง]
      public static int search(int a[], int k) {
        return search(a, k, 0);
      }

      public static int search(int a[], int k, int i) {
        if (i < a.length) {
          if (a[i] == k) {
            return i;
          } else {
            return search(a, k, i + 1);
          }
        } else {
          return -1;
        }
      }




     © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
                                                 ้
ANSWER 10-11                                                       Computer Programming using Java              13
โจทย์ ข้อที่ 17 [ระดับปานกลาง]
public class OverHundredNumber {
  public static void main(String[] args) {
    int d[] = { 99, 101, 13, 78, 200, 534, 47, 1234, 736 };
    System.out.println(overHundred(d));
  }

    public static int overHundred(int d[]) {
      return overHundred(d, 0, 0);
    }

    public static int overHundred(int d[], int i, int count) {
      if (i < d.length) {
        if (d[i] > 100) {
          return overHundred(d, i + 1, count + 1);
        } else {
          return overHundred(d, i + 1, count);
        }
      } else {
        return count;
      }
    }

} //End of class


โจทย์ ข้อที่ 18 [ระดับปานกลาง]
public class TheString {
  public static void main(String[] args) {
    System.out.println(revStr("Computer"));
  }

    public static String revStr(String s) {
      return revStr(s, "", s.length() - 1);
    }

    public static String revStr(String s, String rs, int i) {
      if (i >= 0) {
        rs += s.substring(i, i + 1);
        return revStr(s, rs, i - 1);
      } else {
        return rs;
      }
    }

} //End of class



โจทย์ ข้อที่ 19 [ระดับยาก]
 public static void formulaAtoB(int a, int b) {
   formulaAtoB(a, b, 1);
 }



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


      public static void formulaAtoB(int a, int b, int i) {
        if (a <= b) {
          if (i <= 12) {
            System.out.println(a + " x " + i + " = " + (a * i));
            formulaAtoB(a, b, i + 1);
          } else {
            System.out.println("-------------");
            formulaAtoB(a + 1, b);
          }
        }//End of if (a <= b)
      }


     โจทย์ ข้อที่ 20 [ระดับยาก]
      public static int[] addArray(int a[], int b[]) {
        int sum[] = new int[a.length];
        return addArray(a, b, sum, 0);
      }

      public static int[] addArray(int a[], int b[], int sum[], int i) {
        if (i < sum.length) {
          sum[i] = a[i] + b[i];
          return addArray(a, b, sum, i + 1);
        } else {
          return sum;
        }
      }


     โจทย์ ข้อที่ 21 [ระดับยาก]
      public static boolean isMatrixEquals(int a[][], int b[][]) {
        if (a.length == b.length && a[0].length == b[0].length) {
          return isMatrixEquals(a, b, 0, 0);
        } else {
          return false;
        }
      }

      public static boolean isMatrixEquals(int a[][], int b[][], int i,
        int j) {
        if (i < a.length) {
          if (j < a[i].length) {
            if (a[i][j] != b[i][j]) {
              return false;
            } else {
              return isMatrixEquals(a, b, i, j + 1);
            }
          } else {
            return isMatrixEquals(a, b, i + 1, 0);
          }
        } else {
          return true;
        }
      }


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

More Related Content

What's hot

Java-Answer Chapter 07 (For Print)
Java-Answer Chapter 07 (For Print)Java-Answer Chapter 07 (For Print)
Java-Answer Chapter 07 (For Print)Wongyos Keardsri
 
Java-Answer Chapter 01-04 (For Print)
Java-Answer Chapter 01-04 (For Print)Java-Answer Chapter 01-04 (For Print)
Java-Answer Chapter 01-04 (For Print)Wongyos Keardsri
 
Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)Wongyos Keardsri
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 Recursions
Wongyos Keardsri
 
Java-Answer Chapter 05-06 (For Print)
Java-Answer Chapter 05-06 (For Print)Java-Answer Chapter 05-06 (For Print)
Java-Answer Chapter 05-06 (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 Objects
Wongyos 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 Programming
Wongyos Keardsri
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsWongyos Keardsri
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชันค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
sawed kodnara
 
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญาสูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
Kanomwan Jeab
 
อนุพันธ์
อนุพันธ์อนุพันธ์
อนุพันธ์krurutsamee
 
การอินทีเกรต
การอินทีเกรตการอินทีเกรต
การอินทีเกรต
ANNRockART
 
662305 08
662305 08662305 08
เฉลยอินทิเกรต
เฉลยอินทิเกรตเฉลยอินทิเกรต
เฉลยอินทิเกรตkrurutsamee
 
6.Flow control
6.Flow control6.Flow control
6.Flow control
UsableLabs
 
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจแบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
ชัชชญา ช่างเจริญ
 
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
IMC Institute
 

What's hot (20)

Java-Answer Chapter 05-06
Java-Answer Chapter 05-06Java-Answer Chapter 05-06
Java-Answer Chapter 05-06
 
Java-Answer Chapter 07 (For Print)
Java-Answer Chapter 07 (For Print)Java-Answer Chapter 07 (For Print)
Java-Answer Chapter 07 (For Print)
 
Java-Answer Chapter 01-04 (For Print)
Java-Answer Chapter 01-04 (For Print)Java-Answer Chapter 01-04 (For Print)
Java-Answer Chapter 01-04 (For Print)
 
Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 Recursions
 
Java-Answer Chapter 05-06 (For Print)
Java-Answer Chapter 05-06 (For Print)Java-Answer Chapter 05-06 (For Print)
Java-Answer Chapter 05-06 (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-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
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 Algorithms
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชันค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
 
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญาสูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
 
อนุพันธ์
อนุพันธ์อนุพันธ์
อนุพันธ์
 
ปริพันธ์
ปริพันธ์ปริพันธ์
ปริพันธ์
 
การอินทีเกรต
การอินทีเกรตการอินทีเกรต
การอินทีเกรต
 
662305 08
662305 08662305 08
662305 08
 
เฉลยอินทิเกรต
เฉลยอินทิเกรตเฉลยอินทิเกรต
เฉลยอินทิเกรต
 
6.Flow control
6.Flow control6.Flow control
6.Flow control
 
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจแบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
 
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
 

Similar to Java-Answer Chapter 10-11

Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่น
Thanachart Numnonda
 
Mobile Game and Application with J2ME
Mobile Gameand Application with J2MEMobile Gameand Application with J2ME
Mobile Game and Application with J2ME
Jenchoke Tachagomain
 
Mobile Game and Application with J2ME
Mobile Gameand Application withJ2MEMobile Gameand Application withJ2ME
Mobile Game and Application with J2ME
Jenchoke Tachagomain
 
J2ME Game Concept
J2ME  Game ConceptJ2ME  Game Concept
J2ME Game Concept
Jenchoke Tachagomain
 
Computer Programming 3
Computer Programming 3 Computer Programming 3
Computer Programming 3
Saranyu Srisrontong
 
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
หน่อย หน่อย
 
09 multi arrays
09 multi arrays09 multi arrays
09 multi arraysa-num Sara
 
Computer Programming 2.1
Computer Programming 2.1Computer Programming 2.1
Computer Programming 2.1
Saranyu Srisrontong
 
Computer Programming 2.2
Computer Programming 2.2Computer Programming 2.2
Computer Programming 2.2
Saranyu Srisrontong
 
J2ME Game Concept
J2ME  Game ConceptJ2ME  Game Concept
J2ME Game Concept
Jenchoke Tachagomain
 

Similar to Java-Answer Chapter 10-11 (13)

Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่น
 
Mobile Game and Application with J2ME
Mobile Gameand Application with J2MEMobile Gameand Application with J2ME
Mobile Game and Application with J2ME
 
Mobile Game and Application with J2ME
Mobile Gameand Application withJ2MEMobile Gameand Application withJ2ME
Mobile Game and Application with J2ME
 
J2ME Game Concept
J2ME  Game ConceptJ2ME  Game Concept
J2ME Game Concept
 
Computer Programming 3
Computer Programming 3 Computer Programming 3
Computer Programming 3
 
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
 
09 multi arrays
09 multi arrays09 multi arrays
09 multi arrays
 
Computer Programming 2.1
Computer Programming 2.1Computer Programming 2.1
Computer Programming 2.1
 
07 methods
07 methods07 methods
07 methods
 
Computer Programming 2.2
Computer Programming 2.2Computer Programming 2.2
Computer Programming 2.2
 
1 test
1  test1  test
1 test
 
Method part2
Method part2Method part2
Method part2
 
J2ME Game Concept
J2ME  Game ConceptJ2ME  Game Concept
J2ME Game Concept
 

More from Wongyos Keardsri

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
Wongyos 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 applications
Wongyos Keardsri
 
IP address anonymization
IP address anonymizationIP address anonymization
IP address anonymization
Wongyos 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 Programming
Wongyos 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 System
Wongyos 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 Language
Wongyos Keardsri
 
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part III
Wongyos Keardsri
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part II
Wongyos Keardsri
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part I
Wongyos Keardsri
 
Discrete-Chapter 10 Trees
Discrete-Chapter 10 TreesDiscrete-Chapter 10 Trees
Discrete-Chapter 10 Trees
Wongyos Keardsri
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 Relations
Wongyos Keardsri
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 Probability
Wongyos Keardsri
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 Counting
Wongyos Keardsri
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and Proofs
Wongyos Keardsri
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part II
Wongyos Keardsri
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part I
Wongyos 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 Sequences
Wongyos Keardsri
 
Discrete-Chapter 01 Sets
Discrete-Chapter 01 SetsDiscrete-Chapter 01 Sets
Discrete-Chapter 01 Sets
Wongyos Keardsri
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling Computation
Wongyos Keardsri
 

More from Wongyos Keardsri (20)

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
 
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
 
IP address anonymization
IP address anonymizationIP address anonymization
IP address anonymization
 
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
 
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 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-Answer Chapter 10-11

  • 1. ANSWER 10-11 Computer Programming using Java 1 CHAPTER อาเรย์สองมิติ ANS-10 (Two Dimensional Arrays) โจทย์ ข้อที่ 1 [ระดับง่ าย] 1) int a[][] = {{0,0,0},{0,0,0}}; 2) int b[][] = {{1,1},{1,1},{1,1},{1,1},{1,1}}; 3) String s[][] = {{"Java"},{"Java"},{"Java"}}; 4) String t[][] = {{"Java","Java","Java"}}; 5) double em[][] = {{}}; โจทย์ ข้อที่ 2 [ระดับง่ าย] ข้ อที่ 1 อาเรย์ m[][] ข้ อที่ 2 อาเรย์ n[][] ข้ อที่ 3 อาเรย์ p[][] 1 2 3 4 1 1 2 3 2 3 4 5 2 3 2 3 3 4 5 6 3 4 5 3 4 5 6 4 5 6 7 4 5 6 7 4 5 6 โจทย์ ข้อที่ 3 [ระดับง่ าย] 1) boolean matrix[][] = new boolean[5][8]; 2) String chess[][] = new String[8][8]; 3) int tranMatrix[][] = new int[4][9]; 4) double data[][] = new double[300][3]; 5) n[7] = new int[4]; 6) n[4] = new int[x * x + 1]; © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 2. 2 Computer Programming using Java ANSWER 10-11 โจทย์ ข้อที่ 4 [ระดับปานกลาง] public class RainStatistic { public static double[][] getTable() { double rain[][] = new double[12][]; for (int i = 0; i < 12; i++) { if (i == 1) { rain[i] = new double[28]; // rain[i] = new double[29]; } else if (i == 3 || i == 5 || i == 8 || i == 10) { rain[i] = new double[30]; } else { rain[i] = new double[31]; } } return rain; } public static void main(String[] args) { double x[][] = getTable(); } //End of main } //End of class โจทย์ ข้อที่ 5 [ระดับง่ าย] 1) int x = num[51][49]; 2) char c = code[9][60]; 3) int var1 = bank[0][1]; 4) double var2 = bank[1][bank[1].length - 1]; 5) code[6][4] = x; 6) sName[0][9] = s1; 7) sName[1][2] = s2; 8) a[a.length - 1][a[a.length - 1].length - 1] = y; © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 3. ANSWER 10-11 Computer Programming using Java 3 โจทย์ ข้อที่ 6 [ระดับง่ าย] public class The2DArray { public static void setArray(int n[][]) { for (int i = 0; i < n.length; i++) { for (int j = 0; j < n[i].length; j++) { n[i][j] = (i + 1) * (j + 1); } } } public static void main(String[] args) { int x[][] = new int[5][6]; setArray(x); } //End of main } //End of class โจทย์ ข้อที่ 7 [ระดับปานกลาง] public class EqualityOf2DArray { public static boolean isArrayEquals(int x[][], int y[][]) { if (x.length == y.length) { for (int i = 0; i < x.length; i++) { if (x[i].length != y[i].length) return false; } for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { if (x[i][j] != y[i][j]) return false; } } return true; } else { return false; } } public static void main(String[] args) { int m[][] = {{1},{2,3},{3,4,5,6},{4,5,6,7}}; int n[][] = {{1},{2,3},{3,4,5},{4,5,6,7}}; boolean ch = isArrayEquals(m, n); } //End of main } //End of class © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 4. 4 Computer Programming using Java ANSWER 10-11 โจทย์ ข้อที่ 8 [ระดับปานกลาง - ระดับยาก] import java.util.Scanner; public class DataExperiment { public static double max(double t[][]) { double maxData = t[0][0]; for (int i = 0; i < t.length; i++) { for (int j = 0; j < t[i].length; j++) { if (t[i][j] > maxData) maxData = t[i][j]; } } return maxData; } //End of max(…) public static double min(double t[][]) { double minData = t[0][0]; for (int i = 0; i < t.length; i++) { for (int j = 0; j < t[i].length; j++) { if (t[i][j] < minData) minData = t[i][j]; } } return minData; } //End of min(…) public static double middleRange(double t[][]) { return (max(t) + min(t)) / 2.0; } //End of avgRange(…) public static double meanOfMonth(double t[][], int m) { double sum = 0.0; int i = (m + 3) % 12; for (int j = 0; j < t[i].length; j++) { sum += t[i][j]; } return sum / t[i].length; } //End of meanOfMonth(…) public static double dataOfDay(double t[][], int d, int m) { return t[(m + 3) % 12][d - 1]; } //End of dataOfDay(…) © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 5. ANSWER 10-11 Computer Programming using Java 5 public static void main(String[] args) { Scanner kb = new Scanner(System.in); double test[][] = new double[12][]; for (int i = 0; i < test.length; i++) { if (i == 5) { test[i] = new double[28]; } else if (i == 0 || i == 2 || i == 7 || i == 9) { test[i] = new double[30]; } else { test[i] = new double[31]; for (int j = 0; j < test[i].length; j++) { System.out.print((j + 1) + "/" + ((i + 9) % 12) + " : "); test[i][j] = kb.nextDouble(); }//End of for j }//End of for i //========= Method Call ========= System.out.println(max(test)); System.out.println(min(test)); System.out.println(middleRange(test)); System.out.println(meanOfMonth(test, 10)); System.out.println(dataOfDay(test, 13, 6)); } //End of main(…) } //End of Class โจทย์ ข้อที่ 9 [ระดับง่ าย – ระดับยาก] import java.util.Scanner; public class MatrixOperation { //[ระดับปานกลาง] เขียนเมท็อด addMatrix(…) public static double[][] addMatrix(double x[][], double y[][]) { double add[][] = new double[x.length][x[0].length]; for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { add[i][j] = x[i][j] + y[i][j]; } } return add; } //[ระดับปานกลาง] เขียนเมท็อด mulScalarMatrix(…) public static double[][] mulScalarMatrix(double x[][], double n) { double mulS[][] = new double[x.length][x[0].length]; for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { mulS[i][j] = n * x[i][j]; } } return mulS; } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 6. 6 Computer Programming using Java ANSWER 10-11 //[ระดับยาก] เขียนเมท็อด transposeMatrix(…) public static double[][]transposeMatrix(double x[][]) { double at[][] = new double[x[0].length][x.length]; for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { at[j][i] = x[i][j]; } } return at; } //[ระดับยาก] เขียนเมท็อด mulMatrix(…) public static double[][] mulMatrix(double x[][], double y[][]) { double mul[][] = new double[x.length][y[0].length]; for (int i = 0; i < mul.length; i++) { for (int j = 0; j < mul[i].length; j++) { for (int k = 0; k < y.length; k++) { mul[i][j] += x[i][k] * y[k][j]; } } } return mul; } //[ระดับง่ าย] เขียนเมท็อด printMatrix(…) public static void printMatrix(double x[][]) { for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { System.out.print(x[i][j] + "t"); } System.out.println(); } } //[ระดับง่ าย] เขียนเมท็อด main(…) public static void main(String[] args) { //Define and creat matrix double a[][] = {{1,3,5,9},{2,6,4,3}}; double b[][] = {{3,6},{4,8},{1,0}}; double c[][] = {{0,2,4},{5,3,1},{7,11,2},{6,6,9}}; //Calculate matrix m double t1[][] = transposeMatrix(mulScalarMatrix(a, 2.0)); double t2[][] = transposeMatrix(mulScalarMatrix(b, 0.25)); double t3[][] = mulMatrix(t1, t2); double t4[][] = mulScalarMatrix(c, -1.0); double m[][] = addMatrix(t3, t4); © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 7. ANSWER 10-11 Computer Programming using Java 7 //Print matrix System.out.println("----- Matrix a -----"); printMatrix(a); System.out.println("----- Matrix b -----"); printMatrix(b); System.out.println("----- Matrix c -----"); printMatrix(c); System.out.println("----- Matrix m -----"); printMatrix(m); } //End of main } //End of class โจทย์ ข้อที่ 10 [ระดับยาก] public static int[] splitRowOfArray(int a[][], int row) { int sp[]; if (row > 0 && row <= a.length) { sp = new int[a[row - 1].length]; for (int i = 0; i < sp.length; i++) { sp[i] = a[row - 1][i]; } } else { sp = new int[0]; } return sp; } โจทย์ ข้อที่ 11 [ระดับยาก] public static int[] splitColumnOfArray(int a[][], int col) { int sp[]; if (col > 0 && col <= a[0].length) { sp = new int[a.length]; for (int i = 0; i < sp.length; i++) { sp[i] = a[i][col - 1]; } } else { sp = new int[0]; } return sp; } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 8. 8 Computer Programming using Java ANSWER 10-11 CHAPTER การเวียนเกิด ANS-11 (Recursion) โจทย์ ข้อที่ 1 [ระดับง่ าย] เรี ยก เรี ยก เรี ยก เรี ยก เรี ยก เรี ยก f(5) f(4) f(3) f(2) f(1) f(0) ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ เวียนเข้ า 0 1 + 1 + 2 + 2 + 2 + 3 + 3 + 3 + 3 + 4 + 4 + 4 + 4 + 4 + 5 + 5 + 5 + 5 + 5 + 5 + รอบที่ 1 รอบที่ 2 รอบที่ 3 รอบที่ 4 รอบที่ 5 รอบที่ 6 คืนค่ า คืนค่ า คืนค่ า คืนค่ า คืนค่ า คืนค่ า 15 10 6 3 1 0 ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ 0 1 + 1 + 2 + 2 + 2 + เวียนออก 3 + 3 + 3 + 3 + 4 + 4 + 4 + 4 + 4 + 5 + 5 + 5 + 5 + 5 + 5 + รอบที่ 1 รอบที่ 2 รอบที่ 3 รอบที่ 4 รอบที่ 5 รอบที่ 6 โจทย์ ข้อที่ 2 [ระดับง่ าย] โจทย์ ข้อที่ 3 [ระดับง่ าย] โจทย์ ข้อที่ 4 [ระดับง่ าย] การเรียกใช้ คําตอบ การเรียกใช้ คําตอบ การเรียกใช้ คําตอบ mul(4, 3) 12 gcd(28, 16) 4 expo(4) 16 mul(5, 7) 35 gcd(9, 14) 1 expo(7) 128 mul(20, 10) 200 gcd(75, 30) 15 expo(11) 2048 © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 9. ANSWER 10-11 Computer Programming using Java 9 จงแสดงรายละเอียดของ expo(0), expo(1), expo(2) และ expo(3) expo(0) expo(1) expo(2) 1 2 4 expo(0) expo(0) expo(1) expo(1) 1 1 2 2 expo(0) expo(0) expo(0) expo(0) 1 1 1 1 expo(3) 8 expo(2) expo(2) 4 4 expo(1) expo(1) expo(1) expo(1) 2 2 2 2 expo(0) expo(0) expo(0) expo(0) expo(0) expo(0) expo(0) expo(0) 1 1 1 1 1 1 1 1 โจทย์ ข้อที่ 5 [ระดับง่ าย] การเรียกใช้ และคําตอบ printX(4) printX(7) 1234 1234567 123 123456 12 12345 1 1234 123 12 1 โจทย์ ข้อที่ 6 [ระดับปานกลาง] พารามิเตอร์ n พารามิเตอร์ m 0 1 2 3 4 0 1 2 3 4 5 1 2 3 4 5 6 2 3 5 7 9 11 3 5 13 29 61 125 © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 10. 10 Computer Programming using Java ANSWER 10-11 โจทย์ ข้อที่ 7 [ระดับง่ าย] n กรณีฐาน f n) = 1 ( n = 1 ∑ i3 1 f n) = ( n ≥ 1 i=1 กรณีเวียนเกิด f n) = f n − 1) + 1 / n 3 ( ( n > 1 public static double f(int n) { if (n <= 1) { return 1.0; } else { return f(n - 1) + 1.0 / (n * n * n); } } โจทย์ ข้อที่ 8 [ระดับง่ าย] public static int f(int n) { if (n <= 0) { return 0; } else { return f(n - 1) + (int) Math.pow(n, n) + (2 * n); } } โจทย์ ข้อที่ 9 [ระดับง่ าย] public static double g(int n) { if (n <= 0) { return 0.0; } else if (n == 1) { return 1.0; } else { return g(n - 1) * (1.0 + 1.0 / n); } } โจทย์ ข้อที่ 10 [ระดับง่ าย] public static int fac(int n) { if (n < 2) { return 1; } else { return fac(n - 1) * n; } } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 11. ANSWER 10-11 Computer Programming using Java 11 โจทย์ ข้อที่ 11 [ระดับง่ าย] public static int fibo(int n) { if (n < 2) { return n; } else { return fibo(n - 1) + fibo(n - 2); } } โจทย์ ข้อที่ 12 [ระดับง่ าย] fibo(5) fibo(4) 5 fibo(3) 3 2 fibo(3) fibo(2) fibo(2) fibo(1) 2 1 1 1 fibo(2) fibo(1) fibo(1) fibo(0) fibo(1) fibo(0) 1 1 1 0 1 0 fibo(1) fibo(0) 1 0 โจทย์ ข้อที่ 13 [ระดับปานกลาง] public static double pow(double a, int b) { if (b < 0) { return pow(a, b + 1) * 1.0 / a; } else if (b == 0) { return 1.0; } else { return pow(a, b - 1) * a; } } โจทย์ ข้อที่ 14 [ระดับยาก] public static double f(int n) { if (n <= 1) { return 1.0; } else { return 1.0 + (n / f(n - 1)); } } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 12. 12 Computer Programming using Java ANSWER 10-11 โจทย์ ข้อที่ 15 [ระดับยาก] 1) public static double g(int n, int m) { if (n <= 1) { return 1; } else { if (n % 2 == 0) { return g(n – 1, m) - Math.pow(n, m); } else { return g(n – 1, m) + Math.pow(n, m); } } } 2) public static double h(int n) { if (n <= 1) { return 1; } else { if (n % 2 == 0) { return h(n - 1) + 1.0 / n; } else { return h(n - 1) - 1.0 / n; } } } โจทย์ ข้อที่ 16 [ระดับปานกลาง] public static int search(int a[], int k) { return search(a, k, 0); } public static int search(int a[], int k, int i) { if (i < a.length) { if (a[i] == k) { return i; } else { return search(a, k, i + 1); } } else { return -1; } } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 13. ANSWER 10-11 Computer Programming using Java 13 โจทย์ ข้อที่ 17 [ระดับปานกลาง] public class OverHundredNumber { public static void main(String[] args) { int d[] = { 99, 101, 13, 78, 200, 534, 47, 1234, 736 }; System.out.println(overHundred(d)); } public static int overHundred(int d[]) { return overHundred(d, 0, 0); } public static int overHundred(int d[], int i, int count) { if (i < d.length) { if (d[i] > 100) { return overHundred(d, i + 1, count + 1); } else { return overHundred(d, i + 1, count); } } else { return count; } } } //End of class โจทย์ ข้อที่ 18 [ระดับปานกลาง] public class TheString { public static void main(String[] args) { System.out.println(revStr("Computer")); } public static String revStr(String s) { return revStr(s, "", s.length() - 1); } public static String revStr(String s, String rs, int i) { if (i >= 0) { rs += s.substring(i, i + 1); return revStr(s, rs, i - 1); } else { return rs; } } } //End of class โจทย์ ข้อที่ 19 [ระดับยาก] public static void formulaAtoB(int a, int b) { formulaAtoB(a, b, 1); } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 14. 14 Computer Programming using Java ANSWER 10-11 public static void formulaAtoB(int a, int b, int i) { if (a <= b) { if (i <= 12) { System.out.println(a + " x " + i + " = " + (a * i)); formulaAtoB(a, b, i + 1); } else { System.out.println("-------------"); formulaAtoB(a + 1, b); } }//End of if (a <= b) } โจทย์ ข้อที่ 20 [ระดับยาก] public static int[] addArray(int a[], int b[]) { int sum[] = new int[a.length]; return addArray(a, b, sum, 0); } public static int[] addArray(int a[], int b[], int sum[], int i) { if (i < sum.length) { sum[i] = a[i] + b[i]; return addArray(a, b, sum, i + 1); } else { return sum; } } โจทย์ ข้อที่ 21 [ระดับยาก] public static boolean isMatrixEquals(int a[][], int b[][]) { if (a.length == b.length && a[0].length == b[0].length) { return isMatrixEquals(a, b, 0, 0); } else { return false; } } public static boolean isMatrixEquals(int a[][], int b[][], int i, int j) { if (i < a.length) { if (j < a[i].length) { if (a[i][j] != b[i][j]) { return false; } else { return isMatrixEquals(a, b, i, j + 1); } } else { return isMatrixEquals(a, b, i + 1, 0); } } else { return true; } } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้