Solved

Creating two classes Employee and EmployeeTest

Posted on 2008-10-27
11
701 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

863 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

25 Experts available now in Live!

Get 1:1 Help Now