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

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

מחלקות ועצמיםClasses & objects

מחלקה כתבנית ועצם כמופע, שדות ומתודות, new, והפניות בזיכרון (stack מול heap).

A class as a blueprint and an object as an instance, fields and methods, new, and references in memory (stack vs heap).

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. כמה עצמים יש בזיכרון אחרי הקוד?How many objects are in memory after this code?
main
Book a = new Book();
Book b = a;
Book c = new Book();

2. מה הערך של שדה String title בעצם חדש שאף אחד לא נתן לו ערך?What's a String title field in a new object that nobody gave a value?

3. a ו-b מצביעים על אותו ספר. מה מודפס?a and b point to the same book. What's printed?
main
b.title = "X";
System.out.println(a.title);

תרגיליםExercises

תרגילExercise 1

נקודהA point

כתבו מחלקה Point עם שדות int x, int y ומתודה double distance() שמחזירה את המרחק מראשית הצירים (Math.sqrt(x*x + y*y)). צרו נקודה (3, 4) והדפיסו (3, 4) ואז 5.0.

Write a class Point with fields int x, int y and a method double distance() returning the distance from the origin (Math.sqrt(x*x + y*y)). Create the point (3, 4) and print (3, 4) then 5.0.

איך הפלט אמור להיראות?What should the output look like?
(3, 4)
5.0
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: Point p = new Point(); p.x = 3;Point p = new Point(); p.x = 3;
רמזHint 2: בתוך distance, x הוא השדה של העצם.Inside distance, x is the object's field.
לפתרוןSolution
תרגילExercise 2

שני כלביםTwo dogs

מחלקה Dog עם name, age, מתודה String describe() שמחזירה למשל Rex is 3, ומתודה void birthday() שמעלה את הגיל. צרו את Rex (3) ואת Bella (1), חגגו יום הולדת ל-Bella, והדפיסו Rex is 3 ו-Bella is 2.

A class Dog with name, age, a method String describe() returning e.g. Rex is 3, and a method void birthday() raising the age. Create Rex (3) and Bella (1), give Bella a birthday, and print Rex is 3 and Bella is 2.

איך הפלט אמור להיראות?What should the output look like?
Rex is 3
Bella is 2
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: bella.birthday() משנה רק את הגיל של bella.bella.birthday() changes only bella's age.
לפתרוןSolution
תרגילExercise 3

חשבון בנקA bank account

מחלקה Account עם int balance, void deposit(int amount) ו-boolean withdraw(int amount), שמושכת רק אם יש מספיק ומחזירה האם הצליחה. הפקידו 100, משכו 30 ואז 500, והדפיסו true 70 ו-false 70.

A class Account with int balance, void deposit(int amount) and boolean withdraw(int amount), which withdraws only if there's enough and returns whether it worked. Deposit 100, withdraw 30 then 500, and print true 70 and false 70.

איך הפלט אמור להיראות?What should the output look like?
true 70
false 70
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: אם amount > balance: return false בלי לשנות כלום.If amount > balance: return false without changing anything.
לפתרוןSolution
תרגילExercise 4

צייד באגים: מדף חצי ריקBug hunter: a half-empty shelf

התוכנית מהתזכורת קורסת עם NullPointerException. תקנו את הלולאה כך שעל תא ריק יודפס (empty): הפלט Dune ואז (empty).

The program from the refresher crashes with NullPointerException. Fix the loop so an empty cell prints (empty): the output Dune then (empty).

איך הפלט אמור להיראות?What should the output look like?
Dune
(empty)
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: if (b != null) { ... } else { ... }if (b != null) { ... } else { ... }
לפתרוןSolution
תרגילExercise 5

לשנות או להחליף?Change or replace?

rename(book) משנה את השדה של הספר שקיבלה. replace(book) שמה בפרמטר שלה ספר חדש. מה ידפיס main? כתבו את הקוד, הריצו, וצפו בסימולציה. הפלט הנכון: Renamed ואז שוב Renamed.

rename(book) changes the field of the book it got. replace(book) puts a new book into its parameter. What will main print? Write the code, run it and watch the simulation. The right output: Renamed then Renamed again.

איך הפלט אמור להיראות?What should the output look like?
Renamed
Renamed
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: Java מעבירה עותק של ההפניה. דרך העותק אפשר לשנות את העצם, אבל להחליף את העותק לא נוגע במשתנה של main.Java passes a copy of the reference. Through the copy you can change the object, but replacing the copy doesn't touch main's variable.
לפתרוןSolution
תרגילExercise 6

מדף, צעד 13: המחלקה BookShelf, step 13: the Book class

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

מחלקה Book עם title, author, year, ומתודות String line() (Emma by Austen (1815)) ו-boolean isClassic() (לפני 1950). שימו במערך את Emma (Austen, 1815), Dune (Herbert, 1965) ו-Ulysses (Joyce, 1922), הדפיסו שורה לכל ספר, ובסוף Classics: 2.

A class Book with title, author, year, and methods String line() (Emma by Austen (1815)) and boolean isClassic() (before 1950). Put Emma (Austen, 1815), Dune (Herbert, 1965) and Ulysses (Joyce, 1922) in an array, print a line per book, then Classics: 2.

איך הפלט אמור להיראות?What should the output look like?
Emma by Austen (1815)
Dune by Herbert (1965)
Ulysses by Joyce (1922)
Classics: 2
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: במפגש הבא בנאי יקצר את כל ההשמות.Next session a constructor will shorten all these assignments.
לפתרוןSolution

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

אתגרChallenge 1

יומן טמפרטורותA temperature log

מחלקה TempLog עם מערך double[] values = new double[10], מונה int size, ומתודות add, max ו-average (רק על התאים שמולאו). הוסיפו 21.5, 25.0, 19.5 והדפיסו 3 readings, max 25.0, avg 22.0.

A class TempLog with an array double[] values = new double[10], a counter int size, and methods add, max and average (over the filled cells only). Add 21.5, 25.0, 19.5 and print 3 readings, max 25.0, avg 22.0.

איך הפלט אמור להיראות?What should the output look like?
3 readings, max 25.0, avg 22.0
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: add: values[size] = v; size++;add: values[size] = v; size++;
רמזHint 2: הלולאות עד size, לא עד values.length.The loops go up to size, not values.length.
לפתרוןSolution
אתגרChallenge 2

מלבן שיודע לגדולA rectangle that can grow

מחלקה Rect עם width, height ומתודות area(), perimeter(), isSquare() ו-Rect scaled(int k) שמחזירה מלבן חדש גדול פי k (המקורי לא משתנה). עבור 3x3 הדפיסו 9 true ואז 6x6 24.

A class Rect with width, height and methods area(), perimeter(), isSquare() and Rect scaled(int k) returning a new rectangle k times bigger (the original doesn't change). For 3x3 print 9 true then 6x6 24.

איך הפלט אמור להיראות?What should the output look like?
9 true
6x6 24
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: בתוך scaled: Rect r = new Rect(); r.width = width * k; ...; return r;Inside scaled: Rect r = new Rect(); r.width = width * k; ...; return r;
לפתרוןSolution

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