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

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

פולימורפיזם ו-dynamic dispatchPolymorphism & dynamic dispatch

הפניה מטיפוס אחד ועצם מטיפוס אחר, איזו מתודה נבחרת בזמן ריצה, casting ו-instanceof.

A reference of one type and an object of another, which method is chosen at run time, casting and instanceof.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. Item x = new Dvd("Alien"); x.loanDays() — של מי ה-loanDays שרצה?Item x = new Dvd("Alien"); x.loanDays() — whose loanDays runs?

2. Item x = new Dvd("Alien"); EBook e = (EBook) x; מה קורה?Item x = new Dvd("Alien"); EBook e = (EBook) x; What happens?

3. Object o = "hi"; ויש f(Object) ו-f(String). מה נקרא ב-f(o)?Object o = "hi"; with f(Object) and f(String). Which runs for f(o)?

תרגיליםExercises

תרגילExercise 1

סך כל השטחיםTotal area

במערך Shape[] יש שני עיגולים (רדיוס 1 ו-2) ומלבן 2x3. חשבו בלולאה אחת את סך השטחים והדפיסו Total: 21.71.

A Shape[] array holds two circles (radius 1 and 2) and a 2x3 rectangle. Compute the total area in one loop and print Total: 21.71.

איך הפלט אמור להיראות?What should the output look like?
Total: 21.71
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: הלולאה קוראת ל-s.area(), וכל צורה עונה בעצמה.The loop calls s.area(), and each shape answers for itself.
לפתרוןSolution
תרגילExercise 2

גן חיותA zoo

מערך של Animal עם כלב, חתול וכלב. הדפיסו את כל הקולות בשורה אחת (Woof Meow Woof), ואחר כך Dogs: 2 (ספירה עם instanceof).

An Animal array with a dog, a cat and a dog. Print all the sounds on one line (Woof Meow Woof), then Dogs: 2 (counted with instanceof).

איך הפלט אמור להיראות?What should the output look like?
Woof Meow Woof 
Dogs: 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: if (a instanceof Dog) dogs++;if (a instanceof Dog) dogs++;
לפתרוןSolution
תרגילExercise 3

צייד באגים: המרה עיוורתBug hunter: a blind cast

התוכנית סוכמת את הגודל של הספרים הדיגיטליים, וקורסת על ה-DVD. תקנו עם instanceof כך שיודפס Total: 2 MB.

The program sums the sizes of the ebooks, and crashes on the DVD. Fix it with instanceof so it prints Total: 2 MB.

איך הפלט אמור להיראות?What should the output look like?
Total: 2 MB
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: if (it instanceof EBook e) { total += e.getSizeMb(); }if (it instanceof EBook e) { total += e.getSizeMb(); }
לפתרוןSolution
תרגילExercise 4

overloading ו-overriding יחדOverloading and overriding together

ל-Greeter יש שתי גרסאות של greet (עם String ועם int). LoudGreeter דורסת רק את גרסת ה-String. עם Greeter g = new LoudGreeter(), הדפיסו g.greet("Noa") ו-g.greet(3): HI NOA! ואז HiHiHi.

Greeter has two versions of greet (with a String and with an int). LoudGreeter overrides only the String one. With Greeter g = new LoudGreeter(), print g.greet("Noa") and g.greet(3): HI NOA! then HiHiHi.

איך הפלט אמור להיראות?What should the output look like?
HI NOA!
HiHiHi
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: javac בוחר איזו greet לפי הארגומנט; בזמן ריצה Java בוחרת של מי לפי העצם.javac picks which greet by the argument; at run time Java picks whose by the object.
לפתרוןSolution
תרגילExercise 5

מתודה שמקבלת את ההורהA method that takes the parent

כתבו static int maxLoan(Item[] items) ו-static int minLoan(Item[] items). הן לא יודעות אילו סוגים יש במערך. עבור Emma, Dune (ebook) ושני DVD, הדפיסו Longest: 21 ו-Shortest: 3.

Write static int maxLoan(Item[] items) and static int minLoan(Item[] items). They don't know which kinds are in the array. For Emma, Dune (ebook) and two DVDs, print Longest: 21 and Shortest: 3.

איך הפלט אמור להיראות?What should the output look like?
Longest: 21
Shortest: 3
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: Integer.MAX_VALUE כהתחלה למינימום.Integer.MAX_VALUE as the start for a minimum.
לפתרוןSolution
תרגילExercise 6

מדף, צעד 21: רשימה אחת לכל הסוגיםShelf, step 21: one list for every kind

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

ב-List<Item> יש Emma (ספר), Dune (ebook), Alien (dvd) ו-Emma (ebook). הדפיסו שורה לכל פריט (Dune (ebook, 21 days)), וספרו כמה יש מכל סוג ב-TreeMap: {dvd=1, ebook=2, item=1}.

A List<Item> holds Emma (a book), Dune (ebook), Alien (dvd) and Emma (ebook). Print a line per item (Dune (ebook, 21 days)), and count each kind in a TreeMap: {dvd=1, ebook=2, item=1}.

איך הפלט אמור להיראות?What should the output look like?
Emma (item, 14 days)
Dune (ebook, 21 days)
Alien (dvd, 3 days)
Emma (ebook, 21 days)
{dvd=1, ebook=2, item=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: it.kind() כמפתח.it.kind() as the key.
לפתרוןSolution

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

אתגרChallenge 1

מלכודת בבנאיA trap in the constructor

הבנאי של Parent קורא ל-name(), ו-Child דורסת את name() ומחזירה שדה. הריצו וצפו: הפלט הוא Hello, null ואז Now: Noa. כתבו את הקוד והבינו למה בסימולציה.

Parent's constructor calls name(), and Child overrides name() returning a field. Run it and watch: the output is Hello, null then Now: Noa. Write the code and understand why in the simulation.

איך הפלט אמור להיראות?What should the output look like?
Hello, null
Now: Noa
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: כשבנאי ההורה רץ, השדות של הילד עוד לא אותחלו, אבל dynamic dispatch כבר בוחר את name של הילד.When the parent's constructor runs, the child's fields aren't set yet, but dynamic dispatch already picks the child's name.
רמזHint 2: הלקח: לא לקרוא למתודה שאפשר לדרוס מתוך בנאי.The lesson: don't call an overridable method from a constructor.
לפתרוןSolution
אתגרChallenge 2

הצורה הגדולהThe largest shape

static Shape largest(Shape[] shapes) מחזירה את הצורה עם השטח הגדול. describe() ב-Shape משתמשת ב-getClass().getSimpleName(). עבור ריבוע 3, עיגול 2 וריבוע 1, הדפיסו Circle 12.57.

static Shape largest(Shape[] shapes) returns the shape with the largest area. describe() in Shape uses getClass().getSimpleName(). For a square of 3, a circle of 2 and a square of 1, print Circle 12.57.

איך הפלט אמור להיראות?What should the output look like?
Circle 12.57
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: getClass() מחזירה את הסוג האמיתי של העצם, גם מתוך מתודה של ההורה.getClass() returns the object's real type, even from inside a parent method.
לפתרוןSolution

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