Illustration of class design will talk about documenting a
/**
Illustration of class design - will talk about documenting a class
Activity:
extend the main method to:
1. store the data of the Student object s onto file student.data
and close the file
2. open the student.data, read the student data into your
program and create a new Student object s2 with the data you\'ve
just obtained from the file.
3. display both Student objects on the screen (invoke their toString)
*/
public class Student {
//attributes
private String name;
private String bd;
private String address;
private double gpa;
private int id;
//constructor (will talk about \"this\" keyword)
public Student(String name, String bd, String address){
this.name = name;
this.bd = bd;
this.address = address;
this.gpa = 0.0;
//create an ID for this student
}
/**
allow user to get the gpa of the student
@param
@return the gpa in this object
*/
public double getGPA() {
return gpa;
}
/**
allow the user to access the student\'s name
@param
@return the object name attribute
*/
public String getName(){
return name;
}
/**
set the gpa to a given value
@param newGPA the new value to be set for this object gpa
@return nothing
*/
public void setGPA(double newGPA){
gpa = newGPA;
}
/**
return the object as a string
*/
public String toString(){
return this.name + \"\\t\" + this.address + \"\\t\" + this.bd + \" \\t\" + this.gpa;
}
//////////////////////////////////////////
//main method to test the Student class //
//////////////////////////////////////////
public static void main(String[] args){
Student s = new Student(\"Ken\", \"Nov. 08, 2016\", \"123 Heavenly Blvd\");
s.setGPA(3.0);
System.out.println(s.getName() + \" GPA: \" + s.getGPA());
System.out.println(s); //invoking the toString method
}
}//end of the class
Solution
Hi, I have extended the main method to include all three of your points. Please let me know if you have any doubts:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Student implements Serializable{
//Implementing Serializable enables writing object onto output stream
//attributes
private String name;
private String bd;
private String address;
private double gpa;
private int id;
//constructor (will talk about \"this\" keyword)
public Student(String name, String bd, String address){
this.name = name;
this.bd = bd;
this.address = address;
this.gpa = 0.0;
//create an ID for this student
}
/**
allow user to get the gpa of the student
@param
@return the gpa in this object
*/
public double getGPA() {
return gpa;
}
/**
allow the user to access the student\'s name
@param
@return the object name attribute
*/
public String getName(){
return name;
}
/**
set the gpa to a given value
@param newGPA the new value to be set for this object gpa
@return nothing
*/
public void setGPA(double newGPA){
gpa = newGPA;
}
/**
return the object as a string
*/
public String toString(){
return this.name + \"\\t\" + this.address + \"\\t\" + this.bd + \" \\t\" + this.gpa;
}
//////////////////////////////////////////
//main method to test the Student class //
//////////////////////////////////////////
public static void main(String[] args) throws Exception{
FileOutputStream fos=new FileOutputStream(\"student.data\");
//Creating student.data for writing student objects
ObjectOutputStream oos=new ObjectOutputStream(fos);
Student s = new Student(\"Ken\", \"Nov. 08, 2016\", \"123 Heavenly Blvd\");
s.setGPA(3.0);
oos.writeObject(s);
oos.close();
//Closing the file
FileInputStream fis=new FileInputStream(\"student.data\");
ObjectInputStream ois=new ObjectInputStream(fis);
Student s2;
s2=(Student) ois.readObject();
//Object is read from file and type-casted to Student type
System.out.println(\"Student s:\");
System.out.println(s); //invoking the toString method
System.out.println(\"\ Student s2\");
System.out.println(s2); //invoking the toString method
}
}//end of the class


