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;
    }
}

Dani AI

Generated

You're almost there, . is right to check errors, but this case is a logic/formatting problem rather than a compile-time one, and is correct that each concrete subclass should provide its own toString so output shows the class name plus the person's name.

Two quick fixes first: the email getter returns the phone value — change it to return the email field. Also prefer the short class name (no package) when printing the class name; use getClass().getSimpleName() instead of getClass().getName().

public String getEmail() {
    return email;
}

A concise, consistent toString pattern that each class can follow:

@Override
public String toString() {
    return getClass().getSimpleName() + ": " + getName();
}

For subclasses add only the extra info they need and keep the same pattern. Example for Student (use an enum or constants for status rather than ad-hoc strings):

public enum ClassStatus { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR }

@Override
public String toString() {
    return getClass().getSimpleName() + ": " + getName() + " (" + status + ")";
}

Practical tips: always annotate overrides with @Override so you catch signature mistakes; call System.out.println(obj) to exercise toString; prefer an enum for class status (stronger type-safety) or public static final constants if required; and override toString in Employee, Faculty, and Staff similarly (either build on super.toString() or replicate the same short format).

Recommended Answers

All 3 Replies

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

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

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

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

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

and

Overridde 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.