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

JavaJava
23
חלקPart 4: OOP מתקדםAdvanced OOP כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

records, enums, sealed ו-pattern matchingrecords, enums, sealed & pattern matching

records לנתונים, enums עם התנהגות, sealed interfaces, ו-switch עם תבניות (Java 21).

records for data, enums with behavior, sealed interfaces, and switch with patterns (Java 21).

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. record P(int x, int y) {}. איך קוראים את x?record P(int x, int y) {}. How do you read x?

2. אפשר לשנות שדה של record אחרי שנוצר?Can you change a record's field after it's created?

3. מה היתרון של sealed ב-switch?What's the benefit of sealed in a switch?

תרגיליםExercises

תרגילExercise 1

נקודה כ-recordA point as a record

record Point(int x, int y) עם מתודה distance() ומתודה withX(int) שמחזירה נקודה חדשה. הדפיסו את (3, 4), אחר כך 5.0 3, ואז true Point[x=0, y=4].

record Point(int x, int y) with a distance() method and a withX(int) method returning a new point. Print (3, 4), then 5.0 3, then true Point[x=0, y=4].

איך הפלט אמור להיראות?What should the output look like?
Point[x=3, y=4]
5.0 3
true Point[x=0, y=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: בתוך record אפשר לכתוב מתודות רגילות. השדות זמינים בשמם.Inside a record you can write normal methods. The fields are available by name.
לפתרוןSolution
תרגילExercise 2

דירוג שבודק את עצמוA rating that checks itself

record Rating(int value) עם בנאי מקוצר שזורק Rating must be 1-5, got 9. הדפיסו Rating[value=4] ואז את ההודעה עבור 9.

record Rating(int value) with a compact constructor throwing Rating must be 1-5, got 9. Print Rating[value=4] then the message for 9.

איך הפלט אמור להיראות?What should the output look like?
Rating[value=4]
Rating must be 1-5, got 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: Rating { if (...) throw ...; }: בלי סוגריים אחרי השם.Rating { if (...) throw ...; }: no brackets after the name.
לפתרוןSolution
תרגילExercise 3

גדלים ומחיריםSizes and prices

enum Size { SMALL(10), MEDIUM(14), LARGE(18) } עם price(). הדפיסו כל גודל ומחיר (SMALL 10), ואז Total: 42, biggest: LARGE.

enum Size { SMALL(10), MEDIUM(14), LARGE(18) } with price(). Print each size and price (SMALL 10), then Total: 42, biggest: LARGE.

איך הפלט אמור להיראות?What should the output look like?
SMALL 10
MEDIUM 14
LARGE 18
Total: 42, biggest: LARGE
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: לבנאי של enum אין public, ו-Java קוראת לו לבד.An enum's constructor has no public, and Java calls it itself.
לפתרוןSolution
תרגילExercise 4

סוף שבועThe weekend

enum Day (ראשון עד שבת). static String kind(Day d) עם switch בחצים: שישי ושבת weekend, השאר workday. הדפיסו workday weekend, ואז 5 MONDAY (FRIDAY.ordinal() ו-valueOf).

enum Day (Sunday to Saturday). static String kind(Day d) with an arrow switch: Friday and Saturday weekend, the rest workday. Print workday weekend, then 5 MONDAY (FRIDAY.ordinal() and valueOf).

איך הפלט אמור להיראות?What should the output look like?
workday weekend
5 MONDAY
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: case FRIDAY, SATURDAY -> "weekend";case FRIDAY, SATURDAY -> "weekend";
לפתרוןSolution
תרגילExercise 5

צייד באגים: case חסרBug hunter: a missing case

התוכנית מהתזכורת לא מתקמפלת. הוסיפו case למשולש (בסיס × גובה ÷ 2) כך שיודפס 6.0.

The program from the refresher doesn't compile. Add a case for the triangle (base × height ÷ 2) so it prints 6.0.

איך הפלט אמור להיראות?What should the output look like?
6.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.
רמזHint 1: case Triangle t -> t.base() * t.height() / 2;case Triangle t -> t.base() * t.height() / 2;
לפתרוןSolution
תרגילExercise 6

מדף, צעד 23: סוגי פריטים כ-sealed ו-recordsShelf, step 23: item kinds as sealed types and records

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

sealed interface Item permits PrintBook, EBook, AudioBook, שלושתם records. describe(Item) עם פירוק records, ו-loanDays(Item) (ספר מודפס 14, ebook 21, ספר שמע 28 אם הוא מעל 600 דקות, אחרת 14). הדפיסו: Emma: print, 474 pages (14 days) / Dune: ebook, 2.5 MB (21 days) / Ulysses: audio, 12h30m (28 days).

sealed interface Item permits PrintBook, EBook, AudioBook, all three records. describe(Item) with record patterns, and loanDays(Item) (print 14, ebook 21, audio 28 if over 600 minutes, otherwise 14). Print: Emma: print, 474 pages (14 days) / Dune: ebook, 2.5 MB (21 days) / Ulysses: audio, 12h30m (28 days).

איך הפלט אמור להיראות?What should the output look like?
Emma: print, 474 pages (14 days)
Dune: ebook, 2.5 MB (21 days)
Ulysses: audio, 12h30m (28 days)
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: case EBook(String title, double mb) -> ...case EBook(String title, double mb) -> ...
רמזHint 2: שני switch, ואף אחד מהם לא צריך default.Two switches, and neither needs a default.
לפתרוןSolution

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

אתגרChallenge 1

איפה הנקודה?Where's the point?

static String where(Object o) עם switch על Object: נקודה בראשית (origin), על ציר x (on the x axis), על האלכסון (on the diagonal), נקודה אחרת (somewhere), מחרוזת (a string: hi), וכל השאר unknown. הדפיסו לכל אחד מ-(0,0), (5,0), (2,2), (1,7), "hi", 42.

static String where(Object o) with a switch on Object: a point at the origin (origin), on the x axis (on the x axis), on the diagonal (on the diagonal), another point (somewhere), a string (a string: hi), and anything else unknown. Print for each of (0,0), (5,0), (2,2), (1,7), "hi", 42.

איך הפלט אמור להיראות?What should the output look like?
origin
on the x axis
on the diagonal
somewhere
a string: hi
unknown
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: הסדר חשוב: ה-case הכי ספציפי ראשון.Order matters: the most specific case first.
רמזHint 2: על Object חייבים default, כי הוא לא sealed.On Object you need a default, because it isn't sealed.
לפתרוןSolution
אתגרChallenge 2

enum עם מתודה לכל ערךAn enum with a method per value

enum Op עם PLUS, MINUS, TIMES, סמל לכל אחד, ו-abstract int apply(int a, int b) שכל ערך מממש בגוף משלו. הדפיסו 6 + 3 = 9, 6 - 3 = 3, 6 * 3 = 18.

enum Op with PLUS, MINUS, TIMES, a symbol each, and abstract int apply(int a, int b) that each value implements in its own body. Print 6 + 3 = 9, 6 - 3 = 3, 6 * 3 = 18.

איך הפלט אמור להיראות?What should the output look like?
6 + 3 = 9
6 - 3 = 3
6 * 3 = 18
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: PLUS("+") { int apply(int a, int b) { return a + b; } },PLUS("+") { int apply(int a, int b) { return a + b; } },
לפתרוןSolution

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