private ו-public, שמירה על מצב תקין, getters ו-setters במידה, ועצמים שלא משתנים.
private and public, keeping state valid, getters and setters in moderation, and objects that never change.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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 thermometer
מחלקה Thermometer עם private double celsius, getCelsius(), getFahrenheit() (מחושב, בלי שדה), ו-setCelsius שזורקת IllegalArgumentException מתחת ל-273.15-. קבעו 25, הדפיסו 25.0C = 77.0F, נסו 300- (ב-catch הדפיסו Rejected: והודעה), והדפיסו שוב את הצלזיוס.
A class Thermometer with private double celsius, getCelsius(), getFahrenheit() (computed, no field), and setCelsius throwing IllegalArgumentException below -273.15. Set 25, print 25.0C = 77.0F, try -300 (in the catch print Rejected: and the message), and print the celsius again.
איך הפלט אמור להיראות?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.
צייד באגים: מיליונר בקיצור דרךBug hunter: a shortcut millionaire
main מנסה לשים מיליון ישירות ביתרה, ו-javac עוצר. תקנו את main כך שיפקיד 100 דרך המתודה, ויודפס 100.
main tries to put a million straight into the balance, and javac stops it. Fix main to deposit 100 through the method, so it prints 100.
איך הפלט אמור להיראות?What should the output look like?
100
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.
Account עם private final String owner (מהבנאי), private int balance, ו: deposit (זורקת על סכום לא חיובי), withdraw (מחזירה false על סכום לא חיובי או גדול מהיתרה), getOwner, getBalance. בלי setter ליתרה. הפקידו 100 ל-Noa, משכו 30 ו-500, והדפיסו true false ו-Noa: 70.
Account with private final String owner (from the constructor), private int balance, and: deposit (throws on a non-positive amount), withdraw (returns false on a non-positive amount or one larger than the balance), getOwner, getBalance. No setter for the balance. Deposit 100 for Noa, withdraw 30 and 500, and print true false and Noa: 70.
איך הפלט אמור להיראות?What should the output look like?
true false
Noa: 70
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:אין setBalance: היתרה משתנה רק דרך הפקדה ומשיכה.No setBalance: the balance changes only through deposits and withdrawals.
כתבו את Point הבלתי משתנה מהתזכורת (שדות private final, x(), y(), moveBy). הזיזו את (1, 2) ב-(10, 0) ואז ב-(0, 5), בשרשרת אחת, והדפיסו 1,2 -> 11,7.
Write the immutable Point from the refresher (private final fields, x(), y(), moveBy). Move (1, 2) by (10, 0) and then by (0, 5), in one chain, and print 1,2 -> 11,7.
איך הפלט אמור להיראות?What should the output look like?
1,2 -> 11,7
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:p.moveBy(10, 0).moveBy(0, 5): כל קריאה מחזירה נקודה חדשה.p.moveBy(10, 0).moveBy(0, 5): every call returns a new point.
מחלקה בלתי משתנה Range(int min, int max) שהבנאי שלה זורק min 5 > max 1 כשהסדר הפוך. מתודות contains(int) ו-length(). הדפיסו עבור 10–20 את true false 10, ואחר כך את ההודעה של new Range(5, 1).
An immutable class Range(int min, int max) whose constructor throws min 5 > max 1 when the order is reversed. Methods contains(int) and length(). For 10–20 print true false 10, then the message of new Range(5, 1).
איך הפלט אמור להיראות?What should the output look like?
true false 10
min 5 > max 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:הבדיקה בבנאי מבטיחה שלא יהיה אף פעם Range שבור.The check in the constructor ensures there's never a broken Range.
מדף, צעד 15: דירוג תמיד בין 1 ל-5Shelf, step 15: a rating always 1–5
פרויקט מלווהCompanion project
ל-Book יש private final String title ו-private int rating = 3, getters, ו-setRating שזורקת Rating must be 1-5, got 7. קבעו 4, נסו 7 (הדפיסו את ההודעה), והדפיסו Dune 4.
Book has private final String title and private int rating = 3, getters, and setRating throwing Rating must be 1-5, got 7. Set 4, try 7 (print the message), and print Dune 4.
איך הפלט אמור להיראות?What should the output look like?
Rating must be 1-5, got 7
Dune 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:כמו בתזכורת, עם 7.As in the refresher, with 7.
מחלקה בלתי משתנה Money ששומרת long cents (ולא double, שמעגל). מתודות plus(Money), times(int) ו-format() (12.50). חשבו 12.50 כפול 3 ועוד 0.99, והדפיסו 12.50 x3 + 0.99 = 38.49.
An immutable class Money storing long cents (not a double, which rounds). Methods plus(Money), times(int) and format() (12.50). Compute 12.50 times 3 plus 0.99, and print 12.50 x3 + 0.99 = 38.49.
איך הפלט אמור להיראות?What should the output look like?
12.50 x3 + 0.99 = 38.49
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:other.cents מותר: private הוא לפי מחלקה, לא לפי עצם.other.cents is allowed: private is per class, not per object.
רשימה שאי אפשר לשנות מבחוץA list nobody changes from outside
Playlist מחזיקה private final List<String> songs. add דוחה שם ריק. getSongs() מחזירה עותק לקריאה בלבד (List.copyOf). ב-main הוסיפו שני שירים, נסו להוסיף דרך הרשימה שקיבלתם (ב-catch של UnsupportedOperationException הדפיסו can't change it from outside), והדפיסו [Song A, Song B].
Playlist holds private final List<String> songs. add rejects an empty name. getSongs() returns a read-only copy (List.copyOf). In main add two songs, try adding through the list you got (in a catch of UnsupportedOperationException print can't change it from outside), and print [Song A, Song B].
איך הפלט אמור להיראות?What should the output look like?
can't change it from outside
[Song A, Song B]
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.