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

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

abstract ו-interfacesabstract & interfaces

מחלקות מופשטות, interfaces עם default ו-static methods, ומימוש של כמה interfaces.

Abstract classes, interfaces with default and static methods, and implementing several interfaces.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. מה ההבדל העיקרי בין abstract class ל-interface?What's the main difference between an abstract class and an interface?

2. Circle extends Shape ולא מממשת את area() המופשטת. מה קורה?Circle extends Shape and doesn't implement the abstract area(). What happens?

3. a.compareTo(b) מחזיר -3. מה זה אומר?a.compareTo(b) returns -3. What does it mean?

תרגיליםExercises

תרגילExercise 1

שכר לפי סוג עובדPay by employee type

abstract class Employee עם name ו-abstract int pay(). Hourly (תעריף × שעות) ו-Salaried (משכורת חודשית). במערך של Employee הדפיסו Noa: 5000 ו-Avi: 9000.

abstract class Employee with a name and abstract int pay(). Hourly (rate × hours) and Salaried (a monthly salary). In an Employee array print Noa: 5000 and Avi: 9000.

איך הפלט אמור להיראות?What should the output look like?
Noa: 5000
Avi: 9000
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: הלולאה לא יודעת איזה סוג עובד יש בכל תא, ולא צריכה לדעת.The loop doesn't know which kind of employee is in each cell, and doesn't need to.
לפתרוןSolution
תרגילExercise 2

מחיר עם מע"מPrice with VAT

interface Priced עם price(), label() ו-default double withVat() (פי 1.17). Book ו-Coffee מממשות. הדפיסו Dune 70.20 ו-Coffee 14.04.

interface Priced with price(), label() and default double withVat() (times 1.17). Book and Coffee implement it. Print Dune 70.20 and Coffee 14.04.

איך הפלט אמור להיראות?What should the output look like?
Dune 70.20
Coffee 14.04
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: withVat נכתבת פעם אחת, ב-interface.withVat is written once, in the interface.
רמזHint 2: המימושים חייבים public.The implementations must be public.
לפתרוןSolution
תרגילExercise 3

צייד באגים: שכחו לממשBug hunter: forgot to implement

התוכנית לא מתקמפלת. צפו ב"איפה זה נשבר", ותקנו את Circle כך שיודפס 12.57.

The program doesn't compile. Watch "where it breaks", and fix Circle so it prints 12.57.

איך הפלט אמור להיראות?What should the output look like?
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: Math.PI * r * rMath.PI * r * r
לפתרוןSolution
תרגילExercise 4

למיין תלמידיםSorting students

Student implements Comparable<Student> לפי הציון (מהנמוך לגבוה). Arrays.sort על Noa 92, Avi 78, Dana 85, והדפיסו [Avi=78, Dana=85, Noa=92].

Student implements Comparable<Student> by grade (low to high). Arrays.sort Noa 92, Avi 78, Dana 85, and print [Avi=78, Dana=85, Noa=92].

איך הפלט אמור להיראות?What should the output look like?
[Avi=78, Dana=85, Noa=92]
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.compare(grade, other.grade) בטוח יותר מחיסור.Integer.compare(grade, other.grade) is safer than subtracting.
לפתרוןSolution
תרגילExercise 5

static ו-default ב-interfacestatic and default in an interface

interface Shape עם area(), default boolean isBiggerThan(Shape other), ו-static double totalArea(Shape[] shapes). Rect מממשת. עבור 2x3, 1x1, 4x5 הדפיסו 27.0 ואז true.

interface Shape with area(), default boolean isBiggerThan(Shape other), and static double totalArea(Shape[] shapes). Rect implements it. For 2x3, 1x1, 4x5 print 27.0 then true.

איך הפלט אמור להיראות?What should the output look like?
27.0
true
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: קוראים למתודה static של interface דרך השם שלו: Shape.totalArea(...).Call an interface's static method through its name: Shape.totalArea(...).
לפתרוןSolution
תרגילExercise 6

מדף, צעד 20: interface בשם LendableShelf, step 20: a Lendable interface

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

interface Lendable עם name(), borrower(), lendTo, giveBack ו-default String status() (Dune: lent to Noa או DVD Alien: available). Book ו-Dvd מממשות. השאילו את Dune ל-Noa, הדפיסו את המצב של שניהם, החזירו, והדפיסו שוב את Dune.

interface Lendable with name(), borrower(), lendTo, giveBack and default String status() (Dune: lent to Noa or DVD Alien: available). Book and Dvd implement it. Lend Dune to Noa, print both statuses, give it back, and print Dune again.

איך הפלט אמור להיראות?What should the output look like?
Dune: lent to Noa
DVD Alien: available
Dune: available
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: status כתובה פעם אחת ב-interface ומשתמשת ב-name() וב-borrower().status is written once in the interface and uses name() and borrower().
לפתרוןSolution

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

אתגרChallenge 1

Template MethodTemplate Method

ב-abstract class Report יש final String render() שבונה כותרת, גוף וסיום, ושתי מתודות מופשטות: title() ו-body(). SalesReport ממלאת אותן. הדפיסו: == Sales == / Books: 12, Coffee: 30 / -- end --.

abstract class Report has final String render() building a header, a body and an ending, plus two abstract methods: title() and body(). SalesReport fills them in. Print: == Sales == / Books: 12, Coffee: 30 / -- end --.

איך הפלט אמור להיראות?What should the output look like?
== Sales ==
Books: 12, Coffee: 30
-- end --
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: final מונע מתת-מחלקה לשנות את הסדר.final stops a subclass from changing the order.
לפתרוןSolution
אתגרChallenge 2

שני default באותו שםTwo defaults with the same name

ל-interfaces A ול-B יש default String hello() (מחזירות A ו-B). מחלקה שמממשת את שניהם חייבת להכריע. דרסו את hello כך שתחזיר A+B בעזרת A.super.hello() ו-B.super.hello().

Interfaces A and B both have default String hello() (returning A and B). A class implementing both must decide. Override hello to return A+B using A.super.hello() and B.super.hello().

איך הפלט אמור להיראות?What should the output look like?
A+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.
רמזHint 1: בלי הדריסה javac עוצר: types A and B are incompatible.Without the override javac stops: types A and B are incompatible.
לפתרוןSolution

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