פרויקט קטן: המדף כמערכת מחלקותMini project: the shelf as classes
מתכננים ובונים יחד את Shelf, Book ו-Author: אחריות לכל מחלקה, ממשק נקי ובדיקה ידנית.
Designing and building Shelf, Book and Author together: one job per class, a clean interface and manual testing.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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 book that holds an author
כתבו את Author (שם, equals ו-hashCode לפי השם, toString שמחזיר את השם) ואת Book (כותר, Author, דירוג, getters, toString Dune (5)). הדפיסו Dune (5) by Herbert ואז true (הסופר שווה לסופר חדש באותו שם).
Write Author (a name, equals and hashCode by name, toString returning the name) and Book (a title, an Author, a rating, getters, toString Dune (5)). Print Dune (5) by Herbert then true (the author equals a new author with the same name).
איך הפלט אמור להיראות?What should the output look like?
Dune (5) by Herbert
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:השדה author ב-Book הוא מסוג Author, לא String.The author field in Book is of type Author, not String.
ב-Shelf (עם private final List<Book> books) כתבו boolean add(Book b) שמחזירה false ולא מוסיפה אם כבר יש ספר עם אותו כותר ואותו סופר, ו-int size(). הדפיסו true, false, 1.
In Shelf (with private final List<Book> books) write boolean add(Book b) that returns false and adds nothing if a book with the same title and author is already there, and int size(). Print true, false, 1.
איך הפלט אמור להיראות?What should the output look like?
true
false
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:משווים את הסופרים עם equals: זה סופר חדש באותו שם.Compare the authors with equals: it's a new author with the same name.
רמזHint 2:size() פשוט מחזירה books.size() (האצלה).size() just returns books.size() (delegation).
Book find(String title) מחזירה את הספר (בלי תלות באותיות גדולות וקטנות) או null. הדפיסו found Dune (5) עבור "dune", ואז not found עבור Hamlet.
Book find(String title) returns the book (ignoring upper and lower case) or null. Print found Dune (5) for "dune", then not found for Hamlet.
איך הפלט אמור להיראות?What should the output look like?
found Dune (5)
not found
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.
List<Book> byAuthor(Author a) מחזירה רשימה חדשה עם הספרים של הסופר. הוסיפו את Emma ו-Persuasion של Austen ואת Dune, והדפיסו [Emma (4), Persuasion (5)].
List<Book> byAuthor(Author a) returns a new list with the author's books. Add Austen's Emma and Persuasion and Dune, and print [Emma (4), Persuasion (5)].
איך הפלט אמור להיראות?What should the output look like?
[Emma (4), Persuasion (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:b.getAuthor().equals(a): כל ספר כאן קיבל עצם Author חדש, ולכן == לא היה עובד.b.getAuthor().equals(a): every book here got a new Author object, so == wouldn't work.
double averageRating() מחזירה את ממוצע הדירוגים, ו-0.0 למדף ריק (בלי חריגה). הדפיסו 0.0 לפני ההוספות ו-4.5 אחרי Emma (4) ו-Dune (5).
double averageRating() returns the average rating, and 0.0 for an empty shelf (no exception). Print 0.0 before adding and 4.5 after Emma (4) and Dune (5).
איך הפלט אמור להיראות?What should the output look like?
0.0
4.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:בודקים books.isEmpty() לפני החילוק.Check books.isEmpty() before dividing.
מדף, צעד 18: Shelf, Book ו-Author עובדים יחדShelf, step 18: Shelf, Book and Author working together
פרויקט מלווהCompanion project
חברו הכול: Shelf.add (בלי כפולים) ו-Shelf.report() שמדפיסה את מספר הספרים, את הכותרים לפי סופר (לפי סדר ההופעה), ואת הספר עם הדירוג הגבוה. עם Emma, Dune, Persuasion ושוב Emma, הדוח הוא:
Shelf: 3 books / Austen: Emma, Persuasion / Herbert: Dune / Best: Dune (5).
Put it all together: Shelf.add (no duplicates) and Shelf.report() printing the number of books, the titles per author (in order of appearance), and the top-rated book. With Emma, Dune, Persuasion and Emma again, the report is:
Shelf: 3 books / Austen: Emma, Persuasion / Herbert: Dune / Best: Dune (5).
איך הפלט אמור להיראות?What should the output look like?
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.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:LinkedHashMap<Author, List<String>> שומר סדר הופעה. Author צריך equals ו-hashCode כדי להיות מפתח.A LinkedHashMap<Author, List<String>> keeps order of appearance. Author needs equals and hashCode to be a key.
רמזHint 2:groups.computeIfAbsent(author, k -> new ArrayList<>()).add(title);groups.computeIfAbsent(author, k -> new ArrayList<>()).add(title);
הוסיפו ל-Shelf מעקב השאלות: Map<String, String> lentTo (כותר → שם). lend(title, person) מחזירה false אם הספר כבר מושאל, giveBack(title) מחזירה אותו, ו-status(title) מחזירה lent to Noa או on the shelf. הדפיסו true false, lent to Noa, on the shelf.
Add lending to Shelf: Map<String, String> lentTo (title → name). lend(title, person) returns false if the book is already lent, giveBack(title) returns it, and status(title) returns lent to Noa or on the shelf. Print true false, lent to Noa, on the shelf.
איך הפלט אמור להיראות?What should the output look like?
true false
lent to Noa
on the shelf
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:Book לא משתנה: המצב "מושאל" שייך למדף, לא לספר.Book doesn't change: the "lent" state belongs to the shelf, not the book.
כתבו ב-Main מתודת עזר static void check(boolean ok, String name) שמדפיסה PASS: ... או FAIL: ... וסופרת. בדקו 4 דברים על המדף: הוספה, דחיית כפול, גודל 1, וחיפוש בלי תלות באותיות. בסוף 4/4 passed.
In Main write a helper static void check(boolean ok, String name) printing PASS: ... or FAIL: ... and counting. Check 4 things about the shelf: adding, rejecting a duplicate, size 1, and case-insensitive find. Finally 4/4 passed.
איך הפלט אמור להיראות?What should the output look like?
PASS: add a new book
PASS: reject a duplicate
PASS: size is 1
PASS: find ignores case
4/4 passed
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.