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
Solved

Creating two classes Employee and EmployeeTest

Posted on 2008-10-27
11
707 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
ID: 22819015
What is the question or error you are having?  Looks like you are making progress.
0
 
LVL 59

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 59

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 45 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 80 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iterator/ListIterator approach 17 39
Java syntax, or is it Selenium 6 30
Eclipse Java import and method not resolved 4 49
American Express @Work site and Java 4 28
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

829 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