Results 1 to 5 of 5
Thread: Overriding toString method
- 11-19-2011, 01:08 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
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:
This is the output that I get: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 should get:Person@addbf1PersonJohn Doe
Student@9304b1PersonJohn DoeStudentJohn Doe
Employee@a90653PersonJohn DoeEmployeeJohn Doe
Faculty@c17164PersonJohn DoeEmployeeJohn DoeFacultyJohn Doe
Staff@61de33PersonJohn DoeEmployeeJohn DoeStaffJohn 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?Person John Doe
Student John Doe
Employee John Doe
Faculty John Doe
Staff John Doe
- 11-19-2011, 01:51 AM #2
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 01:13 PM.
- 11-19-2011, 07:40 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
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?
- 11-19-2011, 08:16 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Overriding toString method
All your toString() methods look like this:
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.Java Code:public String toString() { return super.toString() + "Staff" + name; }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-20-2011, 05:04 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
overriding ToString()
By MetalR0 in forum New To JavaReplies: 7Last Post: 08-04-2011, 10:09 AM -
What is use of method overloading and overriding
By srinivasmallabathula in forum New To JavaReplies: 3Last Post: 04-06-2011, 07:07 AM -
Overriding .clone() Method
By Moncleared in forum New To JavaReplies: 1Last Post: 02-21-2011, 02:57 PM -
overriding toString method
By matin1234 in forum New To JavaReplies: 3Last Post: 06-01-2010, 04:35 AM -
Overriding equals method
By sky in forum New To JavaReplies: 7Last Post: 03-12-2010, 03:39 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks