SlideShare une entreprise Scribd logo
1  sur  5
Télécharger pour lire hors ligne
ANSWER 07                                                         Computer Programming using Java             1   2        Computer Programming using Java                                                           ANSWER 07


     CHAPTER                                       อาเรย์หนึงมิติ                                                     3)     double a[] = new double[100];
                                                                                                                             for (int i = 0; i < a.length; i++) {
 ANS-07
 ANS-                                         (One Dimensional Arrays)                                                         System.out.print("Enter a[" + i + "]");
                                                                                                                               a[i] = kb.nextDouble();
โจทย์ข้อที 1 [ระดับง่ าย]                                                                                                    }

 3                                              0     1     2      3     4     5     6     7      8     9
 14                                             0     4     4     6      7     1     2     5      7    10             4)     int odd = 0, even = 0;
 9                                                                                                                           for (int i = 0; i < x.length; i++) {
 Index Out of Bounds                                                                                                           if (x[i] % 2 == 0) even++;
 2                                                                                                                             else odd++;
 7                                                                                                                           }
 5                                                                                                                           System.out.println("Odd Number: " + odd);
 21                                                                                                                          System.out.println("Even Number: " + even);


                                                                                                                      5)     System.out.print("Enter x: ");
โจทย์ ข้อที 2 [ระดับง่ าย]                                                                                                   int x = kb.nextInt()
                                                                                                                             int index = -1;
1) long num[] = new long[200];                                                                                               for (int i = 0; i < num.length; i++) {
2) int dice[] = new int[6];                                                                                                    if(num[i] == x) index = i; break;
                                                                                                                             }
3) double avgGrade[] = new double[451];                                                                                      System.out.println("First Index: " + index);
4) String grade[] = new String[369];
5) boolean x[] = new boolean[11];
6) long merge[] = new long[num.length +                         dice.length];
                                                                                                                      โจทย์ข้อที 5 [ระดับปานกลาง]
                                                                                                                      import java.util.Scanner;
โจทย์ ข้อที 3 [ระดับง่ าย]                                                                                            public class MergeArrays {
1) int x = num[49];                                                                                                     public static void main(String[] args) {
                                                                                                                          int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
2) int y = num[50];                                                                                                       int b[] = { 1, 4, 6, 8, 10, 12 };
3) char c = code[code.length - 1];                                                                                             int ab[] = new int[a.length + b.length];
4) bank[12] = var1;                                                                                                            for (int i = 0; i < a.length; i++) {
5) bank[0] = var2;                                                                                                               ab[i] = a[i];
                                                                                                                               }
6) bank[bank.length - 2] = var3;                                                                                               for (int i = a.length; i < ab.length; i++) {
                                                                                                                                 ab[i] = b[i - a.length];
                                                                                                                               }
โจทย์ข้อที 4 [ระดับง่ าย–ปานกลาง]                                                                                              for (int i = 0; i < ab.length; i++) {
1) int n[] = { 1, 3, 5, 7,    9, 11, 13, 15, 17, 19 };                                                                         }
                                                                                                                                 System.out.println(ab[i]);

      for (int i = 0; i < n.length; i++) {
        System.out.println(n[i]);                                                                                       } //End of main
      }                                                                                                               } //End of class


2)    int m[] = new int[1000];
      for (int i = 0; i < m.length; i++) {
        m[i] = i + 1;
      }




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


โจทย์ข้อที 6 [ระดับยาก]                                                                                               โจทย์ข้อที 8 [ระดับยาก]
import java.util.Scanner;                                                                                             import java.util.Scanner;
public class SplitArray {                                                                                             public class EqualityOfArrays {
  public static void main(String[] args) {                                                                              public static void main(String[] args) {
    int num[] = {95, 1, 6, 34, 5, 9, 123, -2, 57, 82, 12, 79, 45, 34, 1};                                                 int a[] = { 1, 2, 3, 4, 5, 6, 7 }, b[] = { 1, 2, 3, 5, 5, 7, 7 };
       Scanner kb = new Scanner(System.in);                                                                                  if (a.length == b.length) {
       System.out.print("Enter x: ");                                                                                          int count = 0;
       int x = kb.nextInt();                                                                                                   for (i = 0; i < a.length; i++) {
       int count = 0, a = 0, b = 0;                                                                                              if (a[i] == b[i]) count++;
       for (int i = 0; i < num.length; i++) {                                                                                  }
         if (num[i] >= x) count++;                                                                                             if (count == a.length) System.out.println("A = B");
       }                                                                                                                       else System.out.println("A != B");
       int upper[] = new int[count];                                                                                         } else {
       int lower[] = new int[num.length - count];                                                                              System.out.println("A != B");
       for (int i = 0; i < num.length; i++) {                                                                                }
         if (num[i] >= x) upper[a++] = num[i];
         else lower[b++] = num[i];
       }                                                                                                                } //End of main
                                                                                                                      } //End of class
  } //End of main
} //End of class

                                                                                                                      โจทย์ข้อที 9 [ระดับยาก]
                                                                                                                      import java.util.Scanner;
