Solved

Creating two classes Employee and EmployeeTest

Posted on 2008-10-27
11
698 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
  • 4
  • 4
  • 3
11 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
What is the question or error you are having?  Looks like you are making progress.
0
 
LVL 59

Expert Comment

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

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

Expert Comment

by:Kevin Cross
Comment Utility
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
 
LVL 11

Expert Comment

by:spoxox
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 45 total points
Comment Utility
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 80 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Don't forget to show that your "getters" and "setters" (in Employee) work.
0
 

Author Comment

by:April_Denise
Comment Utility
Thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights  challenge 17 59
groovy example issue 10 65
backtracking recursion  code 19 39
eclipse formatting 6 38
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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 …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now