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

JavaJava
2
חלקPart 1: יסודותFoundations כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

משתנים וטיפוסים פרימיטיבייםVariables & primitive types

int, double, boolean, char ו-long, var, המרות טיפוסים, גלישה (overflow) וחלוקה של מספרים שלמים.

int, double, boolean, char and long, var, type conversions, overflow and integer division.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. מה ידפיס הקוד?What does this code print?
main
int x = 7;
x = x * 2;
x = x - 4;
System.out.println(x);

2. מה הערך של (int) 9.99?What is the value of (int) 9.99?

3. איזו הצהרה לא תתקמפל?Which declaration will not compile?

תרגיליםExercises

תרגילExercise 1

בעוד שנהNext year

צרו משתנה name מסוג String ומשתנה age מסוג int. הדפיסו <שם> בן/בת <גיל>, אחר כך הוסיפו 1 לגיל בתוך המשתנה והדפיסו שוב את הגיל החדש.

Create a String variable name and an int variable age. Print <name> is <age>, then add 1 to the age inside the variable and print the new age.

איך הפלט אמור להיראות?What should the output look like?
נועה בת 15
בשנה הבאה: 16
Noa is 15
Next year: 16
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: int age = 15; יוצר קופסה עם 15.int age = 15; makes a box holding 15.
רמזHint 2: כדי לשנות: age = age + 1; (או age++;).To change it: age = age + 1; (or age++;).
לפתרוןSolution
תרגילExercise 2

מחליפים קופסאותSwapping boxes

ב-a יש 5 וב-b יש 9. החליפו ביניהם בלי לכתוב את המספרים שוב, והדפיסו a = 9, b = 5. רמז: צריך קופסה שלישית.

a holds 5 and b holds 9. Swap them without writing the numbers again, then print a = 9, b = 5. Hint: you need a third box.

איך הפלט אמור להיראות?What should the output look like?
a = 9, b = 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: אם כותבים a = b; מיד, הערך הישן של a נעלם!If you write a = b; right away, the old value of a is gone!
רמזHint 2: קודם שומרים את a בקופסה זמנית: int temp = a;.First keep a in a temporary box: int temp = a;.
רמזHint 3: ואז a = b; ואחריו b = temp;.Then a = b; followed by b = temp;.
לפתרוןSolution
תרגילExercise 3

ממוצע ציוניםGrade average

שלושה ציונים: 85, 90 ו-78. שמרו כל אחד במשתנה int, חשבו את הממוצע במשתנה double והדפיסו אותו עם שתי ספרות אחרי הנקודה: 84.33.

Three grades: 85, 90 and 78. Keep each in an int variable, compute the average in a double variable and print it with two digits after the point: 84.33.

איך הפלט אמור להיראות?What should the output look like?
84.33
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: (g1 + g2 + g3) / 3 הוא חלוקה של שלמים, ויוצא 84!(g1 + g2 + g3) / 3 is integer division and gives 84!
רמזHint 2: מחלקים ב-3.0, ואז התוצאה double.Divide by 3.0, and the result is a double.
רמזHint 3: להדפסה: System.out.printf("%.2f%n", avg);.To print: System.out.printf("%.2f%n", avg);.
לפתרוןSolution
תרגילExercise 4

מצלזיוס לפרנהייטCelsius to Fahrenheit

במשתנה double celsius שימו 30. חשבו fahrenheit = celsius * 9 / 5 + 32 והדפיסו 30.0C = 86.0F.

Put 30 in a double celsius variable. Compute fahrenheit = celsius * 9 / 5 + 32 and print 30.0C = 86.0F.

איך הפלט אמור להיראות?What should the output look like?
30.0C = 86.0F
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: כשהמשתנה הוא double, גם 30 יודפס כ-30.0.When the variable is a double, 30 prints as 30.0.
רמזHint 2: כי celsius הוא double, כל החישוב עשרוני.Since celsius is a double, the whole calculation is decimal.
לפתרוןSolution
תרגילExercise 5

צייד באגים: הממוצע שנעלםBug hunter: the vanishing half

הקוד אמור להדפיס 7.5, אבל מדפיס 7.0. אין שגיאה: זה באג לוגי. מצאו ותקנו.

The code should print 7.5 but prints 7.0. There's no error: it's a logic bug. Find it and fix it.

איך הפלט אמור להיראות?What should the output look like?
7.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: מה הטיפוס של (a + b) / 2? שני הצדדים int.What type is (a + b) / 2? Both sides are int.
רמזHint 2: החלוקה קורית לפני שהתוצאה נכנסת ל-double, ולכן החצי כבר אבד.The division happens before the result goes into the double, so the half is already lost.
רמזHint 3: מחלקים ב-2.0.Divide by 2.0.
לפתרוןSolution
תרגילExercise 6

מדף, צעד 2: ספר במשתניםShelf, step 2: a book in variables

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

שמרו ספר אחד במשתנים: String title, String author, int year, double rating ו-boolean read. הדפיסו כרטיס של 4 שורות: שם וסופר, שנה, Rating: 4.5/5, ו-Read: true.

Keep one book in variables: String title, String author, int year, double rating and boolean read. Print a 4-line card: title and author, year, Rating: 4.5/5, and Read: true.

איך הפלט אמור להיראות?What should the output look like?
הנסיך הקטן - Antoine de Saint-Exupéry
Year: 1943
Rating: 4.5/5
Read: true
The Little Prince - Antoine de Saint-Exupéry
Year: 1943
Rating: 4.5/5
Read: true
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: חמש קופסאות, כל אחת עם הטיפוס המתאים.Five boxes, each with the right type.
רמזHint 2: boolean מודפס כ-true או false.A boolean prints as true or false.
לפתרוןSolution

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

אתגרChallenge 1

בלש הגלישהThe overflow detective

כמה שניות יש ב-100 שנה (365 ימים בשנה)? חשבו פעם אחת עם int ופעם עם long, והדפיסו את שתי התוצאות בשתי שורות. איזו נכונה? (הנכונה: 3153600000.)

How many seconds are in 100 years (365 days a year)? Compute it once with int and once with long, and print both results on two lines. Which is right? (The right one: 3153600000.)

איך הפלט אמור להיראות?What should the output look like?
-1141367296
3153600000
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: 100 * 365 * 24 * 60 * 60 כולו int, ולכן גולש.100 * 365 * 24 * 60 * 60 is all int, so it overflows.
רמזHint 2: מספיק 100L בהתחלה כדי שכל החישוב יהיה long.Just 100L at the start makes the whole calculation long.
לפתרוןSolution
אתגרChallenge 2

סכום הספרותSum of the digits

במשתנה n יש 4721. בלי לולאות, חשבו את סכום הספרות שלו (4+7+2+1) והדפיסו 14. השתמשו ב-% וב-/.

n holds 4721. Without loops, compute the sum of its digits (4+7+2+1) and print 14. Use % and /.

איך הפלט אמור להיראות?What should the output look like?
14
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: n % 10 נותן את הספרה האחרונה (1).n % 10 gives the last digit (1).
רמזHint 2: n / 10 מוחק את הספרה האחרונה (472).n / 10 drops the last digit (472).
רמזHint 3: חוזרים על זה ארבע פעמים, ושומרים כל ספרה במשתנה.Do it four times, keeping each digit in a variable.
לפתרוןSolution

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