static, final וקבועיםstatic, final & constants
שדות static משותפים, final למשתנים, לשדות ולמחלקות, וקבועים בשמות גדולים.
Shared static fields, final for variables, fields and classes, and constants in capital letters.
עובר או נכשלPass or fail
תזכורת: מה ביקשוReminder: the task
מחלקה 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.
סימולציה: הפתרון רץ שורה אחרי שורה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
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
System.out.println(Grade.result(90));
System.out.println(Grade.result(40));
System.out.println(Grade.result(130));
}
}
class Grade {
static final int PASS = 56;
static final int MAX = 100;
static String result(int score) {
if (score < 0 || score > MAX) {
return "invalid";
}
return score >= PASS ? "pass" : "fail";
}
}pass fail invalid
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
צייד באגים: מונה שאסור לו לזוזBug hunter: a counter that can't move
תזכורת: מה ביקשוReminder: the task
התוכנית סופרת כמה מספרים מ-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.
סימולציה: הפתרון רץ שורה אחרי שורה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
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0) {
count++;
}
}
System.out.println(count);
}
}3
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
כרטיסים ממוספריםNumbered tickets
תזכורת: מה ביקשוReminder: the task
מחלקה 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.
סימולציה: הפתרון רץ שורה אחרי שורה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
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
Ticket a = new Ticket("Noa");
Ticket b = new Ticket("Avi");
System.out.println("#" + a.getId() + " " + a.getOwner());
System.out.println("#" + b.getId() + " " + b.getOwner());
System.out.println("issued " + Ticket.issued());
}
}
class Ticket {
private static int nextId = 1;
private final int id;
private final String owner;
Ticket(String owner) {
this.owner = owner;
this.id = nextId;
nextId++;
}
int getId() {
return id;
}
String getOwner() {
return owner;
}
static int issued() {
return nextId - 1;
}
}#1 Noa #2 Avi issued 2
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
בלי מספרי קסםNo magic numbers
תזכורת: מה ביקשוReminder: the task
בקוד יש שלושה מספרי קסם: 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.
סימולציה: הפתרון רץ שורה אחרי שורה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
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
static final double VAT = 0.17;
static final int BULK_QTY = 10;
static final double BULK_DISCOUNT = 0.1;
static double total(double price, int qty) {
double sum = price * qty;
if (qty > BULK_QTY) {
sum = sum * (1 - BULK_DISCOUNT);
}
return sum * (1 + VAT);
}
public static void main(String[] args) {
System.out.println(String.format("%.2f %.2f", total(10, 5), total(10, 20)));
}
}58.50 210.60
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ימי השבועDays of the week
תזכורת: מה ביקשוReminder: the task
מחלקה 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.
סימולציה: הפתרון רץ שורה אחרי שורה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
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
System.out.println(Week.dayName(1) + " " + Week.dayName(7));
System.out.println(Week.DAYS.length);
try {
Week.dayName(8);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
class Week {
static final String[] DAYS = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static String dayName(int day) {
if (day < 1 || day > DAYS.length) {
throw new IllegalArgumentException("day must be 1-7, got " + day);
}
return DAYS[day - 1];
}
}Sun Sat 7 day must be 1-7, got 8
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 16: מונה ספרים וקבועי דירוגShelf, step 16: a book counter and rating constants
תזכורת: מה ביקשוReminder: the task
ל-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.
סימולציה: הפתרון רץ שורה אחרי שורה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
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
String[] titles = {"Dune", "Emma", "Ulysses"};
int[] ratings = {5, 4, 9};
for (int i = 0; i < titles.length; i++) {
try {
Book b = new Book(titles[i], ratings[i]);
System.out.println(b.line());
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
}
System.out.println("Books: " + Book.getCount());
}
}
class Book {
static final int MIN_RATING = 1;
static final int MAX_RATING = 5;
private static int count = 0;
private final int id;
private final String title;
private final int rating;
Book(String title, int rating) {
if (rating < MIN_RATING || rating > MAX_RATING) {
throw new IllegalArgumentException("Rating must be " + MIN_RATING + "-" + MAX_RATING + ", got " + rating);
}
count++;
this.id = count;
this.title = title;
this.rating = rating;
}
String line() {
return "#" + id + " " + title + " (" + rating + ")";
}
static int getCount() {
return count;
}
}#1 Dune (5) #2 Emma (4) Error: Rating must be 1-5, got 9 Books: 2
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מתודות יצרןFactory methods
תזכורת: מה ביקשוReminder: the task
ל-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.
סימולציה: הפתרון רץ שורה אחרי שורה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
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
Temperature boil = Temperature.ofCelsius(100);
Temperature body = Temperature.ofFahrenheit(98.6);
System.out.println(String.format("%.1f %.1f", boil.getCelsius(), body.getCelsius()));
}
}
final class Temperature {
private final double celsius;
private Temperature(double celsius) {
this.celsius = celsius;
}
static Temperature ofCelsius(double c) {
return new Temperature(c);
}
static Temperature ofFahrenheit(double f) {
return new Temperature((f - 32) * 5 / 9);
}
double getCelsius() {
return celsius;
}
}100.0 37.0
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מחלקת עזר אמיתיתA real utility class
תזכורת: מה ביקשוReminder: the task
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.
סימולציה: הפתרון רץ שורה אחרי שורה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
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
System.out.println(TextUtil.initials("Ariel Yohimovich"));
System.out.println(TextUtil.isPalindrome("level") + " " + TextUtil.isPalindrome("java"));
}
}
final class TextUtil {
private TextUtil() {
}
static String initials(String name) {
String out = "";
for (String part : name.split(" ")) {
out += part.charAt(0) + ".";
}
return out;
}
static boolean isPalindrome(String s) {
return new StringBuilder(s).reverse().toString().equals(s);
}
}A.Y. true false
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.