הגדרת מתודות, פרמטרים וערך החזרה, overloading, ומה באמת עובר למתודה: עותק של הערך, גם כשהערך הוא הפניה.
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.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
מתודה שמחשבת ריבועA method that squares
כתבו static int square(int n) שמחזירה את n בריבוע. ב-main הדפיסו square(7).
Write static int square(int n) that returns n squared. In main print square(7).
איך הפלט אמור להיראות?What should the output look like?
49
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:ההגדרה הולכת מחוץ ל-main אבל בתוך המחלקה.The definition goes outsidemain but inside the class.
Write static boolean isEven(int n). In main print isEven(10) and isEven(7) (two lines).
איך הפלט אמור להיראות?What should the output look like?
true
false
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:אפשר להחזיר ישר את התנאי: return n % 2 == 0;You can return the condition directly: return n % 2 == 0;
כתבו static int max(int a, int b), ואז static int max(int a, int b, int c) שמשתמשת בראשונה (overloading). הדפיסו max(4, 9, 6).
Write static int max(int a, int b), then static int max(int a, int b, int c) that uses the first one (overloading). Print max(4, 9, 6).
איך הפלט אמור להיראות?What should the output look like?
9
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:return a > b ? a : b;return a > b ? a : b;
כתבו static double discount(double price, int percent) שמחזירה את המחיר אחרי ההנחה. הדפיסו את discount(80, 25) עם printf ושתי ספרות: 60.00.
Write static double discount(double price, int percent) returning the price after the discount. Print discount(80, 25) with printf and two digits: 60.00.
איך הפלט אמור להיראות?What should the output look like?
60.00
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
צייד באגים: ההחלפה שלא עבדהBug hunter: the swap that didn't
swap אמורה להחליף בין שני המספרים, אבל main מדפיס 1 2. למה? תקנו כך ש-swap תקבל מערך של שני מספרים ותחליף בתוכו, ו-main ידפיס 2 1. (צפו ב-pass-by-value בתזכורת.)
swap should swap two numbers, but main prints 1 2. Why? Fix it so swap receives a two-number array and swaps inside it, and main prints 2 1. (See pass-by-value in the refresher.)
איך הפלט אמור להיראות?What should the output look like?
2 1
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:swap קיבלה עותקים, והחליפה רק אותם.swap got copies and swapped only those.
רמזHint 2:מערך מועבר כהפניה לאותו עצם: static void swap(int[] p) ומחליפים p[0] ו-p[1].An array is passed as a reference to the same object: static void swap(int[] p) and swap p[0] and p[1].
מדף, צעד 7: מתודות לספרShelf, step 7: book methods
פרויקט מלווהCompanion project
כתבו static String card(String title, int year, int stars) שמחזירה Dune (1965) **** (כוכב לכל דירוג), ו-static double average(int a, int b, int c). ב-main הדפיסו שני כרטיסים ואחר כך Average: 3.67 (ממוצע של 4, 3, 4) עם printf.
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). In main print two cards and then Average: 3.67 (the average of 4, 3, 4) with printf.
איך הפלט אמור להיראות?What should the output look like?
Dune (1965) ****
Emma (1815) ***
Average: 3.67
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:return title + " (" + year + ") " + "*".repeat(stars);return title + " (" + year + ") " + "*".repeat(stars);
כתבו static int digitSum(int n) (סכום הספרות, בלולאה) ו-static int digitCount(int n) (כמה ספרות). הדפיסו בשורה אחת 4721: 4 digits, sum 14.
Write static int digitSum(int n) (the sum of the digits, in a loop) and static int digitCount(int n) (how many digits). Print on one line 4721: 4 digits, sum 14.
איך הפלט אמור להיראות?What should the output look like?
4721: 4 digits, sum 14
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:while (n > 0) { sum += n % 10; n /= 10; }while (n > 0) { sum += n % 10; n /= 10; }
רמזHint 2:ב-digitCount: אותה לולאה, רק סופרים. (ל-0 יש ספרה אחת.)In digitCount: the same loop, just counting. (0 has one digit.)
כתבו static boolean isPrime(int n) (ראשוני: גדול מ-1 ומתחלק רק ב-1 ובעצמו). הדפיסו בשורה אחת את כל הראשוניים עד 12 עם רווחים: 2 3 5 7 11.
Write static boolean isPrime(int n) (prime: above 1 and divisible only by 1 and itself). Print on one line every prime up to 12, separated by spaces: 2 3 5 7 11.
איך הפלט אמור להיראות?What should the output look like?
2 3 5 7 11
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:בודקים מחלקים מ-2 עד i * i <= n.Check divisors from 2 while i * i <= n.
רמזHint 2:אם נמצא מחלק: return false; מיד.If a divisor turns up: return false; at once.