מחרוזות הן עצמים שלא משתנים, מתודות שימושיות, השוואה עם equals, String.format ו-StringBuilder לבנייה יעילה.
Strings are immutable objects, useful methods, comparing with equals, String.format, and StringBuilder for efficient building.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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
שם באותיות גדולותName in capitals
במשתנים first ו-last שמרו שם פרטי ושם משפחה באנגלית. הדפיסו את השם המלא באותיות גדולות, ובשורה שנייה את מספר התווים שלו (כולל הרווח).
Keep a first and last name (in English) in first and last. Print the full name in capitals, and on a second line the number of characters in it (space included).
איך הפלט אמור להיראות?What should the output look like?
NOA LEVI
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:String full = first + " " + last;String full = first + " " + last;
רמזHint 2:full.toUpperCase() ו-full.length().full.toUpperCase() and full.length().
במשתנה name יש "Noa Levi". הדפיסו את ראשי התיבות N.L. בעזרת charAt ו-indexOf (בלי לכתוב את האותיות בעצמכם).
name holds "Noa Levi". Print the initials N.L. using charAt and indexOf (without writing the letters yourself).
איך הפלט אמור להיראות?What should the output look like?
N.L.
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:האות הראשונה: name.charAt(0).The first letter: name.charAt(0).
רמזHint 2:איפה הרווח? name.indexOf(" "). האות שאחריו באינדקס הבא.Where's the space? name.indexOf(" "). The letter after it is at the next index.
רמזHint 3:"" + c1 + "." + c2 + ".": מתחילים במחרוזת ריקה כדי שהתווים לא יתחברו כמספרים."" + c1 + "." + c2 + ".": start with an empty string so the chars aren't added as numbers.
במשתנה s יש "Java is hard". צרו משתנה חדש עם replace שבו hard הוחלף ב-fun, והדפיסו את שניהם: קודם את המקורי ואחר כך את החדש.
s holds "Java is hard". Make a new variable with replace where hard became fun, and print both: the original first, then the new one.
איך הפלט אמור להיראות?What should the output look like?
Java is hard
Java is fun
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:String t = s.replace("hard", "fun");String t = s.replace("hard", "fun");
רמזHint 2:שימו לב: s לא השתנה, כי String לא משתנה.Notice: s didn't change, because a String never changes.
הקוד משווה שתי מחרוזות עם אותו תוכן ומדפיס false. תקנו כך שיודפס true. (צפו בזיכרון בתזכורת למעלה כדי להבין למה.)
The code compares two strings with the same content and prints false. Fix it so it prints true. (Look at the memory view in the refresher above to see why.)
איך הפלט אמור להיראות?What should the output look like?
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:== בודק אם זה אותו עצם.== checks whether it's the same object.
רמזHint 2:a.equals(b) בודק את התוכן.a.equals(b) checks the content.
הדפיסו קבלה עם printf: שלוש שורות של מוצר ומחיר (Coffee 12.5, Cake 18.9, Juice 25), שם המוצר ברוחב 8 וצמוד לשמאל, המחיר ברוחב 7 עם 2 ספרות. בשורה האחרונה Total והסכום (שימו את המחירים במשתנים ותנו ל-Java לחבר).
Print a receipt with printf: three product-and-price lines (Coffee 12.5, Cake 18.9, Juice 25), the name 8 wide and left-aligned, the price 7 wide with 2 digits. On the last line Total and the sum (keep the prices in variables and let Java add them up).
איך הפלט אמור להיראות?What should the output look like?
Coffee 12.50
Cake 18.90
Juice 25.00
Total 56.40
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.
במשתנה word יש "level". הפכו אותו עם StringBuilder, הדפיסו את המילה ההפוכה, ובשורה שנייה true אם היא זהה למקור.
word holds "level". Reverse it with StringBuilder, print the reversed word, and on a second line true if it equals the original.
איך הפלט אמור להיראות?What should the output look like?
level
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.
מדף, צעד 3: כרטיס מעוצבShelf, step 3: a formatted card
פרויקט מלווהCompanion project
שמרו ספר במשתנים (title, author, year, stars מסוג int בין 1 ל-5). בנו עם String.format שורה בפורמט <title> | <author> | <year>, והדפיסו אותה ואחריה שורת כוכבים: * כפול stars (רמז: "*".repeat(stars)).
Keep a book in variables (title, author, year, stars as an int 1–5). Build with String.format a line like <title> | <author> | <year>, print it, and then a line of stars: * times stars (hint: "*".repeat(stars)).
איך הפלט אמור להיראות?What should the output look like?
הנסיך הקטן | Saint-Exupéry | 1943
****
The Little Prince | Saint-Exupéry | 1943
****
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.
במשתנה email יש "noa.levi@example.com". הדפיסו user: noa.levi ו-domain: example.com, ובשורה שלישית את שם המשתמש באותיות גדולות בלי הנקודה: NOALEVI.
email holds "noa.levi@example.com". Print user: noa.levi and domain: example.com, and on a third line the user name in capitals without the dot: NOALEVI.
איך הפלט אמור להיראות?What should the output look like?
user: noa.levi
domain: example.com
NOALEVI
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 at = email.indexOf("@");int at = email.indexOf("@");
במשתנה s יש "the little prince" (שלוש מילים). בלי לולאות, הדפיסו The Little Prince: האות הראשונה של כל מילה גדולה.
s holds "the little prince" (three words). Without loops, print The Little Prince: each word's first letter in capitals.
איך הפלט אמור להיראות?What should the output look like?
The Little Prince
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:מוצאים את שני הרווחים: indexOf(" ") ואז indexOf(" ", first + 1).Find both spaces: indexOf(" ") then indexOf(" ", first + 1).