Results 1 to 6 of 6
Thread: [SOLVED] Need direction...
- 11-23-2008, 02:24 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 18
- Rep Power
- 0
[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); } }
- 11-24-2008, 04:42 PM #2
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)
- 11-24-2008, 08:01 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 18
- Rep Power
- 0
Thanks I will try that.
As far as what showReport() is supposed to do - output info on a selected Employee.
Ex: showReport(e1)
- 11-24-2008, 08:12 PM #4
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
-
A nitpick:
Don't do this:
Do this:Java Code:public Manager(String name, int salary, String myDepartment) { super.empName = name; super.empSal = salary; this.department = myDepartment; }
Also, the empName and empSal fields should have a private or the very least protected access modifier.Java Code:public Manager(String name, int salary, String myDepartment) { super(name, empSal); this.department = myDepartment; }
- 11-28-2008, 09:03 AM #6
Member
- Join Date
- Sep 2008
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
[SOLVED] Advice on current code and a new direction...
By Zrob in forum New To JavaReplies: 14Last Post: 09-18-2008, 04:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks