انتقل إلى المحتوى
أمين أبوزيد
BEG-014

الدوال (Methods)

  • 8 درس
  • 94 دقيقة قراءة
  • 5 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • المعاملات (Parameters)
  • القيم المُرجَعة
  • التوقيعات (signatures)
  • التحميل الزائد (overloading)
  • النطاق (scope)
  • مكدس الاستدعاءات (call stack)
  • أساسيات الاستدعاء الذاتي (recursion)

تعلّم أولًا

94 دقيقة قراءة · 5 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Extract repeated or named logic into a method with clear parameters and a return type.
  2. Explain pass-by-value for primitives and for references.
  3. Overload methods and explain how Java picks one.
  4. Describe the call stack and trace a sequence of method calls.
  5. Write a simple recursive method with a correct base case (factorial, sum of digits).

الدروس

  1. 01لماذا الدوال: التسمية وإعادة الاستخدام11 د قراءة
  2. 02المعاملات والقيم المُرجَعة12 د قراءة
  3. 03توقيعات الدوال والتحميل الزائد11 د قراءة
  4. 04النطاق: أين تعيش المتغيرات12 د قراءة
  5. 05مكدس الاستدعاءات (call stack)12 د قراءة
  6. 06أساسيات الاستدعاء الذاتي (recursion)12 د قراءة
  7. 07عرض عملي: بناء الآلة الحاسبة دالة بعد دالة12 د قراءة
  8. 08عرض عملي: تتبّع الاستدعاء الذاتي على مكدس الاستدعاءات12 د قراءة

المختبر والتمارين

Lab

BEG-014-LAB1 · Command-line calculator (mini-project 2 of 3)

Outcomes: 1–5 · Time: ~3 h

  1. Build a menu-driven calculator: add, subtract, multiply, divide, power, factorial, quit.
  2. Write each operation as its own method that returns a result and does not print.
  3. Make power iterative and factorial recursive.
  4. Handle division by zero and negative factorial input.
  5. 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)

IDOutcomeTask
BEG-014-EX011Extract the repeated "area of a rectangle" code from a given program into area(w, h).
BEG-014-EX022Predict the output: a method does x = x * 2 on a parameter; the caller prints its own variable afterwards. Explain.
BEG-014-EX033Write overloaded print(int), print(String), print(int, int). Which is called for print("a")?
BEG-014-EX044Draw the call stack at the deepest point of a() calling b() calling c().
BEG-014-EX055Write a recursive sumOfDigits(int n).
BEG-014-EX065Write 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 the String version; 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 in StackOverflowError.
  • 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.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1A method that returns nothing has return type:O1
  2. 2A good method usually:O1
  3. 3A method changes its int parameter. The caller's variable:O2
  4. 4Java passes arguments:O2
  5. 5Overloaded methods must differ in:O3
  6. 6max(2.5, 1.0) with max(int,int) and max(double,double) calls:O3
  7. 7The call stack holds:O4
  8. 8When a method returns, its frame is:O4
  9. 9A recursive method needs:O5
  10. 10Recursion without a base case ends in:O5

الجزء العملي

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

اكتب للبحث في المستويات والمراحل والوحدات والدروس.