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

JavaJava
14
חלקPart 3: OOP: היסודותOOP: the basics כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

בנאים ו-thisConstructors & this

בנאים, overloading של בנאים, this() ו-this.field, וסדר האתחול.

Constructors, constructor overloading, this() and this.field, and initialization order.

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

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

מחלקה Student עם בנאי Student(String name, int grade) ומתודה String info() (Noa: 95). צרו את Noa (95) ואת Avi (82) והדפיסו את שניהם.

A class Student with a constructor Student(String name, int grade) and a method String info() (Noa: 95). Create Noa (95) and Avi (82) and 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
public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Noa", 95);
        Student s2 = new Student("Avi", 82);
        System.out.println(s1.info());
        System.out.println(s2.info());
    }
}

class Student {
    String name;
    int grade;

    Student(String name, int grade) {
        this.name = name;
        this.grade = grade;
    }

    String info() {
        return name + ": " + grade;
    }
}
פלטOutput
Noa: 95
Avi: 82

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

תרגילExercise 2

צייד באגים: הכותרת שנעלמהBug hunter: the vanishing title

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

התוכנית מדפיסה null במקום Dune, ולא קורסת. עצרו בתוך הבנאי (או צפו בסימולציה) ותקנו.

The program prints null instead of Dune, and doesn't crash. Stop inside the constructor (or watch the simulation) and fix it.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.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 b = new Book("Dune");
        System.out.println(b.title);
    }
}

class Book {
    String title;

    Book(String title) {
        this.title = title;
    }
}
פלטOutput
Dune

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

תרגילExercise 3

פיצה עם ברירת מחדלA pizza with a default

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

מחלקה Pizza עם size ו-topping. בנאי מלא Pizza(String size, String topping), ובנאי קצר Pizza(String size) שקורא לו עם "plain". הדפיסו large plain ו-small olives.

A class Pizza with size and topping. A full constructor Pizza(String size, String topping), and a short Pizza(String size) calling it with "plain". Print large plain and small olives.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.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) {
        Pizza a = new Pizza("large");
        Pizza b = new Pizza("small", "olives");
        System.out.println(a.size + " " + a.topping);
        System.out.println(b.size + " " + b.topping);
    }
}

class Pizza {
    String size;
    String topping;

    Pizza(String size, String topping) {
        this.size = size;
        this.topping = topping;
    }

    Pizza(String size) {
        this(size, "plain");
    }
}
פלטOutput
large plain
small olives

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

תרגילExercise 4

צייד באגים: אין בנאי ריקBug hunter: no empty constructor

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

התוכנית מהתזכורת לא מתקמפלת. בלי לשנות את main, הוסיפו ל-Book בנאי בלי פרמטרים שנותן את הכותרת Untitled.

The program from the refresher doesn't compile. Without changing main, add to Book a no-parameter constructor giving the title Untitled.

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

class Book {
    String title;

    Book() {
        this("Untitled");
    }

    Book(String title) {
        this.title = title;
    }
}
פלטOutput
Untitled

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

תרגילExercise 5

משך זמןA duration

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

מחלקה Duration ששומרת int seconds. בנאי Duration(int minutes, int seconds) ובנאי Duration(int seconds) (שקורא לראשון עם 0 דקות). המתודה format() מחזירה MM:SS. הדפיסו new Duration(2, 5) ו-new Duration(75): 02:05 01:15.

A class Duration storing int seconds. A constructor Duration(int minutes, int seconds) and a constructor Duration(int seconds) (calling the first with 0 minutes). The method format() returns MM:SS. Print new Duration(2, 5) and new Duration(75): 02:05 01:15.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.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) {
        Duration d = new Duration(2, 5);
        Duration e = new Duration(75);
        System.out.println(d.format() + " " + e.format());
    }
}

class Duration {
    int seconds;

    Duration(int minutes, int seconds) {
        this.seconds = minutes * 60 + seconds;
    }

    Duration(int seconds) {
        this(0, seconds);
    }

    String format() {
        return String.format("%02d:%02d", seconds / 60, seconds % 60);
    }
}
פלטOutput
02:05 01:15

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

תרגילExercise 6

מדף, צעד 14: בנאים ל-BookShelf, step 14: Book constructors

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

ל-Book יש title, author, year, rating. בנאי מלא, ובנאי Book(String title, String author) שנותן שנה 0 ודירוג 3. line() מחזירה Dune by Herbert (1965) rating 5, ולשנה 0 unknown year. הדפיסו את Dune ואת new Book("Notes", "Me").

Book has title, author, year, rating. A full constructor, and Book(String title, String author) giving year 0 and rating 3. line() returns Dune by Herbert (1965) rating 5, and for year 0 unknown year. Print Dune and new Book("Notes", "Me").

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.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 a = new Book("Dune", "Herbert", 1965, 5);
        Book b = new Book("Notes", "Me");
        System.out.println(a.line());
        System.out.println(b.line());
    }
}

class Book {
    String title;
    String author;
    int year;
    int rating;

    Book(String title, String author, int year, int rating) {
        this.title = title;
        this.author = author;
        this.year = year;
        this.rating = rating;
    }

    Book(String title, String author) {
        this(title, author, 0, 3);
    }

    String line() {
        String when = year == 0 ? "unknown year" : "" + year;
        return title + " by " + author + " (" + when + ") rating " + rating;
    }
}
פלטOutput
Dune by Herbert (1965) rating 5
Notes by Me (unknown year) rating 3

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

אתגרChallenge 1

מי רץ קודם?Who runs first?

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

במחלקה Box יש שדה String log = "field", בנאי Box() שקורא ל-this(1) ואחר כך מוסיף " > Box()", ובנאי Box(int n) שמוסיף " > Box(int)". נחשו לפני שמריצים, וכתבו את הקוד: הפלט field > Box(int) > Box().

Class Box has a field String log = "field", a constructor Box() that calls this(1) and then appends " > Box()", and a constructor Box(int n) appending " > Box(int)". Guess before running, then write the code: the output field > Box(int) > Box().

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

class Box {
    String log = "field";

    Box() {
        this(1);
        log += " > Box()";
    }

    Box(int n) {
        log += " > Box(int)";
    }
}
פלטOutput
field > Box(int) > Box()

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

אתגרChallenge 2

בנאי מעתיקA copy constructor

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

ל-Point יש בנאי Point(int x, int y). הוסיפו בנאי מעתיק Point(Point other) שיוצר נקודה חדשה עם אותם ערכים. העתיקו את (1, 2), שנו את x של העותק ל-9, והדפיסו 1,2 9,2.

Point has a constructor Point(int x, int y). Add a copy constructor Point(Point other) creating a new point with the same values. Copy (1, 2), change the copy's x to 9, and print 1,2 9,2.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.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) {
        Point p = new Point(1, 2);
        Point q = new Point(p);
        q.x = 9;
        System.out.println(p.x + "," + p.y + " " + q.x + "," + q.y);
    }
}

class Point {
    int x;
    int y;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    Point(Point other) {
        this(other.x, other.y);
    }
}
פלטOutput
1,2 9,2

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

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