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

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

מערכים ומערכים דו-ממדייםArrays & 2D arrays

יצירה, גישה ואורך, לולאות על מערכים, Arrays.toString ו-sort, ומערכים דו-ממדיים כמו לוח משחק.

Creating, accessing and length, looping over arrays, Arrays.toString and sort, and 2D arrays like a game board.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. מה האינדקס של התא האחרון במערך באורך 5?What's the index of the last cell in an array of length 5?

2. מה ידפיס הקוד?What does it print?
main
int[] a = {1, 2};
int[] b = a;
b[0] = 9;
System.out.println(a[0]);

3. איך מדפיסים מערך בצורה קריאה?How do you print an array readably?

תרגיליםExercises

תרגילExercise 1

ממוצע ציוניםAverage of grades

במערך {85, 92, 78, 95} חשבו בלולאה את הממוצע והדפיסו Average: 87.5.

For the array {85, 92, 78, 95} compute the average in a loop and print Average: 87.5.

איך הפלט אמור להיראות?What should the output look like?
Average: 87.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: סכום בלולאת for-each, ואז מחלקים ב-(double) grades.length.A sum in a for-each, then divide by (double) grades.length.
לפתרוןSolution
תרגילExercise 2

הכי חםThe hottest

במערך טמפרטורות {24, 31, 28, 35, 30} מצאו את הגבוהה ואת היום שלה (אינדקס מ-0), והדפיסו Max 35 on day 3.

In the temperature array {24, 31, 28, 35, 30} find the highest and its day (index from 0), and print Max 35 on day 3.

איך הפלט אמור להיראות?What should the output look like?
Max 35 on day 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: שומרים גם max וגם day.Keep both max and day.
רמזHint 2: צריך לולאה עם אינדקס (for (int i = 1; ...)), לא for-each.You need a loop with an index (for (int i = 1; ...)), not a for-each.
לפתרוןSolution
תרגילExercise 3

הופכים מערךReversing an array

כתבו static void reverse(int[] a) שהופכת את המערך במקום (מחליפה את הראשון באחרון וכן הלאה). הדפיסו את {1, 2, 3, 4, 5} אחרי ההיפוך עם Arrays.toString.

Write static void reverse(int[] a) that reverses the array in place (swap first and last, and so on). Print {1, 2, 3, 4, 5} after reversing, with Arrays.toString.

איך הפלט אמור להיראות?What should the output look like?
[5, 4, 3, 2, 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: לולאה עד האמצע: for (int i = 0; i < a.length / 2; i++).Loop to the middle: for (int i = 0; i < a.length / 2; i++).
רמזHint 2: מחליפים את a[i] ואת a[a.length - 1 - i] עם משתנה זמני.Swap a[i] and a[a.length - 1 - i] with a temporary variable.
לפתרוןSolution
תרגילExercise 4

צייד באגים: מעבר לקצהBug hunter: past the edge

התוכנית קורסת עם ArrayIndexOutOfBoundsException. צפו בסימולציה "איפה זה נשבר" בעמוד הפתרונות, ותקנו כך שתדפיס 60.

The program crashes with ArrayIndexOutOfBoundsException. Watch the "where it breaks" simulation on the solutions page, and fix it so it prints 60.

איך הפלט אמור להיראות?What should the output look like?
60
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: מה האינדקס האחרון במערך באורך 3?What's the last index of a length-3 array?
רמזHint 2: < במקום <=.< instead of <=.
לפתרוןSolution
תרגילExercise 5

לוח איקס-עיגולA tic-tac-toe board

צרו char[][] board = new char[3][3] ומלאו בנקודות (.). שימו X באמצע ו-O בפינה הימנית העליונה (board[0][2]), והדפיסו את הלוח (3 שורות כמו ..O).

Create char[][] board = new char[3][3] and fill it with dots (.). Put X in the middle and O in the top-right corner (board[0][2]), and print the board (3 lines like ..O).

איך הפלט אמור להיראות?What should the output look like?
..O
.X.
...
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: לולאה כפולה למילוי.A double loop to fill.
רמזHint 2: new String(board[r]) הופך שורה למחרוזת.new String(board[r]) turns a row into a string.
לפתרוןSolution
תרגילExercise 6

מדף, צעד 8: מערך של ספריםShelf, step 8: an array of books

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

מערך String[] titles = {"Emma", "Dune", "Ulysses", "Beloved"} ומערך int[] years = {1815, 1965, 1922, 1987}. הדפיסו את הספר הישן ביותר (Oldest: Emma (1815)), ואת השמות ממוינים לפי א-ב (Arrays.sort על עותק).

An array String[] titles = {"Emma", "Dune", "Ulysses", "Beloved"} and int[] years = {1815, 1965, 1922, 1987}. Print the oldest book (Oldest: Emma (1815)), and the titles sorted alphabetically (Arrays.sort on a copy).

איך הפלט אמור להיראות?What should the output look like?
Oldest: Emma (1815)
[Beloved, Dune, Emma, Ulysses]
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: מחפשים את האינדקס של השנה הקטנה ביותר.Find the index of the smallest year.
רמזHint 2: String[] sorted = titles.clone(); Arrays.sort(sorted);, כדי ש-titles ו-years יישארו מתואמים.String[] sorted = titles.clone(); Arrays.sort(sorted);, so titles and years stay matched.
לפתרוןSolution

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

אתגרChallenge 1

ספירת הופעותCounting occurrences

קוביות הוטלו: {3, 6, 1, 3, 3, 6, 2}. בעזרת מערך מונים int[] count = new int[7] (תא לכל תוצאה), הדפיסו לכל מספר שהופיע שורה כמו 3: 3 times, לפי הסדר.

Dice rolls: {3, 6, 1, 3, 3, 6, 2}. With a counter array int[] count = new int[7] (a cell per result), print a line for every number that came up, like 3: 3 times, in order.

איך הפלט אמור להיראות?What should the output look like?
1: 1 times
2: 1 times
3: 3 times
6: 2 times
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: count[roll]++ לכל הטלה.count[roll]++ for each roll.
רמזHint 2: אחר כך לולאה מ-1 עד 6 שמדפיסה רק אם count[i] > 0.Then a loop from 1 to 6 printing only if count[i] > 0.
לפתרוןSolution
אתגרChallenge 2

הזזה מעגליתRotate

כתבו static int[] rotate(int[] a) שמחזירה מערך חדש שבו כל איבר זז מקום אחד ימינה, והאחרון עובר להתחלה: {1, 2, 3, 4} נהיה {4, 1, 2, 3}. המקורי לא משתנה. הדפיסו את שניהם.

Write static int[] rotate(int[] a) returning a new array where every item moves one place right and the last goes to the start: {1, 2, 3, 4} becomes {4, 1, 2, 3}. The original doesn't change. Print both.

איך הפלט אמור להיראות?What should the output look like?
[1, 2, 3, 4]
[4, 1, 2, 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: int[] r = new int[a.length];int[] r = new int[a.length];
רמזHint 2: r[(i + 1) % a.length] = a[i];r[(i + 1) % a.length] = a[i];
לפתרוןSolution

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