== מול equals, החוזה בין equals ל-hashCode, ולמה HashSet מתבלבל בלעדיהם. toString שעוזר לדבג.
== vs equals, the contract between equals and hashCode, and why a HashSet gets confused without them. A toString that helps debugging.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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
toString לנקודהtoString for a point
הוסיפו ל-Point את toString שמחזירה (3, 4). הדפיסו את הנקודה לבד, בתוך מחרוזת (Point: (3, 4)), ובתוך רשימה: [(1, 2), (3, 4)].
Add a toString to Point returning (3, 4). Print the point alone, inside a string (Point: (3, 4)), and inside a list: [(1, 2), (3, 4)].
איך הפלט אמור להיראות?What should the output look like?
(3, 4)
Point: (3, 4)
[(1, 2), (3, 4)]
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:@Override public String toString(): חייבת להיות public.@Override public String toString(): it must be public.
צייד באגים: equals שלא נקראתBug hunter: an equals nobody calls
shelf.contains(new Book("Emma")) מחזירה false, למרות שיש equals. הוסיפו @Override מעל ה-equals ותראו מה javac אומר, ואז תקנו כך שיודפס true.
shelf.contains(new Book("Emma")) returns false, even though there's an equals. Add @Override above the equals and see what javac says, then fix it so it prints true.
איך הפלט אמור להיראות?What should the output look like?
true
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:contains קוראת ל-equals(Object), ו-equals(Book) היא מתודה אחרת (overloading, לא overriding).contains calls equals(Object), and equals(Book) is a different method (overloading, not overriding).
רמזHint 2:public boolean equals(Object o) { return o instanceof Book other && title.equals(other.title); }public boolean equals(Object o) { return o instanceof Book other && title.equals(other.title); }
תלמידים שווים אם יש להם אותו id (גם אם השם נכתב אחרת). כתבו equals ו-hashCode לפי id בלבד. הוסיפו ל-HashSet את 101 Noa, 102 Avi, 101 Noa L., והדפיסו 2 ואז true (ל-contains של 102).
Students are equal if they have the same id (even if the name is spelled differently). Write equals and hashCode by id only. Add 101 Noa, 102 Avi, 101 Noa L. to a HashSet, and print 2 then true (for contains of 102).
איך הפלט אמור להיראות?What should the output look like?
2
true
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.
HashMap<Point, String> שומר שמות של מקומות. כדי ש-get(new Point(1, 2)) ימצא את home, ל-Point צריך equals ו-hashCode. הדפיסו home ואז nothing (ל-(9, 9) עם getOrDefault).
A HashMap<Point, String> stores place names. For get(new Point(1, 2)) to find home, Point needs equals and hashCode. Print home then nothing (for (9, 9) with getOrDefault).
איך הפלט אמור להיראות?What should the output look like?
home
nothing
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:o instanceof Point p && x == p.x && y == p.yo instanceof Point p && x == p.x && y == p.y
static int countCopies(List<Book> shelf, Book wanted) סופרת כמה ספרים ברשימה שווים (equals) ל-wanted. ספרים שווים אם הכותר והסופר זהים. עבור מדף עם Dune פעמיים, הדפיסו 2, ואז 0 עבור Dune של סופר אחר.
static int countCopies(List<Book> shelf, Book wanted) counts how many books in the list equal (equals) wanted. Books are equal if title and author match. For a shelf with Dune twice, print 2, then 0 for a Dune by another author.
איך הפלט אמור להיראות?What should the output look like?
2
0
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.
מדף, צעד 17: ספרים כפולים לא נכנסיםShelf, step 17: duplicates stay off
פרויקט מלווהCompanion project
ל-Book (כותר וסופר) הוסיפו equals, hashCode ו-toString (Dune by Herbert). הכניסו ל-LinkedHashSet את Dune, Emma ושוב Dune. add מחזירה false לכפול, ואז הדפיסו Skipped duplicate: Dune by Herbert. בסוף הדפיסו את המדף: [Dune by Herbert, Emma by Austen].
Add equals, hashCode and toString (Dune by Herbert) to Book (title and author). Put Dune, Emma and Dune again into a LinkedHashSet. add returns false for the duplicate, so print Skipped duplicate: Dune by Herbert. Finally print the shelf: [Dune by Herbert, Emma by Austen].
איך הפלט אמור להיראות?What should the output look like?
Skipped duplicate: Dune by Herbert
[Dune by Herbert, Emma by Austen]
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:LinkedHashSet שומר את סדר ההכנסה, בשונה מ-HashSet.LinkedHashSet keeps insertion order, unlike HashSet.
Fraction(1, 2) ו-Fraction(2, 4) צריכים להיות שווים. צמצמו בבנאי (עם gcd), ואז equals ו-hashCode פשוטים עובדים. הדפיסו true true (סימטריה), ואז 2 1/2 (גודל ה-Set אחרי הוספת 2/4, ו-toString של 2/4).
Fraction(1, 2) and Fraction(2, 4) should be equal. Reduce in the constructor (with gcd), and then simple equals and hashCode work. Print true true (symmetry), then 2 1/2 (the Set's size after adding 2/4, and 2/4's toString).
איך הפלט אמור להיראות?What should the output look like?
true true
2 1/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:gcd(a, b) = b == 0 ? a : gcd(b, a % b)gcd(a, b) = b == 0 ? a : gcd(b, a % b)
רמזHint 2:אם תשוו num * other.den == other.num * den בלי לצמצם, יהיה קשה לכתוב hashCode תואם.If you compare num * other.den == other.num * den without reducing, a matching hashCode is hard to write.
ל-Shelf יש שם ורשימת Book. ה-toString שלו משתמש ב-toString של הרשימה, שקוראת ל-toString של כל ספר. הדפיסו Fiction(2): [Dune (1965), Emma (1815)].
Shelf has a name and a list of Book. Its toString uses the list's toString, which calls each book's toString. Print Fiction(2): [Dune (1965), Emma (1815)].
איך הפלט אמור להיראות?What should the output look like?
Fiction(2): [Dune (1965), Emma (1815)]
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.