الدوال (Methods)
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- المعاملات (Parameters)
- القيم المُرجَعة
- التوقيعات (signatures)
- التحميل الزائد (overloading)
- النطاق (scope)
- مكدس الاستدعاءات (call stack)
- أساسيات الاستدعاء الذاتي (recursion)
تعلّم أولًا
94 دقيقة قراءة · 5 ساعة تدريب (تقدير)
مخرجات التعلم
- Extract repeated or named logic into a method with clear parameters and a return type.
- Explain pass-by-value for primitives and for references.
- Overload methods and explain how Java picks one.
- Describe the call stack and trace a sequence of method calls.
- Write a simple recursive method with a correct base case (factorial, sum of digits).
الدروس
- 01لماذا الدوال: التسمية وإعادة الاستخدام11 د قراءة
- 02المعاملات والقيم المُرجَعة12 د قراءة
- 03توقيعات الدوال والتحميل الزائد11 د قراءة
- 04النطاق: أين تعيش المتغيرات12 د قراءة
- 05مكدس الاستدعاءات (call stack)12 د قراءة
- 06أساسيات الاستدعاء الذاتي (recursion)12 د قراءة
- 07عرض عملي: بناء الآلة الحاسبة دالة بعد دالة12 د قراءة
- 08عرض عملي: تتبّع الاستدعاء الذاتي على مكدس الاستدعاءات12 د قراءة
المختبر والتمارين
Lab
BEG-014-LAB1 · Command-line calculator (mini-project 2 of 3)
Outcomes: 1–5 · Time: ~3 h
- Build a menu-driven calculator: add, subtract, multiply, divide, power, factorial, quit.
- Write each operation as its own method that returns a result and does not print.
- Make
poweriterative andfactorialrecursive. - Handle division by zero and negative factorial input.
- Keep reading input and printing in separate methods from the math.
Acceptance: no duplicated logic; division by zero handled; recursion has a base case.
Exercises (assessment: six exercises plus the calculator)
| ID | Outcome | Task |
|---|---|---|
| BEG-014-EX01 | 1 | Extract the repeated "area of a rectangle" code from a given program into area(w, h). |
| BEG-014-EX02 | 2 | Predict the output: a method does x = x * 2 on a parameter; the caller prints its own variable afterwards. Explain. |
| BEG-014-EX03 | 3 | Write overloaded print(int), print(String), print(int, int). Which is called for print("a")? |
| BEG-014-EX04 | 4 | Draw the call stack at the deepest point of a() calling b() calling c(). |
| BEG-014-EX05 | 5 | Write a recursive sumOfDigits(int n). |
| BEG-014-EX06 | 5 | Write recursive factorial(n) and say what happens without the base case. |
أظهر الحلول
- EX01
static double area(double w, double h) { return w * h; }, called wherever the code was repeated. - EX02 The caller's variable is unchanged; Java passes a copy of the value. (For an object reference the copy points to the same object.)
- EX03
print("a")matches theStringversion; Java picks the signature matching the argument types. - EX04 Bottom to top:
main,a,b,c(top, running). - EX05
if (n < 10) return n; return n % 10 + sumOfDigits(n / 10);Example: 123 gives 6. - EX06
if (n <= 1) return 1; return n * factorial(n - 1);Without the base case it recurses forever and ends inStackOverflowError. - Outcome 1's "clear parameters and return type" and the pass-by-value of references are also checked in the quiz and the calculator.
الاختبار
Outcomes: O1 extract methods · O2 pass-by-value · O3 overloading · O4 call stack · O5 recursion.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
Six exercises BEG-014-EX01 to EX06 plus the command-line calculator. Pass: at least five exercises correct and a calculator with no duplicated logic, handled division by zero, and a recursive factorial with a base case.
أخطاء شائعة
Methods that both compute and print; missing base case (stack overflow); expecting a method to change a caller's primitive.
التقييم
6 exercises plus the calculator.
الصلة بالمسار الوظيفي
Method design is the unit of clean code (JNR-003) and testing (BEG-045).