In an assignment that I have to do, I have to write a Student object which comprises of a name, social security number, number of courses passed, and the grades for each of those courses. I made a gpa method to calculate the gpa based on and also made overrides of the writeObject and readObject methods with my own methods. Here is the complete source code- http://pastebin.com/dNMrc1zK
This is what it looks like without the getter and setter methods for each instance:
import java.io.*;
public class Student implements Serializable{
private String name;
private int ssNum;
private int coursesCompleted;
private char[] grades;
public Student(String n, int num, int c){
n = name;
num = ssNum;
c = coursesCompleted;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSsNum() {
return ssNum;
}
public void setSsNum(int ssNum) {
this.ssNum = ssNum;
}
public int getCoursesCompleted() {
return coursesCompleted;
}
public void setCoursesCompleted(int coursesCompleted) {
this.coursesCompleted = coursesCompleted;
}
public char[] getGrades() {
return grades;
}
public void setGrades(char[] grades) {
this.grades = grades;
}
public double gpa(char[] grades){
double gpa = 0;
for (int i = 0; i < grades.length; i++){
if (grades[i] == 'A'){
gpa += 4.0;
}
else if (grades[i] == 'B'){
gpa += 3.0;
}
else if (grades[i] == 'C'){
gpa += 2.0;
}
else if (grades[i] == 'D'){
gpa += 1.0;
}
else if (grades[i] == 'F'){
gpa += 0.0;
}
}
return gpa / grades.length;
}
public void ReadObject(ObjectInputStream in) throws IOException{
String readTitle = "";
for(int i = 0; i < name.length(); i++){
readTitle += in.readChar();
}
ssNum = in.readInt();
coursesCompleted = in.readInt();
for (int i = 0; i < grades.length; i++){
grades[i] = in.readChar();
}
}
public void WriteObjectOverride(ObjectOutputStream out) throws IOException, ClassNotFoundException{
out.writeChars(name);
out.writeInt(ssNum);
out.writeInt(coursesCompleted);
for (int i = 0; i < grades.length; i++){
out.writeChar(grades[i]);
}
gpa(grades);
}
public String toString(){
String student = name + " " + "\nGPA: " + gpa(grades);
return student;
}
}
public void ReadObject(ObjectInputStream in) throws IOException{
String readTitle = "";
for(int i = 0; i < name.length(); i++){
readTitle += in.readChar();
}
ssNum = in.readInt();
coursesCompleted = in.readInt();
for (int i = 0; i < grades.length; i++){
grades[i] = in.readChar();
}
}
public void WriteObjectOverride(ObjectOutputStream out) throws IOException, ClassNotFoundException{
out.writeChars(name);
out.writeInt(ssNum);
out.writeInt(coursesCompleted);
for (int i = 0; i < grades.length; i++){
out.writeChar(grades[i]);
}
gpa(grades);
}
public String toString(){
String student = name + " " + "\nGPA: " + gpa(grades);
return student;
}
}
I then have to write a separate program which writes the object to a file. I made a constructor so that it gives the name, ss number, and courses completed a value and then I used a setter method to set the grades array value. I then used the values I had to use writeObject and wrote the object like this:
Student john = new Student("John Doe", 123456, 4);
Student jane = new Student("Jane Doe", 987654, 3);
Student jack = new Student("Jack Doe", 528491, 4);
ObjectOutputStream out;
john.setGrades(student1);
jane.setGrades(student2);
jack.setGrades(student3);
try {
out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("students.bin")));
out.writeObject(john);
out.writeObject(jane);
out.writeObject(jack);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The problem I'm having is that when I use another program to execute the readObject() method, like this:
public static void main (String[] args)throws FileNotFoundException, IOException, ClassNotFoundException{
Student student = null;
try(ObjectInputStream in = new ObjectInputStream(new FileInputStream("students.bin"));){
student = (Student)in.readObject();
System.out.println(student);
}
It prints a line which not something I expected:
UnitSeven.Student@70dea4e
Does anyone know how to fix this problem so that it shows the actual object? Any help would be appreciated.