Path ו-Files, קריאה וכתיבה של טקסט, CSV, ושמירה וטעינה של JSON.
Path and Files, reading and writing text, CSV, and saving and loading JSON.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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
פתקיםNotes
כתבו לקובץ notes.txt שלוש שורות (buy milk, call Noa, read Dune), קראו אותן בחזרה, והדפיסו 3 lines, 25 chars ואת השורה האחרונה.
Write three lines to notes.txt (buy milk, call Noa, read Dune), read them back, and print 3 lines, 25 chars and the last line.
איך הפלט אמור להיראות?What should the output look like?
3 lines, 25 chars
read Dune
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:Files.write(p, List.of(...)) כותב שורה לכל איבר.Files.write(p, List.of(...)) writes a line per item.
לכל דף (home, shop, cart) הוסיפו לסוף visits.log שורה visit <page> (עם CREATE ו-APPEND), אחרי שמוחקים יומן ישן עם deleteIfExists. הדפיסו את הקובץ ואת מספר השורות (3).
For each page (home, shop, cart) append a line visit <page> to visits.log (with CREATE and APPEND), after deleting any old log with deleteIfExists. Print the file and the number of lines (3).
איך הפלט אמור להיראות?What should the output look like?
visit home
visit shop
visit cart
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:StandardOpenOption.CREATE, StandardOpenOption.APPEND: יוצר אם אין, ומוסיף אם יש.StandardOpenOption.CREATE, StandardOpenOption.APPEND: creates if missing, appends if present.
כתבו books.csv עם שורת כותרת ושלושה ספרים, קראו, דלגו על הכותרת, והפכו כל שורה ל-record Book(title, author, year). הדפיסו 3 books, average year 1901 ואת הספר השני.
Write books.csv with a header line and three books, read it, skip the header, and turn each line into record Book(title, author, year). Print 3 books, average year 1901 and the second book.
איך הפלט אמור להיראות?What should the output look like?
3 books, average year 1901
Book[title=Dune, author=Herbert, year=1965]
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:subList(1, ...) מדלג על הכותרת. line.split(",").subList(1, ...) skips the header. line.split(",").
התוכנית מהתזכורת קורסת. אם הקובץ לא קיים, צרו אותו עם Dune ו-Emma והדפיסו created a new books.txt. אחר כך 2 books.
The program from the refresher crashes. If the file doesn't exist, create it with Dune and Emma and print created a new books.txt. Then 2 books.
איך הפלט אמור להיראות?What should the output look like?
created a new books.txt
2 books
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.
toJson(List<Book>) בונה מערך JSON עם StringJoiner. שמרו ל-shelf.json, קראו בחזרה והדפיסו:
[{"title": "Dune", "rating": 5}, {"title": "Emma", "rating": 4}] ואז true 64.
toJson(List<Book>) builds a JSON array with a StringJoiner. Save to shelf.json, read it back and print:
[{"title": "Dune", "rating": 5}, {"title": "Emma", "rating": 4}] then true 64.
איך הפלט אמור להיראות?What should the output look like?
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
מדף, צעד 29: שמירה וטעינהShelf, step 29: save and load
פרויקט מלווהCompanion project
Storage.save(List<Book>, Path) כותב שורה title;author;year לכל ספר, ו-Storage.load(Path) קורא בחזרה. שמרו, הדפיסו את הקובץ, טענו, והדפיסו 2 loaded, same: true (records משווים לפי תוכן).
Storage.save(List<Book>, Path) writes a title;author;year line per book, and Storage.load(Path) reads it back. Save, print the file, load, and print 2 loaded, same: true (records compare by content).
איך הפלט אמור להיראות?What should the output look like?
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.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:loaded.equals(shelf) עובד כי List.equals משווה איבר-איבר, ו-record משווה שדות.loaded.equals(shelf) works because List.equals compares item by item, and a record compares fields.
parse(String json) מוציאה title ו-year מאובייקט JSON שטוח, בכל סדר ובכל ריווח, בעזרת Pattern ו-Matcher. הדפיסו Book[title=Dune, year=1965] ו-Book[title=Emma, year=1815]. (בפרויקט אמיתי: Jackson.)
parse(String json) pulls title and year out of a flat JSON object, in any order and spacing, using Pattern and Matcher. Print Book[title=Dune, year=1965] and Book[title=Emma, year=1815]. (In a real project: Jackson.)
איך הפלט אמור להיראות?What should the output look like?
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.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 pattern finding "name": value, where the value is a string or a number.
צרו את library/scifi (עם createDirectories), כתבו בה שני קבצים, הדפיסו את שמותיהם ממוינים ([dune.txt, solaris.txt]), מחקו את dune.txt, והדפיסו false library/scifi.
Create library/scifi (with createDirectories), write two files in it, print their names sorted ([dune.txt, solaris.txt]), delete dune.txt, and print false library/scifi.
איך הפלט אמור להיראות?What should the output look like?
[dune.txt, solaris.txt]
false library/scifi
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.