נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
JavaJava

מתודות, overloading ו-pass-by-valueMethods, overloading & pass-by-value

מפגש 7: הגדרת מתודות, פרמטרים וערך החזרה, overloading, ומה באמת עובר למתודה: עותק של הערך, גם כשהערך הוא הפניה.Session 7: Defining methods, parameters and return values, overloading, and what really reaches a method: a copy of the value, even when the value is a reference.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • מגדירים וקוראיםDefine and call
  • void ומתודה שקוראת למתודהvoid, and a method calling a method
  • Overloading: אותו שם, פרמטרים שוניםOverloading: same name, different parameters
  • Pass-by-value, בזיכרוןPass-by-value, in memory
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • פרמטר: משתנה בהגדרת המתודה שמקבל ערך מבחוץ:parameter: A variable in the method's definition that receives a value from outside:
  • ארגומנט: הערך שנותנים לפרמטר בקריאה:argument: The value given to a parameter in a call:
  • ערך החזרה: התשובה שמתודה מחזירה עם return.return value: The answer a method hands back with return.
  • void: מתודה void לא מחזירה ערך:void: A void method returns no value:
  • מסגרת (frame): בכל קריאה למתודה נוצרת מסגרת חדשה ב-stack עם המשתנים שלה.stack frame: Every method call creates a new frame on the stack holding its variables.
  • העמסה (overloading): כמה מתודות עם אותו שם ופרמטרים שונים.overloading: Several methods with the same name and different parameters.
נושאTopic 2

מגדירים וקוראיםDefine and call

מגדירים וקוראיםDefine and call

  • מתודה היא פעולה עם שם.A method is a named action.
  • בהגדרה: טיפוס ההחזרה, שם ופרמטרים.In the definition: the return type, a name and parameters.
  • return מחזיר את התשובה.return hands back the answer.
נושאTopic 3

void ומתודה שקוראת למתודהvoid, and a method calling a method

void ומתודה שקוראת למתודהvoid, and a method calling a method

  • מתודה void לא מחזירה ערך.A void method returns nothing.
  • מתודות יכולות לקרוא זו לזו, וכל קריאה מקבלת מסגרת משלה.Methods can call each other, and each call gets its own frame.
נושאTopic 4

Overloading: אותו שם, פרמטרים שוניםOverloading: same name, different parameters

Overloading: אותו שם, פרמטרים שוניםOverloading: same name, different parameters

  • Java בוחרת איזו גרסה להפעיל לפי הטיפוסים ומספר הארגומנטים.Java picks which version to run from the types and the number of arguments.
נושאTopic 5

Pass-by-value, בזיכרוןPass-by-value, in memory

Pass-by-value, בזיכרוןPass-by-value, in memory

  • המתודה מקבלת עותק של כל ארגומנט.The method gets a copy of every argument.
  • x הוא עותק של 1, ולכן n לא משתנה.x is a copy of 1, so n doesn't change.
  • arr הוא עותק של ההפניה, ולכן הוא מצביע על אותו מערך כמו a, והשינוי ב-arr[0] נראה גם ב-main.arr is a copy of the reference, so it points to the same array as a, and the change to arr[0] shows up in main too.
נושאTopic 6

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 7: מתודות לספרShelf, step 7: book methods

פרמטר: משתנה בהגדרת המתודה שמקבל ערך מבחוץ:parameter: A variable in the method's definition that receives a value from outside:

ארגומנט: הערך שנותנים לפרמטר בקריאה:argument: The value given to a parameter in a call:

ערך החזרה: התשובה שמתודה מחזירה עם return.return value: The answer a method hands back with return.

void: מתודה void לא מחזירה ערך:void: A void method returns no value:

מסגרת (frame): בכל קריאה למתודה נוצרת מסגרת חדשה ב-stack עם המשתנים שלה.stack frame: Every method call creates a new frame on the stack holding its variables.

העמסה (overloading): כמה מתודות עם אותו שם ופרמטרים שונים.overloading: Several methods with the same name and different parameters.

מתודה היא פעולה עם שם.A method is a named action.

