Skip to content · ⁨ข้ามไปยังเนื้อหา⁩
Subjects · ⁨รายวิชา⁩

AP Computer Science A

Tips · ⁨เคล็ดลับ⁩

AP Computer Science A เป็นวิชา Java: ออบเจ็กต์และคลาส, ประเภทพื้นฐานและการควบคุมการไหล, การเขียนคลาส, อาร์เรย์และ ArrayLists, 2D arrays, การสืบทอดและพอลิมอร์ฟิซึม และ รีเคอร์ชัน เป็นหลักสูตรการเขียนโปรแกรมแรกที่ใช้โค้ดเชิงออบเจ็กต์จริง ไม่ใช่伪代码

คำถาม FRQ ทั้งสี่ข้อต้องเขียน Java บนกระดาษ คอมไพเลอร์จะไม่ช่วยจับผิดจุดกึ่งวรรคหรือประเภทการคืนค่าที่ผิด คุณจึงต้อง เขียนโค้ดบนกระดาษ ระหว่างทบทวน – ซึ่งเป็นทักษะต่างจากการพิมพ์

อาร์เรย์และ ArrayLists เป็นหัวข้อที่ถูกทดสอบมากที่สุด ความคล่องแคล่วในการ traversal, insertion และ removal และการรู้ว่าจะเกิด index shift เมื่อลบองค์ประกอบออก ให้ผลตอบแทนมากกว่าชั่วโมงฝึกฝนใดๆ อีก

บันทึกครอบคลุมหน่วย CED ใน Java พร้อมตัวอย่าง runnable ที่แก้ไขได้ในเบราว์เซอร์ ห้องสมุดมี FRQs เกณฑ์การให้คะแนน – เนื่องจากเป็นคำถามเขียนโค้ดทั้งหมด แบบวิธีทำจึงเป็น methods ครบถ้วน ไม่ใช่ fragments

  • 1

    Using Objects and Methods · ⁨การใช้วัตถุและวิธีการ⁩

    Watch lesson · ⁨ดูบทเรียน⁩
    1.1

    Introduction to Algorithms, Programming, and Compilers

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 1.1.A: Represent patterns and algorithms found in everyday life using written language or diagrams.

    • 1.1.A.1 Algorithms define step-by-step processes to follow when completing a task or solving a problem. These algorithms can be represented using written language or diagrams.
    • 1.1.A.2 Sequencing defines an order for when steps in a process are completed. Steps in a process are completed one at a time.

    Learning Objective 1.1.B: Explain the code compilation and execution process.

    • 1.1.B.1 Code can be written in any text editor; however, an integrated development environment (IDE) is often used to write programs because it provides tools for a programmer to write, compile, and run code.
    • 1.1.B.2 A compiler checks code for some errors. Errors detectable by the compiler need to be fixed before the program can be run.

    Learning Objective 1.1.C: Identify types of programming errors.

    • 1.1.C.1 A syntax error is a mistake in the program where the rules of the programming language are not followed. These errors are detected by the compiler.
    • 1.1.C.2 A logic error is a mistake in the algorithm or program that causes it to behave incorrectly or unexpectedly. These errors are detected by testing the program with specific data to see if it produces the expected outcome.
    • 1.1.C.3 A run-time error is a mistake in the program that occurs during the execution of a program. Run-time errors typically cause the program to terminate abnormally.
    • 1.1.C.4 An exception is a type of run-time error that occurs as a result of an unexpected error that was not detected by the compiler. It interrupts the normal flow of the program's execution.
    ไทย

    จุดประสงค์การเรียนรู้ 1.1.A: แสดงรูปแบบและอัลกอริทึมที่พบในชีวิตประจำวันโดยใช้ภาษาเขียนหรือแผนภาพ

    • 1.1.A.1 อัลกอริทึม กำหนดขั้นตอนทีละขั้นตอนที่ต้องปฏิบัติตามเมื่อทำงานหรือแก้ปัญหา อัลกอริทึมเหล่านี้สามารถแสดงได้ด้วยภาษาเขียนหรือแผนภาพ
    • 1.1.A.2 การเรียงลำดับ กำหนดลำดับสำหรับการดำเนินการขั้นตอนต่าง ๆ ในกระบวนการ ขั้นตอนในกระบวนการจะดำเนินการทีละขั้นตอน

    จุดประสงค์การเรียนรู้ 1.1.B: อธิบายกระบวนการคอมไพล์และรันโค้ด

    • 1.1.B.1 โค้ดสามารถเขียนได้ในโปรแกรมแก้ไขข้อความใดก็ได้ แต่มักใช้ สภาพแวดล้อมการพัฒนาแบบบูรณาการ (IDE) ในการเขียนโปรแกรม เพราะมันมอบเครื่องมือให้ผู้พัฒนาโค้ดสามารถเขียน คอมไพล์ และรันโค้ดได้
    • 1.1.B.2 คอมไพเลอร์ ตรวจสอบโค้ดหาข้อผิดพลาดบางประเภท ข้อผิดพลาดที่คอมไพเลอร์ตรวจจับได้ต้องได้รับการแก้ไขก่อนที่โปรแกรมจะถูกรันได้

    จุดประสงค์การเรียนรู้ 1.1.C: ระบุประเภทของข้อผิดพลาดในการเขียนโปรแกรม

    • 1.1.C.1 ข้อผิดพลาดทางไวยากรณ์ (syntax error) คือความผิดพลาดในโปรแกรมที่ไม่ปฏิบัติตามกฎของภาษาการเขียนโปรแกรม ข้อผิดพลาดเหล่านี้ถูกตรวจจับโดยคอมไพเลอร์
    • 1.1.C.2 ข้อผิดพลาดทางตรรกะ (logic error) คือความผิดพลาดในอัลกอริทึมหรือโปรแกรมที่ทำให้มันทำงานผิดหรือผิดปกติไปจากที่คาด ข้อผิดพลาดเหล่านี้ถูกตรวจจับด้วยการทดสอบโปรแกรมด้วยข้อมูลเฉพาะเพื่อดูว่ามันให้ผลลัพธ์ตามที่คาดหวังหรือไม่
    • 1.1.C.3 ข้อผิดพลาดขณะรัน (run-time error) คือความผิดพลาดในโปรแกรมที่เกิดขึ้นระหว่างการรันโปรแกรม ข้อผิดพลาดขณะรันมักทำให้โปรแกรมหยุดทำงานอย่างไม่ปกติ
    • 1.1.C.4 ข้อยกเว้น (exception) เป็นประเภทหนึ่งของข้อผิดพลาดขณะรันที่เกิดขึ้นเป็นผลจากความผิดพลาดที่ไม่คาดคิดซึ่งไม่ถูกตรวจจับโดยคอมไพเลอร์ มันขัดขวางการไหลตามปกติของการรันโปรแกรม

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    Source code on a workstation — programs are written, compiled, and run as precise instructions
    Source code on a workstation — programs are written, compiled, and run as precise instructions

    An algorithm 算法 is a finite, step-by-step procedure that solves a problem. A program 程序 expresses an algorithm in a language a computer can run. Java is compiled 编译: the compiler 编译器 translates your source code into bytecode, which the Java Virtual Machine (JVM) runs. A syntax error 语法错误 (breaking the grammar) is caught by the compiler; a logic error 逻辑错误 (wrong result) is not – the program runs but misbehaves.

    A compiler translates the whole program at once; an interpreter runs it line by line
    A compiler translates the whole program at once; an interpreter runs it line by line
    Several computer processor chips seen from below
    Your Java program is compiled to instructions a CPU like one of these actually runs
    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    algorithm/ˈælɡərɪθəm/ อัลกอริทึม
    program/ˈprəʊɡræm/ โปรแกรม
    compiled/kəmˈpaɪld/ รวบรวมแล้ว
    compiler/kəmˈpaɪlə/ compiler
    syntax error/ˈsɪntæks ˈerə/ ข้อผิดพลาดด้านไวยากรณ์
    logic error/ˈlɒdʒɪk ˈerə/ ข้อผิดพลาดด้านตรรกะ
    1.2

    Variables and Data Types

    Syllabus · ⁨หลักสูตร⁩
    English
    Learning ObjectiveEssential Knowledge

    1.2.A
    Identify the most appropriate data type category for a particular specification.

    • 1.2.A.1 A data type is a set of values and a corresponding set of operations on those values. Data types can be categorized as either primitive or reference.
    • 1.2.A.2 The primitive data types used in this course define the set of values and corresponding operations on those values for numbers and Boolean values.
    • 1.2.A.3 A reference type is used to define objects that are not primitive types.

    1.2.B
    Develop code to declare variables to store numbers and Boolean values.

    • 1.2.B.1 The three primitive data types used in this course are int, double, and boolean. An int value is an integer. A double value is a real number. A boolean value is either true or false.
      • Exclusion statement: The other five primitive data types (long, short, byte, float, and char) are outside the scope of the AP Computer Science A course and exam.
    • 1.2.B.2 A variable is a storage location that holds a value, which can change while the program is running. Every variable has a name and an associated data type. A variable of a primitive type holds a primitive value from that type.
    ไทย
    Learning ObjectiveEssential Knowledge

    1.2.A
    ระบุหมวดหมู่ประเภทข้อมูลที่เหมาะสมที่สุดสำหรับการกำหนดค่า某一个特定

    • 1.2.A.1 Type ข้อมูล คือชุดของค่าและชุดของการดำเนินการที่สอดคล้องกับค่านั้น ประเภทข้อมูลสามารถแบ่งออกเป็นแบบ primitive หรือ reference
    • 1.2.A.2 Types ข้อมูล primitive ที่ใช้ในหลักสูตรนี้กำหนดชุดของค่าและการดำเนินการที่สอดคล้องกับค่านั้นสำหรับตัวเลขและค่า Boolean
    • 1.2.A.3 Reference type ใช้สำหรับกำหนด objects ที่ไม่ใช่ primitive types

    1.2.B
    เขียนโค้ดเพื่อประกาศตัวแปรเพื่อจัดเก็บตัวเลขและค่า Boolean

    • 1.2.B.1 Type ข้อมูล primitive สามชนิดที่ใช้ในหลักสูตรนี้คือ int, double, และ boolean. ค่า int เป็นจำนวนเต็ม ค่า double เป็นจำนวนจริง ค่า boolean เป็นeither true หรือ false
      • Exclusion statement: Type ข้อมูล primitive อีกห้าชนิด (long, short, byte, float, และ char) อยู่ Ngoàiขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A
    • 1.2.B.2 Variable คือสถานที่จัดเก็บที่ถือค่าซึ่งสามารถเปลี่ยนแปลงได้ขณะโปรแกรมทำงาน ทุก variable มีชื่อและ type ข้อมูลที่เกี่ยวข้อง Variable ของ primitive type จะถือค่า primitive จาก type นั้น

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    A variable 变量 is a named box that stores a value of a fixed type 类型. Java's main primitive types 基本类型 are int (whole numbers), double (decimals), and boolean (true/false). Declare with the type first:

    Java's basic data types, each storing a different kind of value
    Java's basic data types, each storing a different kind of value
    int score = 90;
    double price = 4.99;
    boolean passed = true;
    
    Explore · ⁨สำรวจ⁩

    Explore how a variable holds one value at a time · ⁨สำรวจว่าตัวแปรเก็บค่าเพียงค่าเดียวในแต่ละเวลาได้อย่างไร⁩

    A variable is a named box that stores one value of a fixed type. Step through the lines and watch each box take its value; notice that reassigning score overwrites the old number rather than making a new box. · ⁨ตัวแปรคือกล่องที่มีชื่อซึ่งเก็บค่าเพียงค่าเดียวของ ประเภท ที่กำหนดไว้ ก้าวผ่านบรรทัดแต่ละบรรทัดและสังเกตว่าแต่ละกล่องรับค่าของตน; ระวังว่าการกำหนดค่าใหม่ score จะ ทับ ค่าเก่าแทนที่จะสร้างกล่องใหม่⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    variable/ˈveərɪəbl/ ตัวแปร
    type/taɪp/ ตัวอักษร
    primitive types/ˈprɪmɪtɪv taɪps/ ประเภทพื้นฐาน
    1.3

    Expressions and Output

    Syllabus · ⁨หลักสูตร⁩
    English
    Learning ObjectiveEssential Knowledge

    1.3.A
    Develop code to generate output and determine the result that would be displayed.

    • 1.3.A.1 System.out.print and System.out.println display information on the computer display. System.out.println moves the cursor to a new line after the information has been displayed, while System.out.print does not.

    1.3.B
    Develop code to utilize string literals and determine the result of using string literals.

    • 1.3.B.1 A literal is the code representation of a fixed value.
    • 1.3.B.2 A string literal is a sequence of characters enclosed in double quotes.
    • 1.3.B.3 Escape sequences are special sequences of characters that can be included in a string. They start with a \ and have a special meaning in Java. Escape sequences used in this course include double quote \", backslash \\, and newline \n.

    1.3.C
    Develop code for arithmetic expressions and determine the result of these expressions.

    • 1.3.C.1 Arithmetic expressions, which consist of numeric values, variables, and operators, include expressions of type int and double.
    • 1.3.C.2 The arithmetic operators consist of addition +, subtraction -, multiplication *, division /, and remainder %. An arithmetic operation that uses two int values will evaluate to an int value. An arithmetic operation that uses at least one double value will evaluate to a double value.
      • Exclusion statement: Expressions that result in special double values (e.g., infinities and NaN) are outside the scope of the AP Computer Science A course and exam.
    • 1.3.C.3 When dividing numeric values that are both int values, the result is only the integer portion of the quotient. When dividing numeric values that use at least one double value, the result is the quotient.
    • 1.3.C.4 The remainder operator % is used to compute the remainder when one number a is divided by another number b.
      • Exclusion statement: The use of values less than 0 for a and the use of values less than or equal to 0 for b is outside the scope of the AP Computer Science A course and exam.
    • 1.3.C.5 Operators can be used to construct compound expressions. At compile time, numeric values are associated with operators according to operator precedence to determine how they are grouped. Parentheses can be used to modify operator precedence. Multiplication, division, and remainder have precedence over addition and subtraction. Operators with the same precedence are evaluated from left to right.
    • 1.3.C.6 An attempt to divide an integer by the integer zero will result in an ArithmeticException.
      • Exclusion statement: The use of dividing by zero when one numeric value is a double is outside the scope of the AP Computer Science A course and exam.
    ไทย
    Learning ObjectiveEssential Knowledge

    1.3.A
    เขียนโค้ดเพื่อสร้าง output และระบุผลลัพธ์ที่จะแสดงบนหน้าจอ

    • 1.3.A.1 System.out.print และ System.out.println แสดงข้อมูลบนหน้าจอคอมพิวเตอร์ System.out.println ย้ายเคอร์เซอร์ไปบรรทัดใหม่หลังจากแสดงข้อมูลแล้ว ในขณะที่ System.out.print ไม่ทำ如此

    1.3.B
    เขียนโค้ดเพื่อใช้ string literals และระบุผลลัพธ์ของการใช้ string literals

    • 1.3.B.1 Literal คือการแทนค่าโค้ดของค่าคงที่
    • 1.3.B.2 String literal คือลำดับของตัวอักษรที่ห่อหุ้มด้วยเครื่องหมายคำพูดคู่
    • 1.3.B.3 Escape sequences คือลำดับพิเศษของตัวอักษรที่สามารถรวมอยู่ใน string ได้它们เริ่มด้วย \ และมีความหมายพิเศษใน Java Escape sequences ที่ใช้ในหลักสูตรนี้รวมถึงเครื่องหมายคำพูดคู่ \", backslash \\, และ newline \n

    1.3.C
    เขียนโค้ดสำหรับ arithmetic expressions และระบุผลลัพธ์ของ expressions เหล่านี้

    • 1.3.C.1 Arithmetic expressions ซึ่งประกอบด้วยค่าตัวเลข ตัวแปร และ operators รวมถึง expressions ของ type int และ double
    • 1.3.C.2 Arithmetic operators ประกอบด้วยบวก +, ลบ -, คูณ *, หาร /, และเศษเหลือ %. การดำเนินการทางคณิตศาสตร์ที่ใช้ค่า int สองค่าจะประเมิน结果为 int. การดำเนินการทางคณิตศาสตร์ที่ใช้ค่า double อย่างน้อยหนึ่งค่าจะประเมิน结果为 double
      • Exclusion statement: Expressions ที่ให้ผลลัพธ์เป็นค่า double พิเศษ (เช่น无穷大和 NaN) อยู่Outside ขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A
    • 1.3.C.3 เมื่อหารด้วยค่าตัวเลขที่เป็น int ทั้งคู่ ผลลัพธ์จะเป็นเพียงส่วนจำนวนเต็มของผลหารเท่านั้น เมื่อหารด้วยค่าตัวเลขที่มีอย่างน้อยหนึ่ง double ผลลัพธ์จะเป็นผลหาร
    • 1.3.C.4 ตัวดำเนินการเศษเหลือ % ใช้เพื่อคำนวณเศษเมื่อเลข a หนึ่งตัวถูกหารด้วยเลข b อีกตัว
      • ข้อความยกเว้น: การใช้ค่าน้อยกว่า 0 สำหรับ a และการใช้ค่าน้อยกว่าหรือเท่ากับ 0 สำหรับ b ไม่อยู่ในขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A
    • 1.3.C.5 ตัวดำเนินการสามารถนำไปสร้างนิพจน์ที่ซับซ้อนได้ ในช่วงการคอมไพล์ ค่าตัวเลขจะถูกจับคู่กับตัวดำเนินการตามลำดับความสำคัญเพื่อกำหนดวิธีการจัดกลุ่ม สามารถใช้วงเล็บเพื่อแก้ไขลำดับความสำคัญของตัวดำเนินการ การคูณ การหาร และเศษเหลือมีลำดับความสำคัญมากกว่าการบวกและการลบ ตัวดำเนินการที่มีลำดับความสำคัญเท่ากันจะประเมินจากซ้ายไปขวา
    • 1.3.C.6 ความพยายามในการหารจำนวนเต็มด้วยศูนย์ (integer zero) จะส่งผลให้เกิด ArithmeticException
      • ข้อความยกเว้น: การใช้การหารด้วยศูนย์เมื่อค่าตัวเลขหนึ่งเป็น double ไม่อยู่ในขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    An expression 表达式 combines values and operators to compute a result: + - * / and % (modulus 取模, the remainder). Integer division truncates: 7 / 2 is 3, while 7 % 2 is 1. Operator precedence follows math (*,/,% before +,-). Print with:

    System.out.print("no newline");
    System.out.println("with newline");
    

    Dividing an integer by the integer 0 (like 7 / 0) is not allowed and crashes at run time with an ArithmeticException. Inside a string, a backslash marks an escape sequence 转义序列: \" prints a double quote, \\ a single backslash, and \n starts a new line – so System.out.println("She said \"hi\""); prints She said "hi".

    Explore · ⁨สำรวจ⁩

    Explore the order of operations step by step · ⁨สำรวจลำดับขั้นตอนการคำนวณทีละขั้น⁩

    Java applies *, /, % before + and -, working left to right. Watch each step and see why 2 + 3 * 4 is $14$, not $20$ — the multiplication happens first. · ⁨Java นำ *, /, % ไปใช้ ก่อน + และ - โดยทำงานจากซ้ายไปขวา สังเกตทุกขั้นตอนเพื่อดูว่าทำไม 2 + 3 * 4 จึงได้ค่าเป็น $14$ ไม่ใช่ $20$ — เพราะการคูณเกิดขึ้นก่อน⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    expression/ekˈspreʃn/ การแสดงออก
    modulus/ˈmɒdjʊləs/ โมดูลัส
    escape sequence/eˈskeɪp ˈsiːkwəns/ ลำดับการหลบหลีก (escape sequence)
    1.4

    Assignment Statements and Input

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 1.4.A: Develop code for assignment statements with expressions and determine the value that is stored in the variable as a result of these statements.

    • 1.4.A.1 Every variable must be assigned a value before it can be used in an expression. That value must be from a compatible data type. A variable is initialized the first time it is assigned a value. Reference types can be assigned a new object or null if there is no object. The literal null is a special value used to indicate that a reference is not associated with any object.
    • 1.4.A.2 The assignment operator = allows a program to initialize or change the value stored in a variable. The value of the expression on the right is stored in the variable on the left.
      • Exclusion statement: The use of assignment operators inside expressions (e.g., a = b = 4; or a[i += 5]) is outside the scope of the AP Computer Science A course and exam.
    • 1.4.A.3 During execution, an expression is evaluated to produce a single value. The value of an expression has a type based on the evaluation of the expression.

    Learning Objective 1.4.B: Develop code to read input.

    • 1.4.B.1 Input can come in a variety of forms, such as tactile, audio, visual, or text. The Scanner class is one way to obtain text input from the keyboard.
      • Exclusion statement: Any specific form of input from the user is outside the scope of the AP Computer Science A course and exam.
    ไทย

    วัตถุประสงค์การเรียนรู้ 1.4.A: เขียนโค้ดสำหรับคำสั่งการกำหนดค่าพร้อมนิพจน์ และระบุค่าที่จะเก็บไว้ในตัวแปรซึ่งเป็นผลลัพธ์ของคำสั่งเหล่านี้

    • 1.4.A.1 ตัวแปรทุกตัวต้องได้รับค่าก่อนนำไปใช้ใน expression ค่าที่ได้รับต้องมาจาก data type ที่เข้ากันได้ ตัวแปรจะถูก initialize ครั้งแรกเมื่อได้รับค่า Reference types สามารถรับ object ใหม่หรือ null ได้หากไม่มี object_value_literal null เป็นค่าพิเศษที่ใช้บ่งชี้ว่า reference ไม่ได้เชื่อมโยงกับ object ใด
    • 1.4.A.2 ตัวดำเนินการกำหนดค่า = ช่วยให้โปรแกรมสามารถเริ่มต้นหรือเปลี่ยนแปลงค่าที่เก็บไว้ในตัวแปรได้ ค่าของนิพจน์ทางขวาจะถูกเก็บไว้ในตัวแปรทางซ้าย
      • ข้อความยกเว้น: การใช้ตัวดำเนินการกำหนดค่าภายในนิพจน์ (เช่น a = b = 4; หรือ a[i += 5]) ไม่อยู่ในขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A
    • 1.4.A.3 ระหว่างการประมวลผล นิพจน์จะถูกประเมินเพื่อสร้างค่าเดียว ค่าของนิพจน์จะมีชนิดขึ้นอยู่กับผลการประเมินนิพจน์นั้น

    วัตถุประสงค์การเรียนรู้ 1.4.B: เขียนโค้ดเพื่อรับข้อมูลเข้า

    • 1.4.B.1 ข้อมูลเข้าสามารถมาจากหลายรูปแบบ เช่น แบบสัมผัส เสียง ภาพ หรือข้อความ Class Scanner เป็นหนึ่งในวิธีรับข้อมูลข้อความจากคีย์บอร์ด
      • ข้อความยกเว้น: รูปแบบเฉพาะใด ๆ ของข้อมูลเข้าจากผู้ใช้งานไม่อยู่ในขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    An assignment 赋值 x = expr; evaluates the right side and stores it in the left variable. Read input with a Scanner:

    Scanner in = new Scanner(System.in);
    int age = in.nextInt();
    String name = in.next();
    
    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    assignment/əˈsaɪnmənt/ การกำหนดค่า (assignment)
    1.5

    Casting and Range of Variables

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 1.5.A: Develop code to cast primitive values to different primitive types in arithmetic expressions and determine the value that is produced as a result.

    • 1.5.A.1 The casting operators (int) and (double) can be used to convert from a double value to an int value (or vice versa).
    • 1.5.A.2 Casting a double value to an int value causes the digits to the right of the decimal point to be truncated.
    • 1.5.A.3 Some code causes int values to be automatically cast (widened) to double values.
    • 1.5.A.4 Values of type double can be rounded to the nearest integer by (int)(x + 0.5) for non-negative numbers or (int)(x - 0.5) for negative numbers.

    Learning Objective 1.5.B: Describe conditions when an integer expression evaluates to a value out of range.

    • 1.5.B.1 The constant Integer.MAX_VALUE holds the value of the largest possible int value. The constant Integer.MIN_VALUE holds the value of the smallest possible int value.
    • 1.5.B.2 Integer values in Java are represented by values of type int, which are stored using a finite amount (4 bytes) of memory. Therefore, an int value must be in the range from Integer.MIN_VALUE to Integer.MAX_VALUE inclusive.
    • 1.5.B.3 If an expression would evaluate to an int value outside of the allowed range, an integer overflow occurs. The result is an int value in the allowed range but not necessarily the value expected.

    Learning Objective 1.5.C: Describe conditions that limit accuracy of expressions.

    • 1.5.C.1 Computers allot a specified amount of memory to store data based on the data type. If an expression would evaluate to a double that is more precise than can be stored in the allotted amount of memory, a round-off error occurs. The result will be rounded to the representable value. To avoid rounding errors that naturally occur, use int values.
      • Exclusion statement: Other special decimal data types that can be used to avoid rounding errors are outside the scope of the AP Computer Science A course and exam.
    ไทย

    วัตถุประสงค์การเรียนรู้ 1.5.A: เขียนโค้ดเพื่อแปลงค่า primitive เป็นชนิด primitive อื่น ๆ ในนิพจน์ทางคณิตศาสตร์ และระบุค่าที่จะเกิดขึ้นเป็นผลลัพธ์

    • 1.5.A.1 ตัวดำเนินการแปลงชนิด (int) และ (double) สามารถใช้ในการแปลงจากค่า double เป็นค่า int (หรือในทางกลับกัน)
    • 1.5.A.2 การแปลงค่า double เป็นค่า int จะทำให้ตัวเลขทางขวาของจุดทศนิยมถูกตัดออก
    • 1.5.A.3 โค้ดบางอย่างทำให้ค่า int ถูกแปลงอัตโนมัติ (widened) เป็นค่า double
    • 1.5.A.4 ค่าชนิด double สามารถปัดเศษให้เป็นจำนวนเต็มใกล้เคียงที่สุดโดย (int)(x + 0.5) สำหรับจำนวนที่ไม่ติดลบ หรือ (int)(x - 0.5) สำหรับจำนวนติดลบ

    วัตถุประสงค์การเรียนรู้ 1.5.B: อธิบายเงื่อนไขที่นิพจน์จำนวนเต็มประเมินออกมาเป็นค่าที่อยู่นอกระยะ

    • 1.5.B.1 ค่าคงที่ Integer.MAX_VALUE เก็บค่าสูงสุดที่เป็นไปได้ของค่า int ค่าคงที่ Integer.MIN_VALUE เก็บค่าต่ำสุดที่เป็นไปได้ของค่า int
    • 1.5.B.2 ค่าจำนวนเต็มใน Java แทนด้วยค่าชนิด int ซึ่งเก็บโดยใช้หน่วยความจำจำกัด (4 ไบต์) ดังนั้น ค่า int ต้องอยู่ในช่วงตั้งแต่ Integer.MIN_VALUE ถึง Integer.MAX_VALUE รวมทั้งสองข้าง
    • 1.5.B.3 ถ้านิพจน์ประเมินออกมาเป็นค่า int ที่อยู่นอกระยะที่ยอมรับได้ จะเกิดการล้นของจำนวนเต็ม (integer overflow) ผลลัพธ์จะเป็นค่า int ในช่วงที่ยอมรับได้แต่อาจไม่ใช่ค่าที่ต้องการ

    วัตถุประสงค์การเรียนรู้ 1.5.C: อธิบายเงื่อนไขที่จำกัดความแม่นยำของนิพจน์

    • 1.5.C.1 คอมพิวเตอร์จัดสรรหน่วยความจำที่กำหนดไว้เพื่อเก็บข้อมูลตามชนิดข้อมูล ถ้านิพจน์ประเมินออกมาเป็น double ที่มีความละเอียดมากกว่าที่จัดสรรไว้จะเกิดข้อผิดพลาดจากการปัดเศษ (round-off error) ผลลัพธ์จะถูกปัดเศษให้เป็นค่าที่แทนได้ เพื่อหลีกเลี่ยงข้อผิดพลาดจากการปัดเศษที่เกิดขึ้นเอง ให้ใช้ค่า int
      • ข้อความยกเว้น: ชนิดข้อมูลทศนิยมพิเศษอื่น ๆ ที่สามารถใช้เพื่อหลีกเลี่ยงข้อผิดพลาดจากการปัดเศษไม่อยู่ในขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    int range, overflow and truncation

    Each type has a fixed range; an int overflows past about 2.1 billion. Casting 类型转换 converts between types. Widening (int to double) is automatic; narrowing needs an explicit cast, which truncates (does not round):

    double avg = (double) total / count;   // force real division
    int whole = (int) 3.9;                 // 3, truncated
    

    Exam skill: watch for integer division producing a truncated result when a decimal was expected – cast one operand to double first.

    Worked example. Trace each expression:

    • 7 / 2 → 3 (both int, so division truncates);
    • 7.0 / 2 → 3.5 (one double forces real division);
    • 7 % 2 → 1 (the remainder);
    • (double) 7 / 2 → 3.5 (the cast binds tighter than /, so it is 7.0 / 2);
    • (double) (7 / 2) → 3.0 (the parentheses compute 7 / 2 = 3 in int first, then widen).

    The last two look alike but differ – the position of the cast decides whether the truncation happens.

    Explore · ⁨สำรวจ⁩

    Why int and double store numbers differently · ⁨เหตุผลที่ int และ double เก็บตัวเลขต่างกัน⁩

    An int holds only whole numbers in a fixed range; a double stores a mantissa and an exponent, trading exactness for a huge range. Casting double→int throws away the fraction, and a value past an int's range overflows. · ⁨int เก็บได้เฉพาะจำนวนเต็มในช่วงที่กำหนดเท่านั้น; ส่วน double เก็บ mantissa และ exponent ซึ่งแลกเปลี่ยนความแม่นยำกับช่วงค่าที่ใหญ่โต การแปลงชนิด double→int จะทิ้งส่วนทศนิยม และหากค่าเกินช่วงของ int จะเกิดภาวะล้น⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    Casting/ˈkæstɪŋ/ การแปลงชนิดข้อมูล (Casting)
    1.6

    Compound Assignment Operators

    Syllabus · ⁨หลักสูตร⁩
    Learning ObjectiveEssential Knowledge

    1.6.A
    Develop code for assignment statements with compound assignment operators and determine the value that is stored in the variable as a result.

    • 1.6.A.1 Compound assignment operators +=, -=, *=, /=, and %= can be used in place of the assignment operator in numeric expressions. A compound assignment operator performs the indicated arithmetic operation between the value on the left and the value on the right and then assigns the result to the variable on the left.
    • 1.6.A.2 The post-increment operator ++ and post-decrement operator -- are used to add 1 or subtract 1 from the stored value of a numeric variable. The new value is assigned to the variable.
      • Exclusion statement: The use of increment and decrement operators in prefix form (e.g., ++x) is outside the scope of the AP Computer Science A course and exam. The use of increment and decrement operators inside other expressions (e.g., arr[x++]) is outside the scope of the AP Computer Science A course and exam.

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    Shorthands combine an operation with assignment: x += 5 means x = x + 5; likewise -=, *=, /=, %=. The increment and decrement operators x++ and x-- add or subtract one.

    1.7

    Application Program Interface (API) and Libraries

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 1.7.A: Identify the attributes and behaviors of a class found in the libraries contained in an API.

    • 1.7.A.1 Libraries are collections of classes. An application programming interface (API) specification informs the programmer how to use those classes. Documentation found in API specifications and libraries is essential to understanding the attributes and behaviors of a class defined by the API. A class defines a specific reference type. Classes in the APIs and libraries are grouped into packages. Existing classes and class libraries can be utilized to create objects.
    • 1.7.A.2 Attributes refer to the data related to the class and are stored in variables. Behaviors refer to what instances of the class can do (or what can be done with them) and are defined by methods.
    ไทย

    วัตถุประสงค์การเรียนรู้ 1.7.A: ระบุคุณสมบัติและพฤติกรรมของคลาสที่พบในไลบรารีที่มีอยู่ใน API

    • 1.7.A.1 ไลบรารี คือกลุ่มของคลาส อินเตอร์เฟซการเขียนโปรแกรมแอปพลิเคชัน (API) ให้ข้อมูลแก่ผู้เขียนโปรแกรมเกี่ยวกับวิธีใช้คลาสนั้น เอกสารที่ปรากฏในสเปก/API และไลบรารีมีความสำคัญอย่างยิ่งต่อการเข้าใจคุณสมบัติและพฤติกรรมของคลาสที่กำหนดโดย API คลาส กำหนดประเภทอ้างอิงเฉพาะเจาะจง คลาสใน API และไลบรารีจะถูกจัดกลุ่มเป็นแพ็กเกจ คลาสและไลบรารีคลาสที่มีอยู่สามารถนำไปใช้เพื่อสร้างออบเจกต์ได้
    • 1.7.A.2 คุณสมบัติ หมายถึงข้อมูลที่เกี่ยวข้องกับคลาสและเก็บไว้ในตัวแปร พฤติกรรม หมายถึงสิ่งที่อิมพลีเมนต์ของคลาสสามารถทำได้ (หรือสิ่งที่จะทำกับมันได้) ซึ่งกำหนดโดยเมทოდ

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    An API (Application Programming Interface) 应用程序接口 is the published list of classes and methods you may use. A library 库 is a collection of ready-made classes (like Math, String, Scanner). You read the API documentation to learn what a method needs (its parameters) and returns, without seeing its inner code – an example of abstraction 抽象.

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    library/ˈlaɪbrəri/ ไลบรารี (library)
    abstraction/əbˈstrækʃn/ การห่อหุ้ม (abstraction)
    Interface/ˈɪntəfeɪs/ อินเตอร์เฟซ
    1.8

    Documentation with Comments

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 1.8.A: Describe the functionality and use of code through comments.

    • 1.8.A.1 Comments are written for both the original programmer and other programmers to understand the code and its functionality, but are ignored by the compiler and are not executed when the program is run. Three types of comments in Java include /* */, which generates a block of comments; //, which generates a comment on one line; and /** */, which are Javadoc comments and are used to create API documentation.
    • 1.8.A.2 A precondition is a condition that must be true just prior to the execution of a method in order for it to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied.
    • 1.8.A.3 A postcondition is a condition that must always be true after the execution of a method. Postconditions describe the outcome of the execution in terms of what is being returned or the current value of the attributes of an object.
    ไทย

    วัตถุประสงค์การเรียนรู้ 1.8.A: อธิบายฟังก์ชันการใช้งานและการใช้งานโค้ดผ่านคอมเมนต์

    • 1.8.A.1 คอมเมนต์ เขียนขึ้นเพื่อให้ผู้เขียนโค้ดต้นฉบับและผู้เขียนโค้ดคนอื่นเข้าใจโค้ดและฟังก์ชันการทำงาน แต่ถูกข้ามโดยคอมไพเลอร์และไม่被执行เมื่อโปรแกรมทำงาน มีคอมเมนต์สามชนิดใน Java ได้แก่ /* */, ซึ่งสร้างบล็อกคอมเมนต์; //, ซึ่งสร้างคอมเมนต์บนบรรทัดเดียว; และ /** */, ซึ่งเป็นคอมเมนต์ Javadoc ใช้สำหรับสร้างเอกสาร API
    • 1.8.A.2 เงื่อนไขก่อน (precondition) เป็นเงื่อนไขที่ต้องเป็นจริงทันทีก่อนการเรียกใช้เมทอด เพื่อให้เมทอดทำงานตามความคาดหวัง ไม่มีการคาดหวังว่าเมทอดจะตรวจสอบว่าเงื่อนไขก่อนถูกตอบสนองหรือไม่
    • 1.8.A.3 เงื่อนไขหลัง (postcondition) เป็นเงื่อนไขที่ต้องเป็นจริงเสมอหลังจากการเรียกใช้เมทอด เงื่อนไขหลังอธิบายผลลัพธ์ของการดำเนินการในแง่ของสิ่งที่ถูกส่งกลับหรือค่าปัจจุบันของคุณสมบัติของออบเจกต์

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    Comments 注释 are ignored by the compiler but explain code to humans: // for a single line, /* ... */ for a block, and /** ... */ for a Javadoc comment that documents a method's purpose, parameters, and return value. Precise preconditions and postconditions are written here.

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    Comments/ˈkɒments/ ความคิดเห็น (Comments)
    1.9

    Method Signatures

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 1.9.A: Identify the correct method to call based on documentation and method signatures.

    • 1.9.A.1 A method is a named block of code that only runs when it is called. A block of code is any section of code that is enclosed in braces. Procedural abstraction allows a programmer to use a method by knowing what the method does even if they do not know how the method was written.
    • 1.9.A.2 A parameter is a variable declared in the header of a method or constructor and can be used inside the body of the method. This allows values or arguments to be passed and used by a method or constructor. A method signature for a method with parameters consists of the method name and the ordered list of parameter types. A method signature for a method without parameters consists of the method name and an empty parameter list.

    Learning Objective 1.9.B: Describe how to call methods.

    • 1.9.B.1 A void method does not have a return value and is therefore not called as part of an expression.
    • 1.9.B.2 A non-void method returns a value that is the same type as the return type in the header. To use the return value when calling a non-void method, it must be stored in a variable or used as part of an expression.
    • 1.9.B.3 An argument is a value that is passed into a method when the method is called. The arguments passed to a method must be compatible in number and order with the types identified in the parameter list of the method signature. When calling methods, arguments are passed using call by value. Call by value initializes the parameters with copies of the arguments.
    • 1.9.B.4 Methods are said to be overloaded when there are multiple methods with the same name but different signatures.
    • 1.9.B.5 A method call interrupts the sequential execution of statements, causing the program to first execute the statements in the method before continuing. Once the last statement in the method has been executed or a return statement is executed, the flow of control is returned to the point immediately following where the method was called.
    ไทย

    วัตถุประสงค์การเรียนรู้ 1.9.A: ระบุเมทอดที่ถูกต้องที่จะเรียกใช้โดยอิงจากเอกสารและลายเซ็นเมทอด

    • 1.9.A.1 เมทอด คือบล็อกโค้ดที่มีชื่อซึ่งจะทำงานก็ต่อเมื่อถูกเรียกใช้ บล็อกโค้ด คือส่วนใดส่วนของโค้ดที่ถูกห่อหุ้มด้วยวงเล็บ {}. การนามธรรมเชิงขั้นตอน (procedural abstraction) ช่วยให้ผู้เขียนโปรแกรมสามารถใช้เมทอดได้โดยรู้เพียงว่าเมทอดทำอะไร แม้จะไม่รู้ว่าเขียนเมทอดอย่างไรก็ตาม
    • 1.9.A.2 พารามิเตอร์ คือตัวแปรที่ประกาศในหัวเมทอดหรือคอนสตรัคเตอร์และสามารถใช้ภายในตัวเมทอดได้ สิ่งนี้ช่วยให้สามารถส่งค่าหรืออาร์กูเมนต์เข้ามา并使用โดยเมทอดหรือคอนสตรัคเตอร์ ลายเซ็นเมทอด สำหรับเมทอดที่มีพารามิเตอร์ประกอบด้วยชื่อเมทอดและรายการลำดับของประเภทพารามิเตอร์ ลายเซ็นเมทอดสำหรับเมทอดที่ไม่มีพารามิเตอร์ประกอบด้วยชื่อเมทอดและรายการพารามิเตอร์ว่างเปล่า

    วัตถุประสงค์การเรียนรู้ 1.9.B: อธิบายวิธีการเรียกใช้เมทอด

    • 1.9.B.1 เมทอด void ไม่มีค่าส่งกลับดังนั้นจึงไม่ถูกเรียกใช้เป็นส่วนหนึ่งของนิพจน์
    • 1.9.B.2 เมทอด non-void ส่งคืนค่าที่มีประเภทเดียวกับประเภทการส่งกลับในหัวเมทอด เพื่อใช้ค่าส่งกลับเมื่อเรียกใช้เมทอด non-void ต้องเก็บค่าไว้ในตัวแปรหรือใช้เป็นส่วนหนึ่งของนิพจน์
    • 1.9.B.3 อาร์กูเมนต์ คือค่าที่ถูกส่งเข้าไปในเมทอดเมื่อเมทอดถูกเรียกใช้อาร์กูเมนต์ที่ถูกส่งไปยังเมทอดต้องมีจำนวนและลำดับที่สอดคล้องกับประเภทที่ระบุไว้ในรายการพารามิเตอร์ของลายเซ็นเมทอด ในการเรียกใช้เมทอด อาร์กูเมนต์จะถูกส่งโดยใช้ call by value Call by value จะเริ่มต้นพารามิเตอร์ด้วยสำเนาของอาร์กูเมนต์
    • 1.9.B.4 เมทอดถูกเรียกว่า ถูกโอเวอร์โหลด (overloaded) เมื่อมีเมทอดหลายตัวที่มีชื่อเดียวกันแต่มีลายเซ็นต่างกัน
    • 1.9.B.5 การเรียกใช้เมทอด ขัดจังหวะการดำเนินการแบบลำดับของคำสั่ง ทำให้โปรแกรมดำเนินการคำสั่งภายในเมทอดก่อนแล้วจึงดำเนินต่อไป เมื่อคำสั่งสุดท้ายในเมทอดถูกดำเนินการหรือคำสั่ง return ถูกดำเนินการ การไหลของควบคุมจะถูกส่งกลับไปยังจุดทันทีหลังจากตำแหน่งที่เมทอดถูกเรียกใช้

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    A method signature 方法签名 is a method's name plus its parameter types, e.g. nextInt() or substring(int, int). To call a method you must supply arguments 实参 that match the parameters in number, type, and order. The method header (the full declaration) also states the return type – the type of value the method gives back (void if none) – but the return type is not part of the signature, which is why two methods cannot differ by return type alone.

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    method signature/ˈmeθəd ˈsɪɡnɪtʃə/ ลายมือชื่อเมทดอด
    arguments/ˈɑːɡjuːmənts/ อาร์กิวเมนต์
    1.10

    Calling Class Methods

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 1.10.A: Develop code to call class methods and determine the result of those calls.

    • 1.10.A.1 Class methods are associated with the class, not instances of the class. Class methods include the keyword static in the header before the method name.
    • 1.10.A.2 Class methods are typically called using the class name along with the dot operator. When the method call occurs in the defining class, the use of the class name is optional in the call.
    ไทย

    วัตถุประสงค์การเรียนรู้ 1.10.A: เขียนโค้ดเพื่อเรียกใช้เมทอดของคลาสและระบุผลลัพธ์ของการเรียก使用该 those calls.

    • 1.10.A.1 เมทอดของคลาส (Class methods) เชื่อมโยงกับตัวคลาส ไม่ใช่ instances ของคลาส เมทอดของคลาสจะรวมถึง keyword static ใน header ก่อนชื่อเมทოდ
    • 1.10.A.2 เมทอดของคลาสโดยทั่วไปจะถูกเรียกใช้โดยใช้ชื่อคลาสพร้อมกับ dot operator เมื่อการเรียกใช้เมทอดเกิดขึ้นภายในคลาสที่กำหนด การใช้ชื่อคลาสใน call นั้นเป็นทางเลือกได้

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    A class (static) method 类方法 belongs to the class itself, so you call it on the class name: ClassName.method(args). No object is needed.

    Explore · ⁨สำรวจ⁩

    Follow a class-method call on the stack · ⁨ติดตามการเรียกใช้เมทόδของคลาสบนส택⁩

    Calling a class method like Math.max pushes a new frame onto the call stack; when the method returns a value, its frame pops and control goes back to the caller. Step through to watch the stack grow and shrink. · ⁨การเรียกใช้ เมทόδของคลาส เช่น Math.max จะเพิ่ม เฟรม ใหม่ลงบนสักการเรียกเมื่อเมทόδ ส่งคืน ค่า เฟรมนั้นจะถูกนำออกและควบคุมจะกลับไปยังผู้เรียก ก้าวผ่านเพื่อดูว่าสักขยายและหดตัวอย่างไร⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    class (static) method/klæs ˈmeθəd/ เมทดอดคลาส (เมทดอดแบบสถิต)
    1.11

    Math Class

    Syllabus · ⁨หลักสูตร⁩
    Learning ObjectiveEssential Knowledge

    1.11.A
    Develop code to write expressions that incorporate calls to built-in mathematical libraries and determine the value that is produced as a result.

    • 1.11.A.1 The Math class is part of the java.lang package. Classes in the java.lang package are available by default.
    • 1.11.A.2 The Math class contains only class methods. The following Math class methods—including what they do and when they are used—are part of the Java Quick Reference:
      • static int abs(int x) returns the absolute value of an int value.
      • static double abs(double x) returns the absolute value of a double value.
      • static double pow(double base, double exponent) returns the value of the first parameter raised to the power of the second parameter.
      • static double sqrt(double x) returns the nonnegative square root of a double value.
      • static double random() returns a double value greater than or equal to 0.0 and less than 1.0.
    • 1.11.A.3 The values returned from Math.random() can be manipulated using arithmetic and casting operators to produce a random int or double in a defined range based on specified criteria. Each endpoint of the range can be inclusive, meaning the value is included, or exclusive, meaning the value is not included.

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    The Math class provides static math methods: Math.abs(x), Math.pow(base, exp), Math.sqrt(x), and Math.random() (a double in $[0,1)$). To get a random integer from 0 to n-1: (int)(Math.random() * n).

    1.12

    Objects: Instances of Classes

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 1.12.A: Explain the relationship between a class and an object.

    • 1.12.A.1 An object is a specific instance of a class with defined attributes. A class is the formal implementation, or blueprint, of the attributes and behaviors of an object.
    • 1.12.A.2 A class hierarchy can be developed by putting common attributes and behaviors of related classes into a single class called a superclass. Classes that extend a superclass, called subclasses, can draw upon the existing attributes and behaviors of the superclass without replacing these in the code. This creates an inheritance relationship from the subclasses to the superclass.
      • Exclusion statement: Designing and implementing inheritance relationships are outside the scope of the AP Computer Science A course and exam.
    • 1.12.A.3 All classes in Java are subclasses of the Object class.

    Learning Objective 1.12.B: Develop code to declare variables to store reference types.

    • 1.12.B.1 A variable of a reference type holds an object reference, which can be thought of as the memory address of that object.
    ไทย

    วัตถุประสงค์การเรียนรู้ 1.12.A: อธิบายความสัมพันธ์ระหว่างคลาสและวัตถุ

    • 1.12.A.1 วัตถุ (Object) คือ instance เฉพาะของคลาสที่มี attributes ที่กำหนดไว้ คลาส (Class) คือการนำไปใช้แบบทางการ หรือ blueprint ของ attributes และ behaviors ของวัตถุ
    • 1.12.A.2 Class hierarchy สามารถพัฒนาได้โดยการนำ common attributes และ behaviors ของ classes ที่เกี่ยวข้องมาอยู่ใน single class ที่เรียกว่า superclass. Classes ที่ขยาย superclass ซึ่งเรียกว่า subclasses สามารถดึงข้อมูลจาก existing attributes และ behaviors ของ superclass โดยไม่ต้องแทนที่สิ่งเหล่านี้ในโค้ด สิ่งนี้สร้าง inheritance relationship จาก subclasses ไปยัง superclass
      • Exclusion statement: การออกแบบและดำเนินการ inheritance relationships อยู่นอกขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A
    • 1.12.A.3 ทุก class ใน Java เป็น subclass ของ Object คลาส

    วัตถุประสงค์การเรียนรู้ 1.12.B: เขียนโค้ดเพื่อ_declare variables เพื่อเก็บ reference types

    • 1.12.B.1 ตัวแปรของ reference type จะเก็บ object reference ซึ่งสามารถคิดว่าเป็น memory address ของวัตถุนั้น

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    = copies the reference, not the object

    A class 类 is a blueprint; an object 对象 is a concrete instance 实例 built from it. A class bundles data (fields) with behavior (methods) – the heart of object-oriented programming 面向对象编程. String, Scanner, and ArrayList are all classes you instantiate.

    Classes can be organised into a hierarchy. A superclass 父类 holds attributes and behaviors shared by several subclasses 子类 that extend it – an inheritance relationship 继承关系. Every class in Java is ultimately a subclass of the built-in Object class, which is why every object already has a toString method; writing a subclass method with the same signature as a superclass one is method overriding 方法重写. (Designing your own inheritance is beyond this course, but you are expected to recognise this vocabulary.)

    A class diagram: private attributes and public methods
    A class diagram: private attributes and public methods
    A class is a blueprint; each object is one instance built from it
    A class is a blueprint; each object is one instance built from it
    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    class/klæs/ class
    object/ˈɒbdʒekt/ 物体本身
    instance/ˈɪnstəns/ instance object
    object-oriented programming/ˈɒbdʒekt ˈɔːrɪəntɪd ˈprəʊɡræmɪŋ/ การเขียนโปรแกรมเชิงวัตถุ
    superclass/ˈsuːpəklæs/ superclass
    subclasses/ˈsʌbklæsɪz/ คลาสลูก
    inheritance relationship/ɪnˈherɪtəns rɪˈleɪʃənʃɪp/ ความสัมพันธ์การสืบทอด
    method overriding/ˈmeθəd ˌəʊvəˈraɪdɪŋ/ การทวนเมทดอด
    1.13

    Object Creation and Storage (Instantiation)

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 1.13.A: Identify, using its signature, the correct constructor being called.

    • 1.13.A.1 A class contains constructors that are called to create objects. They have the same name as the class.
    • 1.13.A.2 A constructor signature consists of the constructor's name, which is the same as the class name, and the ordered list of parameter types. The parameter list, in the header of a constructor, lists the types of the values that are passed and their variable names.
    • 1.13.A.3 Constructors are said to be overloaded when there are multiple constructors with different signatures.

    Learning Objective 1.13.B: Develop code to declare variables of the correct types to hold object references.

    • 1.13.B.1 A variable of a reference type holds an object reference or, if there is no object, null.

    Learning Objective 1.13.C: Develop code to create an object by calling a constructor.

    • 1.13.C.1 An object is typically created using the keyword new followed by a call to one of the class's constructors.
    • 1.13.C.2 Parameters allow constructors to accept values to establish the initial values of the attributes of the object.
    • 1.13.C.3 A constructor argument is a value that is passed into a constructor when the constructor is called. The arguments passed to a constructor must be compatible in order and number with the types identified in the parameter list in the constructor signature. When calling constructors, arguments are passed using call by value. Call by value initializes the parameters with copies of the arguments.
    • 1.13.C.4 A constructor call interrupts the sequential execution of statements, causing the program to first execute the statements in the constructor before continuing. Once the last statement in the constructor has been executed, the flow of control is returned to the point immediately following where the constructor was called.
    ไทย

    วัตถุประสงค์การเรียนรู้ 1.13.A: ระบุ constructor ที่ถูกต้องโดยใช้ signature ของมัน

    • 1.13.A.1 คลาสมี constructors ที่ถูกเรียกใช้เพื่อสร้างวัตถุ They have the same name as the class.
    • 1.13.A.2 Constructor signature ประกอบด้วยชื่อของ constructor ซึ่งเป็นชื่อเดียวกับชื่อคลาส และรายการลำดับของ parameter types Parameter list ใน header ของ constructor จะระบุ types ของ values ที่ส่งเข้าและ variable names ของมัน
    • 1.13.A.3 Constructors ถูกเรียกว่า overloaded เมื่อมี multiple constructors ที่มี signatures ต่างกัน

    วัตถุประสงค์การเรียนรู้ 1.13.B: เขียนโค้ดเพื่อ_declare variables ของประเภทที่ถูกต้องเพื่อเก็บ object references

    • 1.13.B.1 ตัวแปรของ reference type จะเก็บ object reference หรือ, ถ้าไม่มีวัตถุ, null

    วัตถุประสงค์การเรียนรู้ 1.13.C: เขียนโค้ดเพื่อสร้างวัตถุโดยการเรียกใช้ constructor

    • 1.13.C.1 วัตถุมักจะถูกสร้างโดยใช้ keyword new ตามด้วยการเรียกใช้ one of the class's constructors
    • 1.13.C.2 Parameters ช่วยให้ constructors ยอมรับ values เพื่อตั้งค่าเริ่มต้นของ attributes ของวัตถุ
    • 1.13.C.3 constructor argument คือค่าที่ถูกส่งเข้าไปใน constructor เมื่อ constructor ถูกเรียก arguments ที่ส่งไปยัง constructor ต้องมีความเข้ากันได้ทั้งลำดับและจำนวนกับประเภทที่กำหนดไว้ในรายการพารามิเตอร์ใน signature ของ constructor เมื่อเรียก构造函数 arguments ถูกส่งโดยใช้ call by value Call by value เริ่มต้นพารามิเตอร์ด้วยสำเนาของ arguments
    • 1.13.C.4 การเรียกใช้ constructor จะหยุด execution แบบลำดับของ statements ทำให้โปรแกรม execute statements ใน constructor ก่อนก่อนจะต่อจากนั้น Once the last statement in the constructor has been executed, flow of control จะถูกส่งกลับไปยังจุดทันทีหลังจากที่ constructor ถูกเรียกใช้

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    Instantiation 实例化 creates an object with the new keyword, which calls a constructor 构造函数:

    Scanner in = new Scanner(System.in);
    String s = new String("hi");   // or just "hi"
    

    The variable holds a reference 引用 (the object's address), not the object itself. Two references can point to the same object; comparing them with == compares addresses, not contents.

    A reference can also point to nothing: the special value null 空值 means "not attached to any object". Calling a method on a null reference crashes at run time with a NullPointerException. Guard against it by testing with ==/!= and checking null first, so && short-circuits before the method runs: if (s != null && s.length() > 0).

    A primitive variable holds its value directly, a reference holds an arrow to the object
    A primitive variable holds its value directly, a reference holds an arrow to the object
    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    Instantiation/ˌɪnstænʃɪˈeɪʃn/ การสร้าง实例 (Instantiation)
    constructor/kənˈstrʌktə/ constructor
    reference/ˈrefrəns/ กรอบอ้างอิง
    null/nʌl/ null
    1.14

    Calling Instance Methods

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 1.14.A: Develop code to call instance methods and determine the result of these calls.

    • 1.14.A.1 Instance methods are called on objects of the class. The dot operator is used along with the object name to call instance methods.
    • 1.14.A.2 A method call on a null reference will result in a NullPointerException.
    ไทย

    วัตถุประสงค์การเรียนรู้ 1.14.A: เขียนโค้ดเพื่อเรียกใช้เมทอดของอินสแตนซ์และระบุผลลัพธ์ของการเรียก使用该 those calls

    • 1.14.A.1 เมทอดของอินสแตนซ์ (Instance methods) ถูกเรียกใช้บน objects ของคลาส Dot operator ใช้ร่วมกับชื่อ object เพื่อเรียกใช้メธอดของอินสแตนซ์
    • 1.14.A.2 การเรียกใช้メธอดบน null reference จะ resulting in a NullPointerException

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    An instance method 实例方法 acts on a specific object, so you call it on the object reference: object.method(args). Example: in.nextInt(), word.length().

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    instance method/ˈɪnstəns ˈmeθəd/ วิธีในอินสแตนซ์
    1.15

    String Manipulation

    Syllabus · ⁨หลักสูตร⁩
    English
    Learning ObjectiveEssential Knowledge

    1.15.A
    Develop code to create string objects and determine the result of creating and combining strings.

    • 1.15.A.1 A String object represents a sequence of characters and can be created by using a string literal or by calling the String class constructor.
    • 1.15.A.2 The String class is part of the java.lang package. Classes in the java.lang package are available by default.
    • 1.15.A.3 A String object is immutable, meaning once a String object is created, its attributes cannot be changed. Methods called on a String object do not change the content of the String object.
    • 1.15.A.4 Two String objects can be concatenated together or combined using the + or += operator, resulting in a new String object. A primitive value can be concatenated with a String object. This causes the implicit conversion of the primitive value to a String object.
    • 1.15.A.5 A String object can be concatenated with any object, which implicitly calls the object's toString method (a behavior that is guaranteed to exist by the inheritance relationship every class has with the Object class). An object's toString method returns a string value representing the object. Subclasses of Object often override the toString method with class-specific implementation. Method overriding occurs when a public method in a subclass has the same method signature as a public method in the superclass, but the behavior of the method is specific to the subclass.
      • Exclusion statement: Overriding the toString method of a class is outside the scope of the AP Computer Science A course and exam.

    1.15.B
    Develop code to call methods on string objects and determine the result of calling these methods.

    • 1.15.B.1 A String object has index values from 0 to one less than the length of the string. Attempting to access indices outside this range will result in a StringIndexOutOfBoundsException.
    • 1.15.B.2 The following String methods—including what they do and when they are used—are part of the Java Quick Reference:
      • int length() returns the number of characters in a String object.
      • String substring(int from, int to) returns the substring beginning at index from and ending at index to - 1.
      • String substring(int from) returns substring(from, length()).
      • int indexOf(String str) returns the index of the first occurrence of str; returns -1 if not found.
      • boolean equals(Object other) returns true if this corresponds to the same sequence of characters as other; returns false otherwise.
      • int compareTo(String other) returns a value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 if this is greater than other. Strings are ordered based upon the alphabet.
      • Exclusion statement: Using the equals method to compare one String object with an object of a type other than String is outside the scope of the AP Computer Science A course and exam.
    • 1.15.B.3 A string identical to the single element substring at position index can be created by calling substring(index, index + 1).
    ไทย

    วัตถุประสงค์การเรียนรู้ 1.15.A: เขียนโค้ดเพื่อสร้าง string objects และระบุผลลัพธ์ของการสร้างและรวม strings

    • 1.15.A.1 A String object แทนลำดับของตัวอักษรและสามารถสร้างได้โดยใช้ string literal หรือโดยการเรียก constructor ของ class String
    • 1.15.A.2 Class String เป็นส่วนหนึ่งของแพ็กเกจ java.lang Classes ในแพ็กเกจ java.lang จะถูกโหลดโดยอัตโนมัติ
    • 1.15.A.3 A String object เป็น immutable หมายความว่าเมื่อสร้าง String object แล้ว attribute ของมันไม่สามารถเปลี่ยนได้ Method ที่เรียกบน String object ไม่เปลี่ยนแปลง content ของ String object
    • 1.15.A.4 Object String สองตัวสามารถนำมารวมกันหรือใช้รวมกันด้วย operator + หรือ += เพื่อสร้าง object String ใหม่ ค่า primitive สามารถนำมารวมกับ object String ได้ ซึ่งจะทำให้เกิดการแปลงค่า primitive tersebutเป็น object String แบบอ้อม
    • 1.15.A.5 Object String สามารถนำมารวมกับ object อื่นใดก็ได้ ซึ่งจะเรียกใช้ method toString ของ object นั้นโดยอ้อม (พฤติกรรมนี้รับประกันได้ว่ามีอยู่จริงผ่านความสัมพันธ์การสืบทอด (inheritance) ที่ทุก class มีต่อ class Object) Method toString ของ object จะคืนค่า string ที่แสดงถึง object นั้น Subclasses ของ Object มักจะ Override method toString ด้วย implementation เฉพาะของคลาสนั้น Method overriding เกิดขึ้นเมื่อบน subclass มี public method ที่มี method signature เดียวกับ public method ใน superclass แต่พฤติกรรมของ method นั้นเฉพาะเจาะจงสำหรับ subclass เท่านั้น
      • Exclusion statement: การ Override method toString ของ class หนึ่งอยู่นอกขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A
    Learning ObjectiveEssential Knowledge

    1.15.B
    เขียนโค้ดเพื่อเรียก methods บน string objects และระบุผลลัพธ์ที่จะเกิดขึ้นจากการเรียก methods เหล่านี้

    • 1.15.B.1 Object String มีค่า index ตั้งแต่ 0 ถึงความยาวของ string ลดลง 1 หากพยายามเข้าถึง indices นอกช่วงนี้จะเกิด StringIndexOutOfBoundsException
    • 1.15.B.2 Methods String ต่อไปนี้ รวมถึงหน้าที่และการใช้งานของแต่ละ方法是属于 Java Quick Reference:
      • int length() คืนจำนวนตัวอักษรใน object String
      • String substring(int from, int to) คืน substring ที่เริ่มต้นที่ index from และสิ้นสุดที่ index to - 1
      • String substring(int from) คืน substring(from, length())
      • int indexOf(String str) คืน index ของการปรากฏครั้งแรกของ str; คืน -1 หากไม่พบ
      • boolean equals(Object other) คืน true หาก this ตรงกับลำดับของตัวอักษรเดียวกับ other; คืน false ในกรณีอื่น
      • int compareTo(String other)/value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 หาก this มีค่ามากกว่า other. Strings ถูกเรียงลำดับตามหลักพจนานุกรม
      • Exclusion statement: การใช้ method equals เพื่อเปรียบเทียบ object String กับ object ที่มี type อื่นนอกเหนือจาก String อยู่ ngoàiขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A
    • 1.15.B.3 String ที่เหมือนกันกับ substring ที่มีองค์ประกอบเดียวที่ตำแหน่ง index สามารถสร้างได้โดยการเรียก substring(index, index + 1)

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    Strings are immutable

    String objects are immutable 不可变 – methods return a new string rather than changing the original. Key methods (all indices start at 0):

    s.length();            // number of characters
    s.substring(2, 5);     // chars at index 2,3,4 (5 excluded)
    s.indexOf("ab");       // first position, or -1
    s.equals(other);       // content comparison (never use == for Strings)
    s.compareTo(other);    // <0, 0, >0 by dictionary order
    

    Exam skill: substring(a, b) includes index a but excludes b, and String comparison must use .equals, not == – two of the most-tested String pitfalls.

    Worked example. Let String s = "COMPUTER"; (indices 0–7). Then s.length() is 8; s.substring(0, 4) is "COMP" (indices 0,1,2,3 – index 4 excluded); s.substring(4) is "UTER" (from index 4 to the end); s.indexOf("PU") is 3; and s.indexOf("X") is -1 (not found). Counting the excluded endpoint of substring is the single most common slip.

    Asking for an index outside 0 to length()-1 (a bad substring or charAt argument, e.g. s.substring(0, 20) here) crashes with a StringIndexOutOfBoundsException – the String cousin of the array-index error.

    String indices start at 0
    String indices start at 0
    Explore · ⁨สำรวจ⁩

    Explore string indices and slicing · ⁨สำรวจดัชนีและการตัดทอนของสตริง⁩

    Every character has an index, and the numbering starts at 0. Drag the start and end to see how substring(from, to) takes the characters from from up to — but not including — to. · ⁨ทุกตัวอักษรมี ดัชนี และการนับเริ่มต้นที่ 0 ลากจุดเริ่มต้นและจุดสิ้นสุดเพื่อดูว่า substring(from, to) จะดึงตัวอักษรตั้งแต่ from ขึ้นไปจนถึง (แต่ไม่รวม) to⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    immutable/ɪˈmjuːtəbl/ immutable
    1.15

    Exam tips

    • Trace code by hand line by line, tracking each variable's value in a table — the exam rewards careful tracing over guessing.
    • Know Java's primitive types and that integer division truncates ($7/2$ gives $3$); use a cast or a double for real division.
    • Distinguish compile-time errors (syntax, types) from run-time errors – know the named ones: ArithmeticException (int ÷ 0), NullPointerException (method on a null reference), StringIndexOutOfBoundsException / ArrayIndexOutOfBoundsException – and logic errors (wrong output).
    • Follow operator precedence and initialise every variable before you use it.
    • On the free-response, write complete, compilable Java — return the right type and match the method header exactly.
  • 2

    Selection and Iteration · ⁨การเลือกและการทำซ้ำ⁩

    Watch lesson · ⁨ดูบทเรียน⁩
    2.1

    Selection and Repetition in Algorithms · ⁨การเลือกและการซ้ำในอัลกอริทึม⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 2.1.A: Represent patterns and algorithms that involve selection and repetition found in everyday life using written language or diagrams.

    • 2.1.A.1 The building blocks of algorithms include sequencing, selection, and repetition.
    • 2.1.A.2 Algorithms can contain selection, through decision making, and repetition, via looping.
    • 2.1.A.3 Selection occurs when a choice of how the execution of an algorithm will proceed is based on a true or false decision.
    • 2.1.A.4 Repetition is when a process repeats itself until a desired outcome is reached.
    • 2.1.A.5 The order in which sequencing, selection, and repetition are used contributes to the outcome of the algorithm.
    ไทย

    วัตถุประสงค์การเรียนรู้ 2.1.A: แสดงรูปแบบและอัลกอริทึมที่เกี่ยวข้องกับการเลือกและการซ้ำซ้อนที่พบในชีวิตประจำวันโดยใช้ภาษาเขียนหรือแผนภาพ

    • 2.1.A.1 ส่วนประกอบพื้นฐานของอัลกอริทึมรวมถึงการเรียงลำดับ การเลือก และการซ้ำซ้อน
    • 2.1.A.2 อัลกอริทึมอาจมีการเลือก ผ่านการตัดสินใจ และการซ้ำซ้อน ผ่านการวนลูป
    • 2.1.A.3 การเลือก เกิดขึ้นเมื่อการเลือกวิธีการดำเนินการของอัลกอริทึมขึ้นอยู่กับคำตัดสินที่เป็นจริงหรือเท็จ
    • 2.1.A.4 การซ้ำซ้อน คือกระบวนการที่ทำซ้ำตัวเองจนกว่าจะได้ผลลัพธ์ที่ต้องการ
    • 2.1.A.5 ลำดับที่ใช้ในการเรียงลำดับ การเลือก และการซ้ำซ้อนส่งผลต่อผลลัพธ์ของอัลกอริทึม

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    Algorithms are built from three control structures 控制结构: sequence (steps in order), selection 选择 (choosing a path), and iteration 迭代 (repeating steps). This topic covers selection and iteration – the tools that let a program make decisions and loop.

    ไทย
    แผนภาพการไหลที่มีเพชรตัดสินใจ: การเลือกช่วยกำหนดเส้นทางที่อัลกอริทึมจะผ่าน
    แผนภาพการไหลที่มีเพชรตัดสินใจ: การเลือกช่วยกำหนดเส้นทางที่อัลกอริทึมจะผ่าน

    อัลกอริทึมสร้างจาก โครงสร้างควบคุม สามชนิดคือ sequence (ขั้นตอนตามลำดับ), selection (การเลือกเส้นทาง), และ iteration (การทำซ้ำขั้นตอน) หัวข้อนี้อธิบายถึง selection และ iteration – ซึ่งเป็นเครื่องมือที่ทำให้โปรแกรมสามารถตัดสินใจและวนลูปได้

    โครงสร้างควบคุมสามชนิด: sequence, selection, และ iteration
    โครงสร้างควบคุมสามชนิด: sequence, selection, และ iteration
    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    control structures/kənˈtrəʊl ˈstrʌktʃəz/ โครงสร้างการควบคุม
    selection/sɪˈlekʃn/ การคัดเลือก
    iteration/ˌɪtəˈreɪʃn/ การวนซ้ำ
    boolean expression/ˈbuːlɪən ekˈspreʃn/ นิพจน์บูลีน
    2.2

    Boolean Expressions · ⁨นิพจน์ boolean⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 2.2.A: Develop code to create Boolean expressions with relational operators and determine the result of these expressions.

    • 2.2.A.1 Values can be compared using the relational operators == and != to determine whether the values are the same. With primitive types, this compares the actual primitive values. With reference types, this compares the object references.
    • 2.2.A.2 Numeric values can be compared using the relational operators <, >, <=, and >= to determine the relationship between the values.
    • 2.2.A.3 An expression involving relational operators evaluates to a Boolean value.
    ไทย

    วัตถุประสงค์การเรียนรู้ 2.2.A: เขียนโค้ดเพื่อสร้าง布尔 expressions พร้อมตัวดำเนินการเปรียบเทียบ และกำหนดผลลัพธ์ของ布尔 expressions เหล่านี้

    • 2.2.A.1 ค่าสามารถเปรียบเทียบกันได้โดยใช้ ตัวดำเนินการเปรียบเทียบ (relational operators) == และ != เพื่อดำเนินการว่าค่าเหล่านั้นเท่ากันหรือไม่对于 primitive types การเปรียบเทียบนี้จะตรวจสอบค่า primitive จริง สำหรับ reference types การเปรียบเทียบนี้จะตรวจสอบการอ้างอิงไปยัง object
    • 2.2.A.2 ค่าทางคณิตศาสตร์สามารถเปรียบเทียบกันได้โดยใช้ตัวดำเนินการเปรียบเทียบ <, >, <= และ >= เพื่อดำเนินการความสัมพันธ์ระหว่างค่าเหล่านี้
    • 2.2.A.3布尔 expression ที่เกี่ยวข้องกับตัวดำเนินการเปรียบเทียบจะมีผลลัพธ์เป็น布尔 value

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English
    Logic gates & the half-adder

    A boolean expression 布尔表达式 evaluates to true or false, using relational operators 关系运算符: == (equal), != (not equal), <, >, <=, >=. Note == compares primitive values but object references for objects, so use .equals for Strings.

    ไทย
    Logic gates & the half-adder

    นิพจน์ boolean ให้ผลลัพธ์เป็น true หรือ false โดยใช้ relational operators: == (เท่ากับ), != (ไม่เท่ากัน), <, >, <=, >=. หมายเหตุว่า == เปรียบเทียบ primitive values แต่ใช้สำหรับ object references ของ objects ดังนั้นให้ใช้ .equals สำหรับ Strings.

    ครอบครัวของ operator สามกลุ่ม: arithmetic, relational, และ logical
    ครอบครัวของ operator สามกลุ่ม: arithmetic, relational, และ logical
    Explore · ⁨สำรวจ⁩

    Explore the AND truth table · ⁨สำรวจตารางความจริงของ AND⁩

    A Boolean expression evaluates to true or false. AND is true only when both operands are true; toggle the inputs to see all four cases. · ⁨นิพจน์บูลีน ให้ผลลัพธ์เป็น true หรือ false AND จะเป็นจริงก็ต่อเมื่อ ทั้งสอง ตัว operand เป็นจริง; สลับอินพุตเพื่อดูกรณีทั้งสี่⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    relational operators/rɪˈleɪʃənl ˈɒpəreɪtəz/ ตัวเปรียบเทียบเชิงสัมพันธ์
    if statement/ɪf ˈsteɪtmənt/ คำสั่ง if
    2.3

    The if Statement · ⁨คำสั่ง if⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 2.3.A: Develop code to represent branching logical processes by using selection statements and determine the result of these processes.

    • 2.3.A.1 Selection statements change the sequential execution of statements.
    • 2.3.A.2 An if statement is a type of selection statement that affects the flow of control by executing different segments of code based on the value of a Boolean expression.
    • 2.3.A.3 A one-way selection (if statement) is used when there is a segment of code to execute under a certain condition. In this case, the body is executed only when the Boolean expression is true.
    • 2.3.A.4 A two-way selection (if-else statement) is used when there are two segments of code—one to be executed when the Boolean expression is true and another segment for when the Boolean expression is false. In this case, the body of the if is executed when the Boolean expression is true, and the body of the else is executed when the Boolean expression is false.
    ไทย

    วัตถุประสงค์การเรียนรู้ 2.3.A: เขียนโค้ดเพื่อแสดงกระบวนการตัดสินใจเชิงตรรกะด้วยการใช้ selection statements และกำหนดผลลัพธ์ของกระบวนการเหล่านี้

    • 2.3.A.1 Selection statements เปลี่ยนแปลงลำดับการดำเนินการของคำสั่ง
    • 2.3.A.2 คำสั่ง if เป็นประเภทหนึ่งของ selection statements ที่มีผลต่อการควบคุมการทำงานโดยดำเนินการส่วนต่าง ๆ ของโค้ดตามค่าของ布尔 expression
    • 2.3.A.3 การเลือกแบบทางเดียว (one-way selection) หรือคำสั่ง if ใช้เมื่อมีส่วนหนึ่งของโค้ดที่ต้องการดำเนินการภายใต้เงื่อนไข tertentu ในกรณีนี้ ตัวจะดำเนินการก็ต่อเมื่อ布尔 expression มีค่าเป็น true เท่านั้น
    • 2.3.A.4 การเลือกแบบสองทาง (two-way selection) หรือคำสั่ง if-else ใช้เมื่อมีสองส่วนของโค้ด—one ส่วนสำหรับการดำเนินการเมื่อ布尔 expression มีค่าเป็น true และอีกส่วนหนึ่งสำหรับการดำเนินการเมื่อ布尔 expression มีค่าเป็น false ในกรณีนี้ ตัวของ if จะดำเนินการเมื่อ布尔 expression มีค่าเป็น true และตัวของ else จะดำเนินการเมื่อ布尔 expression มีค่าเป็น false

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    An if statement 条件语句 runs a block only when its condition is true; an optional else gives an alternative:

    ไทย

    คำสั่ง if จะรันบล็อกเมื่อเงื่อนไขเป็นจริงเท่านั้น; ส่วนเสริม else ให้ทางเลือกอื่น:

    if (score >= 60) {
        System.out.println("Pass");
    } else {
        System.out.println("Fail");
    }
    
    ไฟจราจร: การเลือกช่วยกำหนดว่า branch nàoจะถูก اجرا เหมือนกับคำสั่ง if ที่เลือกเส้นทางโค้ด
    ไฟจราจร: การเลือกช่วยกำหนดว่า branch nàoจะถูก اجرا เหมือนกับคำสั่ง if ที่เลือกเส้นทางโค้ด
    Explore · ⁨สำรวจ⁩

    See which branch an if chooses · ⁨ดูว่า if เลือกทางไหน⁩

    An if statement runs its body only when the condition is true, otherwise it skips to else. Slide the score across the boundaries and watch the grade change. · ⁨if statement จะรันโค้ดภายในก็ต่อเมื่อเงื่อนไขเป็นจริง มิฉะนั้นจะข้ามไป else เลื่อนคะแนนข้ามขอบเขตและดูเกรดเปลี่ยนไป⁩

    2.4

    Nested if Statements

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 2.4.A: Develop code to represent nested branching logical processes and determine the result of these processes.

    • 2.4.A.1 Nested if statements consist of if, if-else, or if-else-if statements within if, if-else, or if-else-if statements.
    • 2.4.A.2 The Boolean expression of the inner nested if statement is evaluated only if the Boolean expression of the outer if statement evaluates to true.
    • 2.4.A.3 A multiway selection (if-else-if) is used when there are a series of expressions with different segments of code for each condition. Multiway selection is performed such that no more than one segment of code is executed based on the first expression that evaluates to true. If no expression evaluates to true and there is a trailing else statement, then the body of the else is executed.
    ไทย

    วัตถุประสงค์การเรียนรู้ 2.4.A: เขียนโค้ดเพื่อแสดงกระบวนการตัดสินใจเชิงตรรกะแบบซ้อนและกำหนดผลลัพธ์ของกระบวนการเหล่านี้

    • 2.4.A.1 คำสั่ง if แบบซ้อน (nested statements) ประกอบด้วย if, if-else หรือ if-else-if语句ภายใน if, if-else หรือ if-else-if语句
    • 2.4.A.2布尔 expression ของ命令ifแบบซ้อนด้านในจะถูกประเมินก็ต่อเมื่อ布尔 expression ของ命令ifภายนอกมีค่าเป็นtrueเท่านั้น
    • 2.4.A.3 การเลือกหลายทาง (multiway selection) หรือif-else-ifใช้เมื่อมี سلسلةของ布尔 expressionsพร้อมส่วนหนึ่งของโค้ดสำหรับแต่ละเงื่อนไข การเลือกหลายทางดำเนินการโดยให้ดำเนินการส่วนหนึ่งของโค้ดไม่เกินหนึ่งส่วนตาม布尔 expression แรกที่มีค่าเป็นtrue หากไม่มี布尔 expressionใดมีค่าเป็นtrueและมีcommand else ตามท้าย แล้วตัวของelseจะถูกดำเนินการ

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    Placing an if inside another, or chaining with else if, tests several cases in order. Only the first matching branch runs:

    ไทย

    การวาง if ไว้ภายในอีกอันหนึ่ง หรือเชื่อมต่อกันด้วย else if เพื่อทดสอบหลายกรณีตามลำดับ จะมีการรันเฉพาะ branch แรก ที่ตรงกันเท่านั้น:

    if (g >= 90) grade = 'A';
    else if (g >= 80) grade = 'B';
    else grade = 'C';
    
    2.5

    Compound Boolean Expressions

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 2.5.A: Develop code to represent compound Boolean expressions and determine the result of these expressions.

    • 2.5.A.1 Logical operators ! (not), && (and), and || (or) are used with Boolean expressions. The expression !a evaluates to true if a is false and evaluates to false otherwise. The expression a && b evaluates to true if both a and b are true and evaluates to false otherwise. The expression a || b evaluates to true if a is true, b is true, or both, and evaluates to false otherwise. The order of precedence for evaluating logical operators is ! (not), && (and), then || (or). An expression involving logical operators evaluates to a Boolean value.
    • 2.5.A.2 Short-circuit evaluation occurs when the result of a logical operation using && or || can be determined by evaluating only the first Boolean expression. In this case, the second Boolean expression is not evaluated.
    ไทย

    วัตถุประสงค์การเรียนรู้ 2.5.A: เขียนโค้ดเพื่อแสดงสมการ布尔แบบผสมและหาค่าผลลัพธ์ของสมการเหล่านี้

    • 2.5.A.1 ตัวดำเนินการตรรกะ ! (not), && (and), และ || (or) ใช้ร่วมกับสมการ布尔 สมการ !a จะให้ค่าเป็น true หาก a มีค่าเป็น false และจะให้ค่าเป็น false ในกรณีอื่น ๆ สมการ a && b จะให้ค่าเป็น true หากทั้ง a และ b มีค่าเป็น true และจะให้ค่าเป็น false ในกรณีอื่น ๆ สมการ a || b จะให้ค่าเป็น true หาก a มีค่าเป็น true, b มีค่าเป็น true, หรือทั้งสองอย่าง และมีค่าเป็น false ในกรณีอื่น ๆ ลำดับความสำคัญในการประเมินค่าตัวดำเนินการตรรกะคือ ! (not), && (and), แล้วตามด้วย || (or) สมการที่เกี่ยวข้องกับตัวดำเนินการตรรกะจะให้ค่าเป็น布尔值 (boolean value)
    • 2.5.A.2 การประเมินแบบลัด (short-circuit evaluation) เกิดขึ้นเมื่อผลลัพธ์ของการดำเนินการตรรกะที่ใช้ && หรือ || สามารถระบุได้จากการประเมินเพียงสมการ布尔แรกเท่านั้น ในกรณีนี้ สมการ布尔ที่สองจะไม่ถูกประเมิน

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English
    Short-circuit evaluation

    Logical operators 逻辑运算符 combine conditions: && (and – both true), || (or – at least one true), ! (not – reverse). Java uses short-circuit evaluation 短路求值: && stops if the left side is false, and || stops if the left side is true – useful to guard against errors, e.g. if (n != 0 && total / n > 5).

    ไทย
    Short-circuit evaluation

    Logical operators รวมเงื่อนไขเข้าด้วยกัน: && (and – ทั้งคู่เป็นจริง), || (or – อย่างน้อยหนึ่งเป็นจริง), ! (not – กลับด้าน) Java ใช้ short-circuit evaluation: && จะหยุดหากฝั่งซ้ายเป็น false, และ || จะหยุดหากฝั่งซ้ายเป็นจริง – มีประโยชน์ในการป้องกันข้อผิดพลาด เช่น if (n != 0 && total / n > 5)

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    Logical operators/ˈlɒdʒɪkl ˈɒpəreɪtəz/ ตัวดำเนินการทางตรรกะ
    short-circuit evaluation/ʃɔːt ˈsɜːkɪt ɪˌvæljuːˈeɪʃn/ การประเมินแบบวงจรสั้น
    2.6

    Comparing Boolean Expressions · ⁨เปรียบเทียบนิพจน์ boolean⁩

    Syllabus · ⁨หลักสูตร⁩
    Learning ObjectiveEssential Knowledge

    2.6.A
    Compare equivalent Boolean expressions.

    • 2.6.A.1 Two Boolean expressions are equivalent if they evaluate to the same value in all cases. Truth tables can be used to prove Boolean expressions are equivalent.
    • 2.6.A.2 De Morgan's law can be applied to Boolean expressions to create equivalent Boolean expressions. Under De Morgan's law, the Boolean expression !(a && b) is equivalent to !a || !b and the Boolean expression !(a || b) is equivalent to !a && !b.

    2.6.B
    Develop code to compare object references using Boolean expressions and determine the result of these expressions.

    • 2.6.B.1 Two different variables can hold references to the same object. Object references can be compared using == and !=.
    • 2.6.B.2 An object reference can be compared with null, using == or !=, to determine if the reference actually references an object.
    • 2.6.B.3 Classes often define their own equals method, which can be used to specify the criteria for equivalency for two objects of the class. The equivalency of two objects is most often determined using attributes from the two objects.
      • Exclusion statement: Overriding the equals method is outside the scope of the AP Computer Science A course and exam.

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    De Morgan's laws 德摩根定律 rewrite negations: !(a && b) equals !a || !b, and !(a || b) equals !a && !b. Two boolean expressions are equivalent if they give the same result for every input – a truth table proves it. Simplifying conditions this way is a common exam task.

    ไทย

    De Morgan's laws เขียนใหม่ของการปฏิเสธ: !(a && b) เท่ากับ !a || !b, และ !(a || b) เท่ากับ !a && !b. นิพจน์ boolean สองนิพจน์มีค่า เทียบเท่ากัน หากให้ผลลัพธ์เดียวกันสำหรับอินพุตทั้งหมด – ตารางความจริงพิสูจน์สิ่งนี้ การลดรูปเงื่อนไขด้วยวิธีนี้เป็นโจทย์สอบที่พบบ่อย

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    De Morgan's laws/də ˈmɔːɡənz lɔːz/ กฎของเด摩根
    2.7

    while Loops

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 2.7.A: Identify when an iterative process is required to achieve a desired result.

    • 2.7.A.1 Iteration is a form of repetition. Iteration statements change the flow of control by repeating a segment of code zero or more times as long as the Boolean expression controlling the loop evaluates to true.
    • 2.7.A.2 An infinite loop occurs when the Boolean expression in an iterative statement always evaluates to true.
    • 2.7.A.3 The loop body of an iterative statement will not execute if the Boolean expression initially evaluates to false.
    • 2.7.A.4 Off by one errors occur when the iteration statement loops one time too many or one time too few.

    Learning Objective 2.7.B: Develop code to represent iterative processes using while loops and determine the result of these processes.

    • 2.7.B.1 A while loop is a type of iterative statement. In while loops, the Boolean expression is evaluated before each iteration of the loop body, including the first. When the expression evaluates to true, the loop body is executed. This continues until the Boolean expression evaluates to false, whereupon the iteration terminates.
    ไทย

    วัตถุประสงค์การเรียนรู้ 2.7.A: ระบุเมื่อใดที่ต้องการกระบวนการแบบวนซ้ำ (iterative process) เพื่อให้ได้ผลลัพธ์ที่ต้องการ

    • 2.7.A.1 การวนซ้ำ (Iteration) เป็นรูปแบบหนึ่งของการทำซ้ำ คำสั่งการวนซ้ำจะเปลี่ยนการไหลของควบคุมโดยการทำซ้ำส่วนหนึ่งของโค้ดศูนย์หรือมากกว่าครั้ง ตราบเท่าที่นิพจน์ Boolean ที่ควบคุมลูปมีค่าเป็น true
    • 2.7.A.2 ลูปอนันต์ (infinite loop) เกิดขึ้นเมื่อนิพจน์ Boolean ในคำสั่งการวนซ้ำมีค่าเป็น true เสมอ
    • 2.7.A.3 ส่วนลูปของคำสั่งการวนซ้ำจะไม่ถูกดำเนินการหากนิพจน์ Boolean มีค่าเริ่มต้นเป็น false
    • 2.7.A.4 ความผิดพลาดแบบ Off by one เกิดขึ้นเมื่อคำสั่งการวนซ้ำวนซ้ำมากไปหนึ่งรอบหรือน้อยไปหนึ่งรอบ

    วัตถุประสงค์การเรียนรู้ 2.7.B: เขียนโค้ดเพื่อแสดงกระบวนการแบบวนซ้ำโดยใช้ while loops และหาผลลัพธ์ของกระบวนการเหล่านี้

    • 2.7.B.1 while loop เป็นประเภทหนึ่งของคำสั่งการวนซ้ำ ใน while loops นิพจน์ Boolean จะถูกประเมินก่อนแต่ละรอบของการวนซ้ำ เช่นเดียวกับรอบแรก เมื่อนิพจน์มีค่าเป็น true ส่วนลูปจะถูกดำเนินการ สิ่งนี้จะดำเนินต่อไปจนกว่านิพจน์ Boolean จะมีค่าเป็น false ซึ่งการวนซ้ำจะหยุดลง

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    A while loop 循环 repeats while its condition stays true, testing before each pass. You must change something inside so the loop eventually stops, or it becomes an infinite loop 无限循环:

    ไทย

    while loop ทำซ้ำ while เงื่อนไขยังคงเป็นจริง โดยทดสอบ ก่อน การวนแต่ละครั้ง คุณต้องเปลี่ยนแปลงบางอย่างภายในเพื่อให้ลูปหยุดลงในที่สุด มิฉะนั้นจะเป็น infinite loop:

    ประเภทของลูปทั้งสามแตกต่างกันที่ตำแหน่งที่ทดสอบเงื่อนไข
    ประเภทของลูปทั้งสามแตกต่างกันที่ตำแหน่งที่ทดสอบเงื่อนไข
    int i = 0;
    while (i < 5) {
        System.out.println(i);
        i++;
    }
    
    Explore · ⁨สำรวจ⁩

    Trace a while loop · ⁨ติดตามการทำงานของ while loop⁩

    A while loop repeats as long as its condition stays true, updating its variables each pass. Step through to see the sum of squares build up. · ⁨while loop จะซ้ำจนกว่าเงื่อนไขจะเป็นจริง และอัปเดตตัวแปรทุกครั้งในการวนลูป ก้าวผ่านเพื่อดูผลรวมกำลังสองค่อยๆ สร้างขึ้น⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    while loop/waɪl luːp/ ลูป while
    infinite loop/ˈɪnfɪnət luːp/ ลูปไม่สิ้นสุด
    2.8

    for Loops

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 2.8.A: Develop code to represent iterative processes using for loops and determine the result of these processes.

    • 2.8.A.1 A for loop is a type of iterative statement. There are three parts in a for loop header: the initialization, the Boolean expression, and the update.
    • 2.8.A.2 In a for loop, the initialization statement is only executed once before the first Boolean expression evaluation. The variable being initialized is referred to as a loop control variable. The Boolean expression is evaluated immediately after the loop control variable is initialized and then following each execution of the increment statement until it is false. In each iteration, the update is executed after the entire loop body is executed and before the Boolean expression is evaluated again.
    • 2.8.A.3 A for loop can be rewritten into an equivalent while loop (and vice versa).
    ไทย

    วัตถุประสงค์การเรียนรู้ 2.8.A: เขียนโค้ดเพื่อแสดงกระบวนการแบบวนซ้ำโดยใช้ for loops และหาผลลัพธ์ของกระบวนการเหล่านี้

    • 2.8.A.1 for loop เป็นประเภทหนึ่งของคำสั่งการวนซ้ำ หัวของ for loop ประกอบด้วยสามส่วน: การเริ่มต้น, นิพจน์ Boolean และการอัปเดต
    • 2.8.A.2 ใน for loop คำสั่งเริ่มต้นจะถูกดำเนินการเพียงครั้งเดียวก่อนการประเมินนิพจน์ Boolean ครั้งแรก ตัวแปรที่ถูกเริ่มต้นเรียกว่า ตัวแปรควบคุมลูป (loop control variable) นิพจน์ Boolean จะถูกประเมินทันทีหลังจากตัวแปรควบคุมลูปได้รับการเริ่มต้น และจากนั้นตามหลังการดำเนินการของแต่ละคำสั่งเพิ่มค่า จนกว่าจะมีค่าเป็น false ในแต่ละรอบการวนซ้ำ คำสั่งอัปเดตจะถูกดำเนินการหลังจากส่วนลูปทั้งหมดถูกดำเนินการและก่อนที่นิพจน์ Boolean จะถูกประเมินอีกครั้ง
    • 2.8.A.3 for loop สามารถเขียนใหม่ให้เป็น while loop ที่เทียบเท่ากันได้ (และในทางกลับกัน)

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    A for loop packs initialization, condition, and update into one line – best when you know the count:

    A for and an equivalent while do the same work; be able to convert between them.

    ไทย

    for loop รวบรวมการเริ่มต้น เงื่อนไข และการอัปเดตไว้ในบรรทัดเดียว – เหมาะที่สุดเมื่อคุณทราบจำนวนรอบ:

    for (int i = 0; i < n; i++) {
        // runs n times, i = 0..n-1
    }
    

    A for และ A equivalent while ทำงานเหมือนกัน; ต้องสามารถแปลงระหว่างกันได้

    สายพานลำเลียง: ลูปทำซ้ำกระบวนการสำหรับทุกไอเทม เหมือนกับ for และ while
    สายพานลำเลียง: ลูปทำซ้ำกระบวนการสำหรับทุกไอเทม เหมือนกับ for และ while
    Explore · ⁨สำรวจ⁩

    Trace a for loop · ⁨ติดตามการทำงานของ for loop⁩

    A for loop runs a fixed number of times, its counter stepping through a range. Watch the counter and running total advance one pass at a time. · ⁨for loop จะรันจำนวนครั้งคงที่ ตัวนับจะก้าวผ่านช่วงที่กำหนด สังเกตตัวนับและผลรวมสะสมเคลื่อนที่ทีละรอบ⁩

    2.9

    Building Complete Selection and Iteration Algorithms · ⁨สร้างอัลกอริทึม Selection และ Iteration แบบสมบูรณ์⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 2.9.A: Develop code for standard and original algorithms (without data structures) and determine the result of these algorithms.

    • 2.9.A.1 There are standard algorithms to:
      • identify if an integer is or is not evenly divisible by another integer
      • identify the individual digits in an integer
      • determine the frequency with which a specific criterion is met
      • determine a minimum or maximum value
      • compute a sum or average
    ไทย

    วัตถุประสงค์การเรียนรู้ 2.9.A: เขียนโค้ดสำหรับอัลกอริทึมมาตรฐานและอัลกอริทึมต้นฉบับ (โดยไม่ใช้โครงสร้างข้อมูล) และหาผลลัพธ์ของอัลกอริทึมเหล่านี้

    • 2.9.A.1 มีอัลกอริทึมมาตรฐานสำหรับ:
      • ระบุว่ามีจำนวนเต็มเป็นหรือไม่เป็นผลหารที่ลงตัวกับจำนวนเต็มอื่นหรือไม่
      • ระบุหลักเลขแต่ละหลักของจำนวนเต็ม
      • หาความถี่ที่เกณฑ์เฉพาะถูกบรรลุ
      • หาค่าต่ำสุดหรือสูงสุด
      • คำนวณผลรวมหรือค่าเฉลี่ย

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    Combine loops and conditions to solve real problems – count, sum, find a maximum, or test a property:

    Two integer patterns the exam tests directly use % and /. To read the digits of an integer one at a time, repeatedly take n % 10 (the last digit) and then n = n / 10 (drop it). To test divisibility, n % d == 0 means n is evenly divisible by d. Combine them with a counter to find the frequency with which some criterion is met.

    Standard patterns like a running total, a counter, or a flag 标志 (a boolean that records whether something happened) recur throughout the course.

    ไทย

    รวมลูปและเงื่อนไขเพื่อแก้ปัญหาจริง – นับผล, ผลรวม, หาค่าสูงสุด, หรือทดสอบคุณสมบัติ:

    int max = arr[0];
    for (int k = 1; k < arr.length; k++) {
        if (arr[k] > max) max = arr[k];
    }
    

    รูปแบบจำนวนเต็มสองแบบที่ข้อสอบทดสอบโดยตรงใช้ % และ /. ในการ อ่านตัวเลขของจำนวนเต็ม ตัวต่อตัว ทำซ้ำการนำ n % 10 (เลขตัวสุดท้าย) และตามด้วยการ n = n / 10 (ตัดทิ้ง) เพื่อทดสอบ หารลงตัว, n % d == 0 หมายความว่า n หารด้วย d ลงตัว การรวมเข้ากับ counter เพื่อ หาความถี่ ที่เกณฑ์ใดเกณฑ์หนึ่งเกิดขึ้น

    รูปแบบมาตรฐานเช่น running total, counter, หรือ flag (boolean ที่บันทึกว่าเกิดเหตุการณ์ขึ้นหรือไม่) ปรากฏซ้ำ throughout หลักสูตร

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    flag/flæɡ/ flag
    2.10

    String Algorithms

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 2.10.A: Develop code for standard and original algorithms that involve strings and determine the result of these algorithms.

    • 2.10.A.1 There are standard string algorithms to:
      • find if one or more substrings have a particular property
      • determine the number of substrings that meet specific criteria
      • create a new string with the characters reversed
    ไทย

    วัตถุประสงค์การเรียนรู้ 2.10.A: พัฒนาโค้ดสำหรับอัลกอริทึมมาตรฐานและอัลกอริทึมดั้งเดิมที่เกี่ยวข้องกับสตริงและ xác địnhผลลัพธ์ของอัลกอริทึมเหล่านี้

    • 2.10.A.1 มีอัลกอริทึมสตริงมาตรฐานเพื่อ:
      • ค้นหาว่ามีสับสตริงหนึ่งหรือมากกว่ามีคุณสมบัติเฉพาะหรือไม่
      • จำนวนสับสตริงที่ตรงตามเกณฑ์เฉพาะ
      • สร้างสตริงใหม่โดยกลับลำดับตัวอักษร

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    Loop through a string by index to process each character:

    Typical tasks: count occurrences, build a reversed or filtered copy, or test whether one string contains another.

    ไทย

    วนลูปผ่าน string ด้วย index เพื่อประมวลผลแต่ละอักขระ:

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        // count vowels, reverse, check for a substring, ...
    }
    

    งานทั่วไป: นับจำนวนครั้งที่ปรากฏ, สร้างสำเนาที่กลับหัวหรือกรอง, หรือทดสอบว่า string หนึ่งมีอีก string หนึ่งอยู่หรือไม่

    2.11

    Nested Iteration

    Syllabus · ⁨หลักสูตร⁩
    English
    Learning ObjectiveEssential Knowledge

    2.11.A
    Develop code to represent nested iterative processes and determine the result of these processes.

    • 2.11.A.1 Nested iteration statements are iteration statements that appear in the body of another iteration statement. When a loop is nested inside another loop, the inner loop must complete all its iterations before the outer loop can continue to its next iteration.
    ไทย
    Learning ObjectiveEssential Knowledge

    2.11.A
    พัฒนาโค้ดเพื่อแทนกระบวนการ iterative แบบซ้อนและกำหนดผลลัพธ์ของกระบวนการเหล่านี้

    • 2.11.A.1 คำสั่งทำซ้ำแบบซ้อน (Nested iteration statements) คือคำสั่งทำซ้ำที่ปรากฏอยู่ในตัวของคำสั่งทำซ้ำอื่น เมื่อมีลูปถูกซ้อนอยู่ภายในอีกลูปหนึ่ง ลูปด้านในจะต้องดำเนินการจนครบทุกรอบก่อนที่ลูปภายนอกจะสามารถดำเนินต่อไปยังรอบถัดไปได้

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    A nested loop 嵌套循环 puts one loop inside another; the inner loop completes fully for each pass of the outer. If the outer runs $n$ times and the inner $m$ times, the body runs $n\times m$ times – the basis for processing grids and comparing all pairs.

    ไทย

    nested loop วางลูปหนึ่งไว้ภายในอีกลูปหนึ่ง; ลูปภายในจะทำงานจนเสร็จสำหรับ แต่ละ รอบของลูปภายนอก หากลูปภายนอกวน $n$ ครั้งและลูปภายใน $m$ ครั้ง, body จะวน $n\times m$ ครั้ง – เป็นพื้นฐานสำหรับการประมวลผลตารางและเปรียบเทียบทุกคู่

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    nested loop/ˈnestɪd luːp/ ลูปซ้อน
    2.12

    Informal Run-Time Analysis

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 2.12.A: Calculate statement execution counts and informal run-time comparison of iterative statements.

    • 2.12.A.1 A statement execution count indicates the number of times a statement is executed by the program. Statement execution counts are often calculated informally through tracing and analysis of the iterative statements.
    ไทย

    วัตถุประสงค์การเรียนรู้ 2.12.A: คำนวณจำนวนครั้งของการดำเนินการคำสั่งและเปรียบเทียบเวลาในการรันแบบไม่เป็นทางการของคำสั่งทำซ้ำ

    • 2.12.A.1 จำนวนครั้งของการดำเนินการคำสั่ง (Statement execution count) บ่งบอกถึงจำนวนครั้งที่โปรแกรมได้ดำเนินการคำสั่งนั้น จำนวนครั้งของการดำเนินการคำสั่งมักถูกคำนวณโดยวิธีไม่เป็นทางการผ่านการติดตามและวิเคราะห์คำสั่งทำซ้ำ

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English
    Big-O growth rates

    Run-time analysis 运行时间分析 counts how many basic steps an algorithm takes as the input size $n$ grows. Count the executions of the innermost statement: a single loop over $n$ items is linear ($n$ steps); two nested loops over $n$ are quadratic ($n^2$). This informal counting lets you compare two algorithms' efficiency.

    Exam skill: for a nested loop, be able to state how many times the inner statement runs in terms of the loop bounds – a frequent multiple-choice question.

    Worked example. How many stars does this print?

    The inner loop runs i times for each outer i: 0 + 1 + 2 + 3 = 6 stars. When the inner bound is the outer variable, the total is the triangular sum $0+1+\dots+(n-1)=\dfrac{n(n-1)}{2}$ – here $\dfrac{4\times3}{2}=6$ – not the full $n^2=16$ of a rectangular nested loop.

    ไทย
    Big-O growth rates

    Run-time analysis นับว่ามีขั้นตอนพื้นฐานกี่ขั้นตอนที่อัลกอริทึมใช้ เมื่อขนาดอินพุต $n$ เพิ่มขึ้น นับจำนวนครั้งที่被执行ของ statement ภายในสุด: ลูปเดี่ยวบน $n$ ไอเทมคือ linear ($n$ ขั้นตอน); ลูปซ้อนกันสองชั้นบน $n$ คือ quadratic ($n^2$). การนับแบบไม่ทางการนี้ช่วยให้เปรียบเทียบประสิทธิภาพของอัลกอริทึมสองตัวได้

    เวลาการทำงานเพิ่มขึ้นตามจำนวนองค์ประกอบ n อย่างไร
    เวลาการทำงานเพิ่มขึ้นตามจำนวนองค์ประกอบ n อย่างไร

    ทักษะข้อสอบ: สำหรับ nested loop ต้องระบุได้ว่า statement ภายในวนกี่ครั้งโดยอ้างอิงจากขอบเขตของลูป – ข้อสอบ multiple-choice ที่พบบ่อย

    ตัวอย่างที่ทำแล้ว. ตัวอย่างนี้จะพิมพ์ดาวกี่ดวง?

    for (int i = 0; i < 4; i++)
        for (int j = 0; j < i; j++)
            System.out.print("*");
    

    ลูปภายในทำงาน i ครั้งสำหรับแต่ละลูปภายนอก i: มีดาว 0 + 1 + 2 + 3 = 6 เมื่อขอบเขตของลูปภายในเป็นตัวแปร ภายนอก ผลรวมจะเป็นผลบวกสามเหลี่ยม $0+1+\dots+(n-1)=\dfrac{n(n-1)}{2}$ – ที่นี่คือ $\dfrac{4\times3}{2}=6$ – ไม่ใช่ผลเต็ม $n^2=16$ ของลูปซ้อนแบบสี่เหลี่ยมผืนผ้า

    Explore · ⁨สำรวจ⁩

    Compare how algorithms scale · ⁨เปรียบเทียบการขยายตัวของอัลกอริทึม⁩

    Run-time describes how the number of steps grows with the input size $n$. Increase $n$ and watch a linear $O(n)$ pull far ahead of a quadratic $O(n^2)$. · ⁨Run-time อธิบายว่าจำนวนขั้นตอนเพิ่มขึ้นตามขนาดอินพุต $n$ อย่างไร เพิ่ม $n$ แล้วดูว่า linear $O(n)$ ทิ้งห่าง quadratic $O(n^2)$ ได้มากแค่ไหน⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    Run-time analysis/rʌn taɪm əˈnæləsɪs/ การวิเคราะห์ขณะรันไทม์
    2.12

    Exam tips · ⁨ข้อแนะนำสำหรับการสอบ⁩

    English
    • Get boundary conditions right: use < vs <= deliberately, and watch the first and last iteration of every loop (off-by-one is the classic bug).
    • Build compound conditions with &&, ||, ! and remember short-circuit evaluation (put the null check first).
    • Trace nested loops by counting how many times the inner body runs in total.
    • Choose the right structure — if/else if for ranges, a loop for repetition — and avoid an infinite loop by updating the loop variable.
    • Apply De Morgan's laws when you simplify or negate a boolean condition.
    ไทย
    • ตั้งค่าเงื่อนไขขอบเขตให้ถูกต้อง: ใช้ < เทียบกับ <= อย่างตั้งใจ และสังเกตการวนซ้ำครั้งแรกและครั้งสุดท้ายของแต่ละลูป (ข้อผิดพลาด off-by-one เป็นปัญหาคลาสสิก)
    • สร้างเงื่อนไขที่ซับซ้อนด้วย &&, || และ ! และจำไว้ว่ามีการประเมินแบบ short-circuit (วางการตรวจสอบ null ไว้ก่อน)
    • ติดตามลูปซ้อนด้วยการนับว่า ตัวลูปภายใน ทำงานทั้งหมดกี่ครั้ง
    • เลือกโครงสร้างที่เหมาะสม — ใช้ if/else if สำหรับช่วงจำนวน และใช้ลูปสำหรับการทำซ้ำ — และหลีกเลี่ยงลูปไม่สิ้นสุดโดยอัปเดตตัวแปรลูป
    • ใช้ กฎของเด摩根 เมื่อคุณลดรูปหรือสร้างค่าตรงข้ามของเงื่อนไข boolean
  • 3

    Class Creation · ⁨การสร้างคลาส⁩

    Watch lesson · ⁨ดูบทเรียน⁩
    3.1

    Abstraction and Program Design · ⁨การนามธรรมและการออกแบบโปรแกรม⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 3.1.A: Represent the design of a program by using natural language or creating diagrams that indicate the classes in the program and the data and procedural abstractions found in each class by including all attributes and behaviors.

    • 3.1.A.1 Abstraction is the process of reducing complexity by focusing on the main idea. By hiding details irrelevant to the question at hand and bringing together related and useful details, abstraction reduces complexity and allows one to focus on the idea.
    • 3.1.A.2 Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation. Data abstraction manages complexity by giving data a name without referencing the specific details of the representation. Data can take the form of a single variable or a collection of data, such as in a class or a set of data.
    • 3.1.A.3 An attribute is a type of data abstraction that is defined in a class outside any method or constructor. An instance variable is an attribute whose value is unique to each instance of the class. A class variable is an attribute shared by all instances of the class.
    • 3.1.A.4 Procedural abstraction provides a name for a process and allows a method to be used only knowing what it does, not how it does it. Through method decomposition, a programmer breaks down larger behaviors of the class into smaller behaviors by creating methods to represent each individual smaller behavior. A procedural abstraction may extract shared features to generalize functionality instead of duplicating code. This allows for code reuse, which helps manage complexity.
    • 3.1.A.5 Using parameters allows procedures to be generalized, enabling the procedures to be reused with a range of input values or arguments.
    • 3.1.A.6 Using procedural abstraction in a program allows programmers to change the internals of a method (to make it faster, more efficient, use less storage, etc.) without needing to notify method users of the change as long as the method signature and what the method does is preserved.
    • 3.1.A.7 Prior to implementing a class, it is helpful to take time to design each class including its attributes and behaviors. This design can be represented using natural language or diagrams.
    ไทย

    วัตถุประสงค์การเรียนรู้ 3.1.A: แสดงการออกแบบโปรแกรมโดยใช้ภาษาธรรมชาติหรือสร้างแผนภาพที่ระบุคลาสในโปรแกรมและ抽象ข้อมูลและกระบวนการที่พบในแต่ละคลาสโดยรวมถึงคุณสมบัติและการกระทำทั้งหมด

    • 3.1.A.1 Abstraction คือกระบวนการลดความซับซ้อนด้วยการมุ่งเน้นไปที่แนวคิดหลัก通过将ไม่เกี่ยวข้องกับคำถามปัจจุบันซ่อนไว้และรวบรวมรายละเอียดที่เกี่ยวข้องและมีประโยชน์เข้าด้วยกัน Abstraction ช่วยลดความซับซ้อนและทำให้สามารถมุ่งเน้นไปที่แนวคิดได้
    • 3.1.A.2 Data abstraction ให้การแยกระหว่างคุณสมบัติเชิงนามธรรมของชนิดข้อมูล รายละเอียดจริงของการแสดงผล Data abstraction จัดการความซับซ้อนโดยให้ชื่อแก่ข้อมูลโดยไม่อ้างอิงรายละเอียดเฉพาะของการแสดงผล ข้อมูลอาจอยู่ในรูปแบบของตัวแปรเดียวหรือกลุ่มข้อมูล เช่น ในคลาสหรือชุดข้อมูล
    • 3.1.A.3 Attribute เป็นประเภทหนึ่งของ Data abstraction ที่กำหนดไว้ในคลาสภายนอก-method หรือ constructor Instance variable เป็น attribute ที่มีค่าเฉพาะสำหรับแต่ละ instance ของคลาส Class variable เป็น attribute ที่แบ่งปันโดยทุก instance ของคลาส
    • 3.1.A.4 Procedural abstraction ให้ชื่อสำหรับกระบวนการและทำให้สามารถใช้ method ได้โดยรู้เพียงว่าทำอะไร ไม่ใช่วิธีการทำ ผ่าน method decomposition นักเขียนโปรแกรมจะย่อยพฤติกรรมขนาดใหญ่ของคลาสออกเป็นพฤติกรรมย่อยๆ โดยการสร้าง methods เพื่อแสดงพฤติกรรมย่อยแต่ละอย่าง Procedural abstraction อาจดึงคุณลักษณะที่ร่วมกันมาเพื่อทำให้ฟังก์ชัน性工作ทั่วไปแทนที่จะทำซ้ำโค้ด สิ่งนี้ช่วยให้ใช้โค้ดซ้ำได้ ซึ่งช่วยจัดการความซับซ้อน
    • 3.1.A.5 การใช้ parameters ช่วยให้ procedures可以被 generalize ทำให้ procedures สามารถถูก reuse ด้วยช่วงของ input values หรือ arguments
    • 3.1.A.6 การใช้ procedural abstraction ในโปรแกรมอนุญาตให้นักเขียนโปรแกรมเปลี่ยนแปลงภายในของ method (เพื่อให้เร็วขึ้น มีประสิทธิภาพมากขึ้น ใช้พื้นที่จัดเก็บน้อยลง ฯลฯ) โดยไม่ต้องแจ้ง给用户关于การเปลี่ยนแปลงตราบใดที่ method signature และสิ่งที่ method ทำยังคงอยู่
    • 3.1.A.7 ก่อนที่จะดำเนินการสร้างคลาส จะเป็นการดีที่จะใช้เวลาในการออกแบบแต่ละคลาส รวมถึง properties และ behaviors การออกแบบนี้สามารถแสดงได้ด้วยภาษาธรรมชาติหรือแผนภาพ

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    Abstraction 抽象 means hiding detail behind a simple interface – you use a String without knowing how it stores characters. Good design breaks a problem into classes, each responsible for one idea. This topic is about writing your own classes.

    ไทย
    จิ๊กซอว์กำลังประกอบ: คลาสและเมทόδเป็นชิ้นส่วนแยกส่วนของการออกแบบโปรแกรมที่ใหญ่ขึ้น
    จิ๊กซอว์กำลังประกอบ: คลาสและเมทόδเป็นชิ้นส่วนแยกส่วนของการออกแบบโปรแกรมที่ใหญ่ขึ้น

    นามธรรม หมายถึงการซ่อนรายละเอียดไว้เบื้องหลังอินเทอร์เฟซที่เรียบง่าย – คุณใช้ String โดยไม่ต้องรู้วิธีการจัดเก็บข้อมูล ตัวอักษร การออกแบบที่ดีจะแบ่งปัญหาออกเป็นคลาส แต่ละคลาสรับผิดชอบแนวคิดหนึ่งประเด็น หัวขอนี้เกี่ยวกับการเขียน คลาสของคุณเอง

    การย่อยสลายโปรแกรมออกเป็นโมดูลและซับโมดูล
    การแยกย่อยโปรแกรมออกเป็นโมดูลและ submodule
    3.2

    The Impact of Program Design · ⁨ผลกระทบของการออกแบบโปรแกรม⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 3.2.A: Explain the social and ethical implications of computing systems.

    • 3.2.A.1 System reliability refers to the program being able to perform its tasks as expected under stated conditions without failure. Programmers should make an effort to maximize system reliability by testing the program with a variety of conditions.
    • 3.2.A.2 The creation of programs has impacts on society, the economy, and culture. These impacts can be both beneficial and harmful. Programs meant to fill a need or solve a problem can have unintended harmful effects beyond their intended use.
    • 3.2.A.3 Legal issues and intellectual property concerns arise when creating programs. Programmers often reuse code written by others and published as open source and free to use. Incorporation of code that is not published as open source requires the programmer to obtain permission and often purchase the code before integrating it into their program.
    ไทย

    วัตถุประสงค์การเรียนรู้ 3.2.A: อธิบายผลกระทบทางสังคมและจริยธรรมของระบบคอมพิวเตอร์

    • 3.2.A.1 System reliability หมายถึงความสามารถของโปรแกรมในการดำเนินงานตามหน้าที่ที่กำหนดภายใต้เงื่อนไขที่ระบุไว้โดยไม่เกิดความล้มเหลว นักเขียนโปรแกรมควรพยายามเพิ่ม System reliability ให้มากที่สุดโดยการทดสอบโปรแกรมด้วยสภาวะต่างๆ
    • 3.2.A.2 การสร้างโปรแกรมมีผลกระทบต่อสังคม เศรษฐกิจ และวัฒนธรรม ผลกระทบเหล่านี้สามารถเป็นทั้งประโยชน์และอันตราย โปรแกรมที่ออกแบบมาเพื่อตอบสนองความต้องการหรือแก้ปัญหาอาจก่อให้เกิดผลกระทบทางลบที่ไม่ได้ตั้งใจเหนือกว่าการใช้ตามวัตถุประสงค์
    • 3.2.A.3 ปัญหาทางกฎหมายและข้อกังวลเรื่องทรัพย์สินทางปัญญาเกิดขึ้นเมื่อการสร้างโปรแกรม นักเขียนโปรแกรมมักนำโค้ดที่เขียนโดยผู้อื่นมาใช้ใหม่และเผยแพร่ในรูปแบบ open source ซึ่งใช้งานฟรี การนำโค้ดที่ไม่ได้เผยแพร่ในรูปแบบ open source มาใช้ต้องได้รับอนุญาตจากเจ้าของและมักจะซื้อลิขสิทธิ์ก่อนนำไปรวมในโปรแกรม

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    Design choices affect whether code is correct, readable, and reusable. Encapsulation 封装 – keeping data private and exposing it only through methods – protects an object's state from misuse and lets you change the inside without breaking users of the class. Thoughtful naming, single-purpose methods, and testing reduce bugs.

    Design also carries responsibility beyond the code. System reliability 系统可靠性 - a program performing its tasks as expected, without failure - is something programmers should maximise through careful design and testing. Programs have real impacts on society, the economy, and culture that can be both beneficial and harmful. And creating programs raises legal and intellectual-property 知识产权 concerns: programmers often reuse code published as open source 开源 and free to use, but must respect its licence and give credit rather than copy others' work as their own.

    ไทย

    การเลือกการออกแบบมีผลต่อความถูกต้อง ความอ่านง่าย และการนำโค้ดไปใช้ใหม่ การห่อหุ้ม (Encapsulation) – การเก็บข้อมูลให้เป็นส่วนตัวและเปิดเผยผ่านเมทόδเท่านั้น – ช่วยปกป้องสถานะของออบเจกต์จากการใช้งานที่ไม่เหมาะสมและให้คุณสามารถแก้ไขด้านในโดยไม่ทำให้ผู้ใช้คลาสเสียไป การตั้งชื่ออย่างรอบคอบ เมทόδที่มีวัตถุประสงค์เดียว และการทดสอบช่วยลดข้อผิดพลาดได้

    การออกแบบยังมีความรับผิดชอบเกินกว่าแค่โค้ด ความน่าเชื่อถือของระบบ – โปรแกรมที่ทำหน้าที่ตาม的预期โดยไม่ล้มเหลว – เป็นสิ่งที่นักพัฒนาควรเพิ่มสูงสุดผ่านการออกแบบและการทดสอบอย่างพิถีพิถัน โปรแกรมมีผลกระทบจริงต่อสังคม เศรษฐกิจ และวัฒนธรรม ซึ่งอาจเป็น ทั้งประโยชน์และอันตราย และการสร้างโปรแกรมยกเรื่อง กฎหมายและทรัพย์สินทางปัญญา ขึ้นมา: นักพัฒนามักนำโค้ดที่เผยแพร่เป็น code แบบ open source มาใช้ใหม่และใช้ฟรี แต่ต้องเคารพใบอนุญาตและให้เครดิตแทนที่จะลอกผลงานของผู้อื่นมาเป็นของตนเอง

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    Abstraction/əbˈstrækʃn/ Abstraction
    Encapsulation/ɪnˌkæpsjʊˈleɪʃn/ การห่อหุ้มข้อมูล (Encapsulation)
    System reliability/ˈsɪstəm rɪˌlaɪəˈbɪlɪti/ ความน่าเชื่อถือของระบบ
    legal and intellectual-property/ˈliːɡl ænd ˌɪntəˈlektʃuːəl ˈprɒpəti/ ด้านกฎหมายและทรัพย์สินทางปัญญา
    open source/ˈəʊpən sɔːs/ open source
    instance variables/ˈɪnstəns ˈveərɪəblz/ instance variables
    3.3

    The Anatomy of a Class · ⁨กายวิภาคศาสตร์ของคลาส⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 3.3.A: Develop code to designate access and visibility constraints to classes, data, constructors, and methods.

    • 3.3.A.1 Data encapsulation is a technique in which the implementation details of a class are kept hidden from external classes. The keywords public and private affect the access of classes, data, constructors, and methods. The keyword private restricts access to the declaring class, while the keyword public allows access from classes outside the declaring class.
    • 3.3.A.2 In this course, classes are always designated public and are declared with the keyword class.
    • 3.3.A.3 In this course, constructors are always designated public.
    • 3.3.A.4 Instance variables belong to the object, and each object has its own copy of the variable.
    • 3.3.A.5 Access to attributes should be kept internal to the class in order to accomplish encapsulation. Therefore, it is good programming practice to designate the instance variables for these attributes as private unless the class specification states otherwise.
    • 3.3.A.6 Access to behaviors can be internal or external to the class. Methods designated as public can be accessed internally or externally to a class, whereas methods designated as private can only be accessed internally to the class.
    ไทย

    จุดประสงค์การเรียนรู้ 3.3.A: เขียนโค้ดเพื่อกำหนดขอบเขตการเข้าถึงและเงื่อนไขความมองเห็นสำหรับคลาส ข้อมูล ตัวสร้าง (constructors) และเมท็ด

    • 3.3.A.1 การห่อหุ้มข้อมูล (Data encapsulation) เป็นเทคนิคที่ซ่อนรายละเอียดการดำเนินการภายในของคลาสไม่ให้คลาสภายนอกเห็น คีย์เวิร์ด public และ private มีผลต่อการเข้าถึงของคลาส ข้อมูล ตัวสร้าง และเมท็ด คีย์เวิร์ด private จำกัดการเข้าถึงเฉพาะในคลาสผู้ประกาศ ในขณะที่คีย์เวิร์ด public อนุญาตให้เข้าถึงได้จากคลาสอื่นนอกเหนือจากคลาสผู้ประกาศ
    • 3.3.A.2 ในรายวิชานี้ คลาสจะถูกกำหนดให้始终是 public และประกาศด้วยคีย์เวิร์ด class
    • 3.3.A.3 ในรายวิชานี้ ตัวสร้างจะ始终以 public เสมอ
    • 3.3.A.4 ตัวแปรอินสแตนซ์ (Instance variables) เป็นของวัตถุ และแต่ละวัตถุจะมีสำเนาของตัวแปรนั้นเป็นของตัวเอง
    • 3.3.A.5 การเข้าถึงคุณสมบัติควรถูกจำกัดให้อยู่ภายในคลาสเพื่อให้บรรลุหลักการห่อหุ้มข้อมูล ดังนั้นจึงเป็นการปฏิบัติที่ดีในการกำหนดให้ตัวแปรอินสแตนซ์สำหรับคุณสมบัติเหล่านี้เป็น private เว้นแต่สเปกซิกิเคชันของคลาสจะระบุไว้ differently
    • 3.3.A.6 การเข้าถึงพฤติกรรมสามารถเป็นภายในหรือภายนอกคลาส เมท็ดที่กำหนดให้始终是 public สามารถเข้าถึงได้ทั้งภายในและภายนอกคลาส ในขณะที่เมท็ดที่กำหนดให้始终是 private สามารถเข้าถึงได้เฉพาะภายในคลาสเท่านั้น

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    A class has three parts: instance variables 实例变量 (fields – the object's data), constructors (build objects), and methods (behavior). Fields are usually private; methods are usually public:

    ไทย

    คลาสประกอบด้วยสามส่วน: ตัวแปรinstance (fields) – ข้อมูลของออบเจกต์, คอนสตรัคเตอร์ (constructors) – สร้างออบเจกต์, และ เมทόδ (methods) – พฤติกรรม Fields มักจะเป็น private; Metods มักจะเป็น public:

    A class diagram: private attributes and public methods
    A class diagram: private attributes and public methods
    public class Student {
        private String name;      // instance variable
        private int score;
    
        public Student(String n, int s) {   // constructor
            name = n;
            score = s;
        }
        public int getScore() { return score; }   // accessor
    }
    
    แผนภาพ: คลาสเป็นเทมเพลตที่กำหนดวิธีสร้างออบเจกต์ชนิดนั้น
    แผนภาพ: คลาสเป็นเทมเพลตที่กำหนดวิธีสร้างออบเจกต์ชนิดนั้น
    Explore · ⁨สำรวจ⁩

    See an object's fields as boxes · ⁨เห็นฟิลด์ของ object เป็นกล่อง⁩

    A class groups related data (its fields) and methods. Each object gets its own set of field boxes; assigning to one changes that object only. · ⁨class รวมข้อมูลที่เกี่ยวข้อง (fields) และเมทოდเข้าด้วยกัน Each object จะมีชุดกล่องฟิลด์ของตัวเอง; การ assign เข้าไปในกล่องของวัตถุหนึ่ง จะส่งผลต่อวัตถุนั้นเท่านั้น⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    constructor/kənˈstrʌktə/ constructor
    accessor (getter)/əkˈsesə/ accessor (getter)
    3.4

    Constructors · ⁨คอนสตรัคเตอร์⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 3.4.A: Develop code to declare instance variables for the attributes to be initialized in the body of the constructors of a class.

    • 3.4.A.1 An object's state refers to its attributes and their values at a given time and is defined by instance variables belonging to the object. This defines a has-a relationship between the object and its instance variables.
    • 3.4.A.2 A constructor is used to set the initial state of an object, which should include initial values for all instance variables. When a constructor is called, memory is allocated for the object and the associated object reference is returned. Constructor parameters, if specified, provide data to initialize instance variables.
    • 3.4.A.3 When a mutable object is a constructor parameter, the instance variable should be initialized with a copy of the referenced object. In this way, the instance variable does not hold a reference to the original object, and methods are prevented from modifying the state of the original object.
    • 3.4.A.4 When no constructor is written, Java provides a no-parameter constructor, and the instance variables are set to default values according to the data type of the attribute. This constructor is called the default constructor.
    • 3.4.A.5 The default value for an attribute of type int is 0. The default value of an attribute of type double is 0.0. The default value of an attribute of type boolean is false. The default value of a reference type is null.
    ไทย

    จุดประสงค์การเรียนรู้ 3.4.A: เขียนโค้ดเพื่อประกาศตัวแปรอินสแตนซ์สำหรับคุณสมบัติที่จะเริ่มต้นค่าในส่วนของตัวสร้างของคลาส

    • 3.4.A.1 สถานะ (state) ของวัตถุหมายถึงคุณสมบัติและค่าของมัน ณ เวลาหนึ่งๆ และถูกกำหนดโดยตัวแปรอินสแตนซ์ที่เป็นของวัตถุนั้น สิ่งนี้กำหนดความสัมพันธ์แบบ has-a ระหว่างวัตถุกับตัวแปรอินสแตนซ์ของมัน
    • 3.4.A.2 ตัวสร้างใช้เพื่อตั้งค่าเริ่มต้นของวัตถุ ซึ่งควรประกอบด้วยค่าเริ่มต้นสำหรับตัวแปรอินสแตนซ์ทั้งหมด เมื่อมีการเรียกใช้ตัวสร้าง จะมีการจัดสรรหน่วยความจำสำหรับวัตถุและกลับคืนค่าอ้างอิงของวัตถุที่เกี่ยวข้อง พารามิเตอร์ของตัวสร้าง หากมีการระบุ จะให้ข้อมูล用来เริ่มต้นตัวแปรอินสแตนซ์
    • 3.4.A.3 เมื่อวัตถุที่แก้ไขได้ (mutable object) เป็นพารามิเตอร์ของตัวสร้าง ตัวแปรอินสแตนซ์ควรเริ่มต้นด้วยสำเนาของวัตถุที่ถูกอ้างอิง ด้วยวิธีนี้ ตัวแปรอินสแตนซ์จะไม่ถืออ้างอิงไปยังวัตถุต้นฉบับ และป้องกันไม่ให้เมท็ดแก้ไขสถานะของวัตถุต้นฉบับ
    • 3.4.A.4 เมื่อไม่มีการเขียนตัวสร้าง Java จะให้ตัวสร้างที่ไม่มีพารามิเตอร์ และตัวแปรอินสแตนซ์จะถูกตั้งค่าเป็นค่าเริ่มต้นตามชนิดข้อมูลของคุณสมบัติ ตัวสร้างนี้เรียกว่า ตัวสร้างเริ่มต้น (default constructor)
    • 3.4.A.5 ค่าเริ่มต้นของคุณสมบัติที่มีชนิด int คือ 0 ค่าเริ่มต้นของคุณสมบัติที่มีชนิด double คือ 0.0 ค่าเริ่มต้นของคุณสมบัติที่มีชนิด boolean คือ false ค่าเริ่มต้นของชนิดอ้างอิงคือ null

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    A constructor 构造函数 has the same name as the class and no return type. It runs when you write new, and its job is to initialize the fields. A class can have several constructors with different parameter lists (overloading 重载); a no-argument constructor sets defaults.

    ไทย

    คอนสตรัคเตอร์ มี ชื่อเดียวกับคลาส และไม่มีประเภทข้อมูลกลับคืน มันจะทำงานเมื่อคุณเขียน new และหน้าที่ของมันคือการเริ่มต้นค่า fields คลาสสามารถมีคอนสตรัคเตอร์หลายตัวที่มีรายการพารามิเตอร์ต่างกัน (overloading); คอนสตรัคเตอร์ ไม่มีพารามิเตอร์ จะตั้งค่าเริ่มต้น

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    overloading/ˌəʊvəˈləʊdɪŋ/ การโอเวอร์โหลด (Overloading)
    3.5

    Methods: How to Write Them · ⁨เมทόδ: วิธีเขียน⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 3.5.A: Develop code to define behaviors of an object through methods written in a class using primitive values and determine the result of calling these methods.

    • 3.5.A.1 A void method does not return a value. Its header contains the keyword void before the method name.
    • 3.5.A.2 A non-void method returns a single value. Its header includes the return type in place of the keyword void.
    • 3.5.A.3 In non-void methods, a return expression compatible with the return type is evaluated, and the value is returned. This is referred to as return by value.
    • 3.5.A.4 The return keyword is used to return the flow of control to the point where the method or constructor was called. Any code that is sequentially after a return statement will never be executed. Executing a return statement inside a selection or iteration statement will halt the statement and exit the method or constructor.
    • 3.5.A.5 An accessor method allows objects of other classes to obtain a copy of the value of instance variables or class variables. An accessor method is a non-void method.
    • 3.5.A.6 A mutator (modifier) method is a method that changes the values of the instance variables or class variables. A mutator method is often a void method.
    • 3.5.A.7 Methods with parameters receive values through those parameters and use those values in accomplishing the method's task.
    • 3.5.A.8 When an argument is a primitive value, the parameter is initialized with a copy of that value. Changes to the parameter have no effect on the corresponding argument.
    ไทย

    จุดประสงค์การเรียนรู้ 3.5.A: เขียนโค้ดเพื่อกำหนดพฤติกรรมของวัตถุผ่านเมท็ดที่เขียนไว้ในคลาสโดยใช้ค่าพื้นฐาน และกำหนดผลลัพธ์ของการเรียกใช้เมท็ดเหล่านี้

    • 3.5.A.1 เมท็ด void ไม่คืนค่า หัวเมท็ดจะcontains คีย์เวิร์ด void ก่อนชื่อเมท็ด
    • 3.5.A.2 เมท็ดที่ไม่ใช่ void จะคืนค่าเดียว หัวเมท็ดจะ包含 return type แทนที่คีย์เวิร์ด void
    • 3.5.A.3 ในเมท็ดที่ไม่ใช่ void จะประเมินนิพจน์การคืนค่าที่สอดคล้องกับ return type และคืนค่าออก สิ่งนี้เรียกว่า return by value
    • 3.5.A.4 คีย์เวิร์ด return ใช้เพื่อส่งการควบคุมกลับไปยังจุดที่เมท็ดหรือตัวสร้างถูกเรียกใช้ โค้ดใดๆ ที่อยู่ถัดไปจากคำสั่ง return จะไม่เคย被执行 การ执行คำสั่ง return ภายใน selection หรือ iteration statement จะหยุดการทำงานของ statement นั้นและออกจากメท็ดหรือตัวสร้าง
    • 3.5.A.5 เมท็ด accessors อนุญาตให้วัตถุของคลาสอื่นรับสำเนาของค่าตัวแปรอินสแตนซ์หรือตัวแปรคลาส เมท็ด accessor เป็นเมท็ดที่ไม่ใช่ void
    • 3.5.A.6 เมท็ด mutators (modifiers) เป็นเมท็ดที่เปลี่ยนแปลงค่าของตัวแปรอินสแตนซ์หรือตัวแปรคลาส เมท็ด mutator มักจะเป็นเมท็ด void
    • 3.5.A.7 เมท็ดที่มีพารามิเตอร์จะรับค่าผ่านพารามิเตอร์เหล่านั้นและใช้ค่าเหล่านั้นในการดำเนินงานตามหน้าที่ของเมท็ด
    • 3.5.A.8 เมื่ออาร์กิวเมนต์เป็นค่าพื้นฐาน พารามิเตอร์จะถูกเริ่มต้นด้วยสำเนาของค่านั้น การเปลี่ยนแปลงพารามิเตอร์ไม่มีผลต่ออาร์กิวเมนต์ที่เกี่ยวข้อง

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    A method has a signature, a return type, and a body. An accessor (getter) 访问器 returns information without changing the object; a mutator (setter) 修改器 changes a field. A method returning a value must have a return of the right type on every path; a void method returns nothing.

    ไทย

    เมทόδมี signature, ประเภทข้อมูลกลับคืน และ body accessor (getter) ส่งกลับข้อมูลโดยไม่เปลี่ยนออบเจกต์; mutator (setter) เปลี่ยน field เมทόδที่ส่งกลับค่าต้องมี return ที่มีประเภทที่ถูกต้องในทุกเส้นทาง; เมทόδ void ไม่ส่งอะไรกลับมาเลย

    public void setScore(int s) { score = s; }   // mutator
    public String toString() { return name + ": " + score; }
    
    Explore · ⁨สำรวจ⁩

    Follow a method call and its return · ⁨ติดตามการเรียกเมทόδและการส่งคืนค่า⁩

    Calling a method pushes a frame with its parameters; when it hits return, the frame pops and the value goes back to the caller. · ⁨การเรียก method จะเพิ่ม frame ที่มีพารามิเตอร์ลงบนสัก เมื่อเจอ return frame นั้นจะถูกนำออกและค่าถูกส่งกลับไปยังผู้เรียก⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    mutator (setter)/mjuːˈteɪtə/ mutator (setter)
    static (class) variable/ˈstætɪk ˈveərɪəbl/ ตัวแปรคลาส (ตัวแปรแบบสถิต)
    3.6

    Passing and Returning References of an Object · ⁨การส่งและส่งกลับอ้างอิงของออบเจกต์⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 3.6.A: Develop code to define behaviors of an object through methods written in a class using object references and determine the result of calling these methods.

    • 3.6.A.1 When an argument is an object reference, the parameter is initialized with a copy of that reference; it does not create a new independent copy of the object. If the parameter refers to a mutable object, the method or constructor can use this reference to alter the state of the object. It is good programming practice to not modify mutable objects that are passed as parameters unless required in the specification.
    • 3.6.A.2 When the return expression evaluates to an object reference, the reference is returned, not a reference to a new copy of the object.
    • 3.6.A.3 Methods cannot access the private data and methods of a parameter that holds a reference to an object unless the parameter is the same type as the method's enclosing class.
    ไทย

    จุดประสงค์การเรียนรู้ 3.6.A: เขียนโค้ดเพื่อกำหนดพฤติกรรมของวัตถุผ่านเมท็ดที่เขียนไว้ในคลาสโดยใช้อ้างอิงวัตถุ และกำหนดผลลัพธ์ของการเรียกใช้เมท็ดเหล่านี้

    • 3.6.A.1 เมื่ออาร์กิวเมนต์เป็นอ้างอิงถึงวัตถุ (object reference) พารามิเตอร์จะถูกเริ่มต้นด้วยสำเนาของอ้างอิงนั้น; ไม่มีการสร้างสำเนาอิสระใหม่ของวัตถุ หากพารามิเตอร์อ้างอิงถึงวัตถุที่แก้ไขได้ (mutable object) เมธอดหรือคอนสตรักเตอร์สามารถใช้ อ้างอิงนี้ในการเปลี่ยนแปลงสถานะของวัตถุ เป็นปฏิบัติที่ดีในการเขียนโปรแกรมที่ไม่ควรแก้ไขวัตถุที่แก้ไขได้ซึ่งส่งมาเป็นพารามิเตอร์ เว้นแต่จะระบุไว้ในข้อกำหนด
    • 3.6.A.2 เมื่อนิพจน์การคืนค่า (return expression) ประเมินผลเป็นอ้างอิงถึงวัตถุ จะมีการคืนค่าอ้างอิงนั้น ไม่ใช่การคืนค่าอ้างอิงไปยังสำเนาใหม่ของวัตถุ
    • 3.6.A.3 เมธอดไม่สามารถเข้าถึงข้อมูลส่วนตัวและเมธอดส่วนตัวของพารามิเตอร์ที่ถืออ้างอิงถึงวัตถุได้ เว้นแต่พารามิเตอร์จะมีประเภทเดียวกันกับคลาสที่ห่อหุ้มเมธอด

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English
    = copies the reference, not the object

    When you pass an object to a method, Java copies the reference, so the method acts on the same object – changes to its fields are visible to the caller. (Primitives are copied by value, so changes to them are not.) A method can also return a reference to an object. Because a String is immutable, passing one is safe; passing a mutable object lets a method change it.

    Exam skill: know that mutating an object's fields inside a method affects the original, but reassigning the parameter (param = new...) does not affect the caller.

    Worked example. Suppose s is a Student with score 50, and we call tweak(s):

    Line (1) changes the object s points to, so the caller now sees 100. Line (2) makes the method's own copy of the reference point at a fresh object – the caller's s is untouched – and line (3) affects only that new object. After the call, s.getScore() is 100: the mutation stuck, the reassignment did not.

    ไทย
    = copies the reference, not the object

    เมื่อคุณส่งออบเจกต์เข้าเมทόδ Java จะคัดลอก reference ดังนั้นเมทόδจึงกระทำกับ ออบเจกต์เดียวกัน – การเปลี่ยนแปลงใน fields ของมันจะมองเห็นได้โดยผู้เรียก (Primitives ถูกคัดลอกด้วยค่า ดังนั้นการเปลี่ยนแปลงจึงไม่เห็น) เมทόδยังสามารถ ส่งกลับ reference ไปยังออบเจกต์ได้ เนื่องจาก String เป็นสิ่งที่ไม่สามารถเปลี่ยนแปลงได้ การส่งมันจึงปลอดภัย; การส่งออบเจกต์ที่เปลี่ยนแปลงได้会让メソッドเปลี่ยนมันได้

    Java ส่งค่าด้วยค่า: เมทόδรับสำเนา; การส่งจริงด้วย reference ซึ่ง Java ไม่มี จะช่วยให้เมทόδกำหนดค่าตัวแปรของผู้เรียกใหม่ได้
    Java ส่งค่าด้วย ค่าเสมอ (ซ้าย): เมทόδได้รับสำเนาของ reference การส่งจริงด้วย reference (ขวา) – ซึ่ง Java ไม่มี – จะช่วยให้เมทόδกำหนดค่าตัวแปรของผู้เรียกใหม่ได้.

    ทักษะสอบ: ต้องรู้ว่าการทำ mutate ใน fields ของออบเจกต์ภายในเมทόδจะมีผลต่อของเดิม แต่ การกำหนดค่าใหม่ ให้พารามิเตอร์ (param = new...) จะไม่มีผลต่อผู้เรียก

    ตัวอย่างฝึกฝน. สมมติว่า s เป็น Student ที่คะแนน 50, และเราเรียก tweak(s):

    public static void tweak(Student a) {
        a.setScore(100);          // (1) mutates the shared object
        a = new Student("Z", 0);  // (2) repoints the local copy only
        a.setScore(5);            // (3) changes only the new local object
    }
    

    บรรทัด (1) เปลี่ยนแปลงออบเจกต์ที่ s ชี้ไปยัง ทำให้ผู้เรียกเห็น 100 บรรทัด (2) ทำให้สำเนาของ reference ภายในเมทόδชี้ไปยังออบเจกต์ใหม่ – s ของผู้เรียกไม่ถูกแตะต้อง – และบรรทัด (3) ส่งผลเฉพาะต่อออบเจกต์ใหม่นั้น หลังการเรียก s.getScore() คือ 100: การ mutate ติดไปแต่การกำหนดค่าใหม่ไม่ได้

    3.7

    Class Variables and Class Methods · ⁨ตัวแปรคลาสและเมทόδคลาส⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 3.7.A: Develop code to define behaviors of a class through class methods.

    • 3.7.A.1 Class methods cannot access or change the values of instance variables or call instance methods without being passed an instance of the class via a parameter.
    • 3.7.A.2 Class methods can access or change the values of class variables and can call other class methods.

    Learning Objective 3.7.B: Develop code to declare the class variables that belong to the class.

    • 3.7.B.1 Class variables belong to the class, with all objects of a class sharing a single copy of the class variable. Class variables are designated with the static keyword before the variable type.
    • 3.7.B.2 Class variables that are designated public are accessed outside of the class by using the class name and the dot operator, since they are associated with a class, not objects of a class.
    • 3.7.B.3 When a variable is declared final, its value cannot be modified.
    ไทย

    วัตถุประสงค์การเรียนรู้ 3.7.A: เขียนโค้ดเพื่อกำหนดพฤติกรรมของคลาสผ่านเมธอดคลาส

    • 3.7.A.1 เมธอดคลาสไม่สามารถเข้าถึงหรือเปลี่ยนแปลงค่าของตัวแปรอินสแตนซ์หรือเรียกเมธอดอินสแตนซ์ได้โดยไม่ได้รับอินสแตนซ์ของคลัสมาทางพารามิเตอร์
    • 3.7.A.2 เมธอดคลาสสามารถเข้าถึงหรือเปลี่ยนแปลงค่าของตัวแปรคลาสและสามารถเรียกเมธอดคลาสอื่นๆ ได้

    วัตถุประสงค์การเรียนรู้ 3.7.B: เขียนโค้ดเพื่อประกาศตัวแปรคลาสที่เป็นของคลาส

    • 3.7.B.1 ตัวแปรคลาสเป็นของคลาส โดยวัตถุทั้งหมดของคลัสจะแบ่งปันสำเนาเดียวของตัวแปรคลาส ตัวแปรคลาสจะถูกกำหนดโดยคำสำคัญ static ก่อนชนิดของตัวแปร
    • 3.7.B.2 ตัวแปรคลาสที่กำหนดด้วย public สามารถเข้าถึงภายนอกคลาสโดยใช้ชื่อคลาสและตัวดำเนินการจุด เนื่องจากมีความเกี่ยวข้องกับคลาส ไม่ใช่วัตถุของคลาส
    • 3.7.B.3 เมื่อตัวแปรถูกประกาศด้วย final ค่าของมันไม่สามารถแก้ไขได้

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English
    static vs instance fields

    A static (class) variable 类变量, marked static, is shared by all objects of the class – one copy total (e.g. a counter of how many objects exist). A static method belongs to the class and cannot use instance fields directly. Access them by class name: Student.getCount().

    ไทย
    static เปรียบเทียบกับ instance fields

    ตัวแปร static (class),中标记 static, ถูกแชร์โดย ทุก ออบเจกต์ของคลาส – มีเพียงสำเนาเดียว (เช่น ตัวนับว่ามีออบเจกต์อยู่กี่ตัว) เมทόδ static เป็นของคลาสและไม่สามารถใช้ instance fields โดยตรง เข้าถึงได้โดยชื่อคลาส: Student.getCount().

    3.8

    Scope and Access · ⁨ขอบเขตและการเข้าถึง⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 3.8.A: Explain where variables can be used in the code.

    • 3.8.A.1 Local variables are variables declared in the headers or bodies of blocks of code. Local variables can only be accessed in the block in which they are declared. Since constructors and methods are blocks of code, parameters to constructors or methods are also considered local variables. These variables may only be used within the constructor or method and cannot be declared to be public or private.
    • 3.8.A.2 When there is a local variable or parameter with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable within the body of the constructor or method.
    ไทย

    วัตถุประสงค์การเรียนรู้ 3.8.A: อธิบายว่าตัวแปรสามารถใช้ใน何处ในโค้ด

    • 3.8.A.1 ตัวแปรท้องถิ่น คือตัวแปรที่ประกาศในหัวหรือตัวบล็อกของโค้ด ตัวแปรท้องถิ่นสามารถเข้าถึงได้เฉพาะในบล็อกที่ประกาศไว้เท่านั้น เนื่องจากคอนสตรักเตอร์และเมธอดเป็นบล็อกของโค้ด พารามิเตอร์ของคอนสตรักเตอร์หรือเมธอดจึงถือเป็นตัวแปรท้องถิ่นด้วย ตัวแปรเหล่านี้สามารถใช้ได้เฉพาะภายในคอนสตรักเตอร์หรือเมธอดเท่านั้น และไม่สามารถประกาศให้เป็น public หรือ private ได้
    • 3.8.A.2 เมื่อมีตัวแปรท้องถิ่นหรือพารามิเตอร์ที่มีชื่อกับตัวแปรอินสแตนซ์เหมือนกัน ชื่อตัวแปรจะอ้างอิงไปยังตัวแปรท้องถิ่นแทนที่จะเป็นตัวแปรอินสแตนซ์ภายในตัวบล็อคของคอนสตรักเตอร์หรือเมธอด

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    Scope 作用域 is where a name is visible. A local variable declared in a method exists only inside it; a parameter exists only in its method; an instance variable is visible throughout the object. Access modifiers control visibility across classes: private (this class only) versus public (anywhere). Local variables shadow fields of the same name – a source of bugs.

    ไทย

    Scope คือตำแหน่งที่ชื่อปรากฏให้เห็น local variable ที่ประกาศในเมท่อมจะมีอยู่เฉพาะภายในนั้น; parameter มีอยู่เฉพาะในเมทოდ它那; instance variable เห็นได้ตลอดทั้งออบเจกต์ Access modifiers ควบคุมการมองเห็นข้ามคลาส: private (คลาสนี้เท่านั้น) เทียบกับ public (ทุกที่) Local variables shadow fields ที่มีชื่อเดียวกัน – ซึ่งเป็นแหล่งกำเนิดของข้อผิดพลาด

    ตัวแปร global เห็นได้ทุกที่; local variable เห็นได้เฉพาะภายใน block ของมัน
    ตัวแปร global เห็นได้ทุกที่; local variable เห็นได้เฉพาะภายใน block ของมัน
    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    Scope/skəʊp/ ขอบเขต
    3.9

    The this Keyword · ⁨คำว่า this⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 3.9.A: Develop code for expressions that are self-referencing and determine the result of these expressions.

    • 3.9.A.1 Within an instance method or a constructor, the keyword this acts as a special variable that holds a reference to the current object—the object whose method or constructor is being called.
    • 3.9.A.2 The keyword this can be used to pass the current object as an argument in a method call.
    • 3.9.A.3 Class methods do not have a this reference.
    ไทย

    วัตถุประสงค์การเรียนรู้ 3.9.A: เขียนโค้ดสำหรับนิพจน์ที่อ้างอิงถึงตนเองและกำหนดผลลัพธ์ของนิพจน์เหล่านี้

    • 3.9.A.1 ภายในเมธอดอินสแตนซ์หรือคอนสตรักเตอร์ คำนำหน้า this ทำหน้าที่เป็นตัวแปรพิเศษที่ถืออ้างอิงถึงวัตถุปัจจุบัน—คือวัตถุwhoseメソッドหรือコンストラクタが呼ばれています。
    • 3.9.A.2 คำนำหน้า this สามารถใช้เพื่อส่งวัตถุปัจจุบันเป็นอาร์กิวเมนต์ในการเรียกเมธอด
    • 3.9.A.3 เมธอดคลาสไม่มีอ้างอิงถึง this

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    this is a reference to the current object. Use it to tell a field apart from a parameter with the same name, or to call another method of the same object:

    Exam skill: when a constructor or setter's parameter has the same name as a field, you must write this.field = param – without this, the assignment does nothing useful.

    ไทย

    this อ้างถึง วัตถุปัจจุบัน ใช้เพื่อแยกแยะฟิลด์ออกจากพารามิเตอร์ที่มีชื่อเดียวกัน หรือเรียกเมทόδอื่นของวัตถุเดียวกัน:

    public Student(String name, int score) {
        this.name = name;      // this.name is the field; name is the parameter
        this.score = score;
    }
    

    ทักษะสำหรับการสอบ: เมื่อพารามิเตอร์ของคอนสตรัคเตอร์หรือเซตเตอร์มีชื่อเดียวกับฟิลด์ คุณ ต้อง เขียน this.field = param – หากไม่มี this การกำหนดค่าจะไม่ได้ผลอะไรเลย

    3.9

    Exam tips · ⁨ข้อแนะนำสำหรับการสอบ⁩

    English
    • Design with methods and classes: encapsulate data as private fields and expose behaviour through public methods.
    • Know the difference between an object and its class, and that objects are passed by value — the parameter gets a copy of the reference, so a method can change the object's state, but reassigning the parameter does not affect the caller (Java has no pass-by-reference).
    • Traverse arrays and ArrayLists safely — size is length vs .size(), and removing during a loop shifts indices.
    • Trace a recursive method to determine its result: find the base case first, then follow each recursive call to its return value (writing recursive code is outside the exam's scope).
    • Recognise inheritance vocabulary — superclass, subclass, method overriding, and that every class is a subclass of Object (designing and implementing inheritance is outside the exam's scope).
    ไทย
    • ออกแบบด้วยเมทόδและคลาส: ห่อหุ้มข้อมูลเป็นฟิลด์ private และเปิดใช้งานพฤติกรรมผ่านเมทόδสาธารณะ
    • รู้ความแตกต่างระหว่าง วัตถุ กับ คลาส ของมัน และว่าวัตถุถูกส่งเข้าโดย ค่า — พารามิเตอร์รับสำเนาของรีเฟอเรนซ์ ดังนั้นเมทόδสามารถเปลี่ยนสถานะของวัตถุได้ แต่การกำหนดค่าใหม่ให้พารามิเตอร์จะไม่ส่งผลต่อผู้เรียก (Java ไม่มีระบบส่งเข้าโดยรีเฟอเรนซ์)
    • ท่องผ่านอาร์เรย์และ ArrayList อย่างปลอดภัย — ขนาดคือ length เทียบกับ .size() และการลบองค์ประกอบในระหว่างลูปจะทำให้ดัชนีเลื่อน
    • ติดตาม เมทόδแบบ récursive เพื่อกำหนดผลลัพธ์: หาเคสฐานก่อน แล้วตามการเรียก récursive แต่ละครั้งไปยังค่าที่กลับคืนมา (การเขียนโค้ด récursiveอยู่นอกขอบเขตของการสอบ)
    • จำคำศัพท์เกี่ยวกับ การสืบทอด ได้ — ซูเปอร์คลาส, สับคลาส, การทับเมทόδ และว่าทุกคลาสเป็นสับคลาสของ Object (การออกแบบและดำเนินการสืบทอดอยู่นอกขอบเขตของการสอบ)
  • 4

    Data Collections · ⁨การเก็บรวบรวมข้อมูล⁩

    Watch lesson · ⁨ดูบทเรียน⁩
    4.1

    The Ethics of Collecting Data · ⁨จริยธรรมในการเก็บรวบรวมข้อมูล⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.1.A: Explain the risks to privacy from collecting and storing personal data on computer systems.

    • 4.1.A.1 When using a computer, personal privacy is at risk. When developing new programs, programmers should attempt to safeguard the personal privacy of the user.

    Learning Objective 4.1.B: Explain the importance of recognizing data quality and potential issues when using a data set.

    • 4.1.B.1 Algorithmic bias describes systemic and repeated errors in a program that create unfair outcomes for a specific group of users.
    • 4.1.B.2 Programmers should be aware of the data set collection method and the potential for bias when using this method before using the data to extrapolate new information or drawing conclusions.
    • 4.1.B.3 Some data sets are incomplete or contain inaccurate data. Using such data in the development or use of a program can cause the program to work incorrectly or inefficiently.

    Learning Objective 4.1.C: Identify an appropriate data set to use in order to solve a problem or answer a specific question.

    • 4.1.C.1 Contents of a data set might be related to a specific question or topic and might not be appropriate to give correct answers or extrapolate information for a different question or topic.
    ไทย

    วัตถุประสงค์การเรียนรู้ 4.1.A: อธิบายความเสี่ยงต่อความเป็นส่วนตัวจากการเก็บรวบรวมและจัดเก็บข้อมูลส่วนบุคคลบนระบบคอมพิวเตอร์

    • 4.1.A.1 เมื่อใช้คอมพิวเตอร์ ความเป็นส่วนตัวส่วนบุคคลอาจเสี่ยงต่อการถูกคุกคาม เมื่อพัฒนาโปรแกรมใหม่ๆ นักเขียนโปรแกรมควรพยายามปกป้องความเป็นส่วนตัวของผู้ใช้

    วัตถุประสงค์การเรียนรู้ 4.1.B: อธิบายความสำคัญของการตระหนักถึงคุณภาพของข้อมูลและปัญหาที่อาจเกิดขึ้นเมื่อใช้ชุดข้อมูล

    • 4.1.B.1 ความลำเอียงเชิงอัลกอริทึม (Algorithmic bias) อธิบายถึงความผิดพลาดในระบบและซ้ำซ้อนในโปรแกรมที่ก่อให้เกิดผลลัพธ์ที่ไม่เป็นธรรมต่อกลุ่มผู้ใช้บางกลุ่ม
    • 4.1.B.2 นักเขียนโปรแกรมควรตระหนักถึงวิธีการเก็บรวบรวมชุดข้อมูลและความเสี่ยงของความลำเอียงก่อน使用该ข้อมูลเพื่อสรุปข้อมูลใหม่หรือดึงข้อสรุป
    • 4.1.B.3 บางชุดข้อมูลไม่สมบูรณ์หรือมีข้อมูลที่ไม่ถูกต้อง การใช้ข้อมูลเช่นนี้ในการพัฒนาหรือใช้งานโปรแกรมอาจทำให้โปรแกรมทำงานผิดพลาดหรือไม่มีประสิทธิภาพ

    วัตถุประสงค์การเรียนรู้ 4.1.C: ระบุชุดข้อมูลที่เหมาะสมเพื่อใช้ในการแก้ปัญหาหรือตอบคำถามเฉพาะ

    • 4.1.C.1 เนื้อหาของชุดข้อมูลอาจเกี่ยวข้องกับคำถามหรือหัวข้อเฉพาะและอาจไม่เหมาะสมในการให้คำตอบที่ถูกต้องหรือสรุปข้อมูลสำหรับคำถามหรือหัวข้ออื่น

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    Programs that gather data raise questions of privacy 隐私 and consent 同意. Collect only what is needed, protect it, and be honest about its use. Data can carry bias 偏见 if it does not represent everyone fairly, leading to unfair results – a responsibility that comes with storing information.

    ไทย
    ตู้เซิร์ฟเวอร์ในศูนย์ข้อมูล — ข้อมูลขนาดใหญ่ยกประเด็นทางจริยธรรมเกี่ยวกับการเก็บรวบรวมและการใช้
    ตู้เซิร์ฟเวอร์ในศูนย์ข้อมูล — ข้อมูลขนาดใหญ่ยกประเด็นทางจริยธรรมเกี่ยวกับการเก็บรวบรวมและการใช้

    โปรแกรมที่เก็บรวบรวมข้อมูลก่อให้เกิดคำถามเรื่อง ความเป็นส่วนตัว และ ความยินยอม เก็บเฉพาะสิ่งที่จำเป็น ปกป้องไว้ และrecursive sobre其真实性การใช้ ข้อมูลอาจมี อคติ หากไม่สะท้อนทุกคนอย่างเป็นธรรม นำไปสู่ผลลัพธ์ที่ไม่ยุติธรรม — ซึ่งเป็นความรับผิดชอบที่มาพร้อมกับการจัดเก็บข้อมูล

    4.2

    Why We Need Data Structures · ⁨ทำไมเราจึงต้องใช้โครงสร้างข้อมูล⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.2.A: Represent patterns and algorithms that involve data sets found in everyday life using written language or diagrams.

    • 4.2.A.1 A data set is a collection of specific pieces of information or data.
    • 4.2.A.2 Data sets can be manipulated and analyzed to solve a problem or answer a question. When analyzing data sets, values within the set are accessed and utilized one at a time and then processed according to the desired outcome.
    • 4.2.A.3 Data can be represented in a diagram by using a chart or table. This visual can be used to plan the algorithm that will be used to manipulate the data.
    ไทย

    วัตถุประสงค์การเรียนรู้ 4.2.A: แสดงรูปแบบและอัลกอริทึมที่เกี่ยวข้องกับชุดข้อมูลที่พบในชีวิตประจำวันโดยใช้ภาษาเขียนหรือแผนภาพ

    • 4.2.A.1 ชุดข้อมูล (Data set) คือกลุ่มของข้อมูลหรือชิ้นส่วนข้อมูลเฉพาะเจาะจง
    • 4.2.A.2 ชุดข้อมูลสามารถถูกจัดการและวิเคราะห์เพื่อแก้ปัญหาหรือตอบคำถาม ในการวิเคราะห์ชุดข้อมูล ค่าต่างๆ ภายในชุดจะถูกเข้าถึงและใช้งานทีละค่า จากนั้นจะประมวลผลตามผลลัพธ์ที่ต้องการ
    • 4.2.A.3 ข้อมูลสามารถแสดงในรูปแบบแผนภาพโดยใช้ตารางหรือกราฟ แผนภาพนี้สามารถใช้วางแผนอัลกอริทึมที่จะนำไปใช้จัดการข้อมูล

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    A single variable holds one value; real problems need to store many related values – a class roster, pixels, sensor readings. A data structure 数据结构 organizes a collection so we can store, find, and process items efficiently. The AP course uses three: the array, the ArrayList, and the 2D array.

    ไทย
    ตู้เอกสาร: โครงสร้างข้อมูลจัดเก็บหลายค่าภายใต้ชื่อเดียวเพื่อให้อัลกอริทึมประมวลผลได้
    ตู้เอกสาร: โครงสร้างข้อมูลจัดเก็บหลายค่าภายใต้ชื่อเดียวเพื่อให้อัลกอริทึมประมวลผลได้

    ตัวแปรหนึ่งตัวเก็บค่าได้เพียงหนึ่งค่า ปัญหาจริงต้องการจัดเก็บ หลาย ค่าที่เกี่ยวข้องกัน — รายชื่อนักเรียน, พิกเซล, ค่าอ่านเซ็นเซอร์ โครงสร้างข้อมูล จัดกลุ่มโครงสร้างข้อมูลเพื่อให้เราจัดเก็บ ค้นหา และประมวลผลรายการได้อย่างมีประสิทธิภาพ หลักสูตร AP ใช้สามอย่างคือ อาร์เรย์, ArrayList และ อาร์เรย์ 2 มิติ

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    array/əˈreɪ/ อาร์เรย์
    Traverse/trəˈvɜːs/ การ traversal
    ArrayList/əˈreɪ lɪst/ ArrayList
    4.3

    Making and Reading an Array · ⁨การสร้างและอ่านอาร์เรย์⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.3.A: Develop code used to represent collections of related data using one-dimensional (1D) array objects.

    • 4.3.A.1 An array stores multiple values of the same type. The values can be either primitive values or object references.
    • 4.3.A.2 The length of an array is established at the time of creation and cannot be changed. The length of an array can be accessed through the length attribute.
    • 4.3.A.3 When an array is created using the keyword new, all of its elements are initialized to the default values for the element data type. The default value for int is 0, for double is 0.0, for boolean is false, and for a reference type is null.
    • 4.3.A.4 Initializer lists can be used to create and initialize arrays.
    • 4.3.A.5 Square brackets [ ] are used to access and modify an element in a 1D array using an index.
    • 4.3.A.6 The valid index values for an array are 0 through one less than the length of the array, inclusive. Using an index value outside of this range will result in an ArrayIndexOutOfBoundsException.
    ไทย

    วัตถุประสงค์การเรียนรู้ 4.3.A: พัฒนาโค้ด用於表示由一维(1D)数组对象组成的相关数据集合

    • 4.3.A.1 แอรรے (Array) เก็บหลายค่าที่มีประเภทเดียวกัน ค่าเหล่านี้可以是基本类型或对象引用
    • 4.3.A.2 ความยาวของแอรรےถูกกำหนดตอนสร้างและไม่สามารถเปลี่ยนได้ ความยาวของแอรรےสามารถเข้าถึงผ่านคุณสมบัติ length
    • 4.3.A.3 เมื่อสร้างแอรรےโดยใช้คีย์เวิร์ด new องค์ประกอบทั้งหมดจะถูกเริ่มต้นด้วยค่าเริ่มต้นสำหรับประเภทข้อมูลขององค์ประกอบ ค่าเริ่มต้นสำหรับ int คือ 0, สำหรับ double คือ 0.0, สำหรับ boolean คือ false, และสำหรับประเภทอ้างอิงคือ null
    • 4.3.A.4 สามารถใช้ List ตัวเริ่มต้น (Initializer lists) เพื่อสร้างและเริ่มต้นค่าแอรรے
    • 4.3.A.5 ใช้วงเล็บเหลี่ยม [ ] ในการเข้าถึงและแก้ไของค์ประกอบใน 1D Array โดยใช้ Index
    • 4.3.A.6 ค่า Index ที่ถูกต้องสำหรับแอรรےคือ 0 ถึงความยาวของแอรรےลบ 1 (รวมค่าทั้งสอง) การใช้ค่า Index นอกช่วงนี้จะส่งผลให้เกิด ArrayIndexOutOfBoundsException

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    An array 数组 is a fixed-size, ordered collection of same-type values. Indices run from 0 to length - 1:

    Accessing an index outside 0..length-1 throws an ArrayIndexOutOfBoundsException.

    ไทย

    อาร์เรย์ คือโครงสร้างข้อมูลที่จัดเรียงลำดับขนาดคงที่ของค่าชนิดเดียวกัน ดัชนีเริ่มจาก 0 ไปจนถึง length - 1:

    อาร์เรย์หนึ่งมิติ (รายการ) พร้อมดัชนีและขอบเขต
    อาร์เรย์หนึ่งมิติ (รายการ) พร้อมดัชนีและขอบเขต
    int[] nums = new int[5];        // five zeros
    int[] vals = {3, 1, 4, 1, 5};   // initialized
    int first = vals[0];            // 3
    int n = vals.length;            // 5 (a field, not a method)
    

    การเข้าถึงดัชนีนอกเหนือจาก 0..length-1 จะเกิดข้อผิดพลาด ArrayIndexOutOfBoundsException

    4.4

    Visiting Every Element of an Array · ⁨การเยี่ยมชมแต่ละองค์ประกอบของอาร์เรย์⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.4.A: Develop code used to traverse the elements in a 1D array and determine the result of these traversals.

    • 4.4.A.1 Traversing an array is when repetition statements are used to access all or an ordered sequence of elements in an array.
    • 4.4.A.2 Traversing an array with an indexed for loop or while loop requires elements to be accessed using their indices.
    • 4.4.A.3 An enhanced for loop header includes a variable, referred to as the enhanced for loop variable. For each iteration of the enhanced for loop, the enhanced for loop variable is assigned a copy of an element without using its index.
    • 4.4.A.4 Assigning a new value to the enhanced for loop variable does not change the value stored in the array.
    • 4.4.A.5 When an array stores object references, the attributes can be modified by calling methods on the enhanced for loop variable. This does not change the object references stored in the array.
    • 4.4.A.6 Code written using an enhanced for loop to traverse elements in an array can be rewritten using an indexed for loop or a while loop.
    ไทย

    จุดประสงค์การเรียนรู้ 4.4.A: พัฒนาโค้ดที่ใช้สำรวจองค์ประกอบใน Arroz 1D และกำหนดผลลัพธ์จากการสำรวจเหล่านั้น

    • 4.4.A.1 การสำรวจแอรรے คือการใช้คำสั่งทำซ้ำเพื่อเข้าถึงองค์ประกอบทั้งหมดหรือลำดับขององค์ประกอบในแอรรے
    • 4.4.A.2 การสำรวจแอรรےด้วยวงลูป for แบบมี Index หรือ while loop ต้องเข้าถึงองค์ประกอบโดยใช้ Indices ของมัน
    • 4.4.A.3 Header ของ enhanced for loop รวมถึงตัวแปร ซึ่งเรียกว่าตัวแปร enhanced for loop สำหรับแต่ละ Iteration ของ enhanced for loop, ตัวแปร enhanced for loop จะได้รับค่า Copy ขององค์ประกอบโดยไม่ใช้ Index ของมัน
    • 4.4.A.4 การกำหนดค่าใหม่ให้กับตัวแปร enhanced for loop ไม่ทำให้ค่าที่จัดเก็บใน Arroz เปลี่ยนแปลง
    • 4.4.A.5 เมื่อ Arroz เก็บ Object References, Attributes สามารถแก้ไขได้โดยการเรียก Methods บนตัวแปร enhanced for loop สิ่งนี้ไม่ทำให้ Object References ที่จัดเก็บใน Arroz เปลี่ยนแปลง
    • 4.4.A.6 โค้ดที่เขียนโดยใช้ enhanced for loop เพื่อสำรวจองค์ประกอบใน Arroz สามารถเขียน ulangโดยใช้ Indexed for loop หรือ while loop ได้

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    Traverse 遍历 an array with a for loop (gives the index) or an enhanced for / for-each loop (gives each value, read-only):

    ไทย

    ท่องผ่าน อาร์เรย์ด้วยลูป for (ให้ดัชนี) หรือลูป enhanced for / for-each (ให้แต่ละค่า, อ่านเท่านั้น):

    for (int i = 0; i < a.length; i++) { a[i] *= 2; }   // can modify
    for (int v : a) { System.out.println(v); }          // read each value
    
    4.5

    Standard Array Algorithms · ⁨อัลกอริทึมมาตรฐานสำหรับอาร์เรย์⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.5.A: Develop code for standard and original algorithms for a particular context or specification that involves arrays and determine the result of these algorithms.

    • 4.5.A.1 There are standard algorithms that utilize array traversals to:
      • determine a minimum or maximum value
      • compute a sum or average
      • determine if at least one element has a particular property
      • determine if all elements have a particular property
      • determine the number of elements having a particular property
      • access all consecutive pairs of elements
      • determine the presence or absence of duplicate elements
      • shift or rotate elements left or right
      • reverse the order of the elements
    ไทย

    จุดประสงค์การเรียนรู้ 4.5.A: เขียนโค้ดสำหรับอัลกอริทึมมาตรฐานและอัลกอริทึมต้นฉบับสำหรับบริบทหรือสเปกซิฟิกชันเฉพาะที่เกี่ยวข้องกับอาร์เรย์ และระบุผลลัพธ์ของอัลกอริทึมเหล่านี้

    • 4.5.A.1 มีอัลกอริทึมมาตรฐานที่ใช้การ traversal ของอาร์เรย์เพื่อ:
      • หาค่าต่ำสุดหรือสูงสุด
      • คำนวณผลรวมหรือค่าเฉลี่ย
      • ตรวจสอบว่ามีอย่างน้อยหนึ่งองค์ประกอบมีคุณสมบัติเฉพาะหรือไม่
      • ตรวจสอบว่ามีองค์ประกอบทั้งหมดมีคุณสมบัติเฉพาะหรือไม่
      • หานจำนวนองค์ประกอบที่มีคุณสมบัติเฉพาะ
      • เข้าถึงคู่ขององค์ประกอบที่ต่อเนื่องกันทั้งหมด
      • ตรวจสอบการมีอยู่หรือขาดหายขององค์ประกอบซ้ำ
      • เลื่อนหรือหมุนองค์ประกอบไปทางซ้ายหรือขวา
      • สลับลำดับขององค์ประกอบ

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    Master these patterns: compute a sum or average, find the max/min, count items meeting a condition, check for a duplicate, and reverse or shift elements. Each is a traversal with a running result:

    ไทย

    ทำให้อยู่ในใจในแพทเทิร์นเหล่านี้: คำนวณ ผลรวม หรือ ค่าเฉลี่ย, ค้นหา ค่าสูงสุด/ต่ำสุด, นับ รายการที่ตรงตามเงื่อนไข, ตรวจสอบหา ค่าซ้ำ และ ย้อนกลับ หรือ เลื่อน องค์ประกอบ แต่ละอันเป็นการท่องผ่านพร้อมผลลัพธ์สะสม:

    int sum = 0;
    for (int v : a) sum += v;
    double avg = (double) sum / a.length;
    
    4.6

    Reading Data from a Text File · ⁨การอ่านข้อมูลจากไฟล์ข้อความ⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.6.A: Develop code to read data from a text file.

    • 4.6.A.1 A file is storage for data that persists when the program is not running. The data in a file can be retrieved during program execution.
    • 4.6.A.2 A file can be connected to the program using the File and Scanner classes.
    • 4.6.A.3 A file can be opened by creating a File object, using the name of the file as the argument of the constructor.
      • File(String str) is the File constructor that accepts a String file name to open for reading, where str is the pathname for the file.
    • 4.6.A.4 When using the File class, it is required to indicate what to do if the file with the provided name cannot be opened. One way to accomplish this is to add throws IOException to the header of the method that uses the file. If the file name is invalid, the program will terminate.
    • 4.6.A.5 The File and IOException classes are part of the java.io package. An import statement must be used to make these classes available for use in the program.
    • 4.6.A.6 The following Scanner methods and constructor—including what they do and when they are used—are part of the Java Quick Reference:
      • Scanner(File f) is the Scanner constructor that accepts a File for reading.
      • int nextInt() returns the next int read from the file or input source if available. If the next int does not exist or is out of range, it will result in an InputMismatchException.
      • double nextDouble() returns the next double read from the file or input source. If the next double does not exist, it will result in an InputMismatchException.
      • boolean nextBoolean() returns the next boolean read from the file or input source. If the next boolean does not exist, it will result in an InputMismatchException.
      • String nextLine() returns the next line of text as a String read from the file or input source; can return the empty string if called immediately after another Scanner method that is reading from the file or input source.
      • String next() returns the next String read from the file or input source.
      • boolean hasNext() returns true if there is a next item to read in the file or input source; returns false otherwise.
      • void close() closes this scanner.
      • Exclusion statement: Accepting input from the keyboard is outside the scope of the AP Computer Science A course and exam.
    • 4.6.A.7 Using nextLine and the other Scanner methods together on the same input source sometimes requires code to adjust for the methods' different ways of handling whitespace.
      • Exclusion statement: Writing or analyzing code that uses both nextLine and other Scanner methods on the same input source is outside the scope of the AP Computer Science A course and exam.
    • 4.6.A.8 The following additional String method—including what it does and when it is used—is part of the Java Quick Reference:
      • String[] split(String del) returns a String array where each element is a substring of this String, which has been split around matches of the given expression del.
      • Exclusion statement: The parameter del uses a format called a regular expression. Writing or analyzing code that uses any of the special properties of regular expressions (e.g., \\*, \\.) is outside the scope of the AP Computer Science A course and exam.
    • 4.6.A.9 A while loop can be used to detect if the file still contains elements to read by using the hasNext method as the condition of the loop.
    • 4.6.A.10 A file should be closed when the program is finished using it. The close method from Scanner is called to close the file.
    ไทย

    จุดประสงค์การเรียนรู้ 4.6.A: เขียนโค้ดเพื่ออ่านข้อมูลจากไฟล์ข้อความ

    • 4.6.A.1 ไฟล์ คือพื้นที่จัดเก็บข้อมูลที่คงอยู่ได้แม้โปรแกรมจะไม่ทำงาน ข้อมูลในไฟล์สามารถเข้าถึงได้ระหว่างการทำงานโปรแกรม
    • 4.6.A.2 ไฟล์สามารถเชื่อมต่อกับโปรแกรมโดยใช้คลาส File และ Scanner
    • 4.6.A.3 สามารถเปิดไฟล์ได้โดยการสร้างวัตถุ File โดยใช้ชื่อไฟล์เป็นพารามิเตอร์ของ constructor
      • File(String str) เป็น constructor File ที่รับชื่อไฟล์ String เพื่อเปิดสำหรับการอ่าน โดยที่ str คือ pathname สำหรับไฟล์นั้น
    • 4.6.A.4 เมื่อใช้คลาส File จำเป็นต้องระบุว่าควรทำอย่างไรหากไม่สามารถเปิดไฟล์ที่มีชื่อนั้นได้ วิธีหนึ่งคือเพิ่ม throws IOException ไว้ที่ส่วนหัวของเมทโอดที่ใช้ไฟล์ หากชื่อไฟล์ไม่ถูกต้อง โปรแกรมจะหยุดทำงาน
    • 4.6.A.5 คลาส File และ IOException เป็นส่วนหนึ่งของแพ็กเกจ java.io ต้องใช้ statement import เพื่อให้คลาสเหล่านี้พร้อมใช้งานในโปรแกรม
    • 4.6.A.6 เมทโอดและ constructor ของ Scanner ด้านล่างนี้ รวมถึงหน้าที่การใช้งานของแต่ละอย่าง เป็นส่วนหนึ่งของ Java Quick Reference:
      • Scanner(File f) เป็น constructor Scanner ที่รับ ⟨⟩ File สำหรับการอ่าน
      • int nextInt() return the next int read from the file or input source if available. If the next int does not exist or is out of range, it will result in an InputMismatchException。
      • double nextDouble() return the next double read from the file or input source. If the next double does not exist, it will result in an InputMismatchException。
      • boolean nextBoolean() return the next boolean read from the file or input source. If the next boolean does not exist, it will result in an InputMismatchException。
      • String nextLine() กลับบรรทัดถัดไปของข้อความในฐานะ ⟨⟩ String ที่อ่านจากไฟล์หรือแหล่งอินพุต; สามารถกลับค่า string ว่างได้หากถูกเรียกทันทีหลังจากเมทโอด Scanner อื่นที่อ่านจากไฟล์หรือแหล่งอินพุต
      • String next() จะคืนค่า String ถัดไปที่ถูกอ่านจากไฟล์หรือแหล่งข้อมูลเข้า
      • boolean hasNext() กลับค่า true หากมีรายการถัดไปให้อ่านในไฟล์หรือแหล่งอินพุต; กลับค่า false ในกรณีอื่น
      • void close() ปิด Scanner นี้
      • ข้อตัดออก: การรับข้อมูลจากคีย์บอร์ดอยู่นอกขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A
    • 4.6.A.7 การใช้ nextLine และเมทโッド Scanner อื่นๆ ร่วมกันบนแหล่งอินพุตเดียวกัน บางครั้งจำเป็นต้องมีโค้ดเพื่อปรับให้เข้ากับการจัดการ whitespace ที่แตกต่างกันของเมทโอดเหล่านี้
      • ข้อตัดออก: การเขียนหรือวิเคราะห์โค้ดที่ใช้ทั้ง nextLine และเมทโッド Scanner อื่นๆ บนแหล่งอินพุตเดียวกันอยู่นอกขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A
    • 4.6.A.8 เมทโอด String เพิ่มเติมด้านล่างนี้ รวมถึงหน้าที่การใช้งาน เป็นส่วนหนึ่งของ Java Quick Reference:
      • String[] split(String del) กลับค่า ⟨⟩ String Array โดยที่แต่ละองค์ประกอบเป็น substring ของ ⟨⟩ this String ซึ่งถูกแยกด้วย matches ของนิพจน์ที่ระบุ del
      • ข้อตัดออก: พารามิเตอร์ del ใช้รูปแบบที่เรียกว่า regular expression การเขียนหรือวิเคราะห์โค้ดที่ใช้คุณสมบัติพิเศษใดๆ ของ regular expressions (เช่น \\*, \\.) นอกรอบของหลักสูตรและข้อสอบ AP Computer Science A
    • 4.6.A.9 สามารถใช้ loop while เพื่อตรวจสอบว่ายังเหลือ ⟨⟩ ให้อ่านในไฟล์หรือไม่ โดยใช้เมทโอด hasNext เป็นเงื่อนไขของ loop
    • 4.6.A.10 ควรปิดไฟล์เมื่อโปรแกรมใช้เสร็จแล้ว调用 closeメソッドจาก Scanner เพื่อปิดไฟล์

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    File and IOException live in java.io, so a program that reads a file needs import java.io.*;. Opening a file can fail (it might not exist), and Java forces you to handle that – the simplest way is to add throws IOException to the method header. A Scanner then reads the file line by line, using hasNext... to test before reading:

    Reading typed tokens with nextInt(), nextDouble(), or nextBoolean() throws an InputMismatchException if the next token is the wrong type – for example calling nextInt() when the next thing in the file is the word cat.

    ไทย

    File และ IOException อยู่ภายใน java.io ดังนั้นโปรแกรมที่อ่านไฟล์จำเป็นต้องใช้ import java.io.*; การเปิดไฟล์อาจล้มเหลว (อาจไม่มีการมีอยู่), และ Java บังคับให้คุณจัดการกับสิ่งนั้น — วิธีที่ง่ายที่สุดคือการเพิ่ม throws IOException เข้าไปในหัวเมทόδ Scanner จากนั้นอ่านไฟล์บรรทัดต่อบรรทัด โดยใช้ hasNext... เพื่อทดสอบก่อนอ่าน:

    import java.io.*;
    ...
    public static void readFile() throws IOException {
        Scanner f = new Scanner(new File("data.txt"));
        while (f.hasNextLine()) {
            String line = f.nextLine();
        }
    }
    

    การอ่านท็อกเคนที่ระบุประเภทด้วย nextInt(), nextDouble() หรือ nextBoolean() จะเกิดข้อผิดพลาด InputMismatchException หากท็อกเคนถัดไปไม่ใช่ประเภทที่ถูกต้อง — ตัวอย่างเช่นการเรียก nextInt() เมื่อสิ่งถัดไปในไฟล์คือคำ cat

    4.7

    Wrapping a Number in an Object · ⁨การห่อหุ้มตัวเลขในวัตถุ⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.7.A: Develop code to use Integer and Double objects from their primitive counterparts and determine the result of using these objects.

    • 4.7.A.1 The Integer class and Double class are part of the java.lang package. An Integer object is immutable, meaning once an Integer object is created, its attributes cannot be changed. A Double object is immutable, meaning once a Double object is created, its attributes cannot be changed.
    • 4.7.A.2 Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an int to an Integer and a double to a Double. The Java compiler applies autoboxing when a primitive value is:
      • passed as a parameter to a method that expects an object of the corresponding wrapper class
      • assigned to a variable of the corresponding wrapper class
    • 4.7.A.3 Unboxing is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer to an int and a Double to a double. The Java compiler applies unboxing when a wrapper class object is:
      • passed as a parameter to a method that expects a value of the corresponding primitive type
      • assigned to a variable of the corresponding primitive type
    • 4.7.A.4 The following class Integer method—including what it does and when it is used—is part of the Java Quick Reference:
      • static int parseInt(String s) returns the String argument as an int.
    • 4.7.A.5 The following class Double method—including what it does and when it is used—is part of the Java Quick Reference:
      • static double parseDouble(String s) returns the String argument as a double.
    ไทย

    จุดประสงค์การเรียนรู้ 4.7.A: เขียนโค้ดเพื่อใช้วัตถุ Integer และ Double จากชนิด primitive ของพวกมัน และระบุผลลัพธ์ของการใช้วัตถุเหล่านี้

    • 4.7.A.1 คลาส Integer และคลาส Double เป็นส่วนหนึ่งของแพ็กเกจ java.lang วัตถุ Integer เป็น immutable หมายความว่าเมื่อสร้างวัตถุ Integer แล้ว คุณสมบัติของมันไม่สามารถเปลี่ยนแปลงได้ วัตถุ Double เป็น immutable หมายความว่าเมื่อสร้างวัตถุ Double แล้ว คุณสมบัติของมันไม่สามารถเปลี่ยนแปลงได้
    • 4.7.A.2 Autoboxing คือการแปลงอัตโนมัติที่ Java Compiler ทำระหว่างชนิด primitive กับ wrapper class ที่สอดคล้อง与之 This includes converting an int to an Integer and a double to a Double. The Java compiler applies autoboxing when a primitive value is:
      • ส่งเป็นพารามิเตอร์ไปยังเมทโอดที่คาดหวังวัตถุของ wrapper class ที่สอดคล้อง与之
      • ถูกกำหนดให้กับตัวแปรของ wrapper class ที่สอดคล้อง与之
    • 4.7.A.3 Unboxing คือการแปลงอัตโนมัติที่ Java Compiler ทำจาก wrapper class เป็นชนิด primitive This includes converting an Integer to an int and a Double to a double. The Java compiler applies unboxing when a wrapper class object is:
      • ส่งเป็นพารามิเตอร์ไปยังเมทโอดที่คาดหวังค่าของชนิด primitive ที่สอดคล้อง与之
      • ถูกกำหนดให้กับตัวแปรของชนิด primitive ที่สอดคล้อง与之
    • 4.7.A.4 เมทโอด Integer ของคลาสด้านล่างนี้ รวมถึงหน้าที่การใช้งาน เป็นส่วนหนึ่งของ Java Quick Reference:
      • static int parseInt(String s) กลับค่าพารามิเตอร์ String ในฐานะ int
    • 4.7.A.5 เมทโอด Double ของคลาสด้านล่างนี้ รวมถึงหน้าที่การใช้งาน เป็นส่วนหนึ่งของ Java Quick Reference:
      • static double parseDouble(String s) กลับค่าพารามิเตอร์ String ในฐานะ double

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    An ArrayList stores objects, not primitives, so a primitive is wrapped in an object: Integer wraps int, Double wraps double. Java does this with autoboxing 自动装箱 (int to Integer) and unboxing (back again) automatically, so you can write list.add(5) and int x = list.get(0).

    ไทย

    ArrayList เก็บ วัตถุ ไม่ใช่พรีมิทีฟ ดังนั้นพรีมิทีฟจะถูก ห่อหุ้ม ในวัตถุ: Integer ห่อหุ้ม int, Double ห่อหุ้ม double Java ทำสิ่งนี้ด้วย autoboxing (int เป็น Integer) และ unboxing (กลับคืนมา) โดยอัตโนมัติ ดังนั้นคุณสามารถเขียน list.add(5) และ int x = list.get(0)

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    autoboxing/ˌɔːtəʊˈbɒksɪŋ/ การห่อหุ้มอัตโนมัติ (Autoboxing)
    4.8

    The ArrayList Toolbox · ⁨ชุดเครื่องมือ ArrayList⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.8.A: Develop code for collections of related objects using ArrayList objects and determine the result of calling methods on these objects.

    • 4.8.A.1 An ArrayList object is mutable in size and contains object references.
    • 4.8.A.2 The ArrayList constructor ArrayList() constructs an empty list.
    • 4.8.A.3 Java allows the generic type ArrayList<E>, where the type parameter E specifies the type of the elements. When ArrayList<E> is specified, the types of the reference parameters and return type when using the ArrayList methods are type E. ArrayList<E> is preferred over ArrayList. For example, ArrayList<String> names = new ArrayList<String>(); allows the compiler to find errors that would otherwise be found at run-time.
    • 4.8.A.4 The ArrayList class is part of the java.util package. An import statement must be used to make this class available for use in the program.
    • 4.8.A.5 The following ArrayList methods—including what they do and when they are used—are part of the Java Quick Reference:
      • int size() returns the number of elements in the list.
      • boolean add(E obj) appends obj to end of list; returns true.
      • void add(int index, E obj) inserts obj at position index (0 <= index <= size), moving elements at position index and higher to the right (adds 1 to their indices) and adds 1 to size.
      • E get(int index) returns the element at position index in the list.
      • E set(int index, E obj) replaces the element at position index with obj; returns the element formerly at position index.
      • E remove(int index) removes element from position index, moving elements at position index + 1 and higher to the left (subtracts 1 from their indices) and subtracts 1 from size; returns the element formerly at position index.
    • 4.8.A.6 The indices for an ArrayList start at 0 and end at the number of elements - 1.
    ไทย

    จุดประสงค์การเรียนรู้ 4.8.A: เขียนโค้ดสำหรับกลุ่มของวัตถุที่เกี่ยวข้องกันโดยใช้วัตถุ ArrayList และระบุผลลัพธ์ของการเรียกเมทโอดบนวัตถุเหล่านี้

    • 4.8.A.1 ArrayList เป็นออบเจกต์ที่ปรับขนาดได้และประกอบด้วยตัวอ้างอิงถึงออบเจกต์
    • 4.8.A.2 ตัวสร้าง ArrayList ArrayList() สร้างลิสต์ว่าง
    • 4.8.A.3 Java รองรับชนิดแบบ泛型 (generic type) ArrayList<E> โดยที่พารามิเตอร์ชนิด E ระบุชนิดขององค์ประกอบ เมื่อ ArrayList<E> ถูกกำหนดไว้ ชนิดของพารามิเตอร์อ้างอิงและชนิดกลับคืนเมื่อใช้วิธีการ ArrayList จะเป็นชนิด E 。 ArrayList<E> ดีกว่า ArrayList 。 ตัวอย่างเช่น ArrayList<String> names = new ArrayList<String>(); ช่วยให้ผู้Compileค้นหาข้อผิดพลาดที่จะพบได้ในเวลารัน
    • 4.8.A.4 คลาส ArrayList เป็นส่วนหนึ่งของแพ็กเกจ java.util 。 ต้องใช้คำสั่ง import เพื่อให้คลาสนี้พร้อมใช้งานในโปรแกรม
    • 4.8.A.5 วิธีการ ArrayList berikut—including what they do and when they are used—are part of the Java Quick Reference:
      • int size() คืนค่าจำนวนองค์ประกอบในลิสต์
      • boolean add(E obj) เพิ่ม obj เข้าไปท้ายลิสต์; คืนค่า true
      • void add(int index, E obj)แทรก obj ที่ตำแหน่ง index (0 <= index <= size), ย้ายองค์ประกอบที่ตำแหน่ง index และสูงกว่าไปทางขวา (เพิ่ม 1 ให้กับดัชนี) และเพิ่ม 1 ให้กับขนาด
      • E get(int index) คืนค่าองค์ประกอบที่ตำแหน่ง index ในลิสต์
      • E set(int index, E obj) แทนที่องค์ประกอบที่ตำแหน่ง index ด้วย obj; คืนค่าองค์ประกอบที่เคยอยู่ที่ตำแหน่ง index
      • E remove(int index) ลบองค์ประกอบออกจากตำแหน่ง index, ย้ายองค์ประกอบที่ตำแหน่ง index + 1 และต่ำกว่าไปทางซ้าย (ลบ 1 ออกจากดัชนี) และลบ 1 ออกจากขนาด; คืนค่าองค์ประกอบที่เคยอยู่ที่ตำแหน่ง index
    • 4.8.A.6 ดัชนีสำหรับ ArrayList เริ่มที่ 0 และสิ้นสุดที่จำนวนองค์ประกอบ - 1

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English
    What an ArrayList really is

    An ArrayList 动态数组 grows and shrinks as you add or remove items. Declare it with the element type in <>:

    ไทย

    *ArrayList จริงๆ แล้วคืออะไร

    ArrayList ขยายและหดตัวเมื่อคุณเพิ่มหรือลบรายการ ประกาศ它以 element type ใน <>:

    ArrayList<String> names = new ArrayList<String>();
    names.add("Amy");           // append
    names.add(0, "Bob");        // insert at index
    names.get(0);               // read
    names.set(1, "Cara");       // replace
    names.remove(0);            // delete, shifts the rest left
    names.size();               // count (a method, unlike array.length)
    
    4.9

    Visiting Every Element of an ArrayList · ⁨การเยี่ยมชมแต่ละองค์ประกอบของ ArrayList⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.9.A: Develop code used to traverse the elements of an ArrayList and determine the results of these traversals.

    • 4.9.A.1 Traversing an ArrayList is when iteration or recursive statements are used to access all or an ordered sequence of the elements in an ArrayList.
    • 4.9.A.2 Deleting elements during a traversal of an ArrayList requires the use of special techniques to avoid skipping elements.
    • 4.9.A.3 Attempting to access an index value outside of its range will result in an IndexOutOfBoundsException.
    • 4.9.A.4 Changing the size of an ArrayList while traversing it using an enhanced for loop can result in a ConcurrentModificationException. Therefore, when using an enhanced for loop to traverse an ArrayList, you should not add or remove elements.
    ไทย

    จุดประสงค์การเรียนรู้ 4.9.A: เขียนโค้ดเพื่อ traversal (สำรวจผ่าน) องค์ประกอบของ ArrayList และกำหนดผลลัพธ์ของการสำรวจเหล่านั้น

    • 4.9.A.1 การ traversing ⟨ArrayList⟩ คือการใช้คำสั่ง iterative (วนซ้ำ) หรือ recursive เพื่อเข้าถึงองค์ประกอบทั้งหมดหรือลำดับที่กำหนดขององค์ประกอบใน ⟨ArrayList⟩
    • 4.9.A.2 การลบองค์ประกอบระหว่างการวน遍历 ArrayList ต้องการการใช้เทคนิคพิเศษเพื่อหลีกเลี่ยงการข้ามองค์ประกอบ
    • 4.9.A.3 การพยายามเข้าถึงค่าดัชนีนอกเหนือจากช่วงที่กำหนดจะส่งผลให้เกิด IndexOutOfBoundsException
    • 4.9.A.4 การเปลี่ยนขนาดของ ArrayList ระหว่างการวน遍历โดยใช้ enhanced for loop อาจทำให้เกิด ConcurrentModificationException ดังนั้น เมื่อใช้ enhanced for loop ในการวน遍历 ArrayList คุณไม่ควรเพิ่มหรือลบองค์ประกอบ

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    Traverse with an index loop or a for-each loop, just like arrays (use size() and get(i)):

    Exam skill: when removing items in an index loop, either loop backwards or do not increment i after a removal – otherwise removing shifts elements left and you skip one. And never add or remove elements while traversing an ArrayList with a for-each loop: changing its size mid-loop throws a ConcurrentModificationException, so use an index loop (backwards, as above) whenever you must remove.

    ไทย

    ท่องผ่านด้วยลูปดัชนีหรือลูป for-each เหมือนกับอาร์เรย์ (ใช้ size() และ get(i)):

    for (int i = 0; i < list.size(); i++) { ... list.get(i) ... }
    for (String s : list) { ... }
    

    ทักษะสำหรับการสอบ: เมื่อ ลบ รายการในลูปดัชนี ให้either ลูป ย้อนกลับ หรือ อย่า เพิ่ม i หลังการลบ — ไม่เช่นนั้นการลบจะเลื่อนองค์ประกอบไปทางซ้ายและคุณจะข้ามหนึ่งรายการ และห้ามเพิ่มหรือลบองค์ประกอบในขณะที่ท่องผ่าน ArrayList ด้วยลูป for-each: การเปลี่ยนแปลงขนาดกลางลูปจะเกิดข้อผิดพลาด ConcurrentModificationException ดังนั้นใช้ลูปดัชนี (ย้อนกลับ ตามข้างต้น) ทุกครั้งที่จำเป็นต้องลบ

    4.10

    Standard ArrayList Algorithms · ⁨อัลกอริทึมมาตรฐานสำหรับ ArrayList⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.10.A: Develop code for standard and original algorithms for a particular context or specification that involve ArrayList objects and determine the result of these algorithms.

    • 4.10.A.1 There are standard ArrayList algorithms that utilize traversals to:
      • determine a minimum or maximum value
      • compute a sum or average
      • determine if at least one element has a particular property
      • determine if all elements have a particular property
      • determine the number of elements having a particular property
      • access all consecutive pairs of elements
      • determine the presence or absence of duplicate elements
      • shift or rotate elements left or right
      • reverse the order of the elements
      • insert elements
      • delete elements
    • 4.10.A.2 Some algorithms require multiple String, array, or ArrayList objects to be traversed simultaneously.
    ไทย

    วัตถุประสงค์การเรียนรู้ 4.10.A: เขียนโค้ดสำหรับอัลกอริทึมมาตรฐานและต้นฉบับสำหรับบริบทหรือข้อกำหนดเฉพาะที่เกี่ยวข้องกับวัตถุ ArrayList และกำหนดผลลัพธ์ของอัลกอริทึมเหล่านี้

    • 4.10.A.1 มี ArrayList อัลกอริทึมมาตรฐานที่ใช้การ traversals เพื่อ:
      • หาค่าต่ำสุดหรือสูงสุด
      • คำนวณผลรวมหรือค่าเฉลี่ย
      • ตรวจสอบว่ามีอย่างน้อยหนึ่งองค์ประกอบมีคุณสมบัติเฉพาะหรือไม่
      • ตรวจสอบว่ามีองค์ประกอบทั้งหมดมีคุณสมบัติเฉพาะหรือไม่
      • หานจำนวนองค์ประกอบที่มีคุณสมบัติเฉพาะ
      • เข้าถึงคู่ขององค์ประกอบที่ต่อเนื่องกันทั้งหมด
      • ตรวจสอบการมีอยู่หรือขาดหายขององค์ประกอบซ้ำ
      • เลื่อนหรือหมุนองค์ประกอบไปทางซ้ายหรือขวา
      • สลับลำดับขององค์ประกอบ
      • เพิ่มองค์ประกอบ
      • ลบองค์ประกอบ
    • 4.10.A.2 บางอัลกอริทึมต้องการหลาย String, array, หรือ ArrayList วัตถุต้องถูก traverses พร้อมกัน

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    The same algorithms as arrays – max/min, count, sum – plus insertion and deletion that arrays cannot do easily. A common task is to remove all elements matching a condition, handling the index-shift carefully.

    ไทย

    อัลกอริทึมเดียวกันกับอาร์เรย์ — ค่าสูงสุด/ต่ำสุด, นับ, ผลรวม — plus การแทรก และ การลบ ที่อาร์เรย์ไม่สามารถทำได้ง่าย งานทั่วไปคือการลบองค์ประกอบทั้งหมดที่ตรงตามเงื่อนไข จัดการกับการเลื่อนดัชนีอย่างระมัดระวัง

    4.11

    Grids: Two-Dimensional Arrays · ⁨ตาราง: อาร์เรย์สองมิติ⁩

    Syllabus · ⁨หลักสูตร⁩
    Learning ObjectiveEssential Knowledge

    4.11.A
    Develop code used to represent collections of related data using two-dimensional (2D) array objects.

    • 4.11.A.1 A 2D array is stored as an array of arrays. Therefore, the way 2D arrays are created and indexed is similar to 1D array objects. The size of a 2D array is established at the time of creation and cannot be changed. 2D arrays can store either primitive data or object reference data.
      • Exclusion statement: Nonrectangular 2D array objects are outside the scope of the AP Computer Science A course and exam.
    • 4.11.A.2 When a 2D array is created using the keyword new, all of its elements are initialized to the default values for the element data type. The default value for int is 0, for double is 0.0, for boolean is false, and for a reference type is null.
    • 4.11.A.3 The initializer list used to create and initialize a 2D array consists of initializer lists that represent 1D arrays; for example, int[][] arr2D = { {1, 2, 3}, {4, 5, 6} };.
    • 4.11.A.4 The square brackets [row][col] are used to access and modify an element in a 2D array. For the purposes of the exam, when accessing the element at arr[first][second], the first index is used for rows, the second index is used for columns.
    • 4.11.A.5 A single array that is a row of a 2D array can be accessed using the 2D array name and a single set of square brackets containing the row index.
    • 4.11.A.6 The number of rows contained in a 2D array can be accessed through the length attribute. The valid row index values for a 2D array are 0 through one less than the number of rows or the length of the array, inclusive. The number of columns contained in a 2D array can be accessed through the length attribute of one of the rows. The valid column index values for a 2D array are 0 through one less than the number of columns or the length of any given row of the array, inclusive. For example, given a 2D array named values, the number of rows is values.length and the number of columns is values[0].length. Using an index value outside of these ranges will result in an ArrayIndexOutOfBoundsException.

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    A 2D array 二维数组 is a grid (rows and columns) – an array of arrays:

    ไทย

    2D array คือตาราง (แถวและคอลัมน์) — อาร์เรย์ของอาร์เรย์:

    อาร์เรย์สองมิติ (ตาราง) พร้อมดัชนียาวและคอลัมน์
    อาร์เรย์สองมิติ (ตาราง) พร้อมดัชนียาวและคอลัมน์
    int[][] grid = new int[3][4];   // 3 rows, 4 columns
    grid[r][c] = 7;                 // row r, column c
    int rows = grid.length;         // 3
    int cols = grid[0].length;      // 4
    
    Explore · ⁨สำรวจ⁩

    Index a 2D array by row and column · ⁨เข้าถึง 2D array ด้วยแถวและคอลัมน์⁩

    A 2D array is a grid addressed by [row][col]. Move the indices and watch which cell they select — row first, then column, both counting from 0. · ⁨2D array คือตารางที่ระบุตำแหน่งด้วย [row][col] เลื่อนดัชนีเพื่อดูว่าเซลล์ใดถูกเลือก — แถวก่อน คอลัมน์ถัดไป โดยนับตั้งแต่ 0 ทั้งคู่⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    2D array/ˌtuː ˈdiː əˈreɪ/ 2D Array
    row-major order/rəʊ ˈmeɪdʒə ˈɔːdə/ ลำดับหลักแถว
    4.12

    Walking Through a Grid · ⁨การเดินผ่านตาราง⁩

    Syllabus · ⁨หลักสูตร⁩
    Learning ObjectiveEssential Knowledge

    4.12.A
    Develop code used to traverse the elements in a 2D array and determine the result of these traversals.

    • 4.12.A.1 Nested iteration statements are used to traverse and access all or an ordered sequence of elements in a 2D array. Since 2D arrays are stored as arrays of arrays, the way 2D arrays are traversed using for loops and enhanced for loops is similar to 1D array objects. Nested iteration statements can be written to traverse the 2D array in row-major order, column-major order, or a uniquely defined order. Row-major order refers to an ordering of 2D array elements where traversal occurs across each row, whereas column-major order traversal occurs down each column.
    • 4.12.A.2 The outer loop of a nested enhanced for loop used to traverse a 2D array traverses the rows. Therefore, the enhanced for loop variable must be the type of each row, which is a 1D array. The inner loop traverses a single row. Therefore, the inner enhanced for loop variable must be the same type as the elements stored in the 1D array. Assigning a new value to the enhanced for loop variable does not change the value stored in the array.

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English
    Traversing a 2-D array

    Visit every cell with nested loops – the outer over rows, the inner over columns (row-major order 行主序):

    ไทย
    การ遍历 2-D array

    เยี่ยมชมเซลล์ทั้งหมดด้วย ลูปซ้อน — ลูปภายนอกสำหรับแถว, ลูปภายในสำหรับคอลัมน์ (ลำดับ row-major):

    for (int r = 0; r < grid.length; r++)
        for (int c = 0; c < grid[0].length; c++)
            System.out.print(grid[r][c]);
    
    4.13

    Standard 2D Array Algorithms · ⁨อัลกอริทึมมาตรฐานสำหรับ 2D Array⁩

    Syllabus · ⁨หลักสูตร⁩
    Learning ObjectiveEssential Knowledge

    4.13.A
    Develop code for standard and original algorithms for a particular context or specification that involves 2D arrays and determine the result of these algorithms.

    • 4.13.A.1 There are standard algorithms that utilize 2D array traversals to:
      • determine a minimum or maximum value of all the elements or for a designated row, column, or other subsection
      • compute a sum or average of all the elements or for a designated row, column, or other subsection
      • determine if at least one element has a particular property in the entire 2D array or for a designated row, column, or other subsection
      • determine if all elements of the 2D array or a designated row, column, or other subsection have a particular property
      • determine the number of elements in the 2D array or in a designated row, column, or other subsection having a particular property
      • access all consecutive pairs of elements
      • determine the presence or absence of duplicate elements in the 2D array or in a designated row, column, or other subsection
      • shift or rotate elements in a row left or right or in a column up or down
      • reverse the order of the elements in a row or column

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English

    Typical grid tasks: sum a row or column, find the max in the grid, count matching cells, or sum a diagonal (where r == c). Each is a nested traversal with a running result.

    ไทย

    งานตารางทั่วไป: รวมแถวหรือคอลัมน์, ค้นหาค่าสูงสุดในตาราง, นับเซลล์ที่ตรงกัน, หรือรวมเส้นทแยงมุม (ที่ r == c) แต่ละอันเป็นการท่องผ่านแบบซ้อนพร้อมผลลัพธ์สะสม

    4.14

    Finding a Value: Linear and Binary Search · ⁨การค้นหาค่า: การค้นหาเชิงเส้นและการค้นหาแบบทวิภาค⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.14.A: Develop code used for linear search algorithms to search for specific information in a collection and determine the results of executing a search.

    • 4.14.A.1 Linear search algorithms are standard algorithms that check each element in order until the desired value is found or all elements in the array or ArrayList have been checked. Linear search algorithms can begin the search process from either end of the array or ArrayList.
    • 4.14.A.2 When applying linear search algorithms to 2D arrays, each row must be accessed then linear search applied to each row of the 2D array.
    ไทย

    วัตถุประสงค์การเรียนรู้ 4.14.A: พัฒนาโค้ด用於อัลกอริทึมการค้นหาเชิงเส้น (linear search algorithms) เพื่อค้นหาข้อมูลเฉพาะในชุดข้อมูลและตรวจสอบผลลัพธ์จากการดำเนินการค้นหา

    • 4.14.A.1 อัลกอริทึมการค้นหาระดับเส้น เป็นอัลกอริทึมมาตรฐานที่ตรวจสอบแต่ละองค์ประกอบตามลำดับจนกว่าจะพบค่าที่ต้องการหรือตรวจสอบองค์ประกอบทั้งหมดในเมทริกซ์หรือ ArrayList แล้ว อัลกอริทึมการค้นหาระดับเส้นสามารถเริ่มกระบวนการค้นหาจากปลายด้านหนึ่งของเมทริกซ์หรือ ArrayList ได้
    • 4.14.A.2 เมื่อใช้ Linear Search Algorithms กับ Arroz 2D, แต่ละแถวต้องถูกเข้าถึงก่อนแล้วจึงทำ Linear Search กับแต่ละแถวของ Arroz 2D

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English
    Binary search: halve and conquer
    • Linear search 线性搜索 checks each element in turn – works on any list, taking up to $n$ steps.
    • Binary search 二分搜索 works only on a sorted list: check the middle, then discard the half that cannot contain the target, repeating. It takes about $\log_2 n$ steps – far faster on large data.

    Exam skill: binary search requires sorted data; know how many comparisons it makes and how lo, hi, mid update.

    Worked example. Search for target = 40 in the sorted array {3, 9, 14, 23, 31, 42, 55} (indices 0–6). Start lo=0, hi=6:

    • mid = (0+6)/2 = 3, a[3]=23 < 40, so lo = 4;
    • mid = (4+6)/2 = 5, a[5]=42 > 40, so hi = 4;
    • mid = (4+4)/2 = 4, a[4]=31 < 40, so lo = 5;
    • now lo (5) > hi (4), so the loop ends – 40 is not present.

    Each step halved the range, so even this miss took only three comparisons.

    ไทย
    Binary search:减半และพิชิต
    • Linear search ตรวจสอบแต่ละองค์ประกอบตามลำดับ – ใช้ได้กับรายการใดๆ โดยใช้เวลาสูงสุด $n$ ขั้นตอน
    • Binary search ใช้งานได้เฉพาะกับรายการที่ เรียงลำดับแล้ว: ตรวจสอบค่าตรงกลาง แล้วตัดครึ่งที่ไม่อาจมีค่าเป้าหมายออก ทำซ้ำจนกว่าจะพบ ค่าใช้จ่ายประมาณ $\log_2 n$ ขั้นตอน – เร็วกว่ามากสำหรับข้อมูลขนาดใหญ่
    Binary search halves the range at each step
    Binary search halves the range at each step
    การค้นหาแบบเชิงเส้นตรวจสอบแต่ละองค์ประกอบตามลำดับจนกว่าจะพบเป้าหมาย
    การค้นหาแบบเชิงเส้นตรวจสอบแต่ละองค์ประกอบตามลำดับจนกว่าจะพบเป้าหมาย
    int lo = 0, hi = a.length - 1;
    while (lo <= hi) {
        int mid = (lo + hi) / 2;
        if (a[mid] == target) return mid;
        else if (a[mid] < target) lo = mid + 1;
        else hi = mid - 1;
    }
    

    ทักษะการสอบ: binary search ต้องการข้อมูลที่เรียงลำดับ; ต้องรู้จำนวนการเปรียบเทียบที่ทำ以及如何 lo, hi, mid อัปเดต

    ตัวอย่างทำพร้อมคำตอบ. ค้นหา target = 40 ในอาเรย์ที่เรียงลำดับ {3, 9, 14, 23, 31, 42, 55} (indices 0–6). เริ่มต้น lo=0, hi=6:

    • mid = (0+6)/2 = 3, a[3]=23 < 40, ดังนั้น lo = 4;
    • mid = (4+6)/2 = 5, a[5]=42 > 40, ดังนั้น hi = 4;
    • mid = (4+4)/2 = 4, a[4]=31 < 40, ดังนั้น lo = 5;
    • ตอนนี้ lo (5) > hi (4), ดังนั้นลูปจบลง – 40 ไม่มีอยู่จริง

    แต่ละขั้นตอนลดช่วงลงครึ่งหนึ่ง ดังนั้นแม้การค้นหาไม่พบก็ใช้เพียงสามครั้งในการเปรียบเทียบ

    Explore · ⁨สำรวจ⁩

    Compare linear and binary search · ⁨เปรียบเทียบ linear search กับ binary search⁩

    Linear search checks every element in turn; binary search halves a sorted list each step. Watch binary search reach the target in far fewer comparisons. · ⁨Linear search ตรวจสอบทุกองค์ประกอบทีละตัว; binary search ลดรายการที่ เรียงลำดับแล้ว ลงครึ่งหนึ่งทุกขั้นตอน ดูว่า binary search ถึงเป้าหมายด้วยการเปรียบเทียบน้อยกว่ามาก⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    Linear search/ˈlɪnɪə sɜːtʃ/ การค้นหาแบบเชิงเส้น
    Binary search/ˈbaɪnəri sɜːtʃ/ การค้นหาแบบทวิภาคี
    4.15

    Putting Data in Order: Selection and Insertion Sort · ⁨การจัดระเบียบข้อมูล: Selection Sort และ Insertion Sort⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.15.A: Determine the result of executing each step of sorting algorithms to sort the elements of a collection.

    • 4.15.A.1 Selection sort and insertion sort are iterative sorting algorithms that can be used to sort elements in an array or ArrayList.
    • 4.15.A.2 Selection sort repeatedly selects the smallest (or largest) element from the unsorted portion of the list and swaps it into its correct (and final) position in the sorted portion of the list.
    • 4.15.A.3 Insertion sort inserts an element from the unsorted portion of a list into its correct (but not necessarily final) position in the sorted portion of the list by shifting elements of the sorted portion to make room for the new element.
    ไทย

    วัตถุประสงค์การเรียนรู้ 4.15.A: ตรวจสอบผลลัพธ์จากการดำเนินการขั้นตอนต่างๆ ของอัลกอริทึมการจัดเรียงเพื่อจัดเรียงองค์ประกอบของชุดข้อมูล

    • 4.15.A.1 Selection sort และ insertion sort เป็นอัลกอริทึมการจัดเรียงแบบวนซ้ำที่สามารถใช้用於จัดเรียงองค์ประกอบในเมทริกซ์หรือ ArrayList ได้
    • 4.15.A.2 Selection sort เลือกองค์ประกอบที่เล็กที่สุด (หรือใหญ่ที่สุด) จากส่วนที่ยังไม่ได้จัดเรียงของรายการซ้ำๆ และสลับมันเข้ากับตำแหน่งที่ถูกต้อง (และสุดท้าย) ในส่วนที่จัดเรียงของรายการ
    • 4.15.A.3 การเรียงลำดับด้วยการแทรก (Insertion sort) จะแทรกองค์ประกอบจากส่วนที่ยังไม่เรียงของรายการลงในตำแหน่งที่ถูกต้อง (แต่ไม่จำเป็นต้องเป็นตำแหน่งสุดท้าย) ในส่วนที่เรียงแล้วของรายการ โดยการเลื่อนองค์ประกอบในส่วนที่เรียงแล้วออกไปเพื่อเปิดพื้นที่สำหรับองค์ประกอบใหม่

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English
    Insertion sort
    Bubble sort, pass by pass
    • Selection sort 选择排序 repeatedly finds the smallest remaining element and swaps it into place.
    • Insertion sort 插入排序 grows a sorted front, inserting each new element where it belongs.

    Both are simple and take about $n^2$ steps on average – fine for small arrays. Be able to trace the array after each pass.

    ไทย
    Insertion sort
    Bubble sort, pas-by-pass
    • Selection sort หาองค์ประกอบที่เหลือที่มีค่าน้อยที่สุดซ้ำๆ และสลับตำแหน่งให้เข้าที่
    • Insertion sort สร้างส่วนหน้าที่เป็นลำดับโดยเพิ่มองค์ประกอบใหม่เข้าไปในตำแหน่งที่เหมาะสม
    การเรียงลำดับแบบแทรก ย้ายคีย์แต่ละตัวเข้าตำแหน่งทีละรอบผ่าน
    การเรียงลำดับแบบแทรก ย้ายคีย์แต่ละตัวเข้าตำแหน่งทีละรอบผ่าน

    ทั้งสองวิธีง่ายและใช้เวลาประมาณ $n^2$ ขั้นตอนเฉลี่ย – เหมาะกับอาเรย์ขนาดเล็ก สามารถติดตามอาเรย์ได้ หลังจากแต่ละรอบ

    Explore · ⁨สำรวจ⁩

    Watch a sorting algorithm order a list · ⁨ดูอัลกอริทึมการจัดเรียงลำดับรายการ⁩

    A sort rearranges elements into order. Step through selection/insertion sort to see the sorted region grow one element at a time. · ⁨Sort จัดเรียงองค์ประกอบให้เป็นลำดับ ก้าวผ่าน selection/insertion sort เพื่อดูว่าพื้นที่ที่เรียงแล้วขยายขึ้นทีละองค์ประกอบ⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    Selection sort/sɪˈlekʃn sɔːt/ การจัดเรียงแบบเลือก
    Insertion sort/ɪnˈsɜːʃn sɔːt/ Insertion sort
    4.16

    Methods That Call Themselves: Recursion · ⁨วิธีที่เรียกตัวเอง: Recursion⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.16.A: Determine the result of calling recursive methods.

    • 4.16.A.1 A recursive method is a method that calls itself. Recursive methods contain at least one base case, which halts the recursion, and at least one recursive call. Recursion is another form of repetition.
    • 4.16.A.2 Each recursive call has its own set of local variables, including the parameters. Parameter values capture the progress of a recursive process, much like loop control variable values capture the progress of a loop.
    • 4.16.A.3 Any recursive solution can be replicated through the use of an iterative approach and vice versa.
      • Exclusion statement: Writing recursive code is outside the scope of the AP Computer Science A course and exam.
    ไทย

    วัตถุประสงค์การเรียนรู้ 4.16.A: ประเมินผลลัพธ์ของการเรียกใช้ฟังก์ชันแบบเรียกซ้ำ

    • 4.16.A.1 ฟังก์ชันแบบเรียกซ้ำ คือฟังก์ชันที่เรียกตัวเอง ฟังก์ชันแบบเรียกซ้ำจะมีอย่างน้อยหนึ่งกรณีฐาน (base case) ซึ่งหยุดการเรียกซ้ำ และมีอย่างน้อยหนึ่งการเรียกซ้ำ การเรียกซ้ำเป็นอีก的一种方式ของการทำซ้ำ
    • 4.16.A.2 แต่ละการเรียกซ้ำจะมีชุดตัวแปรเฉพาะในท้องถิ่นของตนเอง รวมถึงพารามิเตอร์ ค่าพารามิเตอร์จะบันทึกความก้าวหน้าของกระบวนการแบบเรียกซ้ำ เปรียบเสมือนค่าตัวแปรควบคุมวงลูปที่จะบันทึกความก้าวหน้าของวงลูป
    • 4.16.A.3 วิธีแก้ปัญหาแบบเรียกซ้ำสามารถทำซ้ำได้ผ่านการใช้อีทราทีฟ (iterative approach) และในทางกลับกัน亦然
      • ข้อความยกเว้น: การเขียนโค้ดแบบเรียกซ้ำอยู่นอกขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English
    Recursion & the call stack

    Recursion 递归 is a method that calls itself on a smaller input. It needs a base case 基本情况 that stops the calls, and a recursive case that moves toward the base:

    Without a reachable base case, recursion never stops (a stack overflow).

    Recursion and iteration are interchangeable. Any recursive solution can be rewritten with a loop (an iterative approach), and any loop can be rewritten with recursion - they solve the same problems. The factorial above is identical in effect to an iterative version:

    So the choice is about clarity, not capability: recursion reads naturally for problems with a self-similar structure (trees, merge sort), while iteration avoids the memory cost of stacking a call frame per step. The exam may ask you to convert one into the other.

    ไทย
    Recursion & the call stack

    Recursion คือวิธีการที่เรียกตัวเองด้วยอินพุตที่เล็กลง จำเป็นต้องมี base case ที่หยุดการเรียก และมี recursive case ที่พาไปสู่ base case:

    public static int factorial(int n) {
        if (n <= 1) return 1;          // base case
        return n * factorial(n - 1);   // recursive case
    }
    

    หากไม่มี base case ที่เข้าถึงได้ recursion จะไม่หยุด (stack overflow)

    Recursion และ iteration เปลี่ยนแปลงกันได้ การแก้ปัญหาแบบ递归สามารถเขียนใหม่ด้วย loop (approach แบบ iterative) และ loop ก็เขียนใหม่ด้วย recursion ได้ – ทั้งคู่แก้ปัญหาร่วมกัน The factorial ข้างต้นมีผลเทียบเท่าแบบ iterative:

    public static int factorial(int n) {
        int result = 1;
        for (int i = 2; i <= n; i++) result *= i;   // same answer, no self-call
        return result;
    }
    

    ดังนั้นการเลือกจึงเป็นเรื่องของ ความชัดเจน ไม่ใช่ความสามารถ: recursion อ่านง่ายสำหรับปัญหาที่มีโครงสร้างคล้ายกันเอง (trees, merge sort) ในขณะที่ iteration หลีกเลี่ยงค่าใช้จ่ายด้านหน่วยความจำจากการวาง call frame ต่อขั้นตอน การสอบอาจถามให้คุณแปลงอย่างหนึ่งเป็นอีกอย่างหนึ่ง

    Explore · ⁨สำรวจ⁩

    Unfold a recursive call · ⁨คลี่Recursive call ออกมา⁩

    A recursive method calls itself on a smaller input until it hits a base case, then the results fold back up. Step through to watch the calls stack and unwind. · ⁨Recursive method เรียกตัวเองด้วยอินพุตที่เล็กลงจนกระทั่งถึง base case จากนั้นผลลัพธ์จะ folded กลับขึ้นมา ก้าวผ่านเพื่อดูว่า calls สะสมและคลายออกอย่างไร⁩

    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    Recursion/rɪˈkɜːʃn/ Recursion
    base case/beɪs keɪs/ กรณีฐาน (Base Case)
    Merge sort/mɜːdʒ sɔːt/ การเรียงลำดับแบบรวม (Merge sort)
    4.17

    Recursive Search and Merge Sort · ⁨การค้นหาแบบ Recursive และการเรียงลำดับแบบ Merge⁩

    Syllabus · ⁨หลักสูตร⁩
    English

    Learning Objective 4.17.A: Determine the result of executing recursive algorithms that use strings or collections.

    • 4.17.A.1 Recursion can be used to traverse String objects, arrays, and ArrayList objects.

    Learning Objective 4.17.B: Determine the result of each iteration of a binary search algorithm used to search for information in a collection.

    • 4.17.B.1 Data must be in sorted order to use the binary search algorithm. Binary search starts at the middle of a sorted array or ArrayList and eliminates half of the array or ArrayList in each recursive call until the desired value is found or all elements have been eliminated.
    • 4.17.B.2 Binary search is typically more efficient than linear search.
      • Exclusion statement: Search algorithms other than linear and binary search are outside the scope of the AP Computer Science A course and exam.
    • 4.17.B.3 The binary search algorithm can be written either iteratively or recursively.

    Learning Objective 4.17.C: Determine the result of each iteration of the merge sort algorithm when used to sort a collection.

    • 4.17.C.1 Merge sort is a recursive sorting algorithm that can be used to sort elements in an array or ArrayList.
      • Exclusion statement: Sorting algorithms other than selection, insertion, and merge sort are outside the scope of the AP Computer Science A course and exam.
    • 4.17.C.2 Merge sort repeatedly divides an array into smaller subarrays until each subarray is one element and then recursively merges the sorted subarrays back together in sorted order to form the final sorted array.
    ไทย

    วัตถุประสงค์การเรียนรู้ 4.17.A: ประเมินผลลัพธ์ของการดำเนินการอัลกอริทึมแบบเรียกซ้ำที่ใช้กับสตริงหรือคอลเลกชัน

    • 4.17.A.1 การเรียกซ้ำสามารถใช้เพื่อสำรวจ String ออบเจกต์, แอรรے และ ArrayList ออบเจกต์

    วัตถุประสงค์การเรียนรู้ 4.17.B: ประเมินผลลัพธ์ของแต่ละรอบ lặpของอัลกอริทึมการค้นหาแบบไบนารีที่ใช้ในการค้นหาข้อมูลในคอลเลกชัน

    • 4.17.B.1 ข้อมูลต้องอยู่ในรูปแบบที่เรียงลำดับก่อนจึงจะใช้ อัลกอริทึมการค้นหาแบบไบนารีได้ การค้นหาแบบไบนารี เริ่มจากจุดกึ่งกลางของแอรรےที่เรียงลำดับแล้วหรือ ArrayList และตัดครึ่งหนึ่งของแอรรےหรือ ArrayList ออกในแต่ละการเรียกซ้ำ จนกว่าจะพบค่าที่ต้องการหรือ ELEMENTS ทั้งหมดถูกตัดออก
    • 4.17.B.2 การค้นหาแบบไบนารีมีประสิทธิภาพมากกว่าการค้นหาแบบเส้นตรงโดยทั่วไป
      • ข้อความยกเว้น: อัลกอริทึมการค้นหาอื่น ๆ ที่ไม่ใช่การค้นหาแบบเส้นตรงและแบบไบนารีอยู่นอกขอบเขตของหลักสูตรและข้อสอบ AP Computer Science A
    • 4.17.B.3 อัลกอริทึมการค้นหาแบบไบนารีสามารถเขียนได้ทั้งแบบอีทราทีฟหรือแบบเรียกซ้ำ

    วัตถุประสงค์การเรียนรู้ 4.17.C: ประเมินผลลัพธ์ของแต่ละรอบ lặpของอัลกอริทึม Merge Sort เมื่อใช้ในการเรียงลำดับคอลเลกชัน

    • 4.17.C.1 Merge sort เป็นอัลกอริทึมการเรียงลำดับแบบเรียกซ้ำที่สามารถใช้เพื่อเรียงลำดับองค์ประกอบในแอรรےหรือ ArrayList
      • ข้อความยกเว้น: อัลกอริทึมการเรียงลำดับอื่น ๆ ที่ไม่ใช่ Selection Sort, Insertion Sort และ Merge Sort Out of scope ของหลักสูตรและข้อสอบ AP Computer Science A
    • 4.17.C.2 Merge sort จะแบ่งแอรรےออกเป็นแอรรےย่อยที่มีขนาดเล็กลงเรื่อยๆ จนกว่าแต่ละแอรรےย่อยจะมีเพียง 1 องค์ประกอบ แล้วจึงรวมแอรรےย่อยที่เรียงลำดับแล้วกลับเข้าด้วยกันแบบเรียกซ้ำเพื่อสร้างแอรรےที่เรียงลำดับสมบูรณ์

    Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

    English
    Merge sort: split, then merge

    Recursion powers efficient algorithms. Binary search can be written recursively (search the correct half). Merge sort 归并排序 splits the array in half, sorts each half recursively, then merges the two sorted halves – taking about $n\log_2 n$ steps, much faster than selection or insertion sort on large data.

    Worked example. Trace factorial(4). Each call defers to a smaller one: factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * 2 * factorial(1). factorial(1) hits the base case and returns 1, so the calls unwind inward: 2 * 1 = 2, then 3 * 2 = 6, then 4 * 6 = 24. Writing each call above its returned value is the reliable way to trace recursion.

    Exam skill: trace a recursive method by writing out each call and its return value, and know that merge sort's efficiency ($n\log n$) beats the $n^2$ simple sorts.

    ไทย
    Merge sort: แยก แล้วรวม

    Recursion เป็นพลังของอัลกอริทึมที่มีประสิทธิภาพ Binary search สามารถเขียนแบบ recursive ได้ (ค้นหาครึ่งที่ถูกต้อง) Merge sort แบ่งอาเรย์เป็นสองส่วน เรียงลำดับแต่ละส่วนแบบ recursive แล้ว merge ครึ่งทั้งสองส่วนที่เป็นลำดับเข้าด้วยกัน – ใช้เวลาประมาณ $n\log_2 n$ ขั้นตอน เร็วกว่า selection หรือ insertion sort มากสำหรับข้อมูลขนาดใหญ่

    การเรียงลำดับแบบรวม แยกอาร์เรย์ออกเป็นองค์ประกอบเดี่ยว แล้วรวมครึ่งที่เรียงแล้วกลับขึ้นไป
    การเรียงลำดับแบบรวม แยกอาร์เรย์ออกเป็นองค์ประกอบเดี่ยว แล้วรวมครึ่งที่เรียงแล้วกลับขึ้นไป

    ตัวอย่างทำพร้อมคำตอบ. ติดตาม factorial(4) Each call ยอมให้ smaller one: factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * 2 * factorial(1). factorial(1)触碰 base case และส่งกลับ 1, ดังนั้นการเรียกจะคลายตัวเข้าหาภายใน: 2 * 1 = 2, จากนั้น 3 * 2 = 6, แล้ว 4 * 6 = 24. การเขียนแต่ละ call เหนือค่าที่ส่งกลับคือวิธีที่เชื่อถือได้ในการติดตาม recursion

    ทักษะการสอบ: ติดตาม recursive method โดยการเขียนแต่ละ call และค่าที่ส่งกลับออกมา และรู้ว่าประสิทธิภาพของ merge sort ($n\log n$) ดีกว่า $n^2$ sorting methods แบบง่าย

    4.17

    Exam tips · ⁨ข้อแนะนำสำหรับการสอบ⁩

    English
    • Weigh both benefits and harms of collecting data — this unit is tested through short written justification, not code.
    • Protect personally identifiable information (PII) and explain privacy and security risks in context.
    • Name real harms: data breaches, surveillance, and algorithmic bias from unrepresentative data.
    • Respect intellectual property and licensing when you reuse code or data.
    • Give a specific, reasoned answer — a vague "it could be bad" earns no marks.
    ไทย
    • พิจารณาประโยชน์และอันตรายของการเก็บข้อมูล – หน่วยนี้จะทดสอบผ่านการอธิบายสั้นๆ ไม่ใช่โค้ด
    • ปกป้อง ** personally identifiable information (PII)** อธิบายความเสี่ยงด้านความเป็นส่วนตัวและความปลอดภัยในบริบทนั้น
    • ชื่อนิยามความเสียหายจริง: data breaches, การเฝ้าระวัง, และ algorithmic bias จากข้อมูลที่ไม่เป็นตัวแทน
    • เคารพ intellectual property และ licensing เมื่อนำโค้ดหรือข้อมูลกลับมาใช้ใหม่
    • ให้คำตอบที่เฉพาะเจาะจงและมีเหตุผล – คำตอบเบลอเช่น "มันอาจแย่" ไม่ได้คะแนน
    Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
    English ไทย
    privacy/ˈprɪvəsi/ ความเป็นส่วนตัว
    consent/kənˈsent/ ฉันทามติ
    bias/ˈbaɪəs/ อคติ
    data structure/ˈdeɪtə ˈstrʌktʃə/ โครงสร้างข้อมูล

Log in or create account · ⁨เข้าสู่ระบบหรือสร้างบัญชี⁩

IGCSE, A-Level & AP