I didn't understand anything, so I don't even know what to ask...
And some other questions about this and the last tasks:
1. What does it mean when we write something like "classWithStatic.i" and "loadedObject.j" (these ".i" and ".j")? I don't remember seeing something like this before. Until now, I thought that everything that came after the period should be a method (e.g. something.close(); or something.flush();). But now we are writing these variables (?) after the period after an object? Is that it? Why?
2. Why there is always an overridden equals(Object o) method on these tasks? What is its purpose?
3. Why there is also an overridden hashCode() method on the tasks, with a mysterious code in it?
4. What does it mean to say "The main method is not tested." or "The main method exists just for you and is not involved in testing the solution."?
5. Am I really the only person here who didn't understand those things? I'm wondering if I unintentionally skipped some lesson xD.
Anyway, I feel that the difficulty of this lesson so far is not that the tasks are very hard in itself. It's just that they are ununderstandable at this point of learning (at least for me), because there is a lot of new stuff that I've never seen before and CodeGym didn't teach us...
Thanks in advance for any help!
[Code bellow is a copy of another post.]
package com.codegym.task.task20.task2004;
import java.io.*;
/*
Reading and writing static fields to a file
*/
public class Solution {
public static void main(String[] args) {
// You can find your_file_name.tmp in your TMP directory or adjust outputStream/inputStream according to your file's actual location
try {
File yourFile = File.createTempFile("your_file_name", null);
OutputStream outputStream = new FileOutputStream(yourFile);
InputStream inputStream = new FileInputStream(yourFile);
ClassWithStatic classWithStatic = new ClassWithStatic();
classWithStatic.i = 3;
classWithStatic.j = 4;
classWithStatic.save(outputStream);
outputStream.flush();
ClassWithStatic loadedObject = new ClassWithStatic();
loadedObject.staticString = "something";
loadedObject.i = 6;
loadedObject.j = 7;
loadedObject.load(inputStream);
// Here check that the classWithStatic object is equal to the loadedObject object
outputStream.close();
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 ClassWithStatic {
public static String staticString = "This is a static test string";
public int i;
public int j;
public void save(OutputStream outputStream) throws Exception {
// Implement this method
PrintWriter writer = new PrintWriter(outputStream);
writer.println(staticString);
//writer.flush();
writer.println(this.i);
//writer.flush();
writer.println(this.j);
//writer.flush();
}
public void load(InputStream inputStream) throws Exception {
// Implement this method
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String s1 = reader.readLine();
String s2 = reader.readLine();
String s3 = reader.readLine();
ClassWithStatic.staticString = s1;
this.i = Integer.parseInt(s2);
this.j = Integer.parseInt(s3);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ClassWithStatic that = (ClassWithStatic) o;
if (i != that.i) return false;
return j == that.j;
}
@Override
public int hashCode() {
int result = i;
result = 31 * result + j;
return result;
}
}
}