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

JavaJava
9
חלקPart 2: מתודות ומערכיםMethods & arrays כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

ArrayList ו-HashMap ראשוניםFirst ArrayList & HashMap

רשימה שגדלה לבד, מילון של מפתח וערך, טיפוסים עטופים (Integer) ו-autoboxing.

A list that grows by itself, a key-value map, wrapper types (Integer) and autoboxing.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. איך יוצרים רשימה של מספרים שלמים?How do you create a list of whole numbers?

2. מה מחזיר map.get("x") כשאין מפתח כזה?What does map.get("x") return when there's no such key?

3. list היא [a, b, c]. מה היא אחרי list.remove(0)?list is [a, b, c]. What is it after list.remove(0)?

תרגיליםExercises

תרגילExercise 1

רשימת קניותShopping list

צרו ArrayList של מחרוזות, הוסיפו milk, bread, eggs, מחקו את bread, והדפיסו את הרשימה ואת הגודל: [milk, eggs] 2.

Create an ArrayList of strings, add milk, bread, eggs, remove bread, and print the list and its size: [milk, eggs] 2.

איך הפלט אמור להיראות?What should the output look like?
[milk, eggs] 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("bread")list.remove("bread")
רמזHint 2: System.out.println(list + " " + list.size());System.out.println(list + " " + list.size());
לפתרוןSolution
תרגילExercise 2

רק הזוגייםOnly the evens

כתבו static List<Integer> evens(List<Integer> nums) שמחזירה רשימה חדשה עם הזוגיים בלבד. הדפיסו את evens(List.of(3, 8, 5, 12, 6)): [8, 12, 6].

Write static List<Integer> evens(List<Integer> nums) returning a new list with only the evens. Print evens(List.of(3, 8, 5, 12, 6)): [8, 12, 6].

איך הפלט אמור להיראות?What should the output look like?
[8, 12, 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.
רמזHint 1: רשימה חדשה ריקה, ולולאה שמוסיפה רק מה שמתחלק ב-2.A new empty list, and a loop adding only what divides by 2.
רמזHint 2: List.of(...) יוצר רשימה קבועה מהירה.List.of(...) makes a quick fixed list.
לפתרוןSolution
תרגילExercise 3

ספר טלפוניםPhone book

צרו HashMap<String, String> עם שני אנשי קשר (Noa → 050-1111111, Avi → 052-2222222). עדכנו את המספר של Noa ל-050-9999999, והדפיסו את המספר של Noa ואת getOrDefault("Dana", "unknown").

Create a HashMap<String, String> with two contacts (Noa → 050-1111111, Avi → 052-2222222). Update Noa's number to 050-9999999, and print Noa's number and getOrDefault("Dana", "unknown").

איך הפלט אמור להיראות?What should the output look like?
050-9999999
unknown
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: put עם מפתח קיים מחליף את הערך.put with an existing key replaces the value.
לפתרוןSolution
תרגילExercise 4

ספירת אותיותCounting letters

ספרו כמה פעמים מופיעה כל אות במילה "banana" בעזרת TreeMap<Character, Integer>, והדפיסו את המילון: {a=3, b=1, n=2}.

Count how many times each letter appears in "banana" with a TreeMap<Character, Integer>, and print the map: {a=3, b=1, n=2}.

איך הפלט אמור להיראות?What should the output look like?
{a=3, b=1, n=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: for (char c : word.toCharArray())for (char c : word.toCharArray())
רמזHint 2: counts.put(c, counts.getOrDefault(c, 0) + 1);counts.put(c, counts.getOrDefault(c, 0) + 1);
לפתרוןSolution
תרגילExercise 5

צייד באגים: מחיקה תוך כדי לולאהBug hunter: removing while looping

הקוד מנסה למחוק מהרשימה את כל המספרים השליליים, וקורס. צפו ב"איפה זה נשבר", ותקנו עם removeIf כך שיודפס [4, 7].

The code tries to remove every negative number from the list, and crashes. Watch "where it breaks", and fix it with removeIf so it prints [4, 7].

איך הפלט אמור להיראות?What should the output look like?
[4, 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: for-each לא אוהב שמשנים את הרשימה מתחתיו.A for-each doesn't like the list changing under it.
רמזHint 2: nums.removeIf(n -> n < 0); (חץ כמו ב-switch; נלמד על למבדות במפגש 26).nums.removeIf(n -> n < 0); (an arrow as in switch; lambdas come in session 26).
לפתרוןSolution
תרגילExercise 6

מדף, צעד 9: רשימה וספירה לפי סופרShelf, step 9: a list and a count per author

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

שמרו את הספרים ב-List<String> בפורמט Title|Author: Dune|Herbert, Emma|Austen, Persuasion|Austen, Children of Dune|Herbert, Ulysses|Joyce. ספרו ב-TreeMap כמה ספרים יש לכל סופר והדפיסו Austen: 2 וכן הלאה, ובסוף Total: 5.

Keep the books in a List<String> as Title|Author: Dune|Herbert, Emma|Austen, Persuasion|Austen, Children of Dune|Herbert, Ulysses|Joyce. Count with a TreeMap how many books each author has and print Austen: 2 and so on, then Total: 5.

איך הפלט אמור להיראות?What should the output look like?
Austen: 2
Herbert: 2
Joyce: 1
Total: 5
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: הסופר: b.substring(b.indexOf("|") + 1).The author: b.substring(b.indexOf("|") + 1).
רמזHint 2: לולאה על byAuthor.entrySet() עם e.getKey() ו-e.getValue().Loop over byAuthor.entrySet() with e.getKey() and e.getValue().
לפתרוןSolution

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

אתגרChallenge 1

הכפול הראשוןThe first repeat

כתבו static String firstRepeat(List<String> words) שמחזירה את המילה הראשונה שמופיעה בפעם השנייה (או none). עבור [red, blue, green, blue, red] היא blue. השתמשו ב-HashSet (add מחזיר false אם כבר היה).

Write static String firstRepeat(List<String> words) returning the first word that shows up a second time (or none). For [red, blue, green, blue, red] it's blue. Use a HashSet (add returns false if it was already there).

איך הפלט אמור להיראות?What should the output look like?
blue
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: Set<String> seen = new HashSet<>();Set<String> seen = new HashSet<>();
רמזHint 2: if (!seen.add(w)) return w;if (!seen.add(w)) return w;
לפתרוןSolution
אתגרChallenge 2

ציון ממוצע לכל תלמידAverage grade per student

מילון Map<String, List<Integer>> שממפה תלמיד לרשימת ציונים: Noa → 90, 80; Avi → 70, 75, 95. הדפיסו לכל תלמיד (לפי א-ב) Avi: 80.0 ו-Noa: 85.0.

A Map<String, List<Integer>> mapping a student to a list of grades: Noa → 90, 80; Avi → 70, 75, 95. Print for each student (alphabetically) Avi: 80.0 and Noa: 85.0.

איך הפלט אמור להיראות?What should the output look like?
Avi: 80.0
Noa: 85.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: TreeMap לסדר. grades.put("Noa", List.of(90, 80));A TreeMap for order. grades.put("Noa", List.of(90, 80));
רמזHint 2: לכל רשימה: סכום ואז (double) sum / list.size().For each list: a sum, then (double) sum / list.size().
לפתרוןSolution

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