Classes and objects, constructors, encapsulation, static and final, equals and hashCode
75 דקותminutes4 שאלות בחירהmultiple choice3 שאלות קודcoding questions100 נקודותpoints
75:00השעון רק עוזר להתאמן. אפשר גם בלי.The timer is just for practice. You can skip it.
חלק א: שאלות בחירה (5 נקודות לשאלה)Part A: multiple choice (5 points each)
התשובות מתגלות רק כשמסיימים את המבחן.Answers are revealed only when you finish the exam.
חלק ב: שאלות קודPart B: coding questions
שאלהQuestion 5
חשבון בנקA bank account
מחלקה Account עם יתרה private. deposit(double) זורקת IllegalArgumentException על סכום 0 או שלילי. withdraw(double) זורקת IllegalStateException כשאין מספיק כסף. getBalance(). main מדפיס 70.0.
An Account class with a private balance. deposit(double) throws IllegalArgumentException on 0 or a negative amount. withdraw(double) throws IllegalStateException when there isn't enough money. getBalance(). main prints 70.0.
איך הפלט אמור להיראות?What should the output look like?
70.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:בודקים לפני שמשנים.Check before you change.
יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.
מחווןRubric (25 נקודותpoints)
כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.
שאלהQuestion 6
נקודות בלי כפילויותPoints without duplicates
מחלקה Point עם x ו-y (final int). כתבו equals ו-hashCode כך שנקודות עם אותן קואורדינטות שוות, ו-toString שמחזיר (1, 2). main מכניס ל-HashSet את (1,2), (3,4), (1,2) ומדפיס 2.
A Point class with x and y (final int). Write equals and hashCode so points with the same coordinates are equal, and toString returning (1, 2). main puts (1,2), (3,4), (1,2) into a HashSet and prints 2.
איך הפלט אמור להיראות?What should the output look like?
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:Objects.hash(x, y).Objects.hash(x, y).
הפתרון שלנוOur solution
Main.java
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
publicclass Main {
publicstaticvoidmain(String[] args) {
Set<Point> points =newHashSet<>();
points.add(new Point(1, 2));
points.add(new Point(3, 4));
points.add(new Point(1, 2));
System.out.println(points.size());
}
}
class Point {
privatefinalint x;
privatefinalint y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
@Overridepublicbooleanequals(Object o) {
return o instanceof Point p && x == p.x && y == p.y;
}
@Overridepublicint hashCode() {
return Objects.hash(x, y);
}
@OverridepublicString toString() {
return"("+ x +", "+ y +")";
}
}
פלטOutput
2
יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.
מחווןRubric (25 נקודותpoints)
כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.
שאלהQuestion 7
ספרייה קטנהA small library
מחלקה Library שמחזיקה private List<String> של ספרים זמינים ו-private List<String> של מושאלים. add(title), lend(title) מחזירה true אם הספר זמין (ומעבירה אותו), אחרת false. giveBack(title) מחזירה true אם הוא היה מושאל. available() מחזירה כמה זמינים. main מדפיס true false 1 true 2.
A Library class holding a private List<String> of available books and a private List<String> of lent ones. add(title), lend(title) returns true if the book is available (and moves it), otherwise false. giveBack(title) returns true if it was lent. available() returns how many are available. main prints true false 1 true 2.
איך הפלט אמור להיראות?What should the output look like?
true false 1 true 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:list.remove(title) מחזיר true אם הוא הסיר משהו.list.remove(title) returns true if it removed something.
הפתרון שלנוOur solution
Main.java
import java.util.ArrayList;
import java.util.List;
publicclass Main {
publicstaticvoidmain(String[] args) {
Library lib =new Library();
lib.add("Dune");
lib.add("Emma");
boolean a = lib.lend("Dune");
boolean b = lib.lend("Dune");
int n = lib.available();
boolean c = lib.giveBack("Dune");
System.out.println(a +" "+ b +" "+ n +" "+ c +" "+ lib.available());
}
}
class Library {
privatefinalList<String> shelf =newArrayList<>();
privatefinalList<String> lent =newArrayList<>();
void add(String title) {
shelf.add(title);
}
boolean lend(String title) {
if (!shelf.remove(title)) {
returnfalse;
}
lent.add(title);
returntrue;
}
boolean giveBack(String title) {
if (!lent.remove(title)) {
returnfalse;
}
shelf.add(title);
returntrue;
}
int available() {
return shelf.size();
}
}
פלטOutput
true false 1 true 2
יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.
מחווןRubric (30 נקודותpoints)
כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.