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

JavaJava
11
חלקPart 2: מתודות ומערכיםMethods & arrays כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

דיבוג ב-IntelliJDebugging in IntelliJ

נקודות עצירה, Step Over ו-Step Into, צפייה במשתנים, קריאת stack trace ושיטה למציאת באגים.

Breakpoints, Step Over and Step Into, watching variables, reading a stack trace and a method for finding bugs.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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

1. ב-stack trace, איזו שורת at מראה איפה התוכנית נשברה בפועל?In a stack trace, which at line shows where the program actually broke?

2. עצרתם על שורה x = compute(5);. מה עושה Step Into (F7)?You stopped at x = compute(5);. What does Step Into (F7) do?

3. double avg = (2 + 3) / 2; מה הערך?double avg = (2 + 3) / 2; What's the value?

תרגיליםExercises

תרגילExercise 1

ממוצע שיוצא עגול מדיAn average that's too round

הממוצע של 2 ו-3 יוצא 2.0. מצאו למה ותקנו כך שיודפס 2.5.

The average of 2 and 3 comes out 2.0. Find out why and fix it so it prints 2.5.

איך הפלט אמור להיראות?What should the output look like?
2.5
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: בסימולציה, מה הערך של avg מיד אחרי השורה השלישית?In the simulation, what's avg right after the third line?
רמזHint 2: חלקו ב-2.0.Divide by 2.0.
לפתרוןSolution
תרגילExercise 2

האות האחרונהThe last letter

last("Java") אמורה להחזיר a, והתוכנית קורסת. קראו את ה-stack trace, מצאו את השורה ותקנו.

last("Java") should return a, and the program crashes. Read the stack trace, find the line and fix it.

איך הפלט אמור להיראות?What should the output look like?
a
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: ב-"Java" יש 4 אותיות. מה האינדקס של האחרונה?"Java" has 4 letters. What's the last one's index?
לפתרוןSolution
תרגילExercise 3

תנועה חסרהA missing vowel

ב-banana יש 3 תנועות, והתוכנית מדפיסה 2. הריצו את הסימולציה בעמוד הפתרונות (או Debug ב-IntelliJ עם נקודת עצירה בתוך הלולאה) ובדקו אילו ערכים i מקבל.

banana has 3 vowels, and the program prints 2. Run the simulation on the solutions page (or Debug in IntelliJ with a breakpoint inside the loop) and check which values i takes.

איך הפלט אמור להיראות?What should the output look like?
3
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: האם i מגיע ל-5, האות האחרונה?Does i reach 5, the last letter?
רמזHint 2: i < s.length()i < s.length()
לפתרוןSolution
תרגילExercise 4

מקסימום של שלילייםMax of negatives

max({-5, -2, -9}) מחזירה 0, שבכלל לא במערך. מצאו את הבאג ותקנו כך שיודפס -2.

max({-5, -2, -9}) returns 0, which isn't even in the array. Find the bug and fix it so it prints -2.

איך הפלט אמור להיראות?What should the output look like?
-2
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: מאיזה ערך best מתחיל? האם מספר שלילי יכול לעבור אותו?What value does best start at? Can a negative number beat it?
רמזHint 2: מתחילים מהאיבר הראשון: a[0].Start from the first item: a[0].
לפתרוןSolution
תרגילExercise 5

the שלא נמצאThe the that isn't found

התוכנית סופרת כמה פעמים the מופיע, ומדפיסה 0 במקום 2. עצרו בתוך ה-if והסתכלו: w הוא "the", ובכל זאת התנאי false. למה?

The program counts how many times the appears, and prints 0 instead of 2. Stop inside the if and look: w is "the", yet the condition is false. Why?

איך הפלט אמור להיראות?What should the output look like?
2
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: == משווה הפניות (האם זה אותו עצם), לא תוכן. split יוצר מחרוזות חדשות.== compares references (is it the same object), not content. split creates new strings.
רמזHint 2: w.equals("the")w.equals("the")
לפתרוןSolution
תרגילExercise 6

מדף, צעד 11: שלושה באגיםShelf, step 11: three bugs

פרויקט מלווהCompanion project

בקוד של המדף יש שלושה באגים. הראשון מפיל את התוכנית, ושני האחרים מסתתרים מאחוריו. תקנו אחד-אחד, והריצו אחרי כל תיקון. בסוף צריך להודפס Average: 4.333333333333333 ואז Found Emma.

The shelf code has three bugs. The first crashes the program, and the other two hide behind it. Fix them one at a time, running after each fix. In the end it should print Average: 4.333333333333333 then Found Emma.

איך הפלט אמור להיראות?What should the output look like?
Average: 4.333333333333333
Found Emma
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: באג 1: < במקום <=.Bug 1: < instead of <=.
רמזHint 2: באג 2: (double) sum / ratings.length.Bug 2: (double) sum / ratings.length.
רמזHint 3: באג 3: t.equals(wanted).Bug 3: t.equals(wanted).
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

הבאג לא איפה שהוא נשברThe bug isn't where it breaks

התוכנית קורסת ב-parsePrice. אבל parsePrice בסדר: הבעיה היא בקלט, "2,00" עם פסיק. קראו את כל ה-stack trace (parsePrice ← total ← main), ותקנו את parsePrice כך שתקבל גם פסיק. צריך להודפס 12.5.

The program crashes in parsePrice. But parsePrice is fine: the problem is the input, "2,00" with a comma. Read the whole stack trace (parsePrice ← total ← main), and fix parsePrice to accept a comma too. It should print 12.5.

איך הפלט אמור להיראות?What should the output look like?
12.5
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: text.replace(",", ".")text.replace(",", ".")
לפתרוןSolution
אתגרChallenge 2

ראשוני מדיToo prime

התוכנית מדפיסה ראשוניים עד 10 ויוצא 1 2 3 4 5 7. יש שני באגים ב-isPrime. היעזרו בסימולציה (Step Into ל-isPrime(4)) ותקנו כך שיודפס 2 3 5 7.

The program prints primes up to 10 and gets 1 2 3 4 5 7. There are two bugs in isPrime. Use the simulation (Step Into isPrime(4)) and fix it so it prints 2 3 5 7.

איך הפלט אמור להיראות?What should the output look like?
2 3 5 7
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: ל-4: n / 2 הוא 2, אז הלולאה i < 2 לא רצה בכלל.For 4: n / 2 is 2, so the loop i < 2 doesn't run at all.
רמזHint 2: 1 הוא לא ראשוני: if (n < 2) return false;.1 isn't prime: if (n < 2) return false;.
רמזHint 3: i * i <= n בודק עד השורש, וזה מספיק.i * i <= n checks up to the square root, which is enough.
לפתרוןSolution

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