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

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

static מול instancestatic vs instance

מה שייך למחלקה ומה שייך לעצם, מתי static הגיוני, ולמה main הוא static.

What belongs to the class and what belongs to an object, when static makes sense, and why main is static.

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

מחלקת עזרA utility class

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

כתבו מחלקה MathUtil (באותו קובץ, מתחת ל-Main) עם static int max3(int a, int b, int c). מ-main הדפיסו MathUtil.max3(4, 9, 2) ו-MathUtil.max3(-1, -7, -3): 9 ואז -1.

Write a class MathUtil (in the same file, below Main) with static int max3(int a, int b, int c). From main print MathUtil.max3(4, 9, 2) and MathUtil.max3(-1, -7, -3): 9 then -1.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.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) {
        System.out.println(MathUtil.max3(4, 9, 2));
        System.out.println(MathUtil.max3(-1, -7, -3));
    }
}

class MathUtil {
    static int max3(int a, int b, int c) {
        return Math.max(a, Math.max(b, c));
    }
}
פלטOutput
9
-1

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

תרגילExercise 2

קבוע מע"מA VAT constant

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

הגדירו קבוע static final double VAT = 0.17; ומתודה static double withVat(double price). הדפיסו את המחיר של 100 ושל 50 כולל מע"מ: 117.0 ו-58.5.

Define a constant static final double VAT = 0.17; and a method static double withVat(double price). Print the price of 100 and of 50 including VAT: 117.0 and 58.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 {
    static final double VAT = 0.17;

    static double withVat(double price) {
        return price * (1 + VAT);
    }

    public static void main(String[] args) {
        System.out.println(withVat(100));
        System.out.println(withVat(50));
    }
}
פלטOutput
117.0
58.5

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

תרגילExercise 3

מונה מבקריםA visitor counter

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

כתבו מחלקה Visitor עם שדה static int total ושדה String name. הבנאי (Visitor(String name)) שומר את השם ומעלה את total. צרו 3 מבקרים והדפיסו Visitors: 3.

Write a class Visitor with a field static int total and a field String name. The constructor (Visitor(String name)) saves the name and bumps total. Create 3 visitors and print Visitors: 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) {
        new Visitor("Noa");
        new Visitor("Avi");
        new Visitor("Dana");
        System.out.println("Visitors: " + Visitor.total);
    }
}

class Visitor {
    static int total = 0;
    String name;

    Visitor(String name) {
        this.name = name;
        total++;
    }
}
פלטOutput
Visitors: 3

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

תרגילExercise 4

צייד באגים: from a static contextBug hunter: from a static context

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

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

The program doesn't compile. Watch "where it breaks" on the solutions page, and fix it so it prints 25.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.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 {
    static int square(int x) {
        return x * x;
    }

    public static void main(String[] args) {
        System.out.println(square(5));
    }
}
פלטOutput
25

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

תרגילExercise 5

קליקים: של כל אחד ושל כולםClicks: each one's and everyone's

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

במחלקה Counter יש int count (לכל מונה) ו-static int all (לכולם יחד). click() מעלה את שניהם. לחצו 3 פעמים על a ו-2 על b, והדפיסו a=3 b=2 all=5.

Class Counter has int count (per counter) and static int all (for all of them together). click() bumps both. Click a 3 times and b twice, and print a=3 b=2 all=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) {
        Counter a = new Counter();
        Counter b = new Counter();
        a.click();
        a.click();
        a.click();
        b.click();
        b.click();
        System.out.println("a=" + a.count + " b=" + b.count + " all=" + Counter.all);
    }
}

class Counter {
    int count = 0;
    static int all = 0;

    void click() {
        count++;
        all++;
    }
}
פלטOutput
a=3 b=2 all=5

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

תרגילExercise 6

מדף, צעד 10: מתודות עזר סטטיותShelf, step 10: static helper methods

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

כתבו מחלקת עזר ShelfUtil עם static String stars(int rating) (למשל 4 → ****-) ו-static double average(int[] ratings). עבור Dune 5, Emma 4, Ulysses 2 הדפיסו שורה לכל ספר (Dune *****) ובסוף Average: 3.67.

Write a utility class ShelfUtil with static String stars(int rating) (e.g. 4 → ****-) and static double average(int[] ratings). For Dune 5, Emma 4, Ulysses 2 print a line per book (Dune *****) and then Average: 3.67.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.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) {
        String[] titles = {"Dune", "Emma", "Ulysses"};
        int[] ratings = {5, 4, 2};
        for (int i = 0; i < titles.length; i++) {
            System.out.println(titles[i] + " " + ShelfUtil.stars(ratings[i]));
        }
        System.out.println("Average: " + String.format("%.2f", ShelfUtil.average(ratings)));
    }
}

class ShelfUtil {
    static String stars(int rating) {
        return "*".repeat(rating) + "-".repeat(5 - rating);
    }

    static double average(int[] ratings) {
        int sum = 0;
        for (int r : ratings) {
            sum += r;
        }
        return (double) sum / ratings.length;
    }
}
פלטOutput
Dune *****
Emma ****-
Ulysses **---
Average: 3.67

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

אתגרChallenge 1

מחולל מזהיםAn ID generator

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

כתבו מחלקה IdGen עם static int next = 100 ו-static String nextId(String prefix) שמחזירה מזהה כמו B-100 ומקדמת את next. שלוש קריאות (B, B, M) מדפיסות B-100, B-101, M-102.

Write a class IdGen with static int next = 100 and static String nextId(String prefix) returning an id like B-100 and advancing next. Three calls (B, B, M) print B-100, B-101, M-102.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.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) {
        System.out.println(IdGen.nextId("B"));
        System.out.println(IdGen.nextId("B"));
        System.out.println(IdGen.nextId("M"));
    }
}

class IdGen {
    static int next = 100;

    static String nextId(String prefix) {
        String id = prefix + "-" + next;
        next++;
        return id;
    }
}
פלטOutput
B-100
B-101
M-102

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

אתגרChallenge 2

מתודה סטטית שעובדת על עצמיםA static method that works on objects

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

במחלקה Book (String title, static int created) כתבו static Book longest(Book[] books) שמחזירה את הספר עם השם הארוך ביותר. צרו את Dune, Children of Dune ו-Emma, והדפיסו Longest: Children of Dune ו-Books created: 3.

In class Book (String title, static int created) write static Book longest(Book[] books) returning the book with the longest title. Create Dune, Children of Dune and Emma, and print Longest: Children of Dune and Books created: 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) {
        Book[] books = {new Book("Dune"), new Book("Children of Dune"), new Book("Emma")};
        System.out.println("Longest: " + Book.longest(books).title);
        System.out.println("Books created: " + Book.created);
    }
}

class Book {
    static int created = 0;
    String title;

    Book(String title) {
        this.title = title;
        created++;
    }

    static Book longest(Book[] books) {
        Book best = books[0];
        for (Book b : books) {
            if (b.title.length() > best.title.length()) {
                best = b;
            }
        }
        return best;
    }
}
פלטOutput
Longest: Children of Dune
Books created: 3

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

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