Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Employee, first name, last name and ID

Tags:

java

I just started and it is really confusing because when I write my code there is no red warning on the eclipse, however when I run the program, it does not work. The question is:

Write a program that displays employee IDs and first and last names of employees. Use two classes. The first class contains the employee data and separate methods to set the IDs and names. The other class creates objects for the employees and uses the objects to call the set methods. Create several employees and display their data.

My codes are:

public class Employee {
    String lastName = null; 
    String firstName = null; 
    double ID; 

    public Employee(String lastName, String firstName, double ID){
        this.lastName = lastName; 
        this.firstName = firstName; 
        this.ID = ID; 
    }

    public String empStat(){
        return "Last Name: " + lastName + "First Name: " + firstName + "ID" + ID; 
    }

}

and

 public class MainEmployee {
   public static void main(String args[]){

   Employee nub1 = new Employee ("Griffin", "Peter", 000001); 
   System.out.println(nub1);
   Employee nub2 = new Employee ("Griffin", "Lois", 000002); 
   System.out.println(nub2);
   Employee nub3 = new Employee ("Griffin", "Stewie", 000003); 
   System.out.println(nub3); 
   Employee nub4 = new Employee ("Griffin", "Brian", 000004); 
   System.out.println(nub4);


 }
}

and all it does is display

Employee@523ce3f
Employee@71b98cbb
Employee@4cc68351
Employee@7cd76237

can someone tell me why?

like image 252
Max Yuan Avatar asked Dec 25 '22 21:12

Max Yuan


1 Answers

Change

public String empStat()

to

@Override
public String toString()

See how toString() works (and why you see Employee@523ce3f) in docs

When you use System.out.println(nub1); the method nub1.toString() is called implicitly.

like image 122
Alex Avatar answered Dec 28 '22 11:12

Alex