Results 1 to 5 of 5
Like Tree2Likes
  • 2 Post By JosAH

Thread: Overriding toString method

  1. #1
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Cool Overriding toString method

    Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status(freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to display the class name and the person's name. Implement the classes. Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() methods.

    Here is my attempt:
    Java Code:
    import java.util.Date;
    
    public class PersonStudentEmployee {
    	public static void main(String[] args) {
    		Person myPerson = new Person();
    		System.out.println(myPerson.toString());
    		Student myStudent = new Student();
    		System.out.println(myStudent.toString());
    		Employee myEmployee = new Employee();
    		System.out.println(myEmployee.toString());
    		Faculty myFaculty = new Faculty();
    		System.out.println(myFaculty.toString());
    		Staff myStaff = new Staff();
    		System.out.println(myStaff.toString());
    	}
    }
    class Person {
    //define var1, var2, var3, var4
    	String name;
    	String address;
    	String phoneNumber;
    	String email;
    //no arg constructer
    	Person() {
    		name = "John Doe";
    		address = "1234 Main St.";
    		phoneNumber = "(123)-456-7890";
    		email = "yahoo@yahoo.com";
    	}
    //define method toString()
    	public String toString() {
    		return super.toString() + "Person" + name;
    	}
    }
    class Student extends Person {
    //define class status
    	public static final int FRESHMEN = 1;
    	public static final int SOPHOMORE = 2;
    	public static final int JUNIOR = 3;
    	public static final int SENIOR = 4;
    	int classStatus;
    //no arg constructer
    	Student() {
    		classStatus = FRESHMEN;
    	}
    //define method toString()
    	public String toString() {
    		return super.toString() + "Student" + name;
    	}
    }
    
    class Employee extends Person {
    //define var1, var2, var 3
    	String office;
    	String salary;
    	Date dateHired = new java.util.Date();
    //no arg constructer
    	Employee() {
    		office = "111";
    		salary = "$80,000";
    	}
    //define method toString()
    	public String toString() {
    		return super.toString() + "Employee" + name;
    	}
    }
    
    class Faculty extends Employee {
    //define var1, var2
    	String officeHours;
    	String rank;
    //no arg constructer
    	Faculty() {
    		officeHours = "9-10 am";
    		rank = "Professor";
    	}
    //define method toString()
    	public String toString() {
    		return super.toString() + "Faculty" + name;
    	}
    }
    
    class Staff extends Employee {
    //define var1
    	String title;
    //no arg constructer
    	Staff() {
    		title = "Janitor";		
    	}
    //define method toString()	
    	public String toString() {
    		return super.toString() + "Staff" + name;
    	}
    }
    
    class MyDate {
    //define var1, var2, var 3
    	int year;
    	int month;
    	int day;
    }
    This is the output that I get:
    Person@addbf1PersonJohn Doe
    Student@9304b1PersonJohn DoeStudentJohn Doe
    Employee@a90653PersonJohn DoeEmployeeJohn Doe
    Faculty@c17164PersonJohn DoeEmployeeJohn DoeFacultyJohn Doe
    Staff@61de33PersonJohn DoeEmployeeJohn DoeStaffJohn Doe
    This is the output that I should get:
    Person John Doe
    Student John Doe
    Employee John Doe
    Faculty John Doe
    Staff John Doe
    I have no idea why so many random numbers and letters appear in my output. How can I get rid of them? What is wrong with my code?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: Overriding toString method

    The output you are seeing is from the Object class's toString() method. It returns the class name followed by the object's hashcode. Does your code call the Object class's toString() method? That is where the Strings like this "Person@addbf1" comes from.
    Last edited by Norm; 11-19-2011 at 02:13 PM.

  3. #3
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Overriding toString method

    Oh I see. No, my code is supposed to call the toString method from each class. How would I fix this?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Overriding toString method

    Quote Originally Posted by son012189 View Post
    Oh I see. No, my code is supposed to call the toString method from each class. How would I fix this?
    All your toString() methods look like this:

    Java Code:
    public String toString() {        
       return super.toString() + "Staff" + name;    
    }
    It your class directly (implicitly) extends from the Object class, super.toString() is the method defined in the Object class; hence the funny output. Remove the super.toString() part.

    kind regards,

    Jos
    son012189 and Fubarable like this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Overriding toString method

    Thank you. I removed it and finally got the desired output.

Similar Threads

  1. overriding ToString()
    By MetalR0 in forum New To Java
    Replies: 7
    Last Post: 08-04-2011, 11:09 AM
  2. What is use of method overloading and overriding
    By srinivasmallabathula in forum New To Java
    Replies: 3
    Last Post: 04-06-2011, 08:07 AM
  3. Overriding .clone() Method
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 02-21-2011, 03:57 PM
  4. overriding toString method
    By matin1234 in forum New To Java
    Replies: 3
    Last Post: 06-01-2010, 05:35 AM
  5. Overriding equals method
    By sky in forum New To Java
    Replies: 7
    Last Post: 03-12-2010, 04:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •