CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Youngsan
Level 41
Seoul
  • 17.09.2020
  • 207views
  • 2comments

Pls help!

Question about the task Stranger things
Java Core,  Level 10,  Lesson 2
Under discussion

Strange errors are occurring while reading/writing Human objects.
Figure out what's wrong and fix it.

Requirements:
  • The read/write logic in the save/load methods must work correctly when the assets list is null.
  • The read/write logic in the save/load methods must work correctly when the name field and assets list are not null.
  • The Solution.Human class should not support the Serializable interface.
  • The Solution.Human class must be public.
  • The Solution.Human class should not support the Externalizable interface.
  • The equals method must return true for two equal Human objects and false for different objects.
  • The hashCode method must always return the same value for the same Human object.
package com.codegym.task.task20.task2005; import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /* Stranger things */ public class Solution { public static void main(String[] args) { // Update the string passed to the createTempFile method based on the path to a file on your hard drive try { File yourFile = File.createTempFile("your_file_name", null); OutputStream outputStream = new FileOutputStream(yourFile); InputStream inputStream = new FileInputStream(yourFile); Human smith = new Human ("Smith", new Asset ("home"), new Asset ("car")); smith.save(outputStream); outputStream.flush(); Human somePerson = new Human(); somePerson.load(inputStream); // Check that smith is equal to somePerson System.out.println(smith.equals(somePerson)); inputStream.close(); } catch (IOException e) { // e.printStackTrace(); System.out.println("Oops, something is wrong with my file"); } catch (Exception e) { // e.printStackTrace(); System.out.println("Oops, something is wrong with the save/load method"); } } public static class Human { public String name; public List<Asset> assets = new ArrayList<>(); @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Human human = (Human) o; if (name != null ? !name.equals(human.name) : human.name != null) return false; return assets != null ? assets.equals(human.assets) : human.assets == null; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + (assets != null ? assets.hashCode() : 0); return result; } public Human() { } public Human(String name, Asset... assets) { this.name = name; if (assets != null) { this.assets.addAll(Arrays.asList(assets)); } } public void save(OutputStream outputStream) throws Exception { // Implement this method PrintWriter printWriter = new PrintWriter(outputStream); if (name != null) { printWriter.println(name); String isAsset = assets != null ? "yes" : "no"; printWriter.println(isAsset); } if (assets != null) { for (Asset current : assets) printWriter.println(current.getName()); } printWriter.close(); } public void load(InputStream inputStream) throws Exception { // Implement this method BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); while (reader.ready()) { String name = reader.readLine(); String isAsset = reader.readLine(); if (isAsset.equals("yes")){ String assetName; while ((assetName = reader.readLine()) != null){ assets.add(new Asset(assetName)); } } } reader.close(); } } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Nouser
Level 36 , Germany
17 September 2020, 06:15
Validation just asks for one Human object. And it doesn't matter if the name is null. Therefore it is enough to save the name no matter if it is null. Same for the assets, just save them (without yes:no) When reading, you read the name. Then you check if there's a next line and if yes, you read as long as there are next lines and add these lines to the assets list. Cause there's no next Human following that's it. If you want to make it work for several Human objects (not asked in this task, just if you really want to code it), save a marker (like [[[end-Human]]] or similar) after saving a Human (after the name, if there are no assets, after the assets if you have some). Loading: after loading the name, check if the next line is the marker, if yes skip reading assets. If not, read till you reach the marker.
0
Youngsan
Level 41 , Seoul, Korea, Republic of
17 September 2020, 10:39
Modified, but still same problem. I'll try it later. Thanks.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.