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

Using Objects and Methods · ⁨استخدام الأدوات والأساليب⁩

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

Video lesson for this topic · ⁨درس فيديو لهذا الموضوع⁩ Open the video page · ⁨افتح صفحة الفيديو⁩
15:05

استخدام الأدوات والأساليب

إليك برنامجان بلغة جافا. الأول ينقصه فاصلة منقوطة. الثاني يقسم سبعة على اثنين ويخزن الإجابة في عدد صحيح. المحرر يوقف الـ…

English narration · English + 中文 subtitles burned in · ⁨سرد باللغة الإنجليزية · ترجمة مدمجة بالإنجليزية + الصينية⁩

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 كامل وقابل للتجميع – أعد النوع الصحيح وطابق ترويسة الدالة بدقة.

Interactive lessons on this topic · ⁨دروس تفاعلية حول هذا الموضوع⁩

Work through it step by step, with instant-check exercises. · ⁨ا-working عليه خطوة بخطوة، مع تمارين تحقق فوري.⁩

Past Papers · ⁨أوراق الامتحانات السابقة⁩

More topics in AP Computer Science A · ⁨AP علوم الحاسوب A⁩ · ⁨المزيد من المواضيع في AP Computer Science A · ⁨AP علوم الحاسوب A⁩⁩

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

IGCSE, A-Level & AP