מפגש 11: נקודות עצירה, Step Over ו-Step Into, צפייה במשתנים, קריאת stack trace ושיטה למציאת באגים.Session 11: Breakpoints, Step Over and Step Into, watching variables, reading a stack trace and a method for finding bugs.
נקודות עצירה, Step Over ו-Step Into, צפייה במשתנים, קריאת stack trace ושיטה למציאת באגים. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Breakpoints, Step Over and Step Into, watching variables, reading a stack trace and a method for finding bugs. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
מונחיםTerms
קוראים stack trace מלמעלהRead a stack trace from the top
הסימולציה היא דיבאגרThe simulation is a debugger
הדפסות דיבוגDebug prints
טעויות נפוצותCommon mistakes
פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
מונחיםTerms
המונחים של המפגשThis session's terms
דיבאגר: כלי שמריץ את התוכנית לאט, עוצר איפה שביקשתם ומראה את ערכי המשתנים.debugger: A tool that runs the program slowly, stops where you asked and shows the variables' values.
נקודת עצירה: נקודה אדומה שמסמנים בלחיצה ליד מספר השורה.breakpoint: A red dot you set by clicking next to a line number.
Step Over: מריץ את השורה הנוכחית כולה (גם אם יש בה קריאה למתודה) ועוצר בשורה הבאה.Step Over: Runs the current line completely (even if it calls a method) and stops at the next line.
Step Into: נכנס לתוך המתודה שנקראת בשורה הנוכחית, כדי לראות מה קורה בתוכה.Step Into: Goes into the method called on the current line, to see what happens inside it.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.
נושאTopic 2
קוראים stack trace מלמעלהRead a stack trace from the top
קוראים stack trace מלמעלהRead a stack trace from the top
השורה הראשונה אומרת מה קרה (ArithmeticException: / by zero).The first line says what happened (ArithmeticException: / by zero).
השורות at אומרות איפה: העליונה היא המקום שבו זה נשבר, והבאות הן מי שקרא לו.The at lines say where: the top one is where it broke, and the next ones are who called it.
מחפשים את השורה הראשונה מהקוד שלכם (Main.java:7) ומתחילים משם.Find the first line from your code (Main.java:7) and start there.
השורה הראשונה אומרת מה קרה (ArithmeticException: / by zero). השורות at אומרות איפה: העליונה היא המקום שבו זה נשבר, והבאות הן מי שקרא לו. מחפשים את השורה הראשונה מהקוד שלכם (Main.java:7) ומתחילים משם. שימו לב: הבאג האמיתי הוא שמישהו קרא ל-average עם מערך ריק.The first line says what happened (ArithmeticException: / by zero). The at lines say where: the top one is where it broke, and the next ones are who called it. Find the first line from your code (Main.java:7) and start there. Note: the real bug is that someone called average with an empty array.
נושאTopic 3
הסימולציה היא דיבאגרThe simulation is a debugger
הסימולציה היא דיבאגרThe simulation is a debugger
"צפו בזה רץ" עושה בדיוק מה ש-Step Over עושה ב-IntelliJ: שורה אחרי שורה, עם הערכים של המשתנים."Watch it run" does exactly what Step Over does in IntelliJ: line after line, with the variables' values.
כאן הסכום יוצא 10 במקום 15.Here the sum comes out 10 instead of 15.
עקבו אחרי i: הוא אף פעם לא מגיע ל-5.Follow i: it never reaches 5.
"צפו בזה רץ" עושה בדיוק מה ש-Step Over עושה ב-IntelliJ: שורה אחרי שורה, עם הערכים של המשתנים. כאן הסכום יוצא 10 במקום 15. עקבו אחרי i: הוא אף פעם לא מגיע ל-5. התנאי צריך להיות i <= 5."Watch it run" does exactly what Step Over does in IntelliJ: line after line, with the variables' values. Here the sum comes out 10 instead of 15. Follow i: it never reaches 5. The condition should be i <= 5.
שאלה לכיתהQuestion for the class
מה ידפיס הקוד?What will this print?
main
int sum =0;
for (int i =1; i <5; i++) {
sum += i;
}
System.out.println("1+2+3+4+5 = "+ sum);
נותנים לכיתה חצי דקה לנחש, ואז עוברים לשקופית הבאה. (זה הגוף של main.)Give the class half a minute to guess, then move on. (This is the body of main.)
הסימולציה היא דיבאגרThe simulation is a debugger
Main.java · main
int sum =0;
for (int i =1; i <5; i++) {
sum += i;
}
System.out.println("1+2+3+4+5 = "+ sum);
פלטOutput
1+2+3+4+5 = 10
מריצים ומשווים לניחושים.Run it and compare with the guesses.
נושאTopic 4
הדפסות דיבוגDebug prints
הדפסות דיבוגDebug prints
כשאין דיבאגר בהישג יד, מדפיסים את המשתנים בתוך הלולאה.When there's no debugger at hand, print the variables inside the loop.
מסמנים את השורות ב-DEBUG כדי למצוא ולמחוק אותן אחר כך.Mark the lines with DEBUG so you can find and delete them later.
כשאין דיבאגר בהישג יד, מדפיסים את המשתנים בתוך הלולאה. מסמנים את השורות ב-DEBUG כדי למצוא ולמחוק אותן אחר כך.When there's no debugger at hand, print the variables inside the loop. Mark the lines with DEBUG so you can find and delete them later.
נושאTopic 5
טעויות נפוצותCommon mistakes
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says
אלה הטעויות שהתרגילים מהסוג "צייד באגים" בנויים סביבן. כל אחת רצה באמת, והפלט הוא מה ש-Java עונה.These are the mistakes the "bug hunter" exercises are built around. Each one really runs, and the output is what Java says.
נושאTopic 6
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
מדף, צעד 11: שלושה באגיםShelf, step 11: three bugs
בקוד של המדף יש שלושה באגים. הראשון מפיל את התוכנית, ושני האחרים מסתתרים מאחוריו. תקנו אחד-אחד, והריצו אחרי כל תיקון. בסוף צריך להודפס 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.
דיבאגר: כלי שמריץ את התוכנית לאט, עוצר איפה שביקשתם ומראה את ערכי המשתנים.debugger: A tool that runs the program slowly, stops where you asked and shows the variables' values.
המונחים של המפגשThis session's terms
נקודת עצירה: נקודה אדומה שמסמנים בלחיצה ליד מספר השורה.breakpoint: A red dot you set by clicking next to a line number.
המונחים של המפגשThis session's terms
Step Over: מריץ את השורה הנוכחית כולה (גם אם יש בה קריאה למתודה) ועוצר בשורה הבאה.Step Over: Runs the current line completely (even if it calls a method) and stops at the next line.
המונחים של המפגשThis session's terms
Step Into: נכנס לתוך המתודה שנקראת בשורה הנוכחית, כדי לראות מה קורה בתוכה.Step Into: Goes into the method called on the current line, to see what happens inside it.
המונחים של המפגשThis session's terms
השורה הראשונה אומרת מה קרה (ArithmeticException: / by zero).The first line says what happened (ArithmeticException: / by zero).
קוראים stack trace מלמעלהRead a stack trace from the top
השורות at אומרות איפה: העליונה היא המקום שבו זה נשבר, והבאות הן מי שקרא לו.The at lines say where: the top one is where it broke, and the next ones are who called it.
קוראים stack trace מלמעלהRead a stack trace from the top
מחפשים את השורה הראשונה מהקוד שלכם (Main.java:7) ומתחילים משם.Find the first line from your code (Main.java:7) and start there.
קוראים stack trace מלמעלהRead a stack trace from the top
"צפו בזה רץ" עושה בדיוק מה ש-Step Over עושה ב-IntelliJ: שורה אחרי שורה, עם הערכים של המשתנים."Watch it run" does exactly what Step Over does in IntelliJ: line after line, with the variables' values.
הסימולציה היא דיבאגרThe simulation is a debugger
כאן הסכום יוצא 10 במקום 15.Here the sum comes out 10 instead of 15.
הסימולציה היא דיבאגרThe simulation is a debugger
עקבו אחרי i: הוא אף פעם לא מגיע ל-5.Follow i: it never reaches 5.
הסימולציה היא דיבאגרThe simulation is a debugger
כשאין דיבאגר בהישג יד, מדפיסים את המשתנים בתוך הלולאה.When there's no debugger at hand, print the variables inside the loop.
הדפסות דיבוגDebug prints
מסמנים את השורות ב-DEBUG כדי למצוא ולמחוק אותן אחר כך.Mark the lines with DEBUG so you can find and delete them later.
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says
באג 1:Bug 1:
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says
`NumberFormatException:`NumberFormatException:
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says
מדף, צעד 11: שלושה באגיםShelf, step 11: three bugs
הצעד של היום בפרויקט מדףToday's step in the Shelf project
ב-stack trace, איזו שורת at מראה איפה התוכנית נשברה בפועל?In a stack trace, which at line shows where the program actually broke?
העליונהThe top one
התחתונהThe bottom one
האמצעיתThe middle one
אין קשר לסדרOrder doesn't matter
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.
העליונהThe top one
נכון. מתחתיה: מי קרא לה.Right. Below it: who called it.
עצרתם על שורה x = compute(5);. מה עושה Step Into (F7)?You stopped at x = compute(5);. What does Step Into (F7) do?
נכנס לתוך compute ועוצר בשורה הראשונה שלהGoes into compute and stops at its first line
מדלג על השורה בלי להריץSkips the line without running it
מריץ עד הסוףRuns to the end
עוצר את התוכניתStops the program
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.
נכנס לתוך compute ועוצר בשורה הראשונה שלהGoes into compute and stops at its first line
נכון. Step Over היה מריץ את כל compute בבת אחת.Right. Step Over would run all of compute at once.
double avg = (2 + 3) / 2; מה הערך?double avg = (2 + 3) / 2; What's the value?
2.52.5
2.02.0
22
שגיאהAn error
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.
2.02.0
נכון: 5/2 בשלמים הוא 2, ורק אז הופך ל-2.0.Right: 5/2 in ints is 2, and only then becomes 2.0.
ממוצע שיוצא עגול מדיAn average that's too round
הממוצע של 2 ו-3 יוצא 2.0.The average of 2 and 3 comes out 2.0.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.
האות האחרונהThe last letter
last("Java") אמורה להחזיר a, והתוכנית קורסת.last("Java") should return a, and the program crashes.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.
תנועה חסרהA missing vowel
ב-banana יש 3 תנועות, והתוכנית מדפיסה 2.banana has 3 vowels, and the program prints 2.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.
מקסימום של שלילייםMax of negatives
max({-5, -2, -9}) מחזירה 0, שבכלל לא במערך.max({-5, -2, -9}) returns 0, which isn't even in the array.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.
the שלא נמצאThe the that isn't found
התוכנית סופרת כמה פעמים the מופיע, ומדפיסה 0 במקום 2.The program counts how many times the appears, and prints 0 instead of 2.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.
מדף, צעד 11: שלושה באגיםShelf, step 11: three bugs
בקוד של המדף יש שלושה באגים.The shelf code has three bugs.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.
מה למדנוWhat we learned
דיבאגר: כלי שמריץ את התוכנית לאט, עוצר איפה שביקשתם ומראה את ערכי המשתנים.debugger: A tool that runs the program slowly, stops where you asked and shows the variables' values.
השורה הראשונה אומרת מה קרה (ArithmeticException: / by zero).The first line says what happened (ArithmeticException: / by zero).
"צפו בזה רץ" עושה בדיוק מה ש-Step Over עושה ב-IntelliJ: שורה אחרי שורה, עם הערכים של המשתנים."Watch it run" does exactly what Step Over does in IntelliJ: line after line, with the variables' values.
כשאין דיבאגר בהישג יד, מדפיסים את המשתנים בתוך הלולאה.When there's no debugger at hand, print the variables inside the loop.