SlideShare ist ein Scribd-Unternehmen logo
1 von 11
Downloaden Sie, um offline zu lesen
ANSWER 05-06                                                       Computer Programming using Java              1
     CHAPTER                                    การดําเนินการกับสตริง
 ANS-05                                           (String Operations)

โจทย์ ข้อที่ 1 [ระดับง่ าย]
 ข้ อ ประเภทตัวแปร ค่ าที่เก็บในตัวแปร                     ข้ อ ประเภทตัวแปร ค่ าที่เก็บในตัวแปร
  1. int                    6                              11. int           -1
  2. int                    5                              12. int           7
  3. int                    1                              13. String        JavaChula
  4. int                    3                              14. String        I Lo
  5. String                 CHULA chula                    15. String        love java
  6. char                   v                              16. boolean       false
  7. int                    4                              17. boolean       true
  8. int                    9                              18. boolean       true
  9. int                    8                              19. int           -20
 10. int                    8                              20. [Error]       [Error]




โจทย์ ข้อที่ 2 [ระดับง่ าย]
 ข้ อ ประเภทตัวแปร ค่ าที่เก็บในตัวแปร                     ข้ อ ประเภทตัวแปร ค่ าที่เก็บในตัวแปร
  1. int                    2000                            6. [Error]       [Error]
  2. [Error]                [Error]                         7. String        2000.0
  3. double                 2000.0                          8. String        1000.0
  4. double                 1000.0                         *9. boolean       false
  5. String                 2000                           10. boolean       true



โจทย์ ข้อที่ 3 [ระดับง่ าย]
1. p = p.toUpperCase();
      r = r.toUpperCase();

2.    boolean y = p.substring(4, 5).equals(r.substring(4, 5));

3.    double m = Double.parseDouble(p.substring(1, 6));




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


    โจทย์ ข้อที่ 4 [ระดับง่ าย]
     import java.util.Scanner;
     public class InputWords {
       public static void main(String[] args) {
         Scanner kb = new Scanner(System.in);
         String text = "";
         while (true) {
           System.out.print("Enter Word: ");
           String w = kb.next();
           if (w.toLowerCase().equals("stop")) break;
           text += w + " "; /* text = text + w + " "; */
         }
         System.out.println(text);
       }
     }



    โจทย์ ข้อที่ 5 [ระดับปานกลาง]
     import java.util.Scanner;
     public class RemoveSpaceFromSentence {
       public static void main(String[] args) {
         Scanner kb = new Scanner(System.in);
         System.out.print("Enter Sentence: ");
         String s = kb.nextLine();
         String sn = "";
         for (int i = 0; i < s.length(); i++) {
           String t = s.substring(i, i + 1);
           if (!t.equals(" ")) {
             sn = sn + t;
           }
         }
         System.out.println(sn);
       }
     }



    โจทย์ ข้อที่ 6 [ระดับปานกลาง]
     import java.util.Scanner;
     public class ReverseSentence {
       public static void main(String[] args) {
         Scanner kb = new Scanner(System.in);
         System.out.print("Enter Sentence: ");
         String s = kb.nextLine(), rs = "";
         for (int i = s.length() - 1; i >= 0; i--) {
           String t = s.substring(i, i + 1);
           rs = rs + t;
         }
         System.out.println(rs);
       }
     }




    © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
                                                ้
ANSWER 05-06                                                       Computer Programming using Java              3
โจทย์ ข้อที่ 7 [ระดับปานกลาง]
 import java.util.Scanner;
 public class FullName {
   public static void main(String[] args) {
     Scanner kb = new Scanner(System.in);
     System.out.print("Full Name: ");
     String fullName = kb.nextLine();
     int i = fullName.trim().indexOf(" ");
     if (i == -1) {
       System.out.println("Incorrect Name");
     } else {
       String firstName = fullName.substring(0, i);
       String lastName = fullName.substring(i + 1).trim();
       System.out.println("First Name: " + firstName.toUpperCase());
       System.out.println("Last Name: " + lastName.toLowerCase());
     }
   }
 }



โจทย์ ข้อที่ 8 [ระดับปานกลาง]
1) String s1 = "<title>First   Web Page</title>";
     String s2 = "<a href="http://www.javachula.co.cc">JavaChula</a>";

2)   int start = s1.indexOf("<title>") + 7;
     int end = s1.indexOf("</title>");
     String title = s1.substring(start, end);

3)   int start = s2.indexOf(""") + 1;
     int end = s2.lastIndexOf(""");
     String url = s2.substring(start, end);



โจทย์ ข้อที่ 9 [ระดับปานกลาง]
 int first = d.indexOf("/");
 int last = d.lastIndexOf("/");
 System.out.println(d.substring(0, first));
 System.out.println(d.substring(first + 1, last));
 System.out.println(d.substring(last + 1));




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


    โจทย์ ข้อที่ 10 [ระดับยาก]
     import java.util.Scanner;
     public class Palindrome {
       public static void main(String[] args) {
         Scanner kb = new Scanner(System.in);
         System.out.print("Text: ");
         String t = kb.nextLine(), s = "", r = "";
         for (int i = 0; i < t.length(); i++) {
           String x = t.substring(i, i + 1);
           if (!x.equals(" ")) {
             s = s + x;
             r = x + r;
           }
         }
         if (s.equalsIgnoreCase(r)) {
           System.out.println("It is palindrome");
         } else {
           System.out.println("It is not palindrome");
         }
       }
     }


    โจทย์ ข้อที่ 11 [ระดับยาก]
    import java.util.Scanner;
    public class NumberAndCharacter {
      public static void main(String[] args) {
           Scanner kb = new Scanner(System.in);
           System.out.print("STRING: ");
           String s = kb.nextLine();
           int max = -1, min = 10, sum = 0, digit = 0, text = 0;
           for (int i = 0; i < s.length(); i++) {
             String ch = s.substring(i, i + 1);
             if (ch.compareTo("0") >= 0 && ch.compareTo("9") <= 0) {
               int n = Integer.parseInt(ch);
               if (n > max) {
                 max = n;
               }
               if (n < min) {
                 min = n;
               }
               sum += n;
               digit++;
             } else {
               text++;
             }
           } //End of for
           System.out.println("MAX VALUE: " + max);
           System.out.println("MIN VALUE: " + min);
           System.out.println("AVERAGE VALUE (" + sum + "/" + digit + "): " +
                              ((double) sum / (double) digit));
           System.out.println("TOTAL CHARACTER: " + text);

      } //End of main
    } //End of class
    © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
                                                ้
ANSWER 05-06                                                       Computer Programming using Java              5
โจทย์ ข้อที่ 12 [ระดับยาก]
import java.util.Scanner;
public class NoobChat {
  public static void main(String[] args) {
       Scanner kb = new Scanner(System.in);
       System.out.print("Message: ");
       String message = kb.nextLine();
       String m = message.toLowerCase();
       String badWord = "";
       int bw = 0;

       if (m.indexOf("shit")            >= 0) {
         badWord += "Shit ";            bw++;
       }
       if (m.indexOf("fuck")            >= 0) {
         badWord += "Fuck ";            bw++;
       }
       if (m.indexOf("java")            >= 0) {
         badWord += "Java ";            bw++;
       }

       if (bw == 0) {
         System.out.println(message);
       } else if (bw == 1) {
         System.out.println(badWord + "is Bad Word.");
       } else {
         System.out.println(badWord + "are Bad Words.");
       }//End of if

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




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


    โจทย์ ข้อที่ 13 [ระดับเทพ]
    import java.util.Scanner;
    public class GodNoobChat {
      public static void main(String[] args) {
           Scanner kb = new Scanner(System.in);
           String msg = "", lMsg = "", rMsg = "";
           System.out.println("Message: ");
           while (true) {
             lMsg = kb.nextLine();
             if (lMsg.equals("...")) break;
             msg += lMsg + "n";
           }
           for (int i = 0; i < msg.length(); i++) {
             if (!msg.substring(i, i + 1).equals(" ") &&
                 !msg.substring(i, i + 1).equals("n"))
               rMsg += msg.substring(i, i + 1);
           }

           int countShit = 0, countFuck = 0, countJava = 0;
           String temp = rMsg.toLowerCase();
           while (temp.indexOf("shit") >= 0) {
             countShit++;
             temp = temp.substring(temp.indexOf("shit") + 4);
           }
           temp = rMsg.toLowerCase();
           while (temp.indexOf("fuck") >= 0) {
             countFuck++;
             temp = temp.substring(temp.indexOf("fuck") + 4);
           }
           temp = rMsg.toLowerCase();
           while (temp.indexOf("java") >= 0) {
             countJava++;
             temp = temp.substring(temp.indexOf("java") + 4);
           }

           System.out.println("Count "Shit": " + countShit);
           System.out.println("Count "Fuck": " + countFuck);
           System.out.println("Count "Java": " + countJava);

           String badWord = ""; int bw                = 0;
           if (countShit > 0) { badWord               += "Shit "; bw++; }
           if (countFuck > 0) { badWord               += "Fuck "; bw++; }
           if (countJava > 0) { badWord               += "Java "; bw++; }
           if (bw == 0) {
             System.out.println(msg);
           } else if (bw == 1) {
             System.out.println(badWord               + "is bad word.");
           } else {
             System.out.println(badWord               + "are bad word.");
           }

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




    © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
                                                ้
ANSWER 05-06                                                       Computer Programming using Java              7
   CHAPTER                                  การดําเนินการกับแฟมข้อมูล
                                                                ้
 ANS-06                                          (File Operations)
โจทย์ ข้อที่ 1 [ระดับง่ าย]
     in.nextInt()              in.nextDouble()             in.nextLine()                 in.next()
 1                            1.0                       1   2    3                 1
 2                            2.0                       4                          2
 3                            3.0                       5 6                        3
 4                            4.0                       7.0 8 9D 0                 4
 5                            5.0                                                  5
 6                            6.0                                                  6
 [Error]                      7.0                                                  7.0
                              8.0                                                  8
                              [Error]                                              9D
                                                                                   0

   จํานวนรอบของ while          จํานวนรอบของ while        จํานวนรอบของ while          จํานวนรอบของ while
 7 รอบ                        9 รอบ                     4 รอบ                      10 รอบ



โจทย์ ข้อที่ 2 [ระดับง่ าย]
 import java.util.Scanner;
 import java.io.*;
 public class NumberOfLine {
   public static void main(String[] args) throws IOException {
     Scanner in = new Scanner(new File("data.txt"));
     int numOfLine = 0;
     while(in.hasNext()) {
       in.nextLine();
       numOfLine++;
     }
     System.out.println("Number of Lines: " + numOfLine);
     in.close();
   }
 }




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


    โจทย์ ข้อที่ 3 [ระดับง่ าย]
     import java.util.Scanner;
     import java.io.*;
     public class NumberOfWord {
       public static void main(String[] args) throws IOException {
         Scanner in = new Scanner(new File("data.txt"));
         int numOfWord = 0;
         while(in.hasNext()) {
           in.next();
           numOfWord++;
         }
         System.out.println("Number of Words: " + numOfWord);
         in.close();
       }
     }



    โจทย์ ข้อที่ 4 [ระดับง่ าย]
     import java.util.Scanner;
     import java.io.*;
     public class NumberOfCharacter {
       public static void main(String[] args) throws IOException {
         Scanner in = new Scanner(new File("data.txt"));
         int numOfChar = 0;
         while(in.hasNext()) {
           numOfChar += in.nextLine().length();
         }
         System.out.println("Number of Chars: " + numOfChar);
         in.close();
       }
     }



    โจทย์ ข้อที่ 5 [ระดับปานกลาง]
    import java.util.Scanner;
    import java.io.*;
    public class CountEngStudent {
      public static void main(String[] args) throws IOException {
            Scanner in = new Scanner(new File("std.txt"));
            int count = 0;
            while (in.hasNext()) {
              String s = in.nextLine();
              if (s.substring(s.length() - 2).equals("21")) count++;
            }
            in.close();
            System.out.println("Engineering Students: " + count);

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




    © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
                                                ้
ANSWER 05-06                                                       Computer Programming using Java              9
โจทย์ ข้อที่ 6 [ระดับปานกลาง]
import java.util.Scanner;
import java.io.*;
public class StudentGrade {
  public static void main(String[] args) throws IOException {
       Scanner in = new Scanner(new File("score.txt"));
       int i = 1;                       ตัวอย่ างการแสดงผลบนจอภาพ
       while (in.hasNext()) {
         String id = in.next();
         double score = in.nextDouble();
         in.next(); //faculty
         if(score >= 60.0) {
           System.out.println(i + ".t" + id + "tS");
         } else {
           System.out.println(i + ".t" + id + "tU");
         }
         i++;
       }
       in.close();

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


โจทย์ ข้อที่ 7 [ระดับยาก]
import java.util.Scanner;
import java.io.*;
public class StudentInfoFromFile {
  public static void main(String[] args) throws IOException {
       Scanner in = new Scanner(new File("student.dat"));
       int i = 1;
       while (in.hasNext()) {
         String id = in.next();
         String fname = in.next();
         String lname = in.next();
         double grade = in.nextDouble();
         int year = 54 - Integer.parseInt(id.substring(0, 2));
         String y = "";
         if (year == 1) y = year + "st";
         if (year == 2) y = year + "nd";
         if (year == 3) y = year + "rd";
         if (year >= 4) y = year + "th";
         String shortName = fname.substring(0, 1).toUpperCase() + ".";
         String status = "";
         if (grade >= 2.00) status = "Pass";
         if (grade >= 1.00 && grade < 2.00) status = "Critical";
         if (grade < 1.00) status = "Retired";
         System.out.println(i++ + ".t" + id + "t" + y + "t" +
               shortName + " " + lname + "t" +
               grade + "t" + status);
       } //End of while
       in.close();

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

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


     โจทย์ ข้อที่ 8 [ระดับยาก]
      import java.util.Scanner;
      import java.io.*;
      public class CountLoveFromSongFile {
        public static void main(String[] args) throws IOException {
          Scanner in = new Scanner(new File("song.txt"));
          int w1 = 0, w2 = 0;
          String s = "", txt = "";
          while (in.hasNext()) {
            s = in.next().toLowerCase();
            if (s.equals("love")) w1++;
            txt += s;
          }
          while (txt.indexOf("love") >= 0) {
            w2++;
            txt = txt.substring(txt.indexOf("love") + 4);
          }
          in.close();
          System.out.println("Count Words #1: " + w1);
          System.out.println("Count Words #2: " + w2);
        }
      }



     โจทย์ ข้อที่ 9 [ระดับยาก]
     import java.util.Scanner;
     import java.io.*;
     public class FComparison {
       public static void main(String[] args) throws IOException {
            Scanner in1 = new Scanner(new File("data1.dat"));
            Scanner in2 = new Scanner(new File("data2.dat"));
            int f1 = 0, f2 = 0, n1 = 0, n2 = 0;
            in1.nextLine();
            while (in1.hasNext()) {
              in1.next(); in1.next();
              String grade = in1.next();
              if (grade.equals("F")) f1++;
              n1++;
            } //End of while
            in2.nextLine();
            while (in2.hasNext()) {
              in2.next(); in2.next();
              String grade = in2.next();
              if (grade.equals("F")) f2++;
              n2++;
            } //End of while
            in1.close(); in2.close();
            System.out.println("F 2/2552: " + ((double) f1 / n1) * 100);
            System.out.println("F 2/2553: " + ((double) f2 / n2) * 100);
            if (f1 > f2) System.out.println("F (2/2552) > F (2/2553)");
            if (f1 == f2) System.out.println("F (2/2552) = F (2/2553)");
            if (f1 < f2) System.out.println("F (2/2552) < F (2/2553)");

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

     © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
                                                 ้
ANSWER 05-06                                                       Computer Programming using Java              11
โจทย์ ข้อที่ 10 [ระดับง่ าย]
 import java.io.*;
 import java.util.Scanner;
 public class InputStringToFile {
   public static void main(String[] args) throws IOException {
     Scanner kb = new Scanner(System.in);
     PrintStream out = new PrintStream(new File("sentence.txt"));
     int i = 1;
     while (true) {
       System.out.print("Sentence: ");
       String s = kb.nextLine().toUpperCase();
       if (s.trim().equalsIgnoreCase("stop")) break;
       out.println(i + ": " + s);
       i++;
     }
     System.out.println("File is saved");
     out.close();
   }
 }



โจทย์ ข้อที่ 11 [ระดับยาก]
 import java.util.Scanner;
 import java.io.*;
 public class ReverseTextFile {
   public static void main(String[] args) throws IOException {
     Scanner in = new Scanner(new File("text.txt"));
     PrintStream out = new PrintStream(new File("revtext.txt"));
     while (in.hasNext()) {
       String s = in.nextLine();
       String rev = "";
       for (int i = s.length() - 1; i >= 0; i--) {
         rev += s.substring(i, i + 1);
       }
       out.println(rev);
     }
     in.close();
     out.close();
   }
 }




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

Weitere ähnliche Inhalte

Was ist angesagt?

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-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 RecursionsWongyos Keardsri
 
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-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 10-11 (For Print)Java-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 10-11 (For Print)Wongyos Keardsri
 
Java-Answer Chapter 08-09 (For Print)
Java-Answer Chapter 08-09 (For Print)Java-Answer Chapter 08-09 (For Print)
Java-Answer Chapter 08-09 (For Print)Wongyos Keardsri
 
Java-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsJava-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsWongyos Keardsri
 
Java-Chapter 01 Introduction to Java Programming
Java-Chapter 01 Introduction to Java ProgrammingJava-Chapter 01 Introduction to Java Programming
Java-Chapter 01 Introduction to Java ProgrammingWongyos Keardsri
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsWongyos Keardsri
 
2.Java fundamentals
2.Java fundamentals2.Java fundamentals
2.Java fundamentalsUsableLabs
 
6.Flow control
6.Flow control6.Flow control
6.Flow controlUsableLabs
 
บทที่ 5 คลาส
บทที่ 5 คลาสบทที่ 5 คลาส
บทที่ 5 คลาสTheeravaj Tum
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นThanachart Numnonda
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมThanachart Numnonda
 
Java Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionJava Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionIMC Institute
 

Was ist angesagt? (20)

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-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 Recursions
 
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 07
Java-Answer Chapter 07Java-Answer Chapter 07
Java-Answer Chapter 07
 
Java-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 10-11 (For Print)Java-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 10-11 (For Print)
 
Java-Answer Chapter 08-09 (For Print)
Java-Answer Chapter 08-09 (For Print)Java-Answer Chapter 08-09 (For Print)
Java-Answer Chapter 08-09 (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
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
4.Oop
4.Oop4.Oop
4.Oop
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 Algorithms
 
5.Methods cs
5.Methods cs5.Methods cs
5.Methods cs
 
2.Java fundamentals
2.Java fundamentals2.Java fundamentals
2.Java fundamentals
 
3.Expression
3.Expression3.Expression
3.Expression
 
6.Flow control
6.Flow control6.Flow control
6.Flow control
 
662305 08
662305 08662305 08
662305 08
 
บทที่ 5 คลาส
บทที่ 5 คลาสบทที่ 5 คลาส
บทที่ 5 คลาส
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่น
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุม
 
Java Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionJava Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and Collection
 

Andere mochten auch

Graph theory discrete mathmatics
Graph theory discrete mathmaticsGraph theory discrete mathmatics
Graph theory discrete mathmaticsBiplab Debnath
 
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 III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIWongyos 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
 
Trees (slides)
Trees (slides)Trees (slides)
Trees (slides)IIUM
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and GraphsIntro C# Book
 
Characteristics of gifted students
Characteristics of gifted studentsCharacteristics of gifted students
Characteristics of gifted studentsTrinette Atri
 

Andere mochten auch (10)

Graph theory discrete mathmatics
Graph theory discrete mathmaticsGraph theory discrete mathmatics
Graph theory discrete mathmatics
 
Trees
TreesTrees
Trees
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part II
 
Trees and graphs
Trees and graphsTrees and graphs
Trees and graphs
 
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 I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part I
 
Trees (slides)
Trees (slides)Trees (slides)
Trees (slides)
 
Discrete-Chapter 10 Trees
Discrete-Chapter 10 TreesDiscrete-Chapter 10 Trees
Discrete-Chapter 10 Trees
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Characteristics of gifted students
Characteristics of gifted studentsCharacteristics of gifted students
Characteristics of gifted students
 

Ähnlich wie Java-Answer Chapter 05-06

คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
Java Programming [12/12] : Thread
Java Programming [12/12] : ThreadJava Programming [12/12] : Thread
Java Programming [12/12] : ThreadIMC Institute
 
การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา CWarawut
 
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์Thanachart Numnonda
 
บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์Theeravaj Tum
 
Java Programming [6/12] : Object Oriented Java Programming
Java Programming [6/12] : Object Oriented Java ProgrammingJava Programming [6/12] : Object Oriented Java Programming
Java Programming [6/12] : Object Oriented Java ProgrammingIMC Institute
 
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1หน่อย หน่อย
 
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซีใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซีNattapon
 
Java Programming [9/12]: Exception Handling
Java Programming [9/12]: Exception HandlingJava Programming [9/12]: Exception Handling
Java Programming [9/12]: Exception HandlingIMC Institute
 

Ähnlich wie Java-Answer Chapter 05-06 (20)

คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
Computer Programming 3
Computer Programming 3 Computer Programming 3
Computer Programming 3
 
08 arrays
08 arrays08 arrays
08 arrays
 
Computer Programming 2.1
Computer Programming 2.1Computer Programming 2.1
Computer Programming 2.1
 
Computer Programming 2.2
Computer Programming 2.2Computer Programming 2.2
Computer Programming 2.2
 
Java Programming [12/12] : Thread
Java Programming [12/12] : ThreadJava Programming [12/12] : Thread
Java Programming [12/12] : Thread
 
Python101
Python101Python101
Python101
 
02 basic
02 basic02 basic
02 basic
 
การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา C
 
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
 
บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์
 
Java Programming [6/12] : Object Oriented Java Programming
Java Programming [6/12] : Object Oriented Java ProgrammingJava Programming [6/12] : Object Oriented Java Programming
Java Programming [6/12] : Object Oriented Java Programming
 
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
 
..Arrays..
..Arrays....Arrays..
..Arrays..
 
07 methods
07 methods07 methods
07 methods
 
Inet
InetInet
Inet
 
lesson3 JSP
lesson3 JSPlesson3 JSP
lesson3 JSP
 
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซีใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
ใบความรู้ที่ 1 ความรู้พื้นฐานโปรแกรมภาษาซี
 
Java Programming [9/12]: Exception Handling
Java Programming [9/12]: Exception HandlingJava Programming [9/12]: Exception Handling
Java Programming [9/12]: Exception Handling
 
Control structure
Control structureControl structure
Control structure
 

Mehr von 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 ProgramWongyos Keardsri
 
The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsWongyos Keardsri
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingWongyos Keardsri
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemWongyos Keardsri
 
SysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageSysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageWongyos Keardsri
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsWongyos Keardsri
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityWongyos Keardsri
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 CountingWongyos Keardsri
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsWongyos Keardsri
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIWongyos Keardsri
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IWongyos Keardsri
 
Discrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesDiscrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesWongyos Keardsri
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesWongyos Keardsri
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationWongyos Keardsri
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowWongyos Keardsri
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysWongyos Keardsri
 
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsJava-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsWongyos Keardsri
 
Java-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysJava-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysWongyos Keardsri
 

Mehr von 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 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 Relations
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 Probability
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 Counting
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and Proofs
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part II
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part I
 
Discrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesDiscrete-Chapter 03 Matrices
Discrete-Chapter 03 Matrices
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and Sequences
 
Discrete-Chapter 01 Sets
Discrete-Chapter 01 SetsDiscrete-Chapter 01 Sets
Discrete-Chapter 01 Sets
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling Computation
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindow
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional Arrays
 
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsJava-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and Applications
 
Java-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysJava-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional Arrays
 

Java-Answer Chapter 05-06

  • 1. ANSWER 05-06 Computer Programming using Java 1 CHAPTER การดําเนินการกับสตริง ANS-05 (String Operations) โจทย์ ข้อที่ 1 [ระดับง่ าย] ข้ อ ประเภทตัวแปร ค่ าที่เก็บในตัวแปร ข้ อ ประเภทตัวแปร ค่ าที่เก็บในตัวแปร 1. int 6 11. int -1 2. int 5 12. int 7 3. int 1 13. String JavaChula 4. int 3 14. String I Lo 5. String CHULA chula 15. String love java 6. char v 16. boolean false 7. int 4 17. boolean true 8. int 9 18. boolean true 9. int 8 19. int -20 10. int 8 20. [Error] [Error] โจทย์ ข้อที่ 2 [ระดับง่ าย] ข้ อ ประเภทตัวแปร ค่ าที่เก็บในตัวแปร ข้ อ ประเภทตัวแปร ค่ าที่เก็บในตัวแปร 1. int 2000 6. [Error] [Error] 2. [Error] [Error] 7. String 2000.0 3. double 2000.0 8. String 1000.0 4. double 1000.0 *9. boolean false 5. String 2000 10. boolean true โจทย์ ข้อที่ 3 [ระดับง่ าย] 1. p = p.toUpperCase(); r = r.toUpperCase(); 2. boolean y = p.substring(4, 5).equals(r.substring(4, 5)); 3. double m = Double.parseDouble(p.substring(1, 6)); © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 2. 2 Computer Programming using Java ANSWER 05-06 โจทย์ ข้อที่ 4 [ระดับง่ าย] import java.util.Scanner; public class InputWords { public static void main(String[] args) { Scanner kb = new Scanner(System.in); String text = ""; while (true) { System.out.print("Enter Word: "); String w = kb.next(); if (w.toLowerCase().equals("stop")) break; text += w + " "; /* text = text + w + " "; */ } System.out.println(text); } } โจทย์ ข้อที่ 5 [ระดับปานกลาง] import java.util.Scanner; public class RemoveSpaceFromSentence { public static void main(String[] args) { Scanner kb = new Scanner(System.in); System.out.print("Enter Sentence: "); String s = kb.nextLine(); String sn = ""; for (int i = 0; i < s.length(); i++) { String t = s.substring(i, i + 1); if (!t.equals(" ")) { sn = sn + t; } } System.out.println(sn); } } โจทย์ ข้อที่ 6 [ระดับปานกลาง] import java.util.Scanner; public class ReverseSentence { public static void main(String[] args) { Scanner kb = new Scanner(System.in); System.out.print("Enter Sentence: "); String s = kb.nextLine(), rs = ""; for (int i = s.length() - 1; i >= 0; i--) { String t = s.substring(i, i + 1); rs = rs + t; } System.out.println(rs); } } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 3. ANSWER 05-06 Computer Programming using Java 3 โจทย์ ข้อที่ 7 [ระดับปานกลาง] import java.util.Scanner; public class FullName { public static void main(String[] args) { Scanner kb = new Scanner(System.in); System.out.print("Full Name: "); String fullName = kb.nextLine(); int i = fullName.trim().indexOf(" "); if (i == -1) { System.out.println("Incorrect Name"); } else { String firstName = fullName.substring(0, i); String lastName = fullName.substring(i + 1).trim(); System.out.println("First Name: " + firstName.toUpperCase()); System.out.println("Last Name: " + lastName.toLowerCase()); } } } โจทย์ ข้อที่ 8 [ระดับปานกลาง] 1) String s1 = "<title>First Web Page</title>"; String s2 = "<a href="http://www.javachula.co.cc">JavaChula</a>"; 2) int start = s1.indexOf("<title>") + 7; int end = s1.indexOf("</title>"); String title = s1.substring(start, end); 3) int start = s2.indexOf(""") + 1; int end = s2.lastIndexOf("""); String url = s2.substring(start, end); โจทย์ ข้อที่ 9 [ระดับปานกลาง] int first = d.indexOf("/"); int last = d.lastIndexOf("/"); System.out.println(d.substring(0, first)); System.out.println(d.substring(first + 1, last)); System.out.println(d.substring(last + 1)); © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 4. 4 Computer Programming using Java ANSWER 05-06 โจทย์ ข้อที่ 10 [ระดับยาก] import java.util.Scanner; public class Palindrome { public static void main(String[] args) { Scanner kb = new Scanner(System.in); System.out.print("Text: "); String t = kb.nextLine(), s = "", r = ""; for (int i = 0; i < t.length(); i++) { String x = t.substring(i, i + 1); if (!x.equals(" ")) { s = s + x; r = x + r; } } if (s.equalsIgnoreCase(r)) { System.out.println("It is palindrome"); } else { System.out.println("It is not palindrome"); } } } โจทย์ ข้อที่ 11 [ระดับยาก] import java.util.Scanner; public class NumberAndCharacter { public static void main(String[] args) { Scanner kb = new Scanner(System.in); System.out.print("STRING: "); String s = kb.nextLine(); int max = -1, min = 10, sum = 0, digit = 0, text = 0; for (int i = 0; i < s.length(); i++) { String ch = s.substring(i, i + 1); if (ch.compareTo("0") >= 0 && ch.compareTo("9") <= 0) { int n = Integer.parseInt(ch); if (n > max) { max = n; } if (n < min) { min = n; } sum += n; digit++; } else { text++; } } //End of for System.out.println("MAX VALUE: " + max); System.out.println("MIN VALUE: " + min); System.out.println("AVERAGE VALUE (" + sum + "/" + digit + "): " + ((double) sum / (double) digit)); System.out.println("TOTAL CHARACTER: " + text); } //End of main } //End of class © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 5. ANSWER 05-06 Computer Programming using Java 5 โจทย์ ข้อที่ 12 [ระดับยาก] import java.util.Scanner; public class NoobChat { public static void main(String[] args) { Scanner kb = new Scanner(System.in); System.out.print("Message: "); String message = kb.nextLine(); String m = message.toLowerCase(); String badWord = ""; int bw = 0; if (m.indexOf("shit") >= 0) { badWord += "Shit "; bw++; } if (m.indexOf("fuck") >= 0) { badWord += "Fuck "; bw++; } if (m.indexOf("java") >= 0) { badWord += "Java "; bw++; } if (bw == 0) { System.out.println(message); } else if (bw == 1) { System.out.println(badWord + "is Bad Word."); } else { System.out.println(badWord + "are Bad Words."); }//End of if } //End of main } //End of class © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 6. 6 Computer Programming using Java ANSWER 05-06 โจทย์ ข้อที่ 13 [ระดับเทพ] import java.util.Scanner; public class GodNoobChat { public static void main(String[] args) { Scanner kb = new Scanner(System.in); String msg = "", lMsg = "", rMsg = ""; System.out.println("Message: "); while (true) { lMsg = kb.nextLine(); if (lMsg.equals("...")) break; msg += lMsg + "n"; } for (int i = 0; i < msg.length(); i++) { if (!msg.substring(i, i + 1).equals(" ") && !msg.substring(i, i + 1).equals("n")) rMsg += msg.substring(i, i + 1); } int countShit = 0, countFuck = 0, countJava = 0; String temp = rMsg.toLowerCase(); while (temp.indexOf("shit") >= 0) { countShit++; temp = temp.substring(temp.indexOf("shit") + 4); } temp = rMsg.toLowerCase(); while (temp.indexOf("fuck") >= 0) { countFuck++; temp = temp.substring(temp.indexOf("fuck") + 4); } temp = rMsg.toLowerCase(); while (temp.indexOf("java") >= 0) { countJava++; temp = temp.substring(temp.indexOf("java") + 4); } System.out.println("Count "Shit": " + countShit); System.out.println("Count "Fuck": " + countFuck); System.out.println("Count "Java": " + countJava); String badWord = ""; int bw = 0; if (countShit > 0) { badWord += "Shit "; bw++; } if (countFuck > 0) { badWord += "Fuck "; bw++; } if (countJava > 0) { badWord += "Java "; bw++; } if (bw == 0) { System.out.println(msg); } else if (bw == 1) { System.out.println(badWord + "is bad word."); } else { System.out.println(badWord + "are bad word."); } } //End of main } //End of class © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 7. ANSWER 05-06 Computer Programming using Java 7 CHAPTER การดําเนินการกับแฟมข้อมูล ้ ANS-06 (File Operations) โจทย์ ข้อที่ 1 [ระดับง่ าย] in.nextInt() in.nextDouble() in.nextLine() in.next() 1 1.0 1 2 3 1 2 2.0 4 2 3 3.0 5 6 3 4 4.0 7.0 8 9D 0 4 5 5.0 5 6 6.0 6 [Error] 7.0 7.0 8.0 8 [Error] 9D 0 จํานวนรอบของ while จํานวนรอบของ while จํานวนรอบของ while จํานวนรอบของ while 7 รอบ 9 รอบ 4 รอบ 10 รอบ โจทย์ ข้อที่ 2 [ระดับง่ าย] import java.util.Scanner; import java.io.*; public class NumberOfLine { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("data.txt")); int numOfLine = 0; while(in.hasNext()) { in.nextLine(); numOfLine++; } System.out.println("Number of Lines: " + numOfLine); in.close(); } } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 8. 8 Computer Programming using Java ANSWER 05-06 โจทย์ ข้อที่ 3 [ระดับง่ าย] import java.util.Scanner; import java.io.*; public class NumberOfWord { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("data.txt")); int numOfWord = 0; while(in.hasNext()) { in.next(); numOfWord++; } System.out.println("Number of Words: " + numOfWord); in.close(); } } โจทย์ ข้อที่ 4 [ระดับง่ าย] import java.util.Scanner; import java.io.*; public class NumberOfCharacter { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("data.txt")); int numOfChar = 0; while(in.hasNext()) { numOfChar += in.nextLine().length(); } System.out.println("Number of Chars: " + numOfChar); in.close(); } } โจทย์ ข้อที่ 5 [ระดับปานกลาง] import java.util.Scanner; import java.io.*; public class CountEngStudent { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("std.txt")); int count = 0; while (in.hasNext()) { String s = in.nextLine(); if (s.substring(s.length() - 2).equals("21")) count++; } in.close(); System.out.println("Engineering Students: " + count); } //End of main } //End of class © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 9. ANSWER 05-06 Computer Programming using Java 9 โจทย์ ข้อที่ 6 [ระดับปานกลาง] import java.util.Scanner; import java.io.*; public class StudentGrade { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("score.txt")); int i = 1; ตัวอย่ างการแสดงผลบนจอภาพ while (in.hasNext()) { String id = in.next(); double score = in.nextDouble(); in.next(); //faculty if(score >= 60.0) { System.out.println(i + ".t" + id + "tS"); } else { System.out.println(i + ".t" + id + "tU"); } i++; } in.close(); } //End of main } //End of class โจทย์ ข้อที่ 7 [ระดับยาก] import java.util.Scanner; import java.io.*; public class StudentInfoFromFile { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("student.dat")); int i = 1; while (in.hasNext()) { String id = in.next(); String fname = in.next(); String lname = in.next(); double grade = in.nextDouble(); int year = 54 - Integer.parseInt(id.substring(0, 2)); String y = ""; if (year == 1) y = year + "st"; if (year == 2) y = year + "nd"; if (year == 3) y = year + "rd"; if (year >= 4) y = year + "th"; String shortName = fname.substring(0, 1).toUpperCase() + "."; String status = ""; if (grade >= 2.00) status = "Pass"; if (grade >= 1.00 && grade < 2.00) status = "Critical"; if (grade < 1.00) status = "Retired"; System.out.println(i++ + ".t" + id + "t" + y + "t" + shortName + " " + lname + "t" + grade + "t" + status); } //End of while in.close(); } //End of main } //End of class © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 10. 10 Computer Programming using Java ANSWER 05-06 โจทย์ ข้อที่ 8 [ระดับยาก] import java.util.Scanner; import java.io.*; public class CountLoveFromSongFile { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("song.txt")); int w1 = 0, w2 = 0; String s = "", txt = ""; while (in.hasNext()) { s = in.next().toLowerCase(); if (s.equals("love")) w1++; txt += s; } while (txt.indexOf("love") >= 0) { w2++; txt = txt.substring(txt.indexOf("love") + 4); } in.close(); System.out.println("Count Words #1: " + w1); System.out.println("Count Words #2: " + w2); } } โจทย์ ข้อที่ 9 [ระดับยาก] import java.util.Scanner; import java.io.*; public class FComparison { public static void main(String[] args) throws IOException { Scanner in1 = new Scanner(new File("data1.dat")); Scanner in2 = new Scanner(new File("data2.dat")); int f1 = 0, f2 = 0, n1 = 0, n2 = 0; in1.nextLine(); while (in1.hasNext()) { in1.next(); in1.next(); String grade = in1.next(); if (grade.equals("F")) f1++; n1++; } //End of while in2.nextLine(); while (in2.hasNext()) { in2.next(); in2.next(); String grade = in2.next(); if (grade.equals("F")) f2++; n2++; } //End of while in1.close(); in2.close(); System.out.println("F 2/2552: " + ((double) f1 / n1) * 100); System.out.println("F 2/2553: " + ((double) f2 / n2) * 100); if (f1 > f2) System.out.println("F (2/2552) > F (2/2553)"); if (f1 == f2) System.out.println("F (2/2552) = F (2/2553)"); if (f1 < f2) System.out.println("F (2/2552) < F (2/2553)"); } //End of main } //End of class © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
  • 11. ANSWER 05-06 Computer Programming using Java 11 โจทย์ ข้อที่ 10 [ระดับง่ าย] import java.io.*; import java.util.Scanner; public class InputStringToFile { public static void main(String[] args) throws IOException { Scanner kb = new Scanner(System.in); PrintStream out = new PrintStream(new File("sentence.txt")); int i = 1; while (true) { System.out.print("Sentence: "); String s = kb.nextLine().toUpperCase(); if (s.trim().equalsIgnoreCase("stop")) break; out.println(i + ": " + s); i++; } System.out.println("File is saved"); out.close(); } } โจทย์ ข้อที่ 11 [ระดับยาก] import java.util.Scanner; import java.io.*; public class ReverseTextFile { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("text.txt")); PrintStream out = new PrintStream(new File("revtext.txt")); while (in.hasNext()) { String s = in.nextLine(); String rev = ""; for (int i = s.length() - 1; i >= 0; i--) { rev += s.substring(i, i + 1); } out.println(rev); } in.close(); out.close(); } } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้