יצירה, גישה ואורך, לולאות על מערכים, 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
תרגילים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.
במערך טמפרטורות {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.
כתבו 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.
התוכנית קורסת עם 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?
צרו 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.
מדף, צעד 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.
קוביות הוטלו: {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.
כתבו 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];