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

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

static, final וקבועיםstatic, final & constants

שדות static משותפים, final למשתנים, לשדות ולמחלקות, וקבועים בשמות גדולים.

Shared static fields, final for variables, fields and classes, and constants in capital letters.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. final int[] a = {1, 2}; מה מהבאים יתקמפל?final int[] a = {1, 2}; Which of these compiles?

2. איך נהוג לכתוב קבוע של מחלקה?How is a class constant usually written?

3. String היא final class. מה זה אומר?String is a final class. What does that mean?

תרגיליםExercises

תרגילExercise 1

עובר או נכשלPass or fail

מחלקה Grade עם קבועים PASS = 56 ו-MAX = 100, ו-static String result(int score) שמחזירה pass, fail או invalid (מחוץ ל-0 עד MAX). הדפיסו עבור 90, 40, 130.

A class Grade with constants PASS = 56 and MAX = 100, and static String result(int score) returning pass, fail or invalid (outside 0 to MAX). Print for 90, 40, 130.

איך הפלט אמור להיראות?What should the output look like?
pass
fail
invalid
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 final int PASS = 56;static final int PASS = 56;
לפתרוןSolution
תרגילExercise 2

צייד באגים: מונה שאסור לו לזוזBug hunter: a counter that can't move

התוכנית סופרת כמה מספרים מ-1 עד 10 מתחלקים ב-3, ולא מתקמפלת. צפו ב"איפה זה נשבר" ותקנו כך שיודפס 3.

The program counts how many numbers from 1 to 10 divide by 3, and doesn't compile. Watch "where it breaks" and fix it so it prints 3.

איך הפלט אמור להיראות?What should the output look like?
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: מוחקים את final.Remove final.
לפתרוןSolution
תרגילExercise 3

כרטיסים ממוספריםNumbered tickets

מחלקה Ticket עם private static int nextId = 1, ושדות private final int id ו-private final String owner. הבנאי לוקח את המספר הבא. static int issued() מחזירה כמה כרטיסים יצאו. הדפיסו #1 Noa, #2 Avi, issued 2.

A class Ticket with private static int nextId = 1, and fields private final int id and private final String owner. The constructor takes the next number. static int issued() returns how many tickets went out. Print #1 Noa, #2 Avi, issued 2.

איך הפלט אמור להיראות?What should the output look like?
#1 Noa
#2 Avi
issued 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: this.id = nextId; nextId++;this.id = nextId; nextId++;
לפתרוןSolution
תרגילExercise 4

בלי מספרי קסםNo magic numbers

בקוד יש שלושה מספרי קסם: 0.17, 10 ו-0.1. הפכו אותם לקבועים VAT, BULK_QTY ו-BULK_DISCOUNT, בלי לשנות את התוצאה: 58.50 210.60.

The code has three magic numbers: 0.17, 10 and 0.1. Turn them into constants VAT, BULK_QTY and BULK_DISCOUNT, without changing the result: 58.50 210.60.

איך הפלט אמור להיראות?What should the output look like?
58.50 210.60
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 final double VAT = 0.17; מעל total.static final double VAT = 0.17; above total.
לפתרוןSolution
תרגילExercise 5

ימי השבועDays of the week

מחלקה Week עם הקבוע static final String[] DAYS (Sun עד Sat) ו-static String dayName(int day) (1 עד 7, אחרת זורקת day must be 1-7, got 8). הדפיסו Sun Sat, את 7, ואת ההודעה עבור 8.

A class Week with the constant static final String[] DAYS (Sun to Sat) and static String dayName(int day) (1 to 7, otherwise it throws day must be 1-7, got 8). Print Sun Sat, then 7, then the message for 8.

איך הפלט אמור להיראות?What should the output look like?
Sun Sat
7
day must be 1-7, got 8
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: DAYS[day - 1]DAYS[day - 1]
רמזHint 2: שימו לב: final על מערך לא מונע DAYS[0] = "X" (ראו בתזכורת).Note: final on an array doesn't prevent DAYS[0] = "X" (see the refresher).
לפתרוןSolution
תרגילExercise 6

מדף, צעד 16: מונה ספרים וקבועי דירוגShelf, step 16: a book counter and rating constants

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

ל-Book יש MIN_RATING ו-MAX_RATING, מונה private static int count, ו-private final int id. הבנאי בודק את הדירוג לפני שהוא סופר. צרו את Dune (5), Emma (4) ו-Ulysses (9), כל אחד ב-try משלו, והדפיסו #1 Dune (5), #2 Emma (4), Error: Rating must be 1-5, got 9, Books: 2.

Book has MIN_RATING and MAX_RATING, a counter private static int count, and private final int id. The constructor checks the rating before counting. Create Dune (5), Emma (4) and Ulysses (9), each in its own try, and print #1 Dune (5), #2 Emma (4), Error: Rating must be 1-5, got 9, Books: 2.

איך הפלט אמור להיראות?What should the output look like?
#1 Dune (5)
#2 Emma (4)
Error: Rating must be 1-5, got 9
Books: 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: אם סופרים לפני הבדיקה, ספר שנדחה "גונב" מספר.If you count before checking, a rejected book "steals" a number.
רמזHint 2: ההודעה בונה את הטווח מהקבועים.The message builds the range from the constants.
לפתרוןSolution

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

אתגרChallenge 1

מתודות יצרןFactory methods

ל-Temperature יש בנאי private, ושתי מתודות יצרן: ofCelsius(double) ו-ofFahrenheit(double). הדפיסו את הצלזיוס של 100°C ושל 98.6°F, עם ספרה אחת אחרי הנקודה: 100.0 37.0.

Temperature has a private constructor and two factory methods: ofCelsius(double) and ofFahrenheit(double). Print the celsius of 100°C and of 98.6°F, with one digit after the point: 100.0 37.0.

איך הפלט אמור להיראות?What should the output look like?
100.0 37.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: new Temperature(...) מבחוץ לא יתקמפל, וזו המטרה: השם של המתודה אומר באיזו יחידה המספר.new Temperature(...) from outside won't compile, and that's the point: the method's name says which unit the number is in.
רמזHint 2: (f - 32) * 5 / 9(f - 32) * 5 / 9
לפתרוןSolution
אתגרChallenge 2

מחלקת עזר אמיתיתA real utility class

final class TextUtil עם בנאי private ריק (כדי שאיש לא ייצור ממנה עצם), ושתי מתודות static: initials("Ariel Yohimovich") מחזירה A.Y., ו-isPalindrome (level כן, java לא). הדפיסו A.Y. ואז true false.

final class TextUtil with an empty private constructor (so nobody makes an object of it), and two static methods: initials("Ariel Yohimovich") returns A.Y., and isPalindrome (level yes, java no). Print A.Y. then true false.

איך הפלט אמור להיראות?What should the output look like?
A.Y.
true false
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: new StringBuilder(s).reverse().toString()new StringBuilder(s).reverse().toString()
לפתרוןSolution

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