נבנה על ידי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.

נסו לפתור לבד לפני שמציצים. בנגן: חיצים במקלדת או הכפתורים. כל צעד מראה איפה (קובץ ושורה), מה כותבים ולמה.Try on your own before peeking. In the player use the arrow keys or the buttons. Each step shows where (file and line), what to write, and why.
תרגילExercise 1

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

תזכורת: מה ביקשוReminder: the task

במערך {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.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
public class Main {
    public static void main(String[] args) {
        int[] grades = {85, 92, 78, 95};
        int sum = 0;
        for (int g : grades) {
            sum += g;
        }
        System.out.println("Average: " + (double) sum / grades.length);
    }
}
פלטOutput
Average: 87.5

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 2

הכי חםThe hottest

תזכורת: מה ביקשוReminder: the task

במערך טמפרטורות {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.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
public class Main {
    public static void main(String[] args) {
        int[] temps = {24, 31, 28, 35, 30};
        int max = temps[0];
        int day = 0;
        for (int i = 1; i < temps.length; i++) {
            if (temps[i] > max) {
                max = temps[i];
                day = i;
            }
        }
        System.out.println("Max " + max + " on day " + day);
    }
}
פלטOutput
Max 35 on day 3

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 3

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

תזכורת: מה ביקשוReminder: the task

כתבו 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.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
import java.util.Arrays;

public class Main {
    static void reverse(int[] a) {
        for (int i = 0; i < a.length / 2; i++) {
            int t = a[i];
            a[i] = a[a.length - 1 - i];
            a[a.length - 1 - i] = t;
        }
    }

    public static void main(String[] args) {
        int[] n = {1, 2, 3, 4, 5};
        reverse(n);
        System.out.println(Arrays.toString(n));
    }
}
פלטOutput
[5, 4, 3, 2, 1]

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 4

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

תזכורת: מה ביקשוReminder: the task

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

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

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו איפה זה נשברWatch where it breaks
איפה זה נשברWhere it breaksשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
public class Main {
    public static void main(String[] args) {
        int[] a = {10, 20, 30};
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }
        System.out.println(sum);
    }
}
פלטOutput
60

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 5

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

תזכורת: מה ביקשוReminder: the task

צרו 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).

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
public class Main {
    public static void main(String[] args) {
        char[][] board = new char[3][3];
        for (int r = 0; r < 3; r++) {
            for (int c = 0; c < 3; c++) {
                board[r][c] = '.';
            }
        }
        board[1][1] = 'X';
        board[0][2] = 'O';
        for (char[] row : board) {
            System.out.println(new String(row));
        }
    }
}
פלטOutput
..O
.X.
...

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 6

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

תזכורת: מה ביקשוReminder: the task

מערך 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).

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[] titles = {"Emma", "Dune", "Ulysses", "Beloved"};
        int[] years = {1815, 1965, 1922, 1987};
        int oldest = 0;
        for (int i = 1; i < years.length; i++) {
            if (years[i] < years[oldest]) {
                oldest = i;
            }
        }
        System.out.println("Oldest: " + titles[oldest] + " (" + years[oldest] + ")");
        String[] sorted = titles.clone();
        Arrays.sort(sorted);
        System.out.println(Arrays.toString(sorted));
    }
}
פלטOutput
Oldest: Emma (1815)
[Beloved, Dune, Emma, Ulysses]

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

אתגרChallenge 1

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

תזכורת: מה ביקשוReminder: the task

קוביות הוטלו: {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.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
public class Main {
    public static void main(String[] args) {
        int[] rolls = {3, 6, 1, 3, 3, 6, 2};
        int[] count = new int[7];
        for (int r : rolls) {
            count[r]++;
        }
        for (int i = 1; i <= 6; i++) {
            if (count[i] > 0) {
                System.out.println(i + ": " + count[i] + " times");
            }
        }
    }
}
פלטOutput
1: 1 times
2: 1 times
3: 3 times
6: 2 times

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

אתגרChallenge 2

הזזה מעגליתRotate

תזכורת: מה ביקשוReminder: the task

כתבו 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.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy

סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line

לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.

Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.

צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Java מבצעתLine by line, the way Java runs it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

הפתרון המלא והפלט שלוFull solution and its output
Main.java
import java.util.Arrays;

public class Main {
    static int[] rotate(int[] a) {
        int[] r = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            r[(i + 1) % a.length] = a[i];
        }
        return r;
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};
        int[] b = rotate(a);
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(b));
    }
}
פלטOutput
[1, 2, 3, 4]
[4, 1, 2, 3]

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

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