954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with toString method

Problem: (The Person, Sruden, Employee, Faculty, and Staff classes) 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, 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. Overridde the toString method in each class to display the class name and the person's name.
Draw the UML diagram for classes. Implement the classes. Write a tes program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() methods

I am having trouble overriding the toString method in each class, Can any one give me any clarfication? (And If you see any other problems i might have, please help, thanks) And am I using the Superclass correctly?

public class Person {

    private String name, address, phone, email;
    
    public Person(){
    }
    
    public Person(String name, String address, String phone, String email){
        this.name = name;
        this.address = address;
        this.phone = phone;
        this.email = email;
    }
    
    public String getName(){
        return name;
    }
    
    public void setName(String name){
        this.name = name;
    }
    
     public String getAddress(){
        return address;
    }
    
    public void setAddress(String address){
        this.address = address;
    }
    
    public String getPhone(){
        return phone;
    }
    
    public void setPhone(String phone){
        this.phone = phone;
    }
    
    public String getEmail(){
        return phone;
    }
    
    public void setEmail(String email){
        this.email = email;
    }
    
    @Override
    public String toString(){
        return getClass().getName() + "\n" + name;
    }
}
public class Student extends Person{

    private final String CLASS_STATUS;
    
    public Student(String name, String address, String phone, String email,String CLASS_STATUS){
        super(name, address, phone, email);
        this.CLASS_STATUS =CLASS_STATUS;
    }

    public String getClassStatus(){
        return CLASS_STATUS;
    }
}
agmolina90
Newbie Poster
8 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

What trouble are you facing? What error are you getting??

If you analyse the error closely then you might be able to debug.

anuj_sharma
Posting Whiz in Training
208 posts since Jul 2008
Reputation Points: 8
Solved Threads: 13
 

What trouble are you facing? What error are you getting??

If you analyse the error closely then you might be able to debug.


I am not recieving any error but I don't know if I am following the directions right

agmolina90
Newbie Poster
8 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

It looks fine; however, you need to read your instruction carefully.

Design a class named Person and its two subclasses named Student and Employee.


andOverridde the toString method in each class to display the class name and the person's name.

To me, it means that you need to override toString() method of Student & Employee. You need to override toString() method for Person, Student, and Employee. Because you use 'extend' the Person class for Employee & Student, you need to override the toString() of Student & Employee or you would get wrong info.

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: