Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/org/gh/RunMe.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public static void main(String args[]){
// Create a new student, set a name and a grade
// NOTE: since Student inherits from Person we also get person methods
// see getName() and toString() below which are defined in Person
System.out.println("Creating a new student and Teacher");
System.out.println("Creating a new student and Teacher and Sophmore");
Student objStudent = new Student("Student 1", "GradeA");
Teacher objTeacher = new Teacher("Teacher 1", "Class#1, Class#2");
Sophmore objSophmore = new Sophmore("Sophmore 1", "Chemistry, Algebra 2");


// Print details of a student we created above (name - grade)
Expand All @@ -29,17 +30,21 @@ public static void main(String args[]){
System.out.println("Print with name (from person) and Classes (from Teacher)");
System.out.println(objTeacher.getName() + " - " + objTeacher.getClasses());

System.out.println("Print with name (from person) and Classes (from Sophmore)");
System.out.println(objSophmore.getName() + " - " + objSophmore.getKd());

// This uses the polymorphism to call printMe and see different results
System.out.println("Print the Student and teacher using the printMe method from Printable");
System.out.println("Print the Student, teacher, and sophmore using the printMe method from Printable");
System.out.println(objStudent.printMe());
System.out.println(objTeacher.printMe());
System.out.println(objSophmore.printMe());


// Use the toString method of Student (not overriden from the Parent definition)
System.out.println("Use the toString method in person since it's not overridden in the subclasses");
System.out.println(objStudent);
System.out.println(objTeacher);
System.out.println(objSophmore);


// Create a new object and print it
Expand All @@ -55,6 +60,7 @@ public static void main(String args[]){
ObjectOutputStream oOut = new ObjectOutputStream(fOut);
oOut.writeObject(objStudent);
oOut.writeObject(objTeacher);
oOut.writeObject(objSophmore);
oOut.close();
fOut.close();
System.out.println("Wrote the file, now reading it");
Expand All @@ -63,10 +69,12 @@ public static void main(String args[]){
ObjectInputStream in = new ObjectInputStream(fileIn);
Student readStudent = (Student) in.readObject();
Teacher readTeacher = (Teacher) in.readObject();
Sophmore readSophmore = (Sophmore) in.readObject();
in.close();
fileIn.close();
System.out.println(readStudent.printMe());
System.out.println(readTeacher.printMe());
System.out.println(readSophmore.printMe());
System.out.println("See above for results after read");
}catch (Exception e){
System.err.println("Caught:"+e.getMessage());
Expand Down
24 changes: 24 additions & 0 deletions src/org/gh/Sophmore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.gh;

public class Sophmore extends Person implements Printable{

private String kd;

public void setKd(String kd){
this.kd = kd;

}

public String getKd(){
return this.kd;
}

public Sophmore(String name, String kd){
super(name);
this.setKd(kd);
}

public String printMe(){
return "Sophmore details from PrintMe() - " + this.getName()+ " - "+this.getKd();
}
}