?
Solved

Creating two classes Employee and EmployeeTest

Posted on 2008-10-27
11
Medium Priority
?
718 Views
Last Modified: 2014-01-03
I have started a problem...I need to Create two classes Employee and EmployeeTest..write a class Employee that stores the following information: Name, IdNumber, department, position,
Example Name: Jason Smith, ID Number 47899, Department: Account, Postion:Vice President...Then Write appropriate mutator methods that stores the values in each of the fields and accessor methods that returns the values in these fields..What i have so far....
class Employee
       
{

    private String Employee_Name;
    private int Employee_Id;    
    private String Employee_Position;
    private String Employee_Department;
    private int id;
    private String position;
    private String department;
   
    public Employee(int Employee_Id, String Employee_Name,
       String Employee_Position, String Employee_Department,String name)
       
    {
       
        this.Employee_Name = name;
        this.Employee_Id = id;
        this.Employee_Position = position;
        this.Employee_Department = department;
       
       
       
    private int getEmployee_Id()
    {
        return (int)(Employee_Id);
    }
   
    public void setEmployee_Name(String name) {
    this.Employee_Name = name;
  }

     public String getEmployee_Name() {
      return Employee_Name;
  }

    public void setEmployee_Position(String name) {
    this.Employee_Position = position;
  }

     public String getEmployee_Position() {
      return Employee_Position;
  }
    public void setEmployee_Department(String name) {
    this.Employee_Department = department;
  }

     public String getEmployee_Department() {
      return Employee_Department;
  }


     
       
    }
0
Comment
Question by:April_Denise
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22819015
What is the question or error you are having?  Looks like you are making progress.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22819025
Did you mean to make this method private:
private int getEmployee_Id()
??

Usually you want to make accessor methods public.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22819055
Here are some more comments:
// You alread defined id, position, and department variables above -- these are redundant
    private int id;
    private String position;
    private String department;

// Assign the private variables (this.varname) to parameter value
// You define parameters in your constructor that you should use here or else defeats purpose
        this.Employee_Name = name; // might want to use Employee_Name
        this.Employee_Id = id; // might want to use Employee_Id
        this.Employee_Position = position; // might want to use Employee_Position
        this.Employee_Department = department; // might want to use Employee_Department

Hope that helps.

Try to correct these and post back if you have issues.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:spoxox
ID: 22823855
As noted, your Employee class is coming along nicely. Are you wondering what the EmployeeTest class does?

If so:

The assignment probably requires that you demonstrate that your Employee class works.  Create an executable class (has a "main" method) that creates an Employee object and accesses its various methods.

Skeletal* version:

public static void main(String[] args) {
  Employee emp = new Employee(...);
  emp.set...
  System.out.println(emp.get....);
}

Note: you might want to use a package to make the classes accessible....

* skeletal: well, it is almost Halloween. :)
0
 

Author Comment

by:April_Denise
ID: 22827999
Sorry for late reply...this is what i have so far now
class Employee
       
{

    private String Employee_Name;
    private int Employee_Id;    
    private String Employee_Position;
    private String Employee_Department;
   
   
   
    public Employee(int id, String name,
       String position, String department)
       
    {
       
        this.Employee_Name = name;
        this.Employee_Id = id;
        this.Employee_Position = position;
        this.Employee_Department = department;
       
       
    }
   
    public int getEmployee_Id(String id)
    {
        return (int)(Employee_Id);
    }
   
     public void setEmployee_Id(String id) {
    this.Employee_Name = id;
  }
   
    public void setEmployee_Name(String name) {
    this.Employee_Name = name;
  }

     public String getEmployee_Name(String name) {
      return Employee_Name;
  }

    public void setEmployee_Position(String position) {
    this.Employee_Position = position;
  }

     public String getEmployee_Position(String position) {
      return Employee_Position;
  }
    public void setEmployee_Department(String department) {
    this.Employee_Department = department;
  }

     public String getEmployee_Department(String department) {
      return Employee_Department;
  }
     
       
    }

class EmployeeTest
{
    public static void main (String [] args)
    {
        Employee employ1 = new Employee ("Jason Smith");
        Employee employ2 = new Employee ("Sam Jones");
        Employee employ3 = new Employee ("Joy Slicker");
       
        Id id1 = new Id ("47899");
        Id id2 = new Id ("29119");
        Id id3 = new Id ("81774");
       
        Department department1 = new Department (" Accounting ");
        Department department2 = new Department (" IT ");
        Department department3 = new Department (" Manufacturing ");
       
       
        Position position1 = new Position (" Vice President ");
        Position position2 = new Position (" Programmer ");
        Position position3 = new Position (" Engineer ");
       
        System.out.println (employ1, id1, department1, position1);
        System.out.println (employ2, id2, department2, position2);
        System.out.println (employ3, id3, department3, position3);
       
   }
}
       
On the EmployeeTest Class: i have naming errors, what am i doing wrong... :(
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 135 total points
ID: 22828044
Id, Department, and Position aren't separate classes, they are members of the Employee class like name.

Your constructor takes the four parameters to fill the members of your Employee class, but you are only passing one.

you should be doing something more like this:

new Employee (47899, "Jason Smith", "Vice President", "Accounting");
0
 
LVL 11

Accepted Solution

by:
spoxox earned 240 total points
ID: 22828068
There are a few smaller and a few larger issues, some of which mwvisa1 touched on above. With the latest code, I would start with these I've called "big" - but they won't take long to fix!

Big problem #1:
When EmployeeTest creates an Employee object, it has to use an Employee constructor. The only Employee constructor you are showing is:

  public Employee(int Employee_Id,
           String Employee_Name,
           String Employee_Position,
           String Employee_Department,String name)

This part of EmployeeTest won't work because the parameters don't match the constructor above:

Employee employ1 = new Employee ("Jason Smith");

Instead, you'll need to add local parameters to match the constructor signature (i.e., int, String, String, String).


Big problem #2 (related):
In Employee, the type associated with the Employee_Id is int, which is fine. But in EmployeeTest, there's a new (seemingly wrong) type, which is ID.


Big problem #3 (all related!):
The Employee class mutator methods are used to update the Employee  objects. So when you create an Employee object like this:

Employee employ1 = new Employee (someInt, someString, someString, someString);

you later access the related methods this way:
employ1.setEmployee_Name(someNewName);
0
 

Author Closing Comment

by:April_Denise
ID: 31511039
I have this now:
class EmployeeTest
{
    public static void main (String [] args)
    {
 
   Employee employ1 = new Employee (47899, "Jason Smith", "Vice President", "Accounting");
   Employee employ2 = new Employee (29119, "Sam Jones", "Programmer", "IT");
   Employee employ3 = new Employee (81774, "Joy Slicker", "Manufacturing", "Engineer");
     
       
     
     System.out.println();
     System.out.println(employ1);  
     System.out.println(employ2);
     System.out.println(employ3);
   }
}
       
   
0
 

Author Comment

by:April_Denise
ID: 22828207
Thanks both of you...I have this now
class EmployeeTest
{
    public static void main (String [] args)
    {
 
   Employee employ1 = new Employee (47899, "Jason Smith", "Vice President", "Accounting");
   Employee employ2 = new Employee (29119, "Sam Jones", "Programmer", "IT");
   Employee employ3 = new Employee (81774, "Joy Slicker", "Manufacturing", "Engineer");
     
       
     
     System.out.println();
     System.out.println(employ1);  
     System.out.println(employ2);
     System.out.println(employ3);
   }
}
0
 
LVL 11

Expert Comment

by:spoxox
ID: 22828225
Don't forget to show that your "getters" and "setters" (in Employee) work.
0
 

Author Comment

by:April_Denise
ID: 22828360
Thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question