רשימה שגדלה לבד, מילון של מפתח וערך, טיפוסים עטופים (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
תרגילים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.
כתבו 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.
צרו 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.
ספרו כמה פעמים מופיעה כל אות במילה "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())
צייד באגים: מחיקה תוך כדי לולאה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).
מדף, צעד 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.
כתבו 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<>();
מילון 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.