كل المقررات

الرئيسية / مقرر تطبيقات الموبايل / الفصل 6 الجزء 2

الفصل 6 (الجزء 2): Constructors, overloading and recursion

How objects start, how names repeat, and how a method calls itself

على طول: الـ constructor بيبدأ الكائن، والـ overloading بيكرر الاسم، والـ recursion بينادي نفسه.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

رسوم متحركة

الـ Call Stack والـ Recursion

أمثلة كود

أمثلة قصيرة وبسيطة

كل مثال وراه زرار «شغّل خطوة بخطوة»: بيعلّم على السطر اللي بيتنفّذ ويطبع الناتج زي ما بيتنفّذ بالظبط. ده شرح تنفيذ الكود سطر بسطر، مش مترجم حقيقي جوه الصفحة.

المثال ١: كلاس بكونستراكتور

class Note {    String text;    boolean done;     Note(String text) {        this.text = text;        this.done = false;    }}Note n = new Note("Buy milk");System.out.println(n.text + " " + n.done);

    

المثال ٢: نفس الاسم، باراميترز مختلفة

static int add(int a, int b) { return a + b; }static int add(int a, int b, int c) { return a + b + c; }static double add(double a, double b) { return a + b; }System.out.println(add(2, 3));System.out.println(add(2, 3, 4));System.out.println(add(2.5, 3.5));

    

المثال ٣: العامل المضروب بالـ recursion

static int fact(int n) {    if (n == 0) return 1;    return n * fact(n - 1);}System.out.println(fact(3));

    

اختبار قصير

اختبر فهمك

1. A constructor has:

2. Method overloading means:

3. Every recursive method needs:

4. fact(3) with fact(n) = n * fact(n-1) and fact(0) = 1 gives:

5. Overriding requires:

تمارين

جرّب بإيدك

التمرين 1

اكتب كلاس Student بكونستراكتور بياخد الاسم والدرجة، واطبع الاسم والدرجة لكائن واحد.

Write a Student class with a constructor taking a name and a score, then print both for one object.

Starter code:

class Student {    // TODO: fields and constructor}

التمرين 2

اكتب دالة power بتحسب الأس بالـ recursion، واطبع power(2, 3).

Write a power method that computes a power by recursion, and print power(2, 3).

Starter code:

// TODO: power(base, exp) with a base case at exp == 0

مشروع صغير في آخر المحاضرة

بطاقة طالب بكونستراكتور

الداتا: students: our own three students with names and scores (Mona 88, Omar 72, Nour 95).

License / source: Our own data; Oracle constructors tutorial link only. Reference page

الخطوات

  1. اكتب كلاس Student فيه name و score وكونستراكتور بياخد الاتنين.
  2. اعمل تلات كائنات بالبيانات الجاهزة.
  3. اطبع لكل طالب اسمه ودرجته، وجنب الدرجة pass لو 60 أو أكتر.
  4. المتوقع تحت.

Starter code:

class Student {    String name;    int score;    Student(String name, int score) {        this.name = name;        this.score = score;    }}// TODO: three objects// TODO: loop over them and print name, score and pass/fail

مشروع نهاية الفصل

ملف المشروع العملي للفصل 6

ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.

افتح ملف المشروع (PDF)

Credits: the topic order follows the course notes "Mobile Application Development 1" by Dr. Muhammad Kashif Saeed (Applied College, Muhayil, King Khalid University), shared by the course owner. The explanations, diagrams, data and code in this lecture are our own; we copy nothing from the notes. Extra reference: Oracle Java tutorials: defining methods, constructors, overloading and recursion (docs.oracle.com/javase/tutorial).