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

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. כמה עותקים יש לשדה static int count כשיש 5 עצמים?How many copies of static int count are there with 5 objects?

2. איך קוראים למתודה סטטית max3 במחלקה MathUtil מתוך Main?How do you call static method max3 of class MathUtil from Main?

3. למה main חייב להיות static?Why must main be static?

תרגיליםExercises

תרגילExercise 1

מחלקת עזרA utility class

כתבו מחלקה 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.

איך הפלט אמור להיראות?What should the output look like?
9
-1
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: class MathUtil { ... } בלי public, אחרי הסוגר של Main.class MathUtil { ... } without public, after Main's closing brace.
רמזHint 2: Math.max(a, Math.max(b, c))Math.max(a, Math.max(b, c))
לפתרוןSolution
תרגילExercise 2

קבוע מע"מA VAT constant

הגדירו קבוע 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.

איך הפלט אמור להיראות?What should the output look like?
117.0
58.5
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: price * (1 + VAT)price * (1 + VAT)
רמזHint 2: נסו לכתוב VAT = 0.18; בתוך main ותראו מה javac עונה.Try writing VAT = 0.18; inside main and see what javac says.
לפתרוןSolution
תרגילExercise 3

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

כתבו מחלקה 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.

איך הפלט אמור להיראות?What should the output look like?
Visitors: 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.
רמזHint 1: בנאי הוא מתודה בשם המחלקה, בלי טיפוס החזרה: Visitor(String name) { this.name = name; total++; }.A constructor is a method named after the class, with no return type: Visitor(String name) { this.name = name; total++; }.
רמזHint 2: Visitor.total מבחוץ.Visitor.total from outside.
לפתרוןSolution
תרגילExercise 4

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

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

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

איך הפלט אמור להיראות?What should the output look like?
25
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: מה חסר בכותרת של square, שיש בכותרת של main?What's missing from square's header that main's header has?
לפתרוןSolution
תרגילExercise 5

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

במחלקה 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.

איך הפלט אמור להיראות?What should the output look like?
a=3 b=2 all=5
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: click היא מתודת מופע: היא נוגעת גם ב-count של העצם וגם ב-all המשותף.click is an instance method: it touches both the object's count and the shared all.
לפתרוןSolution
תרגילExercise 6

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

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

כתבו מחלקת עזר 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.

איך הפלט אמור להיראות?What should the output look like?
Dune *****
Emma ****-
Ulysses **---
Average: 3.67
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: "*".repeat(rating) + "-".repeat(5 - rating)"*".repeat(rating) + "-".repeat(5 - rating)
רמזHint 2: String.format("%.2f", x) לשתי ספרות אחרי הנקודה.String.format("%.2f", x) for two digits after the point.
לפתרוןSolution

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

אתגרChallenge 1

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

כתבו מחלקה 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.

איך הפלט אמור להיראות?What should the output look like?
B-100
B-101
M-102
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: המונה static, כי כל המזהים חולקים אותו רצף.The counter is static, because all ids share one sequence.
לפתרוןSolution
אתגרChallenge 2

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

במחלקה 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.

איך הפלט אמור להיראות?What should the output look like?
Longest: Children of Dune
Books created: 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.
רמזHint 1: longest סטטית כי היא לא שייכת לספר אחד: היא משווה בין ספרים.longest is static because it doesn't belong to one book: it compares books.
רמזHint 2: בתוכה ניגשים ל-b.title דרך העצם b.Inside, reach b.title through the object b.
לפתרוןSolution

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