Results 1 to 6 of 6
  1. #1
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default [SOLVED] Need direction...

    Hello,
    I have a project that I am working on.
    OK, I have to create an Employee class that has name and salary, a Manager class that inherits Employee with instance field department, and an Executive class that inherits Manager. In Manager class I have to provide a toString method that prints Manager's name, salary and department.
    Actually, toString method is to be implemented for all classes. Then create 10 instances (Manager and Executive) of Employee. Create another method showReport(Employee emp) that prints out the details for each emp.

    So far I got the following, but I am confused at where do I create a showReport() method? And am I doing OK so far? Thanks!
    Java Code:
    public class Employee
    {
      public String empName;
      public int empSal;
      
      public Employee()
      {
      }
      
      public Employee(String myEmpName, int myEmpSal)
      {
        empName = myEmpName;
        empSal = myEmpSal;
        
      }
      
      public String getEmpName()
      {
        return empName;
      }
      
      public int getEmpSal()
      {
        return empSal;
      }
    
      public String toString()
      {
        String s = "";
        return "" + "Employee()" + "Name: " + this.getEmpName() + "Salary $" +
        this.getEmpSal()+ "";
      }
      
    }
    Java Code:
    public class Manager extends Employee
    {
      private String department;
      
      public Manager()
      {
      }
      
      public Manager(String name, int salary, String myDepartment)
      {
        super.empName = name;
        super.empSal = salary;
        this.department = myDepartment;  
      }
      
      
      public String getDepartment()
      {
        return department;
        
      }
      
      public String toString()
      {
        String s = "";
        return "Manager() " + "Name: " + super.getEmpName() + "\tDepartment: " + getDepartment();
        
      }
    }
    Java Code:
    public class Executive extends Manager
    {
      
      public Executive()
      {
      }
      
      public Executive(Manager mgr)
      {
       
      }
      
      public String toString()
      {
        String s = "";
        return "Executive() " + "Name: " + super.getEmpName() +"Department: " + super.getDepartment() + "";
      }
    
      
    }

    My tester class is not ready yet as I get a compilation error when I tried to create an instance of a Manager.


    Java Code:
    public class EmpTester
    {
      
      public static void main(String[] args)
      {
        
        Employee E1 = new Manager("Vlad", 500, 10);
        
        
        
      }
      
    }

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    showReport() is supposed to output the info of a selected employee or each employee created? If it's the latter then you need to have an Employee array that stores all your employees and then just use the toString method of Employee employee[x] to print out their info.

    The array is simple to implement. Have it set that each time you create a new instance you employeeArray.add(newEmployee)

  3. #3
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    Thanks I will try that.
    As far as what showReport() is supposed to do - output info on a selected Employee.
    Ex: showReport(e1)

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Ahh ok if you only need to show the report for an individual employee and not all of them you don't need an array. Just e1, e2, e3 etc for whatever employee you want

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A nitpick:

    Don't do this:
    Java Code:
      public Manager(String name, int salary, String myDepartment)
      {
        super.empName = name;
        super.empSal = salary;
        this.department = myDepartment;  
      }
    Do this:
    Java Code:
      public Manager(String name, int salary, String myDepartment)
      {
        super(name, empSal);
        this.department = myDepartment;  
      }
    Also, the empName and empSal fields should have a private or the very least protected access modifier.

  6. #6
    hotice1027 is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    Thanks all.
    Project is done!

    Fubarable, could you explain why you would do it this way as opposed to what I have?

    Thanks!

Similar Threads

  1. Replies: 14
    Last Post: 09-18-2008, 05:41 AM

Posting Permissions

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