Generic classes and methods, bounded types, wildcards and PECS, and type erasure.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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 pair
class Pair<A, B> עם first(), second(), swap() שמחזירה Pair<B, A> חדש, ו-toString (Dune, 1965). הדפיסו (Dune, 1965) (1965, Dune) ואז 1966 (q.first() + 1, בלי המרה).
class Pair<A, B> with first(), second(), swap() returning a new Pair<B, A>, and a toString (Dune, 1965). Print (Dune, 1965) (1965, Dune) then 1966 (q.first() + 1, with no cast).
איך הפלט אמור להיראות?What should the output look like?
(Dune, 1965) (1965, Dune)
1966
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:return new Pair<>(second, first);return new Pair<>(second, first);
static <T> int count(List<T> list, T wanted) סופרת כמה איברים שווים (equals) ל-wanted. הדפיסו 3 עבור a ברשימה a, b, a, a, ו-2 עבור 2 ב-1, 2, 2.
static <T> int count(List<T> list, T wanted) counts how many items equal (equals) wanted. Print 3 for a in a, b, a, a, and 2 for 2 in 1, 2, 2.
איך הפלט אמור להיראות?What should the output look like?
3
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:<T> לפני טיפוס ההחזרה.<T> before the return type.
static <T extends Comparable<T>> T min(List<T> list). הדפיסו 3 עבור 7, 3, 9, ו-apple עבור pear, apple, fig.
static <T extends Comparable<T>> T min(List<T> list). Print 3 for 7, 3, 9, and apple for pear, apple, fig.
איך הפלט אמור להיראות?What should the output look like?
3
apple
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.
צייד באגים: מספר ברשימת מחרוזותBug hunter: a number in a list of strings
התוכנית לא מתקמפלת. הכניסו את השנה כמחרוזת (String.valueOf) כך שיודפס [Dune, 1965].
The program doesn't compile. Put the year in as a string (String.valueOf) so it prints [Dune, 1965].
איך הפלט אמור להיראות?What should the output look like?
[Dune, 1965]
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:String.valueOf(1965) או "" + 1965.String.valueOf(1965) or "" + 1965.
static double average(List<? extends Number> nums) עובדת גם עם List<Integer> וגם עם List<Double>. הדפיסו 5.0 2.0.
static double average(List<? extends Number> nums) works with a List<Integer> and a List<Double> alike. Print 5.0 2.0.
איך הפלט אמור להיראות?What should the output look like?
5.0 2.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:n.doubleValue()n.doubleValue()
רמזHint 2:עם List<Number> כפרמטר, List<Integer>לא הייתה מתקבלת.With List<Number> as the parameter, a List<Integer> would not be accepted.
מדף, צעד 22: Shelf<T> גנריShelf, step 22: a generic Shelf<T>
פרויקט מלווהCompanion project
class Shelf<T> עם שם ו-List<T> פרטית: add, get, size, ו-toString titles[Dune, Emma]. צרו Shelf<String> ו-Shelf<Integer>, הדפיסו את שניהם, ואז Dune 3780 (get מחזיר את הטיפוס הנכון בלי המרה).
class Shelf<T> with a name and a private List<T>: add, get, size, and a toString titles[Dune, Emma]. Create a Shelf<String> and a Shelf<Integer>, print both, then Dune 3780 (get returns the right type with no cast).
איך הפלט אמור להיראות?What should the output look like?
titles[Dune, Emma] years[1965, 1815]
Dune 3780
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:return name + items;return name + items;
static <T> void copy(List<? extends T> src, List<? super T> dst) מעתיקה הכול מ-src ל-dst. העתיקו List<Integer> ו-List<Double> ל-List<Number>, ואת זו ל-List<Object>. הדפיסו [1, 2, 3.5] 3.
static <T> void copy(List<? extends T> src, List<? super T> dst) copies everything from src to dst. Copy a List<Integer> and a List<Double> into a List<Number>, and that into a List<Object>. Print [1, 2, 3.5] 3.
איך הפלט אמור להיראות?What should the output look like?
[1, 2, 3.5] 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.
class Counter<T> עם add(T), count(T) ו-T top() (הנפוץ ביותר). ספרו את המילים של to be or not to be to והדפיסו to 3 0, ואחר כך את הקובייה הנפוצה מ-3, 6, 6, 1: 6.
class Counter<T> with add(T), count(T) and T top() (the most common). Count the words of to be or not to be to and print to 3 0, then the most common die from 3, 6, 6, 1: 6.
איך הפלט אמור להיראות?What should the output look like?
to 3 0
6
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.