בהגדרה: טיפוס ההחזרה, שם ופרמטרים.In the definition: the return type, a name and parameters.

return מחזיר את התשובה.return hands back the answer.

מתודה void לא מחזירה ערך.A void method returns nothing.

מתודות יכולות לקרוא זו לזו, וכל קריאה מקבלת מסגרת משלה.Methods can call each other, and each call gets its own frame.

Java בוחרת איזו גרסה להפעיל לפי הטיפוסים ומספר הארגומנטים.Java picks which version to run from the types and the number of arguments.

המתודה מקבלת עותק של כל ארגומנט.The method gets a copy of every argument.

x הוא עותק של 1, ולכן n לא משתנה.x is a copy of 1, so n doesn't change.

arr הוא עותק של ההפניה, ולכן הוא מצביע על אותו מערך כמו a, והשינוי ב-arr[0] נראה גם ב-main.arr is a copy of the reference, so it points to the same array as a, and the change to arr[0] shows up in main too.

מדף, צעד 7: מתודות לספרShelf, step 7: book methods

מה הטיפוס של התשובה ש-static double half(int n) מחזירה?What type of answer does static double half(int n) return?

  • intint
  • doubledouble
  • voidvoid
  • halfhalf

doubledouble

מה ידפיס הקוד?What does it print?

  • 55
  • 66
  • שגיאהAn error
  • 00

55

איזו מתודה לא יכולה לחיות ליד static int f(int a) באותה מחלקה?Which method can not sit next to static int f(int a) in the same class?

  • static int f(int a, int b)static int f(int a, int b)
  • static int f(double a)static int f(double a)
  • static double f(int x)static double f(int x)
  • static int f(String s)static int f(String s)

static double f(int x)static double f(int x)

מתודה שמחשבת ריבועA method that squares

  • כתבו static int square(int n) שמחזירה את n בריבוע.Write static int square(int n) that returns n squared.

האם זוגי?Is it even?

  • כתבו static boolean isEven(int n).Write static boolean isEven(int n).

המקסימום מבין שלושהThe biggest of three

  • כתבו static int max(int a, int b), ואז static int max(int a, int b, int c) שמשתמשת בראשונה (overloading).Write static int max(int a, int b), then static int max(int a, int b, int c) that uses the first one (overloading).

מחיר אחרי הנחהPrice after discount

  • כתבו static double discount(double price, int percent) שמחזירה את המחיר אחרי ההנחה.Write static double discount(double price, int percent) returning the price after the discount.

צייד באגים: ההחלפה שלא עבדהBug hunter: the swap that didn't

  • swap אמורה להחליף בין שני המספרים, אבל main מדפיס 1 2.swap should swap two numbers, but main prints 1 2.

מדף, צעד 7: מתודות לספרShelf, step 7: book methods

  • כתבו static String card(String title, int year, int stars) שמחזירה Dune (1965) **** (כוכב לכל דירוג), ו-static double average(int a, int b, int c).Write static String card(String title, int year, int stars) returning Dune (1965) **** (a star per point), and static double average(int a, int b, int c).

ספרות של מספרA number's digits

  • כתבו static int digitSum(int n) (סכום הספרות, בלולאה) ו-static int digitCount(int n) (כמה ספרות).Write static int digitSum(int n) (the sum of the digits, in a loop) and static int digitCount(int n) (how many digits).

ראשוני?Prime?

  • כתבו static boolean isPrime(int n) (ראשוני:Write static boolean isPrime(int n) (prime:

מה למדנוWhat we learned

  • פרמטר: משתנה בהגדרת המתודה שמקבל ערך מבחוץ:parameter: A variable in the method's definition that receives a value from outside:
  • מתודה היא פעולה עם שם.A method is a named action.
  • מתודה void לא מחזירה ערך.A void method returns nothing.
  • Java בוחרת איזו גרסה להפעיל לפי הטיפוסים ומספר הארגומנטים.Java picks which version to run from the types and the number of arguments.
  • המתודה מקבלת עותק של כל ארגומנט.The method gets a copy of every argument.
  • מדף, צעד 7: מתודות לספרShelf, step 7: book methods

עכשיו לתרגולNow, practice

6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich