בנאים, overloading של בנאים, this() ו-this.field, וסדר האתחול.
Constructors, constructor overloading, this() and this.field, and initialization order.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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
תלמיד עם בנאיA student with a constructor
מחלקה 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.
איך הפלט אמור להיראות?What should the output look like?
Noa: 95
Avi: 82
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.
צייד באגים: הכותרת שנעלמהBug hunter: the vanishing title
התוכנית מדפיסה null במקום Dune, ולא קורסת. עצרו בתוך הבנאי (או צפו בסימולציה) ותקנו.
The program prints null instead of Dune, and doesn't crash. Stop inside the constructor (or watch the simulation) and fix it.
איך הפלט אמור להיראות?What should the output look like?
Dune
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:לאיזה title הולכת ההשמה?Which title does the assignment go to?
מחלקה 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.
איך הפלט אמור להיראות?What should the output look like?
large plain
small olives
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.
צייד באגים: אין בנאי ריקBug hunter: no empty constructor
התוכנית מהתזכורת לא מתקמפלת. בלי לשנות את 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.
איך הפלט אמור להיראות?What should the output look like?
Untitled
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.
מחלקה 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.
איך הפלט אמור להיראות?What should the output look like?
02:05 01:15
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.
מדף, צעד 14: בנאים ל-BookShelf, step 14: Book constructors
פרויקט מלווהCompanion project
ל-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").
איך הפלט אמור להיראות?What should the output look like?
Dune by Herbert (1965) rating 5
Notes by Me (unknown year) rating 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.
במחלקה 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().
איך הפלט אמור להיראות?What should the output look like?
field > Box(int) > Box()
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:אתחול השדה רץ לפני גוף הבנאי.The field initializer runs before the constructor body.
רמזHint 2:this(1) רץ לפני שאר השורות של Box().this(1) runs before the rest of Box().
ל-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.
איך הפלט אמור להיראות?What should the output look like?
1,2 9,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.