โจทย์ข้อที 7 [ระดับยาก]                                                                                               public class LastSecondSearch {
import java.util.Scanner;                                                                                               public static void main(String[] args) {
public class ReverseArray {                                                                                               int num[] = { 3, 2, 1, 10, 2, 8, 3, 2, 1, 1, 8, 5, 10, 11, 7, 6, 10 };
  public static void main(String[] args) {
    int num[] = {95, 1, 6, 34, 5, 9, 123, -2, 57, 82, 12, 79, 45, 34, 1};                                                    Scanner kb = new Scanner(System.in);
                                                                                                                             System.out.print("Enter number: ");
       for (int i = 0; i < num.length / 2; i++) {                                                                            int key = kb.nextInt();
         int temp = num[i];                                                                                                  int index = -1, count = 0;
         num[i] = num[num.length - i - 1];                                                                                   for (int i = num.length - 1; i >= 0; i--) {
         num[num.length - i - 1] = temp;                                                                                       if (key == num[i]) count++;
       }                                                                                                                       if (count == 2) {
                                                                                                                                 index = i; break;
  } //End of main                                                                                                              }
} //End of class                                                                                                             }
                                                                                                                             System.out.println("Second Last index: " + index);

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




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


โจทย์ข้อที 10 [ระดับง่ าย]                                                                                               23             2    30        19        14          5        45         9        99
การจัดเรี ยงแบบเลือก
                                                                                                                         23             2    19        30        14          5        45         9        99
รอบที 1
   30        45        23         2        19        14         5         9         99                                   23             2    19        14        30          5        45         9        99
รอบที 2                                                                                                                  23             2    19        14         5         30        45         9        99
   30         9        23         2        19        14         5         45        99
                                                                                                                         23             2    19        14         5         30        45         9        99
รอบที 3
    5         9        23         2        19        14         30        45        99                                   23             2    19        14         5         30         9        45        99

รอบที 4
    5         9        14         2        19        23         30        45        99                                โจทย์ข้อที 11 [ระดับง่ าย]
                                                                                                                      int x[] = {30, 45, 23, 2, 19, 14, 5, 99, 9};
รอบที 5
    5         9        14         2        19        23         30        45        99                                   30         45        23        2        19         14         5         99        9

รอบที 6                                                                                                               จัดเรี ยงข้ อมล
                                                                                                                                    ู
    5         9         2        14        19        23         30        45        99                                    2             5     9        14        19         23        30        45        99
รอบที 7                                                                                                               รอบที 1
    5         2         9        14        19        23         30        45        99                                    2             5     9        14        19         23        30        45        99
รอบที 8                                                                                                                 left                                     mid                                     right

    2         5         9        14        19        23         30        45        99                                รอบที 2
                                                                                                                          2             5     9        14
การจัดเรี ยงแบบฟอง                                                                                                      left       mid

รอบที 1                                                                                                               รอบที 3
                                                                                                                          9         14
                                                                                                                                                                      การสืบค้ นแบบทวิภาคใช้ เวลา 4 รอบในการสืบค้ น
   30        45        23         2        19        14         5         99         9
                                                                                                                        Left      right                               การสืบค้ นแบบลําดับใช้ เวลา 6 รอบในการสืบค้ น
   30        23        45         2        19        14         5         99         9                                   mid                                          ดังนัน การสืบค้ นแบบทวิภาคทํางานได้ เร็วกว่ า
                                                                                                                      รอบที 4
   30        23         2        45        19        14         5         99         9
                                                                                                                         14
   30        23         2        19        45        14         5         99         9                                  Left
                                                                                                                        right
                                                                                                                        mid
   30        23         2        19        14        45         5         99         9

   30        23         2        19        14         5         45        99         9

   30        23         2        19        14         5         45        99         9

   30        23         2        19        14         5         45        9         99

รอบที 2
   23        30         2        19        14         5         45        9         99

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


โจทย์ข้อที 12 [ระดับยาก]                                                                                                   //สร้ างตัวอ่ านชือ in2 …
import java.util.Scanner;
                                                                                                                             String id[] = new String[count];
import java.io.*;
                                                                                                                             Scanner in2 = new Scanner(new File("student.txt"));
public class SortMonthByBirthDate {
                                                                                                                             int i = 0;
  public static void main(String[] args) throws IOException {
                                                                                                                             while (in2.hasNext()) {
       String month[] = { "January", "February", "March",                                                                      id[i] = in2.nextLine();
                          "April", "May", "June",                                                                              i++;
                          "July", "August", "September",                                                                     }
                          "October", "November", "December" };

       int count[] = new int[12];
                                                                                                                           //สร้ างตัวเขียนชือ out1 …
       Scanner in = new Scanner(new File("birthdate.txt"));
                                                                                                                             PrintStream out1 = new PrintStream(new File("student.txt"));
       while (in.hasNext()) {
                                                                                                                             for (i = 0; i < id.length; i++) {
         String d = in.next();
                                                                                                                               out1.print(id[i] + "t");
         int a = d.indexOf("-");
                                                                                                                               out1.print((int)(Math.random() * 6) + "t");
         int b = d.lastIndexOf("-");
                                                                                                                               out1.print((int)(Math.random() * 11) + "t");
         int m = Integer.parseInt(d.substring(a + 1, b));
                                                                                                                               out1.print((int)(Math.random() * 11) + "t");
         count[m - 1]++;
                                                                                                                               out1.print((int)(Math.random() * 31) + "t");
       }
                                                                                                                               out1.print((int)(Math.random() * 46) + "n");
                                                                                                                             }
       for (int i = count.length - 1; i >= 1; i--) {
         int minIndex = 0;
         for (int j = 0; j <= i; j++)
           if (count[j] < count[minIndex]) minIndex = j;                                                                   //สร้ างตัวอ่ านชือ in3 …
         int temp = count[i];
         count[i] = count[minIndex];                                                                                         Scanner in3 = new Scanner(new File("student.txt"));
         count[minIndex] = temp;                                                                                             int sc[] = new int[count];
         String stemp = month[i];                                                                                            i = 0;
         month[i] = month[minIndex];                                                                                         while (in3.hasNext()) {
         month[minIndex] = stemp;                                                                                              in3.next();
       }                                                                                                                       for (int j = 1; j <= 5; j++) {
                                                                                                                                 sc[i] += in3.nextInt();
       for (int i = 0; i < month.length; i++) {                                                                                }
         System.out.println(month[i] + " [" + count[i] + " people]");                                                          i++;
       }                                                                                                                     }

  } //End of main
} //End of class                                                                                                           //สร้ างตัวเขียนชือ out2 …
                                                                                                                             PrintStream out2 = new PrintStream(new File("totalscore.txt"));
โจทย์ข้อที 13 [ระดับเทพ]                                                                                                     for (i = 0; i < id.length; i++) {
import java.io.*;                                                                                                              out2.print(id[i] + "t");
import java.util.Scanner;                                                                                                      out2.print(sc[i] + "t");
public class ScoreCalculator {                                                                                                 if (sc[i] >= 80) out2.print("An");
  public static void main(String[] args) throws IOException {                                                                  if (sc[i] >= 70 && sc[i] < 80) out2.print("Bn");
                                                                                                                               if (sc[i] >= 60 && sc[i] < 70) out2.print("Cn");
     //สร้ างตัวอ่ านชือ in1 …                                                                                                 if (sc[i] >= 50 && sc[i] < 60) out2.print("Dn");
                                                                                                                               if (sc[i] < 50) out2.print("Fn");
       Scanner in1 = new Scanner(new File("student.txt"));                                                                   }
       int count = 0;
       while (in1.hasNext()) {
         in1.nextLine();
         count++;
       }


© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)           © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
ANSWER 07                                                         Computer Programming using Java             9
     //หาคะแนนรวม …
       int max = 0;
       int min = 0;
       double sum = 0.0;
       int fq[] = new int[101];
       for (i = 0; i < sc.length; i++) {
         if (sc[i] > sc[max]) max = i;
         if (sc[i] < sc[min]) min = i;
         sum += sc[i];
         fq[sc[i]]++;
       }
       int maxFq = 0;
       for (i = 0; i < fq.length; i++) {
         if (fq[i] > fq[maxFq]) maxFq = i;
       }



     //แสดงผลลัพธ์ …
       out2.println("Max Score: " + sc[max]);
       out2.println("Max Score Freq: " + fq[sc[max]]);
       out2.println("Max Score Year: " +
                   (54 - Integer.parseInt(id[max].substring(0, 2))));
       out2.println("Min Score: " + sc[min]);
       out2.println("Min Score Freq: " + fq[sc[min]]);
       out2.println("Min Score Year: " +
                   (54 - Integer.parseInt(id[min].substring(0, 2))));
       out2.println("Avg Score: " + sum / count);
       out2.println("Max Freq: " + fq[maxFq]);
       out2.print("List Max Freq Score: ");
       for (i = 0; i < fq.length; i++) {
         if (fq[i] == fq[maxFq]) out2.print(i + " ");
       }
       out2.println();



     //คําสังในการปิ ดแฟมข้ อมล
                        ้     ู
       in1.close();
       in2.close();
       in3.close();
       out1.close();
       out2.close();

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




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

Contenu connexe

Tendances

Java-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and ObjectsJava-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and ObjectsWongyos Keardsri
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 RecursionsWongyos 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
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsWongyos Keardsri
 
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
 
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชันค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชันsawed kodnara
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นThanachart Numnonda
 
6.Flow control
6.Flow control6.Flow control
6.Flow controlUsableLabs
 
อนุพันธ์
อนุพันธ์อนุพันธ์
อนุพันธ์krurutsamee
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysWongyos Keardsri
 
การอินทีเกรต
การอินทีเกรตการอินทีเกรต
การอินทีเกรตANNRockART
 
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
 

Tendances (20)

Java-Answer Chapter 08-09
Java-Answer Chapter 08-09Java-Answer Chapter 08-09
Java-Answer Chapter 08-09
 
Java-Answer Chapter 12-13
Java-Answer Chapter 12-13Java-Answer Chapter 12-13
Java-Answer Chapter 12-13
 
Java-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and ObjectsJava-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and Objects
 
Java-Answer Chapter 01-04
Java-Answer Chapter 01-04Java-Answer Chapter 01-04
Java-Answer Chapter 01-04
 
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 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsJava-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and Objects
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 Algorithms
 
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: อะเรย์และคอลเล็กชั่น
 
662305 08
662305 08662305 08
662305 08
 
6.Flow control
6.Flow control6.Flow control
6.Flow control
 
อนุพันธ์
อนุพันธ์อนุพันธ์
อนุพันธ์
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional Arrays
 
การอินทีเกรต
การอินทีเกรตการอินทีเกรต
การอินทีเกรต
 
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
 

En vedette

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
 
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 04 Iteration Statements
Java-Chapter 04 Iteration StatementsJava-Chapter 04 Iteration Statements
Java-Chapter 04 Iteration StatementsWongyos 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 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
 
Java-Chapter 05 String Operations
Java-Chapter 05 String OperationsJava-Chapter 05 String Operations
Java-Chapter 05 String OperationsWongyos Keardsri
 
Java-Chapter 02 Data Operations and Processing
Java-Chapter 02 Data Operations and ProcessingJava-Chapter 02 Data Operations and Processing
Java-Chapter 02 Data Operations and ProcessingWongyos Keardsri
 
การควบคุมทิศทางการทำงานของโปรแกรม
การควบคุมทิศทางการทำงานของโปรแกรมการควบคุมทิศทางการทำงานของโปรแกรม
การควบคุมทิศทางการทำงานของโปรแกรมkorn27122540
 

En vedette (10)

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
 
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 04 Iteration Statements
Java-Chapter 04 Iteration StatementsJava-Chapter 04 Iteration Statements
Java-Chapter 04 Iteration Statements
 
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 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-Chapter 05 String Operations
Java-Chapter 05 String OperationsJava-Chapter 05 String Operations
Java-Chapter 05 String Operations
 
Java-Chapter 02 Data Operations and Processing
Java-Chapter 02 Data Operations and ProcessingJava-Chapter 02 Data Operations and Processing
Java-Chapter 02 Data Operations and Processing
 
IP address anonymization
IP address anonymizationIP address anonymization
IP address anonymization
 
การควบคุมทิศทางการทำงานของโปรแกรม
การควบคุมทิศทางการทำงานของโปรแกรมการควบคุมทิศทางการทำงานของโปรแกรม
การควบคุมทิศทางการทำงานของโปรแกรม
 

Plus de 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 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
 
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 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
 

Plus de Wongyos Keardsri (18)

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
 
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 07 (For Print)

  • 1. ANSWER 07 Computer Programming using Java 1 2 Computer Programming using Java ANSWER 07 CHAPTER อาเรย์หนึงมิติ 3) double a[] = new double[100]; for (int i = 0; i < a.length; i++) { ANS-07 ANS- (One Dimensional Arrays) System.out.print("Enter a[" + i + "]"); a[i] = kb.nextDouble(); โจทย์ข้อที 1 [ระดับง่ าย] } 3 0 1 2 3 4 5 6 7 8 9 14 0 4 4 6 7 1 2 5 7 10 4) int odd = 0, even = 0; 9 for (int i = 0; i < x.length; i++) { Index Out of Bounds if (x[i] % 2 == 0) even++; 2 else odd++; 7 } 5 System.out.println("Odd Number: " + odd); 21 System.out.println("Even Number: " + even); 5) System.out.print("Enter x: "); โจทย์ ข้อที 2 [ระดับง่ าย] int x = kb.nextInt() int index = -1; 1) long num[] = new long[200]; for (int i = 0; i < num.length; i++) { 2) int dice[] = new int[6]; if(num[i] == x) index = i; break; } 3) double avgGrade[] = new double[451]; System.out.println("First Index: " + index); 4) String grade[] = new String[369]; 5) boolean x[] = new boolean[11]; 6) long merge[] = new long[num.length + dice.length]; โจทย์ข้อที 5 [ระดับปานกลาง] import java.util.Scanner; โจทย์ ข้อที 3 [ระดับง่ าย] public class MergeArrays { 1) int x = num[49]; public static void main(String[] args) { int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 2) int y = num[50]; int b[] = { 1, 4, 6, 8, 10, 12 }; 3) char c = code[code.length - 1]; int ab[] = new int[a.length + b.length]; 4) bank[12] = var1; for (int i = 0; i < a.length; i++) { 5) bank[0] = var2; ab[i] = a[i]; } 6) bank[bank.length - 2] = var3; for (int i = a.length; i < ab.length; i++) { ab[i] = b[i - a.length]; } โจทย์ข้อที 4 [ระดับง่ าย–ปานกลาง] for (int i = 0; i < ab.length; i++) { 1) int n[] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }; } System.out.println(ab[i]); for (int i = 0; i < n.length; i++) { System.out.println(n[i]); } //End of main } } //End of class 2) int m[] = new int[1000]; for (int i = 0; i < m.length; i++) { m[i] = i + 1; } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 2. ANSWER 07 Computer Programming using Java 3 4 Computer Programming using Java ANSWER 07 โจทย์ข้อที 6 [ระดับยาก] โจทย์ข้อที 8 [ระดับยาก] import java.util.Scanner; import java.util.Scanner; public class SplitArray { public class EqualityOfArrays { public static void main(String[] args) { public static void main(String[] args) { int num[] = {95, 1, 6, 34, 5, 9, 123, -2, 57, 82, 12, 79, 45, 34, 1}; int a[] = { 1, 2, 3, 4, 5, 6, 7 }, b[] = { 1, 2, 3, 5, 5, 7, 7 }; Scanner kb = new Scanner(System.in); if (a.length == b.length) { System.out.print("Enter x: "); int count = 0; int x = kb.nextInt(); for (i = 0; i < a.length; i++) { int count = 0, a = 0, b = 0; if (a[i] == b[i]) count++; for (int i = 0; i < num.length; i++) { } if (num[i] >= x) count++; if (count == a.length) System.out.println("A = B"); } else System.out.println("A != B"); int upper[] = new int[count]; } else { int lower[] = new int[num.length - count]; System.out.println("A != B"); for (int i = 0; i < num.length; i++) { } if (num[i] >= x) upper[a++] = num[i]; else lower[b++] = num[i]; } } //End of main } //End of class } //End of main } //End of class โจทย์ข้อที 9 [ระดับยาก] import java.util.Scanner; โจทย์ข้อที 7 [ระดับยาก] public class LastSecondSearch { import java.util.Scanner; public static void main(String[] args) { public class ReverseArray { int num[] = { 3, 2, 1, 10, 2, 8, 3, 2, 1, 1, 8, 5, 10, 11, 7, 6, 10 }; public static void main(String[] args) { int num[] = {95, 1, 6, 34, 5, 9, 123, -2, 57, 82, 12, 79, 45, 34, 1}; Scanner kb = new Scanner(System.in); System.out.print("Enter number: "); for (int i = 0; i < num.length / 2; i++) { int key = kb.nextInt(); int temp = num[i]; int index = -1, count = 0; num[i] = num[num.length - i - 1]; for (int i = num.length - 1; i >= 0; i--) { num[num.length - i - 1] = temp; if (key == num[i]) count++; } if (count == 2) { index = i; break; } //End of main } } //End of class } System.out.println("Second Last index: " + index); } //End of main } //End of class © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 3. ANSWER 07 Computer Programming using Java 5 6 Computer Programming using Java ANSWER 07 โจทย์ข้อที 10 [ระดับง่ าย] 23 2 30 19 14 5 45 9 99 การจัดเรี ยงแบบเลือก 23 2 19 30 14 5 45 9 99 รอบที 1 30 45 23 2 19 14 5 9 99 23 2 19 14 30 5 45 9 99 รอบที 2 23 2 19 14 5 30 45 9 99 30 9 23 2 19 14 5 45 99 23 2 19 14 5 30 45 9 99 รอบที 3 5 9 23 2 19 14 30 45 99 23 2 19 14 5 30 9 45 99 รอบที 4 5 9 14 2 19 23 30 45 99 โจทย์ข้อที 11 [ระดับง่ าย] int x[] = {30, 45, 23, 2, 19, 14, 5, 99, 9}; รอบที 5 5 9 14 2 19 23 30 45 99 30 45 23 2 19 14 5 99 9 รอบที 6 จัดเรี ยงข้ อมล ู 5 9 2 14 19 23 30 45 99 2 5 9 14 19 23 30 45 99 รอบที 7 รอบที 1 5 2 9 14 19 23 30 45 99 2 5 9 14 19 23 30 45 99 รอบที 8 left mid right 2 5 9 14 19 23 30 45 99 รอบที 2 2 5 9 14 การจัดเรี ยงแบบฟอง left mid รอบที 1 รอบที 3 9 14 การสืบค้ นแบบทวิภาคใช้ เวลา 4 รอบในการสืบค้ น 30 45 23 2 19 14 5 99 9 Left right การสืบค้ นแบบลําดับใช้ เวลา 6 รอบในการสืบค้ น 30 23 45 2 19 14 5 99 9 mid ดังนัน การสืบค้ นแบบทวิภาคทํางานได้ เร็วกว่ า รอบที 4 30 23 2 45 19 14 5 99 9 14 30 23 2 19 45 14 5 99 9 Left right mid 30 23 2 19 14 45 5 99 9 30 23 2 19 14 5 45 99 9 30 23 2 19 14 5 45 99 9 30 23 2 19 14 5 45 9 99 รอบที 2 23 30 2 19 14 5 45 9 99 © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 4. ANSWER 07 Computer Programming using Java 7 8 Computer Programming using Java ANSWER 07 โจทย์ข้อที 12 [ระดับยาก] //สร้ างตัวอ่ านชือ in2 … import java.util.Scanner; String id[] = new String[count]; import java.io.*; Scanner in2 = new Scanner(new File("student.txt")); public class SortMonthByBirthDate { int i = 0; public static void main(String[] args) throws IOException { while (in2.hasNext()) { String month[] = { "January", "February", "March", id[i] = in2.nextLine(); "April", "May", "June", i++; "July", "August", "September", } "October", "November", "December" }; int count[] = new int[12]; //สร้ างตัวเขียนชือ out1 … Scanner in = new Scanner(new File("birthdate.txt")); PrintStream out1 = new PrintStream(new File("student.txt")); while (in.hasNext()) { for (i = 0; i < id.length; i++) { String d = in.next(); out1.print(id[i] + "t"); int a = d.indexOf("-"); out1.print((int)(Math.random() * 6) + "t"); int b = d.lastIndexOf("-"); out1.print((int)(Math.random() * 11) + "t"); int m = Integer.parseInt(d.substring(a + 1, b)); out1.print((int)(Math.random() * 11) + "t"); count[m - 1]++; out1.print((int)(Math.random() * 31) + "t"); } out1.print((int)(Math.random() * 46) + "n"); } for (int i = count.length - 1; i >= 1; i--) { int minIndex = 0; for (int j = 0; j <= i; j++) if (count[j] < count[minIndex]) minIndex = j; //สร้ างตัวอ่ านชือ in3 … int temp = count[i]; count[i] = count[minIndex]; Scanner in3 = new Scanner(new File("student.txt")); count[minIndex] = temp; int sc[] = new int[count]; String stemp = month[i]; i = 0; month[i] = month[minIndex]; while (in3.hasNext()) { month[minIndex] = stemp; in3.next(); } for (int j = 1; j <= 5; j++) { sc[i] += in3.nextInt(); for (int i = 0; i < month.length; i++) { } System.out.println(month[i] + " [" + count[i] + " people]"); i++; } } } //End of main } //End of class //สร้ างตัวเขียนชือ out2 … PrintStream out2 = new PrintStream(new File("totalscore.txt")); โจทย์ข้อที 13 [ระดับเทพ] for (i = 0; i < id.length; i++) { import java.io.*; out2.print(id[i] + "t"); import java.util.Scanner; out2.print(sc[i] + "t"); public class ScoreCalculator { if (sc[i] >= 80) out2.print("An"); public static void main(String[] args) throws IOException { if (sc[i] >= 70 && sc[i] < 80) out2.print("Bn"); if (sc[i] >= 60 && sc[i] < 70) out2.print("Cn"); //สร้ างตัวอ่ านชือ in1 … if (sc[i] >= 50 && sc[i] < 60) out2.print("Dn"); if (sc[i] < 50) out2.print("Fn"); Scanner in1 = new Scanner(new File("student.txt")); } int count = 0; while (in1.hasNext()) { in1.nextLine(); count++; } © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
  • 5. ANSWER 07 Computer Programming using Java 9 //หาคะแนนรวม … int max = 0; int min = 0; double sum = 0.0; int fq[] = new int[101]; for (i = 0; i < sc.length; i++) { if (sc[i] > sc[max]) max = i; if (sc[i] < sc[min]) min = i; sum += sc[i]; fq[sc[i]]++; } int maxFq = 0; for (i = 0; i < fq.length; i++) { if (fq[i] > fq[maxFq]) maxFq = i; } //แสดงผลลัพธ์ … out2.println("Max Score: " + sc[max]); out2.println("Max Score Freq: " + fq[sc[max]]); out2.println("Max Score Year: " + (54 - Integer.parseInt(id[max].substring(0, 2)))); out2.println("Min Score: " + sc[min]); out2.println("Min Score Freq: " + fq[sc[min]]); out2.println("Min Score Year: " + (54 - Integer.parseInt(id[min].substring(0, 2)))); out2.println("Avg Score: " + sum / count); out2.println("Max Freq: " + fq[maxFq]); out2.print("List Max Freq Score: "); for (i = 0; i < fq.length; i++) { if (fq[i] == fq[maxFq]) out2.print(i + " "); } out2.println(); //คําสังในการปิ ดแฟมข้ อมล ้ ู in1.close(); in2.close(); in3.close(); out1.close(); out2.close(); } //End of main } //End of class © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)