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
תרגילים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.
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.
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.
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.
התוכנית מהתזכורת לא מתקמפלת. הוסיפו 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;
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
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.
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; } },