SlideShare a Scribd company logo
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) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
                                            ้

More Related Content

PDF
Java-Answer Chapter 01-04
PDF
Java-Chapter 02 Data Operations and Processing
PDF
Java-Chapter 05 String Operations
PDF
ภาษาอังกฤษในชีวิตประจำวัน
PDF
Aerius drug monograph
PDF
จิตปัญญาศึกษา จำเรียง
PDF
แก้โจทย์ปัญหาอสมการ
PDF
หน้าปกโครงงาน
Java-Answer Chapter 01-04
Java-Chapter 02 Data Operations and Processing
Java-Chapter 05 String Operations
ภาษาอังกฤษในชีวิตประจำวัน
Aerius drug monograph
จิตปัญญาศึกษา จำเรียง
แก้โจทย์ปัญหาอสมการ
หน้าปกโครงงาน

What's hot (20)

PPT
ปรัชญาเศรษฐกิจพอเพียง
PPTX
การศึกษานอกสถานที่ (Educational fied trips)
PPTX
การพยาบาลผู้ป่วยผ่าตัดมะเร็งเต้านม
PDF
ทฤษฏีพัฒนาจริยธรรมของ จอง เพียเจย์
PPTX
รูปแบบการสอนแบบทางตรง
PDF
คู่มือการสื่อสารกับคนไข้ต่างชาติ
PDF
Unit 1 about myself
DOCX
เกรดมัธยมปลาย
PDF
0755 l2 2
PDF
ปก คำนำ โครงงานพอเพียงเลี้ยงชีพ
PPTX
สื่อการสอน บทที่ 2 การดูแลรักษาเสื้อผ้า
PPTX
ลำดับขั้นกระบวนการเรียนรู้
PDF
แบบเสนอโครงร่างโครงงาน
PDF
บทเรียนสำเร็จรูป เรื่อง Le futur proche
PDF
อาชีพในฝัน
PDF
Object-Oriented Programming
PDF
รายงานโครงการคอมพิวเตอร์ กลุ่ม Blog 5 สถานที่ท่องเที่ยวยอดนิยมในแต่ละประเทศสม...
PDF
ฟังก์ชันขั้นบันได
ปรัชญาเศรษฐกิจพอเพียง
การศึกษานอกสถานที่ (Educational fied trips)
การพยาบาลผู้ป่วยผ่าตัดมะเร็งเต้านม
ทฤษฏีพัฒนาจริยธรรมของ จอง เพียเจย์
รูปแบบการสอนแบบทางตรง
คู่มือการสื่อสารกับคนไข้ต่างชาติ
Unit 1 about myself
เกรดมัธยมปลาย
0755 l2 2
ปก คำนำ โครงงานพอเพียงเลี้ยงชีพ
สื่อการสอน บทที่ 2 การดูแลรักษาเสื้อผ้า
ลำดับขั้นกระบวนการเรียนรู้
แบบเสนอโครงร่างโครงงาน
บทเรียนสำเร็จรูป เรื่อง Le futur proche
อาชีพในฝัน
Object-Oriented Programming
รายงานโครงการคอมพิวเตอร์ กลุ่ม Blog 5 สถานที่ท่องเที่ยวยอดนิยมในแต่ละประเทศสม...
ฟังก์ชันขั้นบันได
Ad

Viewers also liked (10)

PDF
Graph theory discrete mathmatics
PPT
PDF
Discrete-Chapter 11 Graphs Part II
PPTX
Trees and graphs
PDF
Discrete-Chapter 11 Graphs Part III
PDF
Discrete-Chapter 11 Graphs Part I
PDF
Trees (slides)
PDF
Discrete-Chapter 10 Trees
PPT
17. Trees and Graphs
PPT
Characteristics of gifted students
Graph theory discrete mathmatics
Discrete-Chapter 11 Graphs Part II
Trees and graphs
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part I
Trees (slides)
Discrete-Chapter 10 Trees
17. Trees and Graphs
Characteristics of gifted students
Ad

Similar to Java-Answer Chapter 05-06 (20)

PDF
Java-Answer Chapter 05-06 (For Print)
PDF
Java-Answer Chapter 08-09
PDF
Java-Answer Chapter 12-13
PDF
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
PDF
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
PPTX
Computer Programming 3
PDF
Java-Answer Chapter 10-11
PDF
PDF
Java Programming: อะเรย์และคอลเล็กชั่น
PDF
Java-Answer Chapter 01-04 (For Print)
PDF
Java-Chapter 08 Methods
PPT
08 arrays
PDF
Java-Answer Chapter 07 (For Print)
PDF
Java Programming: โครงสร้างควบคุม
PPTX
Computer Programming 2.1
PPT
Computer Programming 2.2
PDF
Java-Answer Chapter 07
PPT
Program Statement Structure พื้นฐานโครงสร้างประโยคคำสั่งภาษาโปรแกรมคอมพิวเตอร์
PDF
บทที่ 5 คลาส
PPT
Java Programming [12/12] : Thread
Java-Answer Chapter 05-06 (For Print)
Java-Answer Chapter 08-09
Java-Answer Chapter 12-13
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
Computer Programming 3
Java-Answer Chapter 10-11
Java Programming: อะเรย์และคอลเล็กชั่น
Java-Answer Chapter 01-04 (For Print)
Java-Chapter 08 Methods
08 arrays
Java-Answer Chapter 07 (For Print)
Java Programming: โครงสร้างควบคุม
Computer Programming 2.1
Computer Programming 2.2
Java-Answer Chapter 07
Program Statement Structure พื้นฐานโครงสร้างประโยคคำสั่งภาษาโปรแกรมคอมพิวเตอร์
บทที่ 5 คลาส
Java Programming [12/12] : Thread

More from Wongyos Keardsri (20)

PDF
How to Study and Research in Computer-related Master Program
PPT
The next generation intelligent transport systems: standards and applications
PPT
IP address anonymization
PDF
SysProg-Tutor 03 Unix Shell Script Programming
PDF
SysProg-Tutor 02 Introduction to Unix Operating System
PDF
SysProg-Tutor 01 Introduction to C Programming Language
PDF
Discrete-Chapter 09 Algorithms
PDF
Discrete-Chapter 08 Relations
PDF
Discrete-Chapter 07 Probability
PDF
Discrete-Chapter 06 Counting
PDF
Discrete-Chapter 05 Inference and Proofs
PDF
Discrete-Chapter 04 Logic Part II
PDF
Discrete-Chapter 04 Logic Part I
PDF
Discrete-Chapter 03 Matrices
PDF
Discrete-Chapter 02 Functions and Sequences
PDF
Discrete-Chapter 01 Sets
PDF
Discrete-Chapter 12 Modeling Computation
PDF
Java-Chapter 14 Creating Graphics with DWindow
PDF
Java-Chapter 13 Advanced Classes and Objects
PDF
Java-Chapter 11 Recursions
How to Study and Research in Computer-related Master Program
The next generation intelligent transport systems: standards and applications
IP address anonymization
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 01 Introduction to C Programming Language
Discrete-Chapter 09 Algorithms
Discrete-Chapter 08 Relations
Discrete-Chapter 07 Probability
Discrete-Chapter 06 Counting
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 03 Matrices
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 01 Sets
Discrete-Chapter 12 Modeling Computation
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 13 Advanced Classes and Objects
Java-Chapter 11 Recursions

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) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้