Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-23-2008, 03:24 AM
Member
 
Join Date: Sep 2008
Posts: 18
Rep Power: 0
hotice1027 is on a distinguished road
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!
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()+ "";
  }
  
}
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();
    
  }
}
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.


Code:
public class EmpTester
{
  
  public static void main(String[] args)
  {
    
    Employee E1 = new Manager("Vlad", 500, 10);
    
    
    
  }
  
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-24-2008, 05:42 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
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)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-24-2008, 09:01 PM
Member
 
Join Date: Sep 2008
Posts: 18
Rep Power: 0
hotice1027 is on a distinguished road
Default
Thanks I will try that.
As far as what showReport() is supposed to do - output info on a selected Employee.
Ex: showReport(e1)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-24-2008, 09:12 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-24-2008, 09:50 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
Default
A nitpick:

Don't do this:
Code:
  public Manager(String name, int salary, String myDepartment)
  {
    super.empName = name;
    super.empSal = salary;
    this.department = myDepartment;  
  }
Do this:
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-28-2008, 10:03 AM
Member
 
Join Date: Sep 2008
Posts: 18
Rep Power: 0
hotice1027 is on a distinguished road
Default
Thanks all.
Project is done!

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

Thanks!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Advice on current code and a new direction... Zrob New To Java 14 09-18-2008 05:41 AM


All times are GMT +2. The time now is 02:46 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org