Skip to content · ⁨الانتقال إلى المحتوى⁩
Subjects · ⁨المواد⁩

AP Computer Science A · ⁨AP علوم الحاسوب A⁩

Tips

AP Computer Science A is a Java course: objects and classes, primitive types and control flow, writing classes, arrays and ArrayLists, 2D arrays, inheritance and polymorphism, and recursion. It is a first programming course taught with real object-oriented code, not pseudocode.

All four free-response questions are hand-written Java. No compiler will catch a missing semicolon or a wrong return type for you, so write code on paper during revision — it is a different skill from typing it.

Arrays and ArrayLists are the most-tested topic. Being fluent with traversal, insertion and removal, and knowing which index shifts when you remove an element, pays back more than any other single hour of practice.

Notes cover the CED units in Java, with runnable examples you can edit in the browser. Past FRQs and scoring guidelines are in the library — all four are code-writing questions, so the worked answers are complete methods rather than 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 خطأ بناء الجملة هو خطأ في البرنامج لا يتم فيه اتباع قواعد لغة البرمجة. يتم اكتشاف هذه الأخطاء بواسطة المترجم.
    • 1.1.C.2 خطأ المنطق هو خطأ في الخوارزمية أو البرنامج يسبب سلوكًا خاطئًا أو غير متوقع. يتم اكتشاف هذه الأخطاء من خلال اختبار البرنامج ببيانات محددة لمعرفة ما إذا كان ينتج النتيجة المتوقعة.
    • 1.1.C.3 خطأ وقت التشغيل هو خطأ في البرنامج يحدث أثناء تنفيذ البرنامج. عادةً ما يؤدي أوقات التشغيل إلى إنهاء البرنامج بشكل غير طبيعي.
    • 1.1.C.4 الاستثناء هو نوع من أخطاء وقت التشغيل يحدث نتيجة لخطأ غير متوقع لم يتم اكتشافه بواسطة المترجم. إنه يعطل التدفق الطبيعي لتنفيذ البرنامج.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English

    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.

    العربية
    أكواد برمجية على محطة عمل — يتم كتابة البرامج وتجميعها وتشغيلها كتعليمات دقيقة
    الكود المصدري على محطة عمل – تُكتب البرامج وتُترجم وتُنفذ كتعليمات دقيقة

    الخوارزمية هي إجراء محدود ومُرتب خطوة بخطوة يحل مشكلة. البرنامج يعبر عن الخوارزمية بلغة يمكن للحاسوب تشغيلها. لغة جافا مُترجمة: يقوم المُترجم بترجمة الكود المصدري إلى بايت كود، الذي تقوم آلة جافا الافتراضية (JVM) بتشغيله. يتم التقاط خطأ في الصياغة (انتهاك القواعد النحوية) بواسطة المُترجم؛ أما خطأ في المنطق (نتيجة خاطئة) فلا يُلتقط – يعمل البرنامج لكن بأداء غير صحيح.

    المُجمِّع يُترجم البرنامج بأكمله دفعة واحدة؛ المُفسِّر يُنفذه سطراً بسطر
    المُترجم يترجم البرنامج بأكمله دفعة واحدة؛ المُفسّر يشغله سطراً بسطر
    عدة رقائق معالجة حاسوب مرئية من الأسفل
    يتم ترجمة برنامج جافا الخاص بك إلى تعليمات يقوم معالج CPU مثل هذه فعلياً بتشغيلها
    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    algorithm/ˈælɡərɪθəm/ خوارزمية
    program/ˈprəʊɡræm/ برنامج
    compiled/kəmˈpaɪld/ مُجمَّع
    compiler/kəmˈpaɪlə/ مُترجم
    syntax error/ˈsɪntæks ˈerə/ خطأ صياغة
    logic error/ˈlɒdʒɪk ˈerə/ خطأ منطق
    variable/ˈveərɪəbl/ حدث
    1.2

    Variables and Data Types · ⁨المتغيرات وأنواع البيانات⁩

    Syllabus · ⁨المنهج⁩
    English

    Learning Objective 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.

    Learning Objective 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.
    العربية

    الهدف التعليمي 1.2.A: تحديد فئة أنواع البيانات الأنسب لمواصفة معينة.

    • 1.2.A.1 نوع البيانات هو مجموعة من القيم ومجموعة عمليات مقابلة على تلك القيم. يمكن تصنيف أنواع البيانات إما إلى أنواع أولية أو مرجعية.
    • 1.2.A.2 أنواع البيانات الأولية المستخدمة في هذا البرنامج تحدد مجموعة القيم والعمليات المقابلة عليها للأرقام وقيم المنطق.
    • 1.2.A.3 النوع المرجعي يستخدم لتعريف الكائنات التي ليست أنواعاً أولية.

    الهدف التعليمي 1.2.B: تطوير كود لإعلان متغيرات لتخزين الأرقام وقيم المنطق.

    • 1.2.B.1 أنواع البيانات الأولية الثلاثة المستخدمة في هذا البرنامج هي int، double، وboolean. القيمة int هي عدد صحيح. القيمة double هي عدد حقيقي. القيمة boolean هي إما true أو false.
      • عبارة الاستبعاد: أنواع البيانات الأولية الخمسة الأخرى (long، short، byte، float، وchar) هي خارج نطاق برنامج امتحان علوم الحاسوب AP A.
    • 1.2.B.2 المتغير هو موقع تخزين يحمل قيمة، والتي يمكن أن تتغير أثناء تشغيل البرنامج. لكل متغير اسم ونوع بيانات مرتبط به. يحتوي المتغير من النوع الأولي على قيمة أولية من ذلك النوع.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English

    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:

    العربية

    المتغير هو صندوق مُسمّى يخزن قيمة ذات نوع ثابت. أنواع جافا الأساسية الأولية هي int (أعداد صحيحة)، double (عشرية)، وboolean (true/false). يُعلن باستخدام النوع أولاً:

    الأنواع الأساسية للبيانات في لغة جافا، كل منها يخزن نوعاً مختلفاً من القيم
    أنواع بيانات جافا الأساسية، كل منها يخزن نوعاً مختلفاً من القيم
    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 العربية
    type/taɪp/ النص
    primitive types/ˈprɪmɪtɪv taɪps/ الأنواع الأولية
    expression/ekˈspreʃn/ تعبير
    modulus/ˈmɒdjʊləs/ المقياس
    escape sequence/eˈskeɪp ˈsiːkwəns/ تسلسل الهروب
    assignment/əˈsaɪnmənt/ التعيين (assignment)
    Casting/ˈkæstɪŋ/ التحويل
    library/ˈlaɪbrəri/ مكتبة
    abstraction/əbˈstrækʃn/ التجريد
    Comments/ˈkɒments/ التعليقات
    method signature/ˈmeθəd ˈsɪɡnɪtʃə/ توقيع الدالة
    arguments/ˈɑːɡjuːmənts/ وسائط
    class (static) method/klæs ˈmeθəd/ دالة (ستاتيكية) صفية
    1.3

    Expressions and Output · ⁨التعبيرات والإخراج⁩

    Syllabus · ⁨المنهج⁩
    English

    Learning Objective 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.

    Learning Objective 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.

    Learning Objective 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.
    العربية

    الهدف التعليمي 1.3.A: تطوير كود لتوليد المخرجات وتحديد النتيجة التي سيتم عرضها.

    • 1.3.A.1 System.out.print وSystem.out.println يعرضان المعلومات على شاشة الكمبيوتر. System.out.println ينقل المؤشر إلى سطر جديد بعد عرض المعلومات، بينما System.out.print لا يفعل ذلك.

    الهدف التعليمي 1.3.B: تطوير كود للاستخدام الثوابت النصية وتحديد نتيجة استخدام الثوابت النصية.

    • 1.3.B.1 الثابت هو تمثيل الكود لقيمة ثابتة.
    • 1.3.B.2 الثابت النصي هو تسلسل من الأحرف محاط بعلامات اقتباس مزدوجة.
    • 1.3.B.3 تسلسلات الهروب هي تسلسلات خاصة من الأحرف يمكن تضمينها في السلسلة النصية. تبدأ بـ \ ولها معنى خاص في جافا. تسلسلات الهروب المستخدمة في هذا البرنامج تشمل علامات الاقتباس المزدوجة \"، والخط المائل العكسي \\، وسطر جديد \n.

    الهدف التعليمي 1.3.C: تطوير كود للتعبيرات الحسابية وتحديد نتيجة هذه التعبيرات.

    • 1.3.C.1 التعبيرات الحسابية، التي تتكون من قيم رقمية ومتغيرات وعوامل، تتضمن تعبيرات من نوع int وdouble.
    • 1.3.C.2 العوامل الحسابية تتكون من الجمع +، والطرح -، والضرب *، والقسمة /، والباقي %. عملية حسابية تستخدم قيمتين int ستُقيّم إلى قيمة int. عملية حسابية تستخدم قيمة double واحدة على الأقل ستُقيّم إلى قيمة double.
      • عبارة الاستبعاد: التعبيرات التي تؤدي إلى قيم double خاصة (مثل اللانهائيات و NaN) هي خارج نطاق برنامج امتحان علوم الحاسوب AP A.
    • 1.3.C.3 عند قسمة قيم رقمية تكون جميعها int، يكون الناتج هو الجزء الصحيح فقط من القسمة. وعند قسمة قيم رقمية تستخدم قيمة double واحدة على الأقل، يكون الناتج هو ناتج القسمة.
    • 1.3.C.4 يُستخدم مؤثر الباقي % لحساب الباقي عندما يتم قسمة عدد a على عدد آخر b.
      • عبارة الاستبعاد: استخدام قيم أقل من 0 لـ a واستخدام قيم أقل من أو تساوي 0 لـ b يقع خارج نطاق منهج امتحان علوم الحاسوب AP A.
    • 1.3.C.5 يمكن استخدام المؤثرات لبناء تعبيرات مركبة. في وقت التجميع، ترتبط القيم الرقمية بالمؤثرات وفقاً لتسلسل أولويات المؤثرات لتحديد كيفية تجميعها. يمكن استخدام الأقواس لتعديل تسلسل أولويات المؤثرات. للضرب والقسمة والباقي أولوية أعلى من الجمع والطرح. يتم تقييم المؤثرات ذات الأولوية المتساية من اليسار إلى اليمين.
    • 1.3.C.6 سيؤدي محاولة قسمة عدد صحيح على الصفر إلى ظهور ArithmeticException.
      • عبارة الاستبعاد: استخدام القسمة على الصفر عندما تكون إحدى القيم الرقمية double يقع خارج نطاق منهج امتحان علوم الحاسوب AP A.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English

    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:

    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".

    العربية

    التعبير يجمع بين القيم والعمليات الحسابية لحساب نتيجة: + - * / و% (باقي القسمة، الباقي). قسمة الأعداد الصحيحة تقطع: 7 / 2 يساوي 3، بينما 7 % 2 يساوي 1. أسبقية العمليات تتبع الرياضيات (*،/،% قبل +،-). اطبع باستخدام:

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

    قسمة عدد صحيح على العدد الصحيح 0 (مثل 7 / 0) غير مسموح بها وتسبب انهياراً وقت التشغيل مع ArithmeticException. داخل السلسلة النصية، يشير الشرطة العكسية إلى تسلسل الهروب: \" يطبع علامة اقتباس مزدوجة، \\ شرطة عكسية واحدة، و\n يبدأ سطوراً جديداً – لذا System.out.println("She said \"hi\""); يطبع 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. · ⁨يطبق جافا *، /، % قبل + و -، عملًا من اليسار إلى اليمين. راقب كل خطوة ولاحظ لماذا 2 + 3 * 4 هي $14$، وليست $20$ — لأن الضرب يحدث أولاً.⁩

    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 يجب إسناد قيمة لكل متغير قبل استخدامه في تعبير. يجب أن تكون هذه القيمة من نوع بيانات متوافق. يتم تهيئة المتغير لأول مرة يتم فيها إسناد قيمة له. يمكن إسناد نوع مرجعي لكائن جديد أو null إذا لم يكن هناك كائن. المعرف_literal null هو قيمة خاصة تُستخدم للإشارة إلى أن المرجع غير مرتبط بأي كائن.
    • 1.4.A.2 يسمح مؤثر الإسناد = للبرنامج بتهيئة أو تغيير القيمة المخزنة في متغير. يتم تخزين قيمة التعبير الموجود على اليمين في المتغير الموجود على اليسار.
      • عبارة الاستبعاد: استخدام مؤشرات الإسناد داخل التعبيرات (مثل a = b = 4; أو a[i += 5]) يقع خارج نطاق منهج امتحان علوم الحاسوب AP A.
    • 1.4.A.3 أثناء التنفيذ، يتم تقييم التعبير لإنتاج قيمة واحدة. تعتمد قيمة التعبير على نوعه بناءً على تقييم التعبير.

    هدف التعلم 1.4.B: تطوير كود لقراءة الإدخال.

    • 1.4.B.1 يمكن أن يأتي الإدخال بأشكال متنوعة، مثل الملمسي، الصوتي، المرئي، أو النصي. تمثل فئة Scanner إحدى الطرق للحصول على إدخال نصي من لوحة المفاتيح.
      • عبارة الاستبعاد: أي شكل محدد للإدخال من المستخدم يقع خارج نطاق منهج امتحان علوم الحاسوب AP A.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English

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

    العربية

    الإسناد x = expr; يقيّم الطرف الأيمن ويخزنه في المتغير الأيسر. اقرأ الإدخال باستخدام Scanner:

    Scanner in = new Scanner(System.in);
    int age = in.nextInt();
    String name = in.next();
    
    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: تطوير كود لتحويل القيم الأولية إلى أنواع أولية مختلفة في التعبيرات الحسابية وتحديد القيمة الناتجة.

    • 1.5.A.1 يمكن استخدام مؤشرات التحويل (int) و(double) للتحويل من قيمة double إلى قيمة int (أو العكس).
    • 1.5.A.2 يؤدي تحويل قيمة double إلى int إلى قص الأرقام الموجودة على يمين الفاصلة العشرية.
    • 1.5.A.3 يتسبب بعض الكود في تحويل قيم int تلقائياً (توسيع) إلى قيم 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 خارج النطاق المسموح به، يحدث فائض في القيم الصحيحة. يكون الناتج قيمة int ضمن النطاق المسموح به ولكن ليس بالضرورة القيمة المتوقعة.

    هدف التعلم 1.5.C: وصف الظروف التي تقيد دقة التعبيرات.

    • 1.5.C.1 تخصص الحواسيب كمية محددة من الذاكرة لتخزين البيانات بناءً على نوع البيانات. إذا كان التعبير سيُقيم إلى double أدق مما يمكن تخزينه في كمية الذاكرة المخصصة، تحدث خطأ التقريب. سيتم تقريب الناتج إلى القيمة القابلة للتمثيل. لتجنب أخطاء التقريب التي تحدث بشكل طبيعي، استخدم قيم int.
      • عبارة الاستبعاد: أنواع بيانات عشرية خاصة أخرى يمكن استخدامها لتجنب أخطاء التقريب تقع خارج نطاق منهج امتحان علوم الحاسوب AP A.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English
    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):

    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.

    العربية
    نطاق int، الاندفاع والتقطيع

    لكل نوع نطاق ثابت؛ يحدث اندفاع للـ int فوق حوالي 2.1 مليار. التحويل يحوّل بين الأنواع. التوسيع (من int إلى double) تلقائي؛ الضيق يتطلب تحويلاً صريحاً، وهو يقطع (لا يقرب):

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

    مهارة الامتحان: انتبه لقسمة الأعداد الصحيحة التي تنتج نتيجة مقطوعة عندما كان متوقعاً رقم عشري – حول أحد المعاملين إلى double أولاً.

    مثال محلول. تابع كل تعبير:

    • 7 / 2 → 3 (كلاهما int، لذا القسمة تقطع)؛
    • 7.0 / 2 → 3.5 (واحد منهما double يجبر القسمة الحقيقية)؛
    • 7 % 2 → 1 (الباقي);
    • (double) 7 / 2 → 3.5 (الـ cast يرتبط بقوة أكبر من /، لذا فهو 7.0 / 2);
    • (double) (7 / 2) → 3.0 (الأقواس تحسب 7 / 2 = 3 في int أولاً، ثم توسع).

    الشكلان الأخيران متشابهان لكنهما مختلفان – تحديد موقع الـ cast هو ما يحدد متى تحدث عملية الاختصار.

    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 معامل و أُس، مستبدلاً الدقة بنطاق ضخم. تحويل double→int يتخلص من الجزء العشري، وقيمة تتجاوز نطاق int تسبب طفحاً.⁩

    1.6

    Compound Assignment Operators · ⁨معملات التعيين المركبة⁩

    Syllabus · ⁨المنهج⁩
    English

    Learning Objective 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.
    العربية

    هدف التعلم 1.6.A: تطوير كود لجمل الإسناد مع مؤشرات الإسناد المركب وتحديد القيمة التي يتم تخزينها في المتغير كنتيجة.

    • 1.6.A.1 يمكن استخدام مؤشرات الإسناد المركب +=، -=، *=، /=، و%= بدلاً من مؤثر الإسناد في التعبيرات الرقمية. يقوم مؤشر الإسناد المركب بإجراء العملية الحسابية المحددة بين القيمة الموجودة على اليسار والقيمة الموجودة على اليمين ثم إسناد النتيجة إلى المتغير الموجود على اليسار.
    • 1.6.A.2 后置自增运算符(post-increment operator)++ 和后置自减运算符(post-decrement operator)-- 用于向数值变量的存储值添加 1 或减去 1。新值被赋给该变量。
      • 排除说明: 前置形式(prefix form)(例如 ++x)中的自增和自减运算符的使用不在 AP 计算机科学 A 课程和考试范围内。在其他表达式内使用自增和自减运算符(例如 arr[x++])也不在 AP 计算机科学 A 课程和考试范围内。

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English

    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.

    العربية

    تجمع الاختصارات عملية مع إسناد: x += 5 تعني x = x + 5؛ وبالمثل -=، *=، /=، %=. المشغلتان الزيادة والنقصان x++ وx-- تضيفان أو تطرحان واحداً.

    1.7

    Application Program Interface (API) and Libraries · ⁨واجهة برمجة التطبيقات (API) والمكتبات⁩

    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: تحديد الخصائص والسلوكيات لصف موجود في المكتبات المضمنة في واجهة برمجة التطبيقات.

    • 1.7.A.1 المكتبات هي مجموعات من الصفوف. يحدد مخطط واجهة برمجة التطبيقات (API) للمبرمج كيفية استخدام تلك الصفوف. الوثائق الموجودة في مخططات واجهات برمجة التطبيقات والمكتبات ضرورية لفهم خصائص وسلوكيات صف تم تعريفه بواسطة واجهة برمجة التطبيقات. يُعرّف الـ صف نوع مرجعيًا محددًا. يتم تجميع الصفوف في واجهات برمجة التطبيقات والمكتبات في حزم. يمكن استخدام الصفوف والمكتبات الموجودة مسبقًا لإنشاء كائنات.
    • 1.7.A.2 تشير الخصائص إلى البيانات المتعلقة بالصف وتُخزن في المتغيرات. تشير السلوكيات إلى ما يمكن أن تفعله instances من الصف (أو ما يمكن القيام به بها) وتُعرف بواسطة الدوال.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English

    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 抽象.

    العربية

    واجهة برمجة التطبيقات (API) هي القائمة المنشورة للفئات والطرق التي يمكنك استخدامها. المكتبة هي مجموعة من الفئات الجاهزة (مثل Math، String، Scanner). تقرأ وثائق API لتعرف ما تحتاجه الطريقة (معاملاتها) وما تُرجعه، دون رؤية كودها الداخلي – وهو مثال على التجريد.

    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 وتُستخدم لإنشاء وثائق واجهة برمجة التطبيقات.
    • 1.8.A.2 الشرط المسبق هو شرط يجب أن يكون صحيحًا قبل تنفيذ دالة مباشرة لكي تتصرف كما هو متوقع. لا يوجد توقع بأن تقوم الدالة بفحص للتأكد من استيفاء الشروط المسبقة.
    • 1.8.A.3 后置条件(postcondition)是指方法执行后必须始终为真的条件。后置条件描述了执行结果,具体表现为返回值或对象属性的当前值。

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English

    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.

    العربية

    التعليقات يتجاهلها المترجم لكنها تشرح الكود للبشر: // لسطر واحد، /* ... */ لحقل، و/** ... */ لتعليق Javadoc الذي يوثق غرض الطريقة ومعاملاتها وقيمتها المُرجعة. تُكتب هنا المتطلبات المسبقة والمتطلبات اللاحقة بدقة.

    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 الدالة هي كتلة من الكود ذات اسم تعمل فقط عند استدعائها. كتلة الكود هي أي جزء من الكود محاط بأقواس مجعدة. يسمح الت مجرد الإجراء للمبرمج باستخدام الدالة بمعرفة ما تفعله الدالة حتى لو لم يعرف كيف تمت كتابتها.
    • 1.9.A.2 الوسيط هو متغير مُصَرَّح به في ترويسة الدالة أو المُنشئ ويمكن استخدامه داخل جسم الدالة. هذا يسمح بتمرير القيم أو الحجج واستخدامها بواسطة الدالة أو المنشئ. يتكون توقيع الدالة لدالة ذات وسائط من اسم الدالة وقائمة الوسائط المرتبة. يتكون توقيع الدالة لدالة بدون وسائط من اسم الدالة وقائمة وسائط فارغة.

    هدف التعلم 1.9.B: وصف كيفية استدعاء الدوال.

    • 1.9.B.1 دالة فارغة ليس لها قيمة إرجاعية وبالتالي لا يتم استدعاؤها كجزء من تعبير.
    • 1.9.B.2 الدالة غير الفارغة تُرجع قيمة تكون من نفس نوع النوع المُرجع في الترويسة. لاستخدام القيمة المُرجعة عند استدعاء دالة غير فارغة، يجب تخزينها في متغير أو استخدامها كجزء من تعبير.
    • 1.9.B.3 الحجة هي قيمة يتم تمريرها إلى الدالة عند استدعائها. يجب أن تكون الحجج الممررة إلى الدالة متوافقة من حيث العدد والترتيب مع الأنواع المحددة في قائمة الوسائط لتوقيع الدالة. عند استدعاء الدوال، يتم تمرير الحجج باستخدام "التمرير بالقيمة". "التمرير بالقيمة" يُهيئ الوسائط بنسخ من الحجج.
    • 1.9.B.4 يُقال إن الدوال مُفرطة التحميل عندما توجد عدة دوال بنفس الاسم ولكن بتواقيع مختلفة.
    • 1.9.B.5 استدعاء الدالة يعطل التنفيذ التسلسلي للعبارات، مما يؤدي إلى تنفيذ البرنامج أولاً للعبارات الموجودة في الدالة قبل الاستمرار. بمجرد تنفيذ آخر عبارة في الدالة أو تنفيذ عبارة إرجاع، يتم إعادة تدفق التحكم إلى النقطة التي تلي مباشرة مكان استدعاء الدالة.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English

    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.

    العربية

    توقيع الطريقة هو اسم الطريقة بالإضافة إلى أنواع معاملاتها، على سبيل المثال nextInt() أو substring(int, int). لاستدعاء طريقة، يجب عليك تزويد الحجج التي تتطابق مع المعاملات من حيث العدد والنوع والترتيب. يحدد أيضًا ترويسة الطريقة (الإعلان الكامل) نوع الإرجاع – نوع القيمة التي تُرجعها الطريقة (void إذا لم يكن هناك إرجاع) – لكن نوع الإرجاع ليس جزءًا من التوقيع، ولهذا لا يمكن لطريقتين أن تختلفا بنوع الإرجاع وحده.

    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: كتابة كود لاستدعاء دوال الفئة وتحديد نتيجة هذه الاستدعاءات.

    • 1.10.A.1 دوال الفئة مرتبطة بالفئة، وليست بأمثلة من الفئة. تتضمن دوال الفئة الكلمة المفتاحية static في الترويسة قبل اسم الدالة.
    • 1.10.A.2 تُستدعى دوال الفئة عادةً باستخدام اسم الفئة متبوعاً بعامل النقطة. عندما يحدث استدعاء الدالة داخل الفئة المُعرِّفة، يكون استخدام اسم الفئة اختيارياً في الاستدعاء.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English

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

    العربية

    طريقة الفئة (الثابتة) تنتمي إلى الفئة نفسها، لذا تستدعيها على اسم الفئة: ClassName.method(args). لا حاجة لكائن.

    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/klæs/ كلاس
    1.11

    Math Class · ⁨فئة الرياضيات⁩

    Syllabus · ⁨المنهج⁩
    English

    Learning Objective 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.
    العربية

    هدف التعلم 1.11.A: كتابة كود لتعبيرات تتضمن استدعاءات لمكتبات رياضية مدمجة وتحديد القيمة الناتجة عن ذلك.

    • 1.11.A.1 فئة Math هي جزء من حزمة java.lang. فئات حزمة java.lang متاحة بشكل افتراضي.
    • 1.11.A.2 تحتوي فئة Math فقط على دوال الفئة. دوال الفئة التالية من Math - بما فيها وظيفتها ومتى يتم استخدامها - جزء من مرجع Java السريع:
      • static int abs(int x) يُرجع القيمة المطلقة لقيمة int.
      • static double abs(double x) يُرجع القيمة المطلقة لقيمة double.
      • static double pow(double base, double exponent) يُرجع قيمة الوسيط الأول مرفوعة إلى أس الوسيط الثاني.
      • static double sqrt(double x) يُرجع الجذر التربيعي غير السالب لقيمة double.
      • static double random() يُرجع قيمة double أكبر من أو تساوي 0.0 وأقل من 1.0.
    • 1.11.A.3 يمكن معالجة القيم المُرجعة من Math.random() باستخدام العمليات الحسابية وعمليات التحويل لإنتاج رقم عشوائي int أو double في نطاق محدد بناءً على معايير محددة. يمكن أن تكون كل نقطة طرفية للنطاق شاملة، أي أن القيمة مدرجة، أو غير شاملة، أي أن القيمة غير مدرجة.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English

    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).

    العربية

    يوفر الصف Math طرقاً رياضية ثابتة: Math.abs(x)، Math.pow(base, exp)، Math.sqrt(x)، وMath.random() (حيث double في $[0,1)$). للحصول على عدد صحيح عشوائي من 0 إلى 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 الكائن هو مثال محدد للفئة له خصائص مُعرَّفة. الفئة هي التنفيذ الرسمي، أو المخطط الهندسي، لخصائص وسلوكيات الكائن.
    • 1.12.A.2 يمكن تطوير تسلسل هرمي للفئات من خلال وضع الخصائص والسلوكيات المشتركة للفئات ذات الصلة في فئة واحدة تسمى فئة أب. يمكن للفئات التي تمتد فئة الأب (تسمى فئات فرعية) الاستفادة من الخصائص والسلوكيات الموجودة في الفئة الأب دون استبدالها في الكود. هذا يخلق علاقة وراثة من الفئات الفرعية إلى الفئة الأب.
      • عبارة الاستبعاد: تصميم وتنفيذ علاقات الوراثة خارج نطاق مادة علوم الحاسوب A للامتحان AP.
    • 1.12.A.3 جميع الفئات في Java هي فئات فرعية من فئة Object.

    هدف التعلم 1.12.B: كتابة كود لإعلان متغيرات لتخزين أنواع الإشارات المرجعية.

    • 1.12.B.1 المتغير من نوع إشارة مرجعية يحمل إشارة مرجعية لكائن، والتي يمكن اعتبارها عنوان الذاكرة الخاص بذلك الكائن.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English
    = 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.)

    العربية
    = ينسخ المرجع، وليس الكائن

    الفئة هي مخطط أولي؛ الكائن هو مثال ملموس مُنشأ منها. تجمع الفئة بين البيانات (الحقول) والسلوك (الطرق) – وهو جوهر برمجة الكائنات. String، Scanner، وArrayList جميعها فئات تقوم بإنشاء أمثلة منها.

    يمكن تنظيم الفئات في تسلسل هرمي. الفئة الأب تحتوي على سمات وسلوكيات مشتركة بين عدة فئات فرعية که extend لها – وهي علاقة وراثة. كل فئة في جافا هي في النهاية فئة فرعية من الفئة المدمجة Object، ولهذا فإن كل كائن يمتلك بالفعل طريقة toString؛ كتابة طريقة في فئة فرعية بنفس توقيع طريقة في الفئة الأب يُسمى إعادة تعريف الطريقة. (تصميم نظام الوراثة الخاص بك خارج نطاق هذا المقرر، لكن يُتوقع منك التعرف على هذه المصطلحات.)

    مخطط فئة: سمات خاصة وطرق عامة
    مخطط فئة: سمات خاصة وطرق عامة
    الفئة هي مخطط أولي؛ كل كائن هو مثال واحد مُنشأ منها
    الفئة هي مخطط أولي؛ كل كائن هو مثال واحد مُنشأ منها
    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    object/ˈɒbdʒekt/ الجسم
    instance/ˈɪnstəns/ مثال
    object-oriented programming/ˈɒbdʒekt ˈɔːrɪəntɪd ˈprəʊɡræmɪŋ/ البرمجة كائنية التوجه
    superclass/ˈsuːpəklæs/ الكلاس الأب
    subclasses/ˈsʌbklæsɪz/ الفئات الفرعية
    inheritance relationship/ɪnˈherɪtəns rɪˈleɪʃənʃɪp/ علاقة الوراثة
    method overriding/ˈmeθəd ˌəʊvəˈraɪdɪŋ/ إعادة كتابة الدالة
    Instantiation/ˌɪnstænʃɪˈeɪʃn/ التنصيب
    constructor/kənˈstrʌktə/ الدالة الإنشائية
    reference/ˈrefrəns/ مرجعي
    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: تحديد المُنشئ الصحيح الذي يتم استدعاؤه باستخدام التوقيع الخاص به.

    • 1.13.A.1 تحتوي الفئة على منشئات يتم استدعاؤها لإنشاء الكائنات. لها نفس اسم الفئة.
    • 1.13.A.2 يتكون توقيع المنشئ من اسم المنشئ، وهو نفسه اسم الفئة، وقائمة مرتبة من أنواع الوسائط. قائمة الوسائط، في ترويسة المنشئ، تذكر أنواع القيم الممررة وأسماء متغيراتها.
    • 1.13.A.3 يُقال إن المنشئات مُعادة التحميل عندما تكون هناك منشئات متعددة بتوقيعات مختلفة.

    هدف التعلم 1.13.B: كتابة كود لإعلان متغيرات من الأنواع الصحيحة لحمل إشارات مرجعية للكائنات.

    • 1.13.B.1 المتغير من نوع إشارة مرجعية يحمل إشارة مرجعية لكائن أو، إذا لم يكن هناك كائن، null.

    هدف التعلم 1.13.C: كتابة كود لإنشاء كائن عن طريق استدعاء منشئ.

    • 1.13.C.1 يتم إنشاء الكائن عادةً باستخدام الكلمة المفتاحية new متبوعة باستدعاء واحد من منشئات الفئة.
    • 1.13.C.2 تسمح الوسائط للمنشئات بقبول قيم لتعيين القيم الأولية لخصائص الكائن.
    • 1.13.C.3 وسيلة المنشئ هي قيمة يتم تمريرها إلى المنشئ عند استدعائه. يجب أن تكون الوسائط الممررة إلى المنشئ متوافقة من حيث الترتيب والعدد مع الأنواع المحددة في قائمة الوسائط في توقيع المنشئ. عند استدعاء المنشئات، يتم تمرير الوسائط باستخدام النقل بالقيمة. يقوم النقل بالقيمة بتهيئة الوسائط بنسخ من الوسائط.
    • 1.13.C.4 يعطل استدعاء المنشئ التنفيذ التسلسلي للعبارات، مما يؤدي إلى تنفيذ البرنامج أولاً للعبارات الموجودة في المنشئ قبل الاستمرار. بمجرد تنفيذ آخر عبارة في المنشئ، يتم إعادة تدفق التحكم إلى النقطة مباشرة بعد مكان استدعاء المنشئ.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English

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

    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).

    العربية

    إنشاء الأمثلة يُنشئ كائنًا باستخدام الكلمة المفتاحية new، والتي تستدعي مُنشئًا:

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

    يحتوي المتغير على مرجع (عنوان الكائن)، وليس الكائن نفسه. يمكن لمرجعين أن يشيرا إلى نفس الكائن؛ ومقارنتهما باستخدام == تقارن العناوين، وليس المحتوى.

    يمكن أن تشير المرجع أيضاً إلى شيء فارغ: القيمة الخاصة null تعني "غير مرتبطة بأي كائن". استدعاء طريقة على مرجع null يتسبب في انهيار البرنامج عند التشغيل مع خطأ NullPointerException. احمِ نفسك منه باختبار ==/!= والتحقق من null أولاً، حتى يتم التوقف المبكر لـ && قبل تنفيذ الطريقة: if (s != null && s.length() > 0).

    المتغير الأولي يحتوي على قيمته مباشرة، والمرجع يحتوي على سهم نحو الكائن
    المتغير الأولي يحتوي على قيمته مباشرة، والمرجع يحتوي على سهم نحو الكائن
    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    null/nʌl/ null
    instance method/ˈɪnstəns ˈmeθəd/ طريقة العينة
    immutable/ɪˈmjuːtəbl/ غير قابلة للتغيير
    Interface/ˈɪntəfeɪs/ واجهة
    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: كتابة كود لاستدعاء دوال الأمثلة وتحديد نتيجة هذه الاستدعاءات.

    • 1.14.A.1 دوال الأمثلة تُستدعى على أمثلة من الفئة. يُستخدم عامل النقطة جنبًا إلى اسم الكائن لاستدعاء دوال الأمثلة.
    • 1.14.A.2 سيؤدي استدعاء دالة على إشارة مرجعية null إلى حدوث NullPointerException.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English

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

    العربية

    طريقة المثال تعمل على كائن محدد، لذا تستدعيها على مرجع الكائن: object.method(args). مثال: in.nextInt()، word.length().

    1.15

    String Manipulation · ⁨معالجة النصوص⁩

    Syllabus · ⁨المنهج⁩
    English

    Learning Objective 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.

    Learning Objective 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: كتابة كود لإنشاء كائنات سلاسل نصية وتحديد نتيجة إنشاء السلاسل النصية ودمجها.

    • 1.15.A.1 يمثل كائن String سلسلة من الأحرف ويمكن إنشاؤها باستخدام حرف ثابت أو عن طريق استدعاء منشئ فئة String.
    • 1.15.A.2 الفئة String جزء من حزمة java.lang. الفئات الموجودة في الحزمة java.lang متاحة افتراضياً.
    • 1.15.A.3 كائن String غير قابل للتعديل، مما يعني أنه بمجرد إنشاء كائن String، لا يمكن تغيير خصائصه. لا تقوم الطرق المستدعاة على كائن String بتغيير محتوى كائن String.
    • 1.15.A.4 يمكن دمج كائني String معاً أو دمجهما باستخدام العامل + أو +=، مما ينتج عنه كائن String جديد. يمكن دمج قيمة أولية مع كائن String. هذا يؤدي إلى التحويل الضمني للقيمة الأولية إلى كائن String.
    • 1.15.A.5 يمكن دمج كائن String مع أي كائن، مما يستدعي بشكل ضمني طريقة الكائن toString (وهو سلوك مضمون بسبب علاقة الوراثة التي تمتلكها كل فئة مع فئة Object). تُرجع طريقة الكائن toString قيمة نصية تمثل الكائن. غالباً ما تستبدل فئات فرعية من Object طريقة toString بتنفيذ محدد للفئة. استبدال الطريقة يحدث عندما تكون هناك طريقة عامة في فئة فرعية تحمل نفس توقيع الطريقة العامة في الفئة الأب، لكن سلوك الطريقة يكون محدداً للفئة الفرعية.
      • عبارة الاستبعاد: استبدال طريقة toString لفئة ما هو خارج نطاق برنامج امتحان علوم الحاسوب AP A.

    الهدف التعليمي 1.15.B: تطوير كود لاستدعاء طرق على كائنات السلاسل النصية وتحديد نتيجة استدعاء هذه الطرق.

    • 1.15.B.1 لكائن String قيم مؤشرات تتراوح من 0 إلى أقل بمقدار واحد من طول السلسلة النصية. محاولة الوصول إلى مؤشرات خارج هذا النطاق ستؤدي إلى خطأ StringIndexOutOfBoundsException.
    • 1.15.B.2 methods String التالية - بما في ذلك ما تفعله ومتى يتم استخدامها - هي جزء من مرجع جافا السريع:
      • int length() يُرجع عدد الأحرف في كائن String.
      • String substring(int from, int to) يُرجع الجزء الجزئي من السلسلة النصية الذي يبدأ عند المؤشر from وينتهي عند المؤشر to - 1.
      • String substring(int from) يُرجع substring(from, length()).
      • int indexOf(String str) يُرجع مؤشر الحدوث الأول لـ str؛ يُرجع -1 إذا لم يتم العثور عليه.
      • boolean equals(Object other) يُرجع true إذا كان this يماثل نفس تسلسل الأحرف لـ other؛ يُرجع false بخلاف ذلك.
      • int compareTo(String other) يُرجع قيمة < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 إذا كان this أكبر من other. تُرتب السلاسل النصية بناءً على الأبجدية.
      • عبارة الاستبعاد: استخدام طريقة equals لمقارنة كائن String بكائن من نوع آخر غير String هو خارج نطاق برنامج امتحان علوم الحاسوب AP A.
    • 1.15.B.3 يمكن إنشاء سلسلة نصية مطابقة للعنصر الفردي الوحيد عند الموقع index عن طريق استدعاء substring(index, index + 1).

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    English
    Strings are immutable

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

    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 غير قابلة للتغيير – تُرجع الطرق نصًا جديدًا بدلاً من تغيير النص الأصلي. الطرق الرئيسية (جميع الفهارس تبدأ من 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
    

    مهارة الامتحان: substring(a, b) يشمل الفهرس a لكنه يستبعد b، ولازم مقارنة Strings استخدام .equals، لا == – وهما من أكثر الثغرات الشائعة في String اختبارًا.

    مثال محلول. ليكن String s = "COMPUTER"; (الفهارس 0–7). حينئذٍ s.length() يساوي 8؛ s.substring(0, 4) يساوي "COMP" (الفهارس 0,1,2,3 – الفهرس 4 مستبعد)؛ s.substring(4) يساوي "UTER" (من الفهرس 4 إلى النهاية)؛ s.indexOf("PU") يساوي 3؛ وs.indexOf("X") يساوي -1 (غير موجود). يعد احتساب الطرف المستبعد لـ substring الخطأ الأكثر شيوعاً.

    طلب فهرس خارج 0 إلى length()-1 (حجة substring أو charAt خاطئة، مثل s.substring(0, 20) هنا) يسبب انهيارًا مع StringIndexOutOfBoundsException – وهو نظير خطأ فهرس المصفوفة في String.

    فهارس النص تبدأ من 0
    فهارس النص تبدأ من 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.⁩

    1.15

    Exam tips · ⁨نصائح للامتحان⁩

    English
    • 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.
    العربية
    • تتبع الك يدويًا سطراً بسطر، وتتبع قيمة كل متغير في جدول – الامتحان يكافئ التتبع الدقيق على الحساب التخميني.
    • تعرف على الأنواع الأولية في Java وأن القسمة الصحيحة تقوم بالتقطيع ($7/2$ يعطي $3$)؛ استخدم تحويل نوع أو double للقسمة الحقيقية.
    • ميّز بين أخطاء وقت التجميع (التركيب، الأنواع) وأخطاء وقت التشغيل – وتعرّف على الأسماء المحددة: ArithmeticException (int ÷ 0)، NullPointerException (دالة على مرجع فارغ)، StringIndexOutOfBoundsException / ArrayIndexOutOfBoundsException – وأخطاء المنطق (مخرجات خاطئة).
    • اتبع أولوية المعاملات وابدأ قيمة كل متغير قبل استخدامه.
    • في الأسئلة المقالية، اكتب كود Java كامل وقابل للتجميع – أعد النوع الصحيح وطابق ترويسة الدالة بدقة.
  • 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.

    العربية
    مخطط انسيابي مع معين قرار: الاختيار يحدد المسار الذي يتخذه الخوارزمية
    مخطط انسيابي مع معين قرار: الاختيار يحدد المسار الذي يتخذه الخوارزمية

    تُبنى الخوارزميات من ثلاثة هياكل تحكم: التسلسل (خطوات بترتيبها)، الاختيار (اختيار مسار)، والتكرار (إعادة الخطوات). يغطي هذا الموضوع الاختيار والتكرار – وهما الأداة اللتان تسمحان للبرنامج باتخاذ القرارات والتكرار.

    الهياكل التحكمية الثلاثة: التسلسل، الاختيار، والتكرار
    الهياكل التحكمية الثلاثة: التسلسل، الاختيار، والتكرار
    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/ عبارة منطقية
    relational operators/rɪˈleɪʃənl ˈɒpəreɪtəz/ المقارنات العلاقية
    if statement/ɪf ˈsteɪtmənt/ عبارة if
    Logical operators/ˈlɒdʒɪkl ˈɒpəreɪtəz/ المؤثرات المنطقية
    short-circuit evaluation/ʃɔːt ˈsɜːkɪt ɪˌvæljuːˈeɪʃn/ التقييم القصير الدائرة
    De Morgan's laws/də ˈmɔːɡənz lɔːz/ قوانين دي مورغان
    while loop/waɪl luːp/ حلقة while
    infinite loop/ˈɪnfɪnət luːp/ حلقة لا نهائية
    flag/flæɡ/ علامة
    nested loop/ˈnestɪd luːp/ حلقة متداخلة
    Run-time analysis/rʌn taɪm əˈnæləsɪs/ تحليل وقت التشغيل
    2.2

    Boolean Expressions · ⁨العبارات المنطقية⁩

    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: تطوير كود لإنشاء عبارات منطقية باستخدام المعاملات العلائقية وتحديد نتيجة هذه العبارات.

    • 2.2.A.1 يمكن مقارنة القيم باستخدام المعاملات العلائقية == و!= لتحديد ما إذا كانت القيم متساوية. مع الأنواع الأولية، يقارن هذا القيم الأولية الفعلية. مع أنواع المرجع، يقارن هذا مرجعات الكائن.
    • 2.2.A.2 يمكن مقارنة القيم العددية باستخدام المعاملات العلائقية <، >، <=، و>= لتحديد العلاقة بين القيم.
    • 2.2.A.3 تعطي العبارة التي تتضمن معاملات علائقية قيمة منطقية (Boolean 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.

    العربية
    البوابات المنطقية والمجمّع النصف

    تُقيّم العلاقة المنطقية إلى true أو false، باستخدام المعاملات العلائقية: == (متساوي)، != (لا يساوي)، <، >، <=، >=. لاحظ أن == يقارن القيم الأولية ولكن مرجعات الكائنات للكائنات، لذا استخدم .equals لـ Strings.

    عائلات المعاملات الثلاث: الحسابية، العلائقية، والمنطقية
    عائلات المعاملات الثلاث: الحسابية، العلائقية، والمنطقية
    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 صحيحاً فقط عندما يكون كلا المعاملين صحيحين؛ غيّر المدخلات لرؤية الحالات الأربع.⁩

    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: تطوير كود لتمثيل العمليات المنطقية المتفرعة باستخدام جمل الاختيار وتحديد نتيجة هذه العمليات.

    • 2.3.A.1 جمل الاختيار تغير التنفيذ التسلسلي للجمل.
    • 2.3.A.2 جملة if هي نوع من جمل الاختيار تؤثر على تدفق التحكم عن طريق تنفيذ أجزاء مختلفة من الكود بناءً على قيمة عبارة منطقية.
    • 2.3.A.3 الاختيار أحادي المسار (جملة if) يُستخدم عندما يكون هناك جزء من الكود لتنفيذه تحت شرط معين. في هذه الحالة، يتم تنفيذ الجسم فقط عندما تكون العبارة المنطقية true.
    • 2.3.A.4 الاختيار ثنائي المسار (جملة if-else) يُستخدم عندما يكون هناك جزآن من الكود - واحد لتنفيذه عندما تكون العبارة المنطقية true وجزء آخر لتنفيذه عندما تكون العبارة المنطقية false. في هذه الحالة، يتم تنفيذ جسم if عندما تكون العبارة المنطقية true، ويتم تنفيذ جسم else عندما تكون العبارة المنطقية 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");
    }
    
    إشارات المرور: الاختيار يحدد أي فرع يعمل، تماماً كما تختار جمل if مسارات الكود
    إشارات المرور: الاختيار يحدد أي فرع يعمل، تماماً كما تختار جمل 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 جسمه فقط عندما يكون الشرط صحيحاً، وإلا يتخطاه إلى else. حرك الدرجة عبر الحدود وراقب تغير التقدير.⁩

    2.4

    Nested if Statements · ⁨جمل if المتداخلة⁩

    Syllabus · ⁨المنهج⁩
    Learning ObjectiveEssential Knowledge

    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.

    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، يختبر عدة حالات بالترتيب. يتم تنفيذ الفرع الأول المطابق فقط:

    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). تعطي العبارة التي تتضمن معاملات منطقية قيمة منطقية.
    • 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).

    العربية
    التقييم القصري

    المعاملات المنطقية تجمع الشروط: && (و – كلاهما صحيح)، || (أو – واحد على الأقل صحيح)، ! (ليس – عكس). تستخدم Java التقييم القصري: && يتوقف إذا كان الجانب الأيسر خاطئاً، و|| يتوقف إذا كان الجانب الأيسر صحيحاً – مفيد للحماية من الأخطاء، مثلاً if (n != 0 && total / n > 5).

    2.6

    Comparing Boolean Expressions · ⁨مقارنة العبارات المنطقية⁩

    Syllabus · ⁨المنهج⁩
    English

    Learning Objective 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.

    Learning Objective 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.
    العربية

    هدف التعلم 2.6.A: مقارنة العبارات المنطقية المكافئة.

    • 2.6.A.1 تكون العبارتان المنطقيتان مكافئتين إذا أعطتا نفس القيمة في جميع الحالات. يمكن استخدام جداول الحقيقة لإثبات أن العبارات المنطقية مكافئة.
    • 2.6.A.2 يمكن تطبيق قانون دي مورغان على العبارات المنطقية لإنشاء عبارات منطقية مكافئة. تحت قانون دي مورغان، العبارة المنطقية !(a && b) مكافئة لـ !a || !b والعبارة المنطقية !(a || b) مكافئة لـ !a && !b.

    هدف التعلم 2.6.B: تطوير كود لمقارنة مرجعات الكائن باستخدام العبارات المنطقية وتحديد نتيجة هذه العبارات.

    • 2.6.B.1 يمكن لمتغيرين مختلفين أن يحتوي على مرجعين لنفس الكائن. يمكن مقارنة مرجعات الكائن باستخدام == و!=.
    • 2.6.B.2 يمكن مقارنة مرجع كائن مع null، باستخدام == أو !=، لتحديد ما إذا كان المرجع يوجه فعلياً إلى كائن.
    • 2.6.B.3 عادةً ما تُعرّف الفئات (Classes) نفسها طريقة equals، والتي يمكن استخدامها لتحديد معايير التكافؤ لكائنين من هذه الفئة. غالبًا ما يتم تحديد تكافؤ كائنين باستخدام سمات من الكائنين.
      • عبارة الاستبعاد: تجاوز equals خارج نطاق منهج AP علوم الحاسوب A والامتحان.

    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.

    العربية

    قوانين دي مورغان تعيد كتابة النفي: !(a && b) يساوي !a || !b، و!(a || b) يساوي !a && !b. تكون عبارتان منطقيتان متكافئتين إذا أعطتا نفس النتيجة لكل مدخل – ويثبت ذلك جدول الحقيقة. يُعد تبسيط الشروط بهذه الطريقة مهمة شائعة في الامتحانات.

    2.7

    while Loops · ⁨حلقات while⁩

    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: تحديد متى يكون العملية التكرارية ضرورية لتحقيق النتيجة المطلوبة.

    • 2.7.A.1 التكرار هو شكل من أشكال التكرار. تقوم عبارات التكرار بتغيير تدفق التحكم عن طريق تكرار جزء من الكود صفرًا أو أكثر من المرات طالما أن التعبير المنطقي الذي يتحكم في الحلقة يعطي نتيجة true.
    • 2.7.A.2 تحدث الحلقة اللانهائية عندما يعطي التعبير المنطقي في عبارة التكرار دائمًا نتيجة true.
    • 2.7.A.3 لن يتم تنفيذ جسم الحلقة في عبارة التكرار إذا كانت النتيجة الأولية للتعبير المنطقي تساوي false.
    • 2.7.A.4 تحدث أخطاء الانحراف بمقدار واحد عندما تكرر عبارة التكرار مرة واحدة أكثر من اللازم أو مرة واحدة أقل من اللازم.

    هدف التعلم 2.7.B: تطوير كود لتمثيل العمليات التكرارية باستخدام while وحلقات determining result of these processes.

    • 2.7.B.1 while هي نوع من عبارات التكرار. في while، يتم تقييم التعبير المنطقي قبل كل تكرار لجسم الحلقة، بما في ذلك التكرار الأول. عندما تكون النتيجة true، يتم تنفيذ جسم الحلقة. يستمر هذا حتى يصبح التعبير المنطقي 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 طوال بقاء شرطها صحيحاً، مع اختبار قبل كل مرور. يجب تغيير شيء ما داخلها حتى تتوقف الحلقة أخيراً، وإلا تصبح حلقة لا نهائية:

    أنواع الحلقات الثلاثة تختلف في مكان اختبار الشرط
    أنواع الحلقات الثلاثة تختلف في مكان اختبار الشرط
    int i = 0;
    while (i < 5) {
        System.out.println(i);
        i++;
    }
    
    Explore · ⁨استكشف⁩

    Trace a while loop · ⁨تتبع حلقة while⁩

    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 طالما بقي شرطها صحيحاً، محدثة متغيراتها في كل دورة. مرّ بالخطوات لرؤية تجمع مجموع المربعات.⁩

    2.8

    for Loops · ⁨حلقات for⁩

    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 وحلقات determining result of these processes.

    • 2.8.A.1 for هي نوع من عبارات التكرار. تتكون رأس for من ثلاثة أجزاء: التهيئة، والتعبير المنطقي، والتحديث.
    • 2.8.A.2 في for، يتم تنفيذ جملة التهيئة مرة واحدة فقط قبل التقييم الأولي للتعبير المنطقي. يُشار إلى المتغير الذي يتم تهيئته باسم متحكم الحلقة. يتم تقييم التعبير المنطقي فورًا بعد تهيئة متحكم الحلقة ثم بعد كل تنفيذ لجملة الزيادة حتى تصبح false. في كل تكرار، يتم تنفيذ التحديث بعد تنفيذ جسم الحلقة بالكامل وقبل إعادة تقييم التعبير المنطقي.
    • 2.8.A.3 يمكن إعادة كتابة for إلى while مكافئة ( والعكس صحيح).

    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 التهيئة والشرط والتحديث في سطر واحد – الأفضل عندما تعرف العدد:

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

    يفعل for وwhile المكافئ نفس العمل؛ يجب أن تكون قادراً على التحويل بينهما.

    خط إنتاج: تكررت الحلقات عملية لكل عنصر، مثل for وwhile
    خط إنتاج: تكررت الحلقات عملية لكل عنصر، مثل for وwhile
    Explore · ⁨استكشف⁩

    Trace a for loop · ⁨تتبع حلقة for⁩

    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 عدداً ثابتاً من المرات، حيث يمر عدادها عبر نطاق محدد. راقب العداد والمجموع الجاري يتقدمون دورة بدورة.⁩

    2.9

    Building Complete Selection and Iteration Algorithms · ⁨بناء خوارزميات اختيار وتكرار كاملة⁩

    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 يقبل القسمةENCY على d. ادمجها مع عداد لإيجاد التكرار الذي يتحقق فيه معيار ما.

    تتكرر أنماط قياسية مثل المجموع الجاري، العداد، أو علم الإشارة (منطقي يسجل حدوث شيء ما) عبر البرنامج بأكمله.

    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: تطوير كود لخوارزميات قياسية وأصلية تتضمن نصوصًا وتحديد نتيجة هذه الخوارزميات.

    • 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.

    العربية

    تكرّر عبر سلسلة نصية بالمؤشر لمعالجة كل حرف:

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

    المهام النموذجية: عدّ التكرارات، بناء نسخة مقلوبة أو مصفاة، أو اختبار هل تحتوي سلسلة نصية على أخرى.

    2.11

    Nested Iteration · ⁨التكرار المتداخل⁩

    Syllabus · ⁨المنهج⁩
    English

    Learning Objective 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.
    العربية

    هدف التعلم 2.11.A: تطوير كود لتمثيل العمليات التكرارية المتداخلة وتحديد نتيجة هذه العمليات.

    • 2.11.A.1 الجمل التكرارية المتداخلة هي جمل تكرار تظهر في جسم جملة تكرار أخرى. عندما يكون حلقة مفرغة متداخلة داخل حلقة أخرى، يجب أن تكمل الحلقة الداخلية جميع تكراراتها قبل أن تتمكن الحلقة الخارجية من الاستمرار إلى تكرارها التالي.

    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.

    العربية

    تضع الحلقة المتداخلة حلقة داخل أخرى؛ تكتمل الحلقة الداخلية بالكامل لكل مرور من الحلقة الخارجية. إذا مرت الحلقة الخارجية $n$ مرات والحلقة الداخلية $m$ مرة، فإن الجسم يعمل $n\times m$ مرة – وهو أساس معالجة الشبكات ومقارنة جميع الأزواج.

    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 عدد تنفيذ الجملة يشير إلى عدد المرات التي يتم فيها تنفيذ جملة بواسطة البرنامج. غالبًا ما يتم حساب عدد تنفيذ الجمل بشكل غير رسمي من خلال تتبع وتحليل الجمل التكرارية.

    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

    التحليل الزمني يحسب عدد الخطوات الأساسية التي يأخذها الخوارزمية كلما كبر حجم الإدخال $n$. احسب تنفيذ العبارة الداخلية: حلقة واحدة على $n$ عنصر هي خطية ($n$ خطوة)؛ حلقتان متداخلتان على $n$ هما مربعتان ($n^2$). يسمح هذا الحساب غير الرسمي بمقارنة كفاءة خوارزميتين.

    كيف ينمو وقت التنفيذ مع عدد العناصر n
    كيف ينمو وقت التشغيل مع عدد العناصر n

    مهارة الامتحان: بالنسبة للحلقة المتداخلة، كن قادراً على stating كم مرة تعمل العبارة الداخلية بدلالة حدود الحلقات – سؤال خيارات متعددة شائع.

    مثال محلول. كم نجمة يطبع هذا؟

    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)$. · ⁨وقت التشغيل يصف كيف ينمو عدد الخطوات مع حجم الإدخال $n$. زِد $n$ وراقب كيف يسبق خطي $O(n)$ تربيعي $O(n^2)$ مسافة كبيرة.⁩

    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).
    • قم ببناء الشروط المركبة باستخدام &&، ||، و! وتذكر تقييم التقاطع القصير (ضع التحقق من القيم الفارغة أولاً).
    • تتبع الحلقات المتداخلة عن طريق حساب عدد مرات تنفيذ الجسم الداخلي إجماليًا.
    • اختر الهيكل المناسب – if/else if للمدارات، والحلقة للتكرار – وتجنب الحلقة اللانهائية عن طريق تحديث متغير الحلقة.
    • طبق قوانين دي مورغان عند تبسيط أو نفي شرط منطقي.
  • 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 التجريد هو عملية تقليل التعقيد من خلال التركيز على الفكرة الرئيسية. من خلال إخفاء التفاصيل غير ذات الصلة بالسؤال الحالي وتجميع التفاصيل ذات الصلة والمفيدة، يقلل التجريد من التعقيد ويسمح بالتركيز على الفكرة.
    • 3.1.A.2 يوفر تجريد البيانات فصلًا بين الخصائص المجردة لنوع البيانات والتفاصيل الملموسة لتمثيله. يدير تجريد البيانات التعقيد من خلال إعطاء البيانات اسمًا دون الإشارة إلى التفاصيل المحددة للتمثيل. يمكن أن تأخذ البيانات شكل متغير واحد أو مجموعة من البيانات، كما هو الحال في فئة أو مجموعة بيانات.
    • 3.1.A.3 السمة هي نوع من أنواع تجريد البيانات يتم تعريفها في فئة خارج أي طريقة أو باني. متغير الحالة هو سمة تكون قيمتها فريدة لكل حالة من حالات الفئة. متغير الفئة هو سمة مشتركة بين جميع حالات الفئة.
    • 3.1.A.4 يوفر التجريد الإجرائي اسمًا لعملية ويتيح استخدام الطريقة بمعرفة ما تفعله فقط، وليس كيفيةقيام بها. من خلال تحليل الطرق، يقوم المبرمج بتقسيم السلوكيات الكبيرة للفئة إلى سلوكيات أصغر من خلال إنشاء طرق لتمثيل كل سلوك فردي أصغر. قد يستخرج التجريد الإجرائي ميزات مشتركة لتعميل الوظيفة بدلاً من تكرار الكود. هذا يسمح بإعادة استخدام الكود، مما يساعد في إدارة التعقيد.
    • 3.1.A.5 استخدام المعاملات يسمح بتعميل الإجراءات، مما يتيح إعادة استخدامها مع مجموعة من قيم الإدخال أو المعاملات.
    • 3.1.A.6 استخدام التجريد الإجرائي في البرنامج يسمح للمبرمجين بتغيير internals الخاص بالطريقة (لجعلها أسرع، أكثر كفاءة، استخدام تخزين أقل، إلخ.) دون الحاجة إلى إبلاغ مستخدمي الطريقة بالتغيير طالما أن توقيع الطريقة وما تفعله الطريقة محفوظ.
    • 3.1.A.7 قبل تنفيذ الفئة، من المفيد تخصيص الوقت لتصميم كل فئة بما في ذلك سماتها وسلوكياتها. يمكن تمثيل هذا التصميم باستخدام اللغة الطبيعية أو المخططات.

    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 دون معرفة كيفية تخزين الأحرف. التصميم الجيد يجزئ المشكلة إلى فئات، كل فئة مسؤولة عن فكرة واحدة. يتناول هذا الموضوع كتابة فئاتك الخاصة.

    تجزئة البرنامج إلى وحدات وفرع فرعية
    تحليل برنامج إلى وحدات وفرعيات
    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 موثوقية النظام تشير إلى قدرة البرنامج على أداء مهامه كما هو متوقع تحت الظروف المعلنة دون فشل. يجب على المبرغين بذل الجهد لزيادة موثوقية النظام من خلال اختبار البرنامج مع مجموعة متنوعة من الظروف.
    • 3.2.A.2 لإنشاء البرامج تأثيرات على المجتمع والاقتصاد والثقافة. يمكن أن تكون هذه التأثيرات مفيدة أو ضارة. قد يكون للبرامج المصممة لتلبية حاجة ما أو حل مشكلة تأثيرات ضارة غير مقصودة تتجاوز استخداماتها المقصودة.
    • 3.2.A.3 تثار مسائل قانونية ومخاوف تتعلق بالملكية الفكرية عند إنشاء البرامج. غالباً ما يعيد المبرمجون استخدام الكود الذي كتبه آخرون ونشر كمصدر مفتوح ومجاني الاستخدام. يتطلب دمج كود لم يتم نشره كمصدر مفتوح من قبل المبرمج الحصول على الإذن وشراء الكود غالباً قبل دمجه في برنامجهم.

    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.

    العربية

    تؤثر خيارات التصميم على ما إذا كان الكود صحيحًا، ومقروءًا، وقابل لإعادة الاستخدام. التغليف – إبقاء البيانات خاصة وكشفها فقط عبر الدوال – يحمي حالة الكائن من سوء الاستخدام ويسمح لك بتغيير internals دون كسر عمل مستخدمي الفئة. التسمية المدروسة، والدوال ذات الغرض الواحد، والاختبار تقلل الأخطاء.

    يتضمن التصميم أيضًا مسؤولية تتجاوز الكود. موثوقية النظام - أداء برنامج لمهامه كما هو متوقع، بدون فشل - هو ما يجب أن يسعى المبرمجون لتعظيمه من خلال التصميم الدقيق والاختبار. للبرامج تأثيرات حقيقية على المجتمع والاقتصاد والثقافة يمكن أن تكون مفيدة وضارة معًا. وإنتاج البرامج يثير قضايا قانونية وحقوق الملكية الفكرية: غالبًا ما يعيد المبرمجون استخدام الكود المنشور كمصدر مفتوح ومجاني الاستخدام، لكنهم يجب أن يحترموا ترخيصه ويمنحوه التقدير بدلاً من نسخ عمل الآخرين باعتباره عملهم الخاص.

    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 تغليف البيانات هو تقنية يتم فيها إخفاء تفاصيل تنفيذ الفئة عن الفئات الخارجية. الكلمات public وprivate تؤثر على وصول الفئات والبيانات والمُشكِّلات والدوال. الكلمة private تقيد الوصول إلى الفئة المُعلِّنة، بينما تسمح الكلمة public بالوصول من فئات خارج الفئة المُعلِّنة.
    • 3.3.A.2 في هذا المقرر، يتم تحديد الفئات دائماً بـ public وتُعلن باستخدام الكلمة class.
    • 3.3.A.3 في هذا المقرر، يتم تحديد المُشكِّلات دائماً بـ public.
    • 3.3.A.4 المتغيرات العينية تنتمي إلى الكائن، ولكل كائن نسخة خاصة به من المتغير.
    • 3.3.A.5 يجب إبقاء الوصول إلى السمات داخلياً داخل الفئة لتحقيق التغليف. لذلك، فإن ممارسة البرمجة الجيدة هي تحديد متغيرات الكائن الخاصة بهذه السمات بـ private إلا إذا نصت مواصفات الفئة على ما يخالف ذلك.
    • 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:

    العربية

    الفئة لها ثلاثة أجزاء: متغيرات实例 (حقول – بيانات الكائن)، ومُنشئات (بناء الكائنات)، ودوال (سلوك). الحقول عادةً ما تكون private؛ الدوال عادةً ما تكون public:

    مخطط فئة: سمات خاصة وطرق عامة
    مخطط فئة: سمات خاصة وطرق عامة
    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 · ⁨رshan حقول كائن كصناديق⁩

    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. · ⁨الصنف يجمع البيانات ذات الصلة (حقوله) والدوال. يحصل كل كائن على مجموعة خاصة به من صناديق الحقول؛ التعيين في واحد يؤثر على ذلك الكائن فقط.⁩

    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    Abstraction/əbˈstrækʃn/ التجريد
    Encapsulation/ɪnˌkæpsjʊˈleɪʃn/ التغليف
    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/ المصدر المفتوح
    instance variables/ˈɪnstəns ˈveərɪəblz/ المتغيرات العينية
    constructor/kənˈstrʌktə/ الدالة الإنشائية
    overloading/ˌəʊvəˈləʊdɪŋ/ إعادة التحميل
    accessor (getter)/əkˈsesə/ دالة حاصلة (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 يشير حالة الكائن إلى سماته وقيمتها عند وقت معين، وهي معرفة بواسطة المتغيرات العينية التابعة للكائن. وهذا يحدد علاقة "يحتوي على" بين الكائن ومتغيراته العينية.
    • 3.4.A.2 يُستخدم المُشَكِّل لتحديد الحالة الأولية للكائن، والتي يجب أن تشمل قيمًا أولية لجميع المتغيرات العينية. عند استدعاء المُشَكِّل، يتم تخصيص الذاكرة للكائن وإرجاع مرجع الكائن المرتبط به. توفر معاملات المُشَكِّل، إن تم تحديدها، بيانات لتهيئة المتغيرات العينية.
    • 3.4.A.3 عندما يكون الكائن القابل للتعديل هو معلمة مُشَكِّل، يجب تهيئة المتغير العيني بنسخة من الكائن المرجعي. وبهذه الطريقة، لا يحمل المتغير العيني مرجعاً للكائن الأصلي، ويتم منع الدوال من تعديل حالة الكائن الأصلي.
    • 3.4.A.4 عند عدم كتابة مُشَكِّل، يوفر Java مُشَكِّل بدون معلمات، وتُضبط المتغيرات العينية على القيم الافتراضية وفقاً لنوع بيانات السمة. يُسمى هذا المُشَكِّل "المُشَكِّل الافتراضي".
    • 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، ومهمته تهيئة الحقول. يمكن للفئة أن تحتوي على عدة منشئات بقوائم معاملات مختلفة (إعادة التحميل)؛ المنشئ بدون معاملات يحدد قيم افتراضية.

    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 لا تُرجع قيمة. يحتوي رأسها على الكلمة void قبل اسم الدالة.
    • 3.5.A.2 الدالة غير الفارغة تُرجع قيمة واحدة. يتضمن رأسها نوع الإرجاع بدلاً من الكلمة void.
    • 3.5.A.3 في الدوال غير الفارغة، يتم تقييم تعبير إرجاع متوافق مع نوع الإرجاع، وتُرجع القيمة. يُشار إلى هذا باسم "الإرجاع بالقيمة".
    • 3.5.A.4 تُستخدم الكلمة return لإرجاع تدفق التحكم إلى النقطة التي تم فيها استدعاء الدالة أو المُشَكِّل. لن يتم تنفيذ أي كود يأتي تسلسلياً بعد جملة الإرجاع أبداً. تنفيذ جملة إرجاع داخل جملة اختيار أو تكرار سيوقف الجملة ويخرج من الدالة أو المُشَكِّل.
    • 3.5.A.5 دالة الحاصِل تسمح لكائنات الفئات الأخرى بالحصول على نسخة من قيمة المتغيرات العينية أو متغيرات الفئة. دالة الحاصل هي دالة غير فارغة.
    • 3.5.A.6 دالة المُعدِّل (المغيِّر) هي دالة تقوم بتغيير قيم المتغيرات العينية أو متغيرات الفئة. غالباً ما تكون دالة المُعدِّل دالة فارغة.
    • 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.

    العربية

    للدالة توقيع، ونوع إرجاع، وجسم. المُحَصِّل (getter) يُرجع معلومات دون تغيير الكائن؛ المُعدِّل (setter) يغير حقلًا. يجب أن تحتوي الدالة التي تُرجع قيمة على 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. · ⁨استدعاء دالة يدفع إطاراً يحتوي على معاملاتها؛ عندما يصل إلى return، يُزاح الإطار ويعود القيمة إلى المستدعي.⁩

    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    mutator (setter)/mjuːˈteɪtə/ دالة مُعدِّلة (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 عندما يكون الحجة مرجعًا إلى كائن، يتم تهيئة المعامل بنسخة من ذلك المرجع؛ ولا ينشئ نسخة مستقلة جديدة للكائن. إذا كان المعامل يشير إلى كائن قابل للتعديل، يمكن للطريقة أو المُنشئ استخدام هذا المرجع لتغيير حالة الكائن. يُعدّ عدم تعديل الكائنات القابلة للتعديل التي تُمرَر كمعاملات ممارسة برمجية جيدة ما لم يكن مطلوبًا في المواصفات.
    • 3.6.A.2 عندما تكون تعبير الإرجاع يقيّم إلى مرجع كائن، يتم إرجاع المرجع، وليس مرجعًا لنسخة جديدة من الكائن.
    • 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.

    العربية
    = ينسخ المرجع، وليس الكائن

    عما تمرر كائنًا لدالة، تقوم Java بنسخ الإشارة، لذا تعمل الدالة على نفس الكائن – التغييرات في حقوله مرئية للمُنادي. (النوويات تُنسخ بالقيمة، لذا فإن تغييراتها ليست.) يمكن للدالة أيضًا إرجاع إشارة إلى كائن. بما أن String غير قابل للتعديل، فإن تمريره آمن؛ تمرير كائن قابل للتعديل يسمح للدالة بتغييره.

    Java تمرر بالقيمة: تحصل الدالة على نسخة؛ التمرير الحقيقي بالإشارة، والذي تفتقر إليه Java، سمح لها بإعادة تعيين متغير المُنادي
    تقوم Java دائمًا بالتمرير بالقيمة (يسار): تحصل الدالة على نسخة من الإشارة. التمرير الحقيقي بالإشارة (يمين) – والذي تفتقر إليه Java – سمح للدالة بإعادة تعيين متغير المُنادي الخاص بها.

    مهارة الامتحان: تعرف أن تعديل حقول كائن داخل دالة يؤثر على الأصلي، لكن إعادة التعيين للمتغير (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) يجعل النسخة الخاصة بالدالة للإشارة تشير إلى كائن جديد – s المُنادي غير لامس – والسطر (3) يؤثر فقط على ذلك الكائن الجديد. بعد الاستدعاء، s.getScore() هي 100: تم تثبيت التعديل، ولم يحدث إعادة التعيين.

    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().

    العربية
    الحقول الساكنة مقابل متغيرات实例

    متغير instance (فئة) ساكن، مُشار إليه بـ static، يتم مشاركته بواسطة جميع كائنات الفئة – نسخة واحدة إجماليًا (مثل عداد لعدد الكائنات الموجودة). الدالة الساكنة تابعة للفئة ولا يمكنها استخدام متغيرات instance مباشرة. الوصول إليها باسم الفئة: 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.

    العربية

    النطاق هو المكان الذي يظهر فيه الاسم. متغير local مُعلن في دالة يوجد فقط بداخلها؛ معامل يوجد فقط في دالته؛ متغير instance مرئي طوال الكائن. تحكم تعديلات الوصول في الظهور عبر الفئات: private (هذه الفئة فقط) مقابل public (في أي مكان). متغيرات local تظلل الحقول بنفس الاسم – مصدر للأخطاء.

    متغير global مرئي في كل مكان؛ متغير local فقط داخل كتلته
    متغير global مرئي في كل مكان؛ متغير local فقط داخل كتلته
    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 كمتغير خاص يحمل مرجعًا إلى الكائن الحالي - وهو الكائن الذي يتم استدعاء طريقته أو مُنشئه.
    • 3.9.A.2 يمكن استخدام الكلمة this لتمرير الكائن الحالي作为 argument في استدعاء طريقة.
    • 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 لا تدعم التمرير بالمرجح).
    • مرر عبر المصفوفات وArrayLists بأمان — الحجم هو length مقابل .size()، وإزالة عنصر أثناء حلقة يزيح الفهارس.
    • تتبع الطريقة العودية لتحديد نتيجتها: ابحث أولاً عن الحالة الأساسية، ثم اتبع كل استدعاء عودي حتى قيمته الإرجاعية (كتابة الكود العودي خارج نطاق الامتحان).
    • اعرف مصطلحات الإرث — الفئة الأب، الفئة الابن، استبدال الطريقة، وأن كل فئة هي فئة ابن لـ 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 التحيز الخوارزمي يصف أخطاء منهجية ومتكررة في برنامج تنشئ نتائج غير عادلة لمجموعة معينة من المستخدمين.
    • 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⁩

    Server racks in a data centre — large collections of data raise ethical questions about collection and use
    Server racks in a data centre — large collections of data raise ethical questions about collection and use

    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.

    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    privacy/ˈprɪvəsi/ الخصوصية
    consent/kənˈsent/ موافقة
    bias/ˈbaɪəs/ تحيزاً
    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 مجموعة البيانات هي مجموعة من قطع المعلومات أو البيانات المحددة.
    • 4.2.A.2 يمكن معالجة مجموعات البيانات وتحليلها لحل مشكلة أو الإجابة على سؤال. عند تحليل مجموعات البيانات، يتم الوصول إلى القيم داخل المجموعة واستخدامها واحدة تلو الأخرى ثم معالجتها وفقاً للنتيجة المرغوبة.
    • 4.2.A.3 يمكن تمثيل البيانات في مخطط باستخدام جدول أو رسم بياني. يمكن استخدام هذا الرسم التخطيطي لتخطيط الخوارزمية التي سيتم استخدامها لمعالجة البيانات.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    A filing cabinet: collections store many values under one name so algorithms can process them
    A filing cabinet: collections store many values under one name so algorithms can process them

    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.

    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    data structure/ˈdeɪtə ˈstrʌktʃə/ هيكل البيانات
    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 المصفوفة تخزن عدة قيم من نفس النوع. يمكن أن تكون هذه القيم إما قيم أولية أو مرجعات لكائنات.
    • 4.3.A.2 يتم تحديد طول المصفوفة وقت الإنشاء ولا يمكن تغييره. يمكن الوصول إلى طول المصفوفة من خلال السمة length.
    • 4.3.A.3 عند إنشاء مصفوفة باستخدام الكلمة المفتاحية new، يتم تهيئة جميع عناصرها إلى القيم الافتراضية لنوع بيانات العنصر. القيمة الافتراضية لـ int هي 0، ولـ double هي 0.0، ولـ boolean هي false، ولنوع المرجع هي null.
    • 4.3.A.4 يمكن استخدام قوائم المبدئات لإنشاء المصفوفات وتهيئتها.
    • 4.3.A.5 تُستخدم الأقواس المربعة [ ] للوصول إلى عنصر في مصفوفة 1D وتعديله باستخدام مؤشر.
    • 4.3.A.6 قيم الفهارس الصالحة للمصفوفة تتراوح بين 0 وما قبل الطول مباشرة، شاملاً الطرفين. استخدام قيمة فهرس خارج هذا النطاق سيؤدي إلى حدوث ArrayIndexOutOfBoundsException.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

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

    A one-dimensional array (a list) with its indices and bounds
    A one-dimensional array (a list) with its indices and bounds
    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)
    

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

    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    array/əˈreɪ/ المصفوفة (array)
    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: تطوير كود يُستخدم لتجول العناصر في مصفوفة 1D وتحديد نتائج هذا التجول.

    • 4.4.A.1 تجول المصفوفة هو عندما يتم استخدام عبارات التكرار للوصول إلى جميع أو تسلسل مرتب من العناصر في مصفوفة.
    • 4.4.A.2 يتطلب تجول المصفوفة باستخدام حلقة for ذات فهرس أو حلقة while الوصول إلى العناصر باستخدام فهارسها.
    • 4.4.A.3 يتضمن ترويسة حلقة for المحسنة متغيراً، يُشار إليه باسم متغير حلقة for المحسنة. لكل تكرار من حلقة for المحسنة، يتم تعيين متغير حلقة for المحسنة نسخة من عنصر دون استخدام فهرسه.
    • 4.4.A.4 لا يؤدي تعيين قيمة جديدة لمتغير حلقة for المحسنة إلى تغيير القيمة المخزنة في المصفوفة.
    • 4.4.A.5 عندما تخزن المصفوفة مرجعات للكائنات، يمكن تعديل الخصائص عن طريق استدعاء دوال على متغير حلقة for المحسنة. لا يغير هذا مرجعات الكائنات المخزنة في المصفوفة.
    • 4.4.A.6 يمكن إعادة كتابة الكود المكتوب باستخدام حلقة for محسنة لتجول العناصر في مصفوفة باستخدام حلقة for ذات فهرس أو حلقة while.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

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

    for (int i = 0; i < a.length; i++) { a[i] *= 2; }   // can modify
    for (int v : a) { System.out.println(v); }          // read each value
    
    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    Traverse/trəˈvɜːs/ المرور عبر
    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 توجد خوارزميات قياسية تستخدم تجوال المصفوفات لـ:
      • تحديد قيمة حد أدنى أو حد أعلى
      • حساب مجموع أو متوسط
      • تحديد ما إذا كان عنصر واحد على الأقل يمتلك خاصية معينة
      • تحديد ما إذا كانت جميع العناصر possesses خاصية معينة
      • تحديد عدد العناصر possessing خاصية معينة
      • الوصول إلى جميع الأزواج المتتالية من العناصر
      • تحديد وجود أو غياب عناصر مكررة
      • تحريك أو تدوير العناصر يسارًا أو يمينًا
      • عكس ترتيب العناصر

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    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) هو المنشئ File الذي يقبل اسم ملف String لفتحه للقراءة، حيث str هو المسار الكامل للملف.
    • 4.6.A.4 عند استخدام كلاسي File، يجب تحديد ما يجب فعله إذا لم يتمكن الملف ذو الاسم المعطى من الفتح. إحدى الطرق لتحقيق ذلك هي إضافة throws IOException إلى ترويسة الدالة التي تستخدم الملف. إذا كان اسم الملف غير صالح، سينتهي البرنامج.
    • 4.6.A.5 كلاسي File وكلاسي IOException هما جزء من حزمة java.io. يجب استخدام جملة import لجعل هذين الكلاسين متاحين للاستخدام في البرنامج.
    • 4.6.A.6 الدوال والمنشئات التالية من Scanner - بما فيها وظيفتها ومتى تُستخدم - جزء من مرجع جافا السريع:
      • Scanner(File f) هو المنشئ Scanner الذي يقبل مصدر إدخال File للقراءة.
      • int nextInt() يعيد الـ int التالي المقروء من الملف أو مصدر الإدخال إذا كان متاحاً. إذا لم يكن الـ int التالي موجوداً أو كان خارج نطاق، فسيؤدي ذلك إلى حدوث InputMismatchException.
      • double nextDouble() يعيد الـ double التالي المقروء من الملف أو مصدر الإدخال. إذا لم يكن الـ double التالي موجوداً، فسيؤدي ذلك إلى حدوث InputMismatchException.
      • boolean nextBoolean() يعيد الـ boolean التالي المقروء من الملف أو مصدر الإدخال. إذا لم يكن الـ boolean التالي موجوداً، فسيؤدي ذلك إلى حدوث InputMismatchException.
      • String nextLine() يعيد السطر التالي كنص على شكل String مقروء من الملف أو مصدر الإدخال؛ يمكن أن يعيد السلسلة الفارغة إذا تم استدعاؤها مباشرة بعد دالة Scanner أخرى تقرأ من الملف أو مصدر الإدخال.
      • String next() يعيد الـ String التالي المقروء من الملف أو مصدر الإدخال.
      • boolean hasNext() يعيد true إذا كان هناك عنصر آخر للقراءة في الملف أو مصدر الإدخال؛ تعيد false بخلاف ذلك.
      • void close() يغلق هذا القارئ (scanner).
      • جملة الاستبعاد: استقبال المدخلات من لوحة المفاتيح خارج نطاق منهج وامتحان علوم الحاسوب AP A.
    • 4.6.A.7 استخدام nextLine والدوال الأخرى من Scanner معاً على نفس مصدر الإدخال يتطلب أحياناً كوداً للتكيف مع الطرق المختلفة التي تتعامل بها هذه الدوال مع المسافات البيضاء.
      • جملة الاستبعاد: كتابة أو تحليل كود يستخدم كل من nextLine ودوال Scanner الأخرى على نفس مصدر الإدخال خارج نطاق منهج وامتحان علوم الحاسوب AP A.
    • 4.6.A.8 الدالة الإضافية التالية من String - بما فيها وظيفتها ومتى تُستخدم - جزء من مرجع جافا السريع:
      • String[] split(String del) يعيد مصفوفة String يكون كل عنصر فيها قطعة فرعية (substring) من this String، والتي تم تقسيمها حول تطابقات التعبير المعطى del.
      • جملة الاستبعاد: المعامل del يستخدم تنسيقاً يسمى تعبيراً نظامياً (regular expression). كتابة أو تحليل كود يستخدم أياً من الخصائص الخاصة للتعبيرات النظامية (مثل \\*، \\.) خارج نطاق منهج وامتحان علوم الحاسوب AP A.
    • 4.6.A.9 يمكن استخدام حلقة while للكشف عما إذا كان الملف لا يزال يحتوي على عناصر للقراءة عن طريق استخدام الدالة hasNext كشرط للحلقة.
    • 4.6.A.10 يجب إغلاق الملف عندما ينتهي البرنامج من استخدامه. يتم استدعاء الدالة close من Scanner لإغلاق الملف.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    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:

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

    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.

    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 counterparts) وتحديد نتيجة استخدام هذه الكائنات.

    • 4.7.A.1 كلاسي Integer وكلاسي Double هما جزء من حزمة java.lang. كائن Integer غير متغير (immutable)، مما يعني أنه بمجرد إنشاء كائن Integer، لا يمكن تغيير خصائصه. كائن Double غير متغير، مما يعني أنه بمجرد إنشاء كائن Double، لا يمكن تغيير خصائصه.
    • 4.7.A.2 التعبئة التلقائية (Autoboxing) هي التحويل التلقائي الذي يقوم به مترجم جافا بين الأنواع الأولية وفئات التغليف (wrapper classes) المقابلة لها. يشمل ذلك تحويل int إلى Integer وتحويل double إلى Double. يطبق مترجم جافا التعبئة التلقائية عندما تكون القيمة الأولية:
      • مُمررة كمعامل لدالة تتوقع كائناً من فئة التغليف المقابلة
      • مخصصة لمتغير من فئة التغليف المقابلة
    • 4.7.A.3 الفك التلقائي (Unboxing) هو التحويل التلقائي الذي يقوم به مترجم جافا من فئة التغليف إلى النوع الأولي. يشمل ذلك تحويل Integer إلى int وتحويل Double إلى double. يطبق مترجم جافا الفك التلقائي عندما يكون كائن فئة التغليف:
      • مُمرر كمعامل لدالة تتوقع قيمة من النوع الأولي المقابل
      • مخصص لمتغير من النوع الأولي المقابل
    • 4.7.A.4 الدالة التالية من الفئة Integer - بما فيها وظيفتها ومتى تُستخدم - جزء من مرجع جافا السريع:
      • static int parseInt(String s) تعيد الحجة String على شكل int.
    • 4.7.A.5 الدالة التالية من الفئة Double - بما فيها وظيفتها ومتى تُستخدم - جزء من مرجع جافا السريع:
      • static double parseDouble(String s) تعيد الحجة String على شكل double.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    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).

    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    autoboxing/ˌɔːtəʊˈbɒksɪŋ/ التغليف التلقائي
    4.8

    The ArrayList Toolbox

    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 بنوع عام ArrayList<E>، حيث يحدد المعامل النوعي E نوع العناصر. عند تحديد ArrayList<E>، تكون أنواع معلمات المرجع ونوع الإرجاع عند استخدام ArrayList دوال هي نوع E. يُفضل ArrayList<E> على ArrayList. على سبيل المثال، ArrayList<String> names = new ArrayList<String>(); يسمح للمترجم باكتشاف الأخطاء التي قد تُكتشف عادةً وقت التشغيل.
    • 4.8.A.4 فئة ArrayList جزء من حزمة java.util. يجب استخدام جملة import لجعل هذه الفئة متاحة للاستخدام في البرنامج.
    • 4.8.A.5 الدوال ArrayList التالية - بما في ذلك ما تقوم به ومتى يتم استخدامها - جزء من مرجع Java السريع:
      • 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⁩

    What an ArrayList really is

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

    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)
    
    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    ArrayList/əˈreɪ lɪst/ ArrayList
    4.9

    Visiting Every Element of an 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: تطوير أكواد تُستخدم لتجول عناصر ArrayList وتحديد نتائج هذه التجولات.

    • 4.9.A.1 تجول ArrayList هو عندما تُستخدم عبارات التكرار أو العودية للوصول إلى جميع العناصر أو تسلسل مرتب منها في ArrayList.
    • 4.9.A.2 حذف العناصر أثناء تجول ArrayList يتطلب استخدام تقنيات خاصة لتجنب تخطي العناصر.
    • 4.9.A.3 محاولة الوصول إلى قيمة فهرس خارج نطاقها ستؤدي إلى IndexOutOfBoundsException.
    • 4.9.A.4 تغيير حجم مصفوفة ArrayList أثناء مرورها باستخدام حلقة for محسّنة قد يؤدي إلى ConcurrentModificationException. لذلك، عند استخدام حلقة for محسّنة للمرور على مصفوفة ArrayList، لا يجب إضافة أو إزالة عناصر.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

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

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

    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.

    4.10

    Standard ArrayList Algorithms

    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 قياسية تستخدم التنقلات لـ:
      • تحديد قيمة حد أدنى أو حد أعلى
      • حساب مجموع أو متوسط
      • تحديد ما إذا كان عنصر واحد على الأقل يمتلك خاصية معينة
      • تحديد ما إذا كانت جميع العناصر possesses خاصية معينة
      • تحديد عدد العناصر possessing خاصية معينة
      • الوصول إلى جميع الأزواج المتتالية من العناصر
      • تحديد وجود أو غياب عناصر مكررة
      • تحريك أو تدوير العناصر يسارًا أو يمينًا
      • عكس ترتيب العناصر
      • إدراج عناصر
      • حذف عناصر
    • 4.10.A.2 تتطلب بعض الخوارزميات تنقلًا متزامنًا لعدة String، مصفوفة، أو ArrayList كائنات.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    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.

    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⁩

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

    A two-dimensional array (a table) with row and column indices
    A two-dimensional array (a table) with row and column indices
    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 بالصف والعمود⁩

    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 هي شبكة تُستند إلى [row][col]. حوِّع الفهارس وراقب الخلية التي يختارونها — الصف أولاً، ثم العمود، وكلاهما يبدأ العد من 0.⁩

    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    2D array/ˌtuː ˈdiː əˈreɪ/ مصفوفة 2D
    4.12

    Walking Through a Grid

    Syllabus · ⁨المنهج⁩
    English

    Learning Objective 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.
    العربية

    هدف التعلم 4.12.A: تطوير كود يُستخدم لتجول العناصر في مصفوفة 2D وتحديد نتائج هذا التجول.

    • 4.12.A.1 تُستخدم جمل التكرار المتداخلة لتجول والوصول إلى جميع العناصر أو تسلسل مرتب من عناصر مصفوفة 2D. نظرًا لأن المصفوفات 2D تُخزن كمصفوفات من المصفوفات، فإن طريقة تجول المصفوفات 2D باستخدام حلقات for وحلقات for المعززة مشابهة لأشياء المصفوفات 1D. يمكن كتابة جمل تكرار متداخلة لتجول المصفوفة 2D بترتيب يعتمد على الصفوف (row-major) أو ترتيب يعتمد على الأعمدة (column-major)، أو بترتيب محدد فريدًا. يشير ترتيب الصفوف إلى ترتيب عناصر المصفوفة 2D حيث يتم التجول عبر كل صف، بينما يحدث تجول ترتيب الأعمدة لأسفل كل عمود.
    • 4.12.A.2 الحلقة الخارجية لحلقة متداخلة معززة for تستخدم لتجول مصفوفة 2D تجول الصفوف. لذلك، يجب أن تكون متغير الحلقة المعززة for هو نوع كل صف، وهو مصفوفة 1D. الحلقة الداخلية تجول صفًا واحدًا. لذلك، يجب أن يكون متغير الحلقة المعززة for الداخلية هو نفس نوع العناصر المخزنة في المصفوفة 1D. تعيين قيمة جديدة لمتغير الحلقة المعززة for لا يغير القيمة المخزنة في المصفوفة.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    Traversing a 2-D array

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

    for (int r = 0; r < grid.length; r++)
        for (int c = 0; c < grid[0].length; c++)
            System.out.print(grid[r][c]);
    
    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    row-major order/rəʊ ˈmeɪdʒə ˈɔːdə/ ترتيب الصفوف أولاً
    4.13

    Standard 2D Array Algorithms

    Syllabus · ⁨المنهج⁩
    English

    Learning Objective 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
    العربية

    هدف التعلم 4.13.A: تطوير كود لخوارزميات قياسية وأصلية سياقية معينة أو مواصفة تتضمن مصفوفات 2D وتحديد نتائج هذه الخوارزميات.

    • 4.13.A.1 هناك خوارزميات قياسية تستغل تجول المصفوفات 2D لـ:
      • تحديد قيمة حد أدنى أو حد أعلى لجميع العناصر أو لصف أو عمود مخصص أو قسم فرعي آخر
      • حساب مجموع أو متوسط لجميع العناصر أو لصف أو عمود مخصص أو قسم فرعي آخر
      • تحديد ما إذا كان عنصر واحد على الأقل يمتلك خاصية معينة في المصفوفة 2D بأكملها أو لصف أو عمود مخصص أو جزء فرعي آخر
      • تحديد ما إذا كانت جميع عناصر المصفوفة 2D أو صف أو عمود مخصص أو جزء فرعي آخر possesses خاصية معينة
      • تحديد عدد العناصر في المصفوفة 2D أو في صف أو عمود مخصص أو جزء فرعي آخر possessing خاصية معينة
      • الوصول إلى جميع الأزواج المتتالية من العناصر
      • تحديد وجود أو عدم وجود عناصر مكررة في المصفوفة 2D أو في صف أو عمود مخصص أو جزء فرعي آخر
      • تحريك أو تدوير عناصر في صف يساراً أو يميناً أو في عمود فوق أو تحت
      • عكس ترتيب العناصر في صف أو عمود

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    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.

    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: تطوير الكود المستخدم لخوارزميات البحث الخطي للبحث عن معلومات محددة في مجموعة وتحديد نتائج تنفيذ عملية البحث.

    • 4.14.A.1 خوارزميات البحث الخطي هي خوارزميات قياسية تفحص كل عنصر بالترتيب حتى يتم العثور على القيمة المطلوبة أو يتم فحص جميع عناصر المصفوفة أو ArrayList. يمكن لخوارزميات البحث الخطي بدء عملية البحث من أي طرف للمصفوفة أو ArrayList.
    • 4.14.A.2 عند تطبيق خوارزميات البحث الخطي على المصفوفات 2D، يجب الوصول إلى كل صف ثم تطبيق البحث الخطي على كل صف من مصفوفة 2D.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    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.
    Binary search halves the range at each step
    Binary search halves the range at each step
    Linear search checks every element in turn until the target is found
    Linear search checks every element in turn until the target is found
    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;
    }
    

    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.

    Explore · ⁨استكشف⁩

    Compare linear and 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. · ⁨البحث الخطي يفحص كل عنصر بالتتابع؛ البحث الثنائي يقصّر قائمة مرتبة نصفها في كل خطوة. راقب كيف يصل البحث الثنائي للهدف مقارنة أقل بكثير.⁩

    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

    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 فرز الاختيار وفرز الإدراج هي خوارزميات فرز تكرارية يمكن استخدامها لترتيب عناصر في مصفوفة أو ArrayList.
    • 4.15.A.2 فرز الاختيار يختار بشكل متكرر أصغر (أو أكبر) عنصر من الجزء غير المرتب من القائمة ويبدله في موقعه الصحيح (والنهائي) في الجزء المرتب من القائمة.
    • 4.15.A.3 الترتيب بالإدراج يقوم بإدراج عنصر من الجزء غير المرتب في قائمة إلى موقعه الصحيح (وليس بالضرورة النهائي) في الجزء المرتب من القائمة عن طريق إزاحة عناصر الجزء المرتب لتوفير مساحة للعنصر الجديد.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    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.
    An insertion sort, shifting each key into place pass by pass
    An insertion sort, shifting each key into place pass by pass

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

    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. · ⁨الفرز يعيد ترتيب العناصر حسب الترتيب. مرّ باختيار/إدراج الفرز لرؤية المنطقة المرتبة تنمو عنصراً واحداً في كل مرة.⁩

    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    Selection sort/sɪˈlekʃn sɔːt/ فرز الاختيار
    Insertion sort/ɪnˈsɜːʃn sɔːt/ ترتيب الإدراج
    4.16

    Methods That Call Themselves: 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 الدالة ذات الاستدعاء الذاتي هي دالة تستدعي نفسها. تحتوي الدوال ذات الاستدعاء الذاتي على حالة أساسية واحدة على الأقل، توقف عملية الاستدعاء الذاتي، ونداء ذاتي واحد على الأقل. يعد الاستدعاء الذاتي شكلًا آخر من أشكال التكرار.
    • 4.16.A.2 كل نداء ذاتي له مجموعة خاصة به من المتغيرات المحلية، بما في ذلك المعاملات. قيم المعاملات تلتقط تقدم العملية ذاتية الاستدعاء، تمامًا كما تلتقط قيم متحكم الحلقة تقدم الحلقة.
    • 4.16.A.3 يمكن تكرار أي حل ذاتي الاستدعاء باستخدام نهج تكراري والعكس صحيح.
      • بيان الاستبعاد: كتابة كود ذاتي الاستدعاء خارج نطاق منهج امتحان علوم الحاسوب AP A.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    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:

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

    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:

    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;
    }
    

    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.

    Explore · ⁨استكشف⁩

    Unfold a 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. · ⁨الدالة التكرارية تستدعي نفسها على إدخال أصغر حتى تصل إلى حالة أساسية، ثم تعود النتائج للأعلى. مرّ بالخطوات لرؤية تراكم الاستدعاءات وفكها.⁩

    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    Recursion/rɪˈkɜːʃn/ العودية
    base case/beɪs keɪs/ الحالة الأساسية
    4.17

    Recursive Search and Merge Sort

    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: تحديد نتيجة كل تكرار لخوارزمية البحث الثنائي المستخدمة للبحث عن معلومات في مجموعة.

    • 4.17.B.1 يجب أن تكون البيانات مرتبة لاستخدام خوارزمية البحث الثنائي. يبدأ البحث الثنائي من منتصف مصفوفة مرتبة أو ArrayList ويستبعد نصف المصفوفة أو ArrayList في كل نداء ذاتي حتى يتم العثور على القيمة المطلوبة أو تم استبعاد جميع العناصر.
    • 4.17.B.2 عادةً ما يكون البحث الثنائي أكثر كفاءة من البحث الخطي.
      • بيان الاستبعاد: خوارزميات البحث الأخرى غير البحث الخطي والثنائي خارج نطاق منهج امتحان علوم الحاسوب AP A.
    • 4.17.B.3 يمكن كتابة خوارزمية البحث الثنائي إما بطريقة تكرارية أو ذاتية الاستدعاء.

    هدف التعلم 4.17.C: تحديد نتيجة كل تكرار لخوارزمية ترتيب الدمج عند استخدامها لترتيب مجموعة.

    • 4.17.C.1 ترتيب الدمج هو خوارزمية ترتيب ذاتية الاستدعاء يمكن استخدامها لترتيب عناصر في مصفوفة أو ArrayList.
      • بيان الاستبعاد: خوارزميات الترتيب الأخرى غير ترتيب التحديد والإدراج ودمج الترتيب خارج نطاق منهج امتحان علوم الحاسوب AP A.
    • 4.17.C.2 يقوم ترتيب الدمج بتقسيم المصفوفة بشكل متكرر إلى مصفوفات فرعية أصغر حتى تصبح كل مصفوفة فرعية عنصراً واحداً، ثم يدمج المصفوفات الفرعية المرتبة ذاتياً معاً بترتيب مرتب لتشكيل المصفوفة النهائية المرتبة.

    Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

    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.

    Merge sort splits the array to single elements, then merges sorted halves back up
    Merge sort splits the array to single elements, then merges sorted halves back up

    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.

    Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
    English العربية
    Merge sort/mɜːdʒ sɔːt/ فرز الدمج
    4.17

    Exam tips

    • 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.

Log in or create account · ⁨تسجيل الدخول أو إنشاء حساب⁩

IGCSE, A-Level & AP