Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Help with Employee Class

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Help with Employee Class

    Hello, I'm having difficulty with my Java code, my professor wants us to use the EmployeeDemo.java when writing our code and I can't seem to get what I am missing. Any help/tips is appreciated:

    package chapter3;
     
    class Employee
    {
    	private String setName;
    	private String setDepart;
    	private int setEmpID;
    	private String setPosition;
     
    	public Employee()
    	{
    		setName = " ";
    		setDepart = " ";
    		setEmpID = 0;
    		setPosition = " ";
    	}
     
    	public Employee(String name, int num, String dept, String pos)
    	{
    		setName = name;
    		setEmpID = num;
    		setDepart = dept;
    		setPosition = pos;
    	}
     
    	public Employee(String name, int num, String dept)
    	{
    		setName = name;
    		setEmpID = num;
    		setDepart = dept;
    		setPosition = " ";
    	}
     
    	void setValues(String name, int num, String dept, String pos)
    	{
    		setName = name;
    		setEmpID = num;
    		setDepart = dept;
    		setPosition = pos;
    	}
     
    	String getName()
    	{
    		return setName;
    	}
     
    	int getEmpID()
    	{
    		return setEmpID;
    	}
     
    	String getDepart()
    	{
    		return setDepart;
    	}
     
    	String getPosition()
    	{
    		return setPosition;
    	}
     
    public class EmployeeDemo 
    {
    		public void main(String[] args) 
    		{	
    			//creating three Employee objects
    			Employee employee1 = new Employee();
    			Employee employee2 = new Employee();
    			Employee employee3 = new Employee();
     
    			//Set all of Employee attributes 
    			employee1.setName("Susan Meyers");
    			employee1.setDepart("Accounting");
    			employee1.setEmpID(47899);
    			employee1.setPosition("Vice President");
     
    			employee2.setName("Mark Jones");
    			employee2.setDepart("IT");
    			employee2.setEmpID(39119);
    			employee2.setPosition("Programmer");
     
    			employee3.setName("Joy Rogers");
    			employee3.setDepart("Manufacturing");
    			employee3.setEmpID(81774);
    			employee3.setPosition("Engineer");
     
    			//print out employee attributes on screen
    			System.out.println(employee1.getName() + ", " + employee1.getEmpID()
    					+ ", " + employee1.getDepart() + ", " + employee1.getPosition());
    			System.out.println(employee2.getName() + ", " + employee2.getEmpID()
    					+ ", " + employee2.getDepart() + ", " + employee2.getPosition());
    			System.out.println(employee3.getName() + ", " + employee3.getEmpID()
    					+ ", " + employee3.getDepart() + ", " + employee3.getPosition());
    		}
    }


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Help with Employee Class

    employee1.setName("Susan Meyers");
    employee1.setDepart("Accounting");
    employee1.setEmpID(47899);
    employee1.setPosition("Vice President");

    You don't have setter methods likes setName("Emp Name") in your Employee class.
    write setter method for each data member of Employee class like
    public void setName(String name){
    this.name = name;
    }
    and why your variable names are like setName , it's ambiguous, instead of that use some meaningful variable name like empName, empId, empDept, like wise.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help with Employee Class

    Okay, this is what I've got, I dunno if it's 100% right but, now my problem is an error. The error reads
    "Error: Main method not found in class chapter3.Employee, please define the main method as:
    public static void main(String[] args)or a JavaFX application class must extend javafx.application.Application"
    package chapter3;
     
    // Employee class.
    class Employee 
    {
    	private String empName;	// String to hold employee name.
    	private int empID;		// Integer to hold employee ID.
    	private String Department;	// String to hold employee's department.
    	private String position;		// String to hold employee's position.
     
    	public Employee()
    	{
    		empName = " ";			// Empty string to store employee name.
    		empID = 0;				// Store 0 in employee ID number.
    		Department = " ";			// Empty string to store department.
    		position = " ";			// Empty string to store position.
    	}
     
    	public Employee(String name, int num, String dept, String pos)
    	{
    		empName = name;			// Store name into empName.
    		empID = num;				// Store num into empID.
    		Department = dept;			// Store dept into Department.
    		position = pos;				// Store pos into position.
    	}
     
    	public Employee(String name, int num, String dept)
    	{
    		empName = name;			// Store name into empName.
    		empID = num;				// Store num into empID.
    		Department = dept;			// Store dept into Department.
    		position = " ";				// Empty string to store position.
    	}
     
    	void setValues(String name, int num, String dept, String pos)
    	{
    		empName = name;			// Store name into empName.
    		empID = num;				// Store num into empID.
    		Department = dept;			// Store dept into Department.
    		position = pos;				// Store pos into position.
    	}
     
    	void setName(String name)
    	{
    		empName = name;
    	}
     
    	void setDepart(String Depart)
    	{
    		Department = Depart;
    	}
     
    	void setEmpID(int num)
    	{
    		empID = num;
    	}
     
    	void setPosition(String pos)
    	{
    		position = pos;
    	}
     
    	// Returns the employee name.
    	String getName()
    	{
    		return empName;
    	}
     
    	// Returns the employee's ID number.
    	int getEmpID()
    	{
    		return empID;
    	}
     
    	// Returns the employee's department.
    	String getDepart()
    	{
    		return Department;
    	}
     
    	// Returns the employee's position.
    	String getPosition()
    	{
    		return position;
    	}
     
    	public static class EmployeeDemo 
    	{
    		public static void main(String[] args) 
    		{
     
    			//creating three Employee objects
    			Employee employee1 = new Employee();
    			Employee employee2 = new Employee();
    			Employee employee3 = new Employee();
     
    			//Set all of Employee attributes 
    			employee1.setName("Susan Meyers");
    			employee1.setDepart("Accounting");
    			employee1.setEmpID(47899);
    			employee1.setPosition("Vice President");
     
    			employee2.setName("Mark Jones");
    			employee2.setDepart("IT");
    			employee2.setEmpID(39119);
    			employee2.setPosition("Programmer");
     
    			employee3.setName("Joy Rogers");
    			employee3.setDepart("Manufacturing");
    			employee3.setEmpID(81774);
    			employee3.setPosition("Engineer");
     
    			//print out employee attributes on screen
    			System.out.println(employee1.getName() + ", " + employee1.getEmpID()
    					+ ", " + employee1.getDepart() + ", " + employee1.getPosition());
    			System.out.println(employee2.getName() + ", " + employee2.getEmpID()
    					+ ", " + employee2.getDepart() + ", " + employee2.getPosition());
    			System.out.println(employee3.getName() + ", " + employee3.getEmpID()
    					+ ", " + employee3.getDepart() + ", " + employee3.getPosition());
    		}
    	}
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with Employee Class

    The class Employee is not the 'program' being run. The class EmployeeDemo is being run to test your Employee class; it already has a main() method, so the file you're compiling and running is the EmployeeDemo class.

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Help with Employee Class

    There is no compilation error in your code but now your EmployeeDemo class in a inner class of Employee class and the compiler didn't able to find the main method.
    close the body of Employee class before EmployeeDemo class and as your EmployeeDemo class is not an inner class anymore ,static modifier is not allowed for that class so remove it too.

    and you code is ready to run.

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help with Employee Class

    Okay I did as you stated but, it's still throwing that main method not found error.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Employee Class

    What java class is being passed to the java command to start execution?
    Does that class have a main() method as requested in the error message?

    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Help with Employee Class

    can you paste your modified code and the error message you get this time.

  9. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help with Employee Class

     
    package chapter3;
     
    // Employee class.
    public class Employee 
    {
    	private String empName;			// String to hold employee name.
    	private int empID;				// Integer to hold employee ID.
    	private String Department;		// String to hold employee's department.
    	private String position;		// String to hold employee's position.
     
    	public Employee()
    	{
    		empName = " ";				// Empty string to store employee name.
    		empID = 0;					// Store 0 in employee ID number.
    		Department = " ";			// Empty string to store department.
    		position = " ";				// Empty string to store position.
    	}
     
    	public Employee(String name, int num, String dept, String pos)
    	{
    		empName = name;				// Store name into empName.
    		empID = num;				// Store num into empID.
    		Department = dept;			// Store dept into Department.
    		position = pos;				// Store pos into position.
    	}
     
    	public Employee(String name, int num, String dept)
    	{
    		empName = name;				// Store name into empName.
    		empID = num;				// Store num into empID.
    		Department = dept;			// Store dept into Department.
    		position = " ";				// Empty string to store position.
    	}
     
    	void setValues(String name, int num, String dept, String pos)
    	{
    		empName = name;				// Store name into empName.
    		empID = num;				// Store num into empID.
    		Department = dept;			// Store dept into Department.
    		position = pos;				// Store pos into position.
    	}
     
    	void setName(String name)
    	{
    		empName = name;
    	}
     
    	void setDepart(String Depart)
    	{
    		Department = Depart;
    	}
     
    	void setEmpID(int num)
    	{
    		empID = num;
    	}
     
    	void setPosition(String pos)
    	{
    		position = pos;
    	}
     
    	// Returns the employee name.
    	String getName()
    	{
    		return empName;
    	}
     
    	// Returns the employee's ID number.
    	int getEmpID()
    	{
    		return empID;
    	}
     
    	// Returns the employee's department.
    	String getDepart()
    	{
    		return Department;
    	}
     
    	// Returns the employee's position.
    	String getPosition()
    	{
    		return position;
    	}
    }
     
    	class EmployeeDemo
    	{
    		public static void main(String[] args) 
    		{
    			//creating three Employee objects
    			Employee employee1 = new Employee();
    			Employee employee2 = new Employee();
    			Employee employee3 = new Employee();
     
    			//Set all of Employee attributes 
    			employee1.setName("Susan Meyers");
    			employee1.setDepart("Accounting");
    			employee1.setEmpID(47899);
    			employee1.setPosition("Vice President");
     
    			employee2.setName("Mark Jones");
    			employee2.setDepart("IT");
    			employee2.setEmpID(39119);
    			employee2.setPosition("Programmer");
     
    			employee3.setName("Joy Rogers");
    			employee3.setDepart("Manufacturing");
    			employee3.setEmpID(81774);
    			employee3.setPosition("Engineer");
     
    			//print out employee attributes on screen
    			System.out.println(employee1.getName() + ", " + employee1.getEmpID()
    					+ ", " + employee1.getDepart() + ", " + employee1.getPosition());
    			System.out.println(employee2.getName() + ", " + employee2.getEmpID()
    					+ ", " + employee2.getDepart() + ", " + employee2.getPosition());
    			System.out.println(employee3.getName() + ", " + employee3.getEmpID()
    					+ ", " + employee3.getDepart() + ", " + employee3.getPosition());
    		}
    	}

    Error message:
    Error: Main method not found in class chapter3.Employee, please define the main method as:
    public static void main(String[] args)
    or a JavaFX application class must extend javafx.application.Application

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Employee Class

    How are you executing the class? It is in a package, so the name of the package must be included and the directory where the java command is issued must be where the package folder is:
    In the chapter3 folder enter the following command:
    java chapter3.Employee

    I assume that the Employee class has a main() method as described in the error message in post#3

    The posted code shows that the main() method is in the EmployeeDemo class. Then the command must be:
    java chapter3.EmployeeDemo
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help with Employee Class

    When speaking to my professor over email, he tells me they are 2 separate .java files, and that
    Demo contains the main and instantiate the Empoyee class with
    Employee employee1 =
    new Employee();
    I'm failing to understand how to get the 2 separate files to work.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Employee Class

    What problems are you having with using the two files?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Help with Employee Class

    I am guessing he means you need to split your code into two source files.
    One containing the main method which only creates an Instance of Employee()
    and the other class contains the Employee object's blueprint class.

    If you are unsure on how to create a project with two source files, I'l give you a hint:

    They must have the same package name.
    They must both belong to the same project destination folder.

    TBH I do not know why your constructor needs two separate definitions - would it not
    be easer to have one constructor method which just calls the set methods for each
    instance data member? Then you write lesser code and it might be a bit clearer about
    what exactly is going on.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  14. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    GregBrannon (June 3rd, 2014)

  15. #14
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help with Employee Class

    Yeah he mentioned how running the Employee.Demo will use the Employee.java as a sub file. So when putting both files in the same destination folder and running the Demo.java file, I get this output:

    Susan Meyers, 47899, Accounting, Vice President
    Mark Jones, 39119, IT, Programmer
    Joy Rogers, 81774, Manufacturing, Engineer

    So is it safe to assume it is using the Employee.java as a sub file and executing correctly?

  16. #15
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Help with Employee Class

    Gong by what you just posted - have you actually placed the executing class (the one that holds the main method)
    into a separate file? Did it compile? If so what output are you getting if any?
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  17. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    GregBrannon (June 3rd, 2014)

  18. #16
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help with Employee Class

    Yeah both EmployeeDemo.java and Employee.java are separate, but both are in the chapter3 folder. And after running the EmployeeDemo.java file, it did compile and execute with the output I posted. Which was:

    Susan Meyers, 47899, Accounting, Vice President
    Mark Jones, 39119, IT, Programmer
    Joy Rogers, 81774, Manufacturing, Engineer

  19. #17
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Help with Employee Class

    Ok - so is that the output your professor expects you to have? If so - then congratulations
    assignment completed

    If not - what are you missing from the assignment handout from the professor?
    I expect part of this assignment was to learn how to split a project into files - so well done
    for accomplishing that.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  20. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    GregBrannon (June 3rd, 2014)

  21. #18
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help with Employee Class

    I'll soon find out

    Thank you all for your help, I appreciate it.

  22. #19
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Help with Employee Class

    No problem. Best of luck with your course and happy coding.
    Remember you can always ask here anytime for help.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  23. #20
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help with Employee Class

    Thank you.

Similar Threads

  1. Help with sorting Employee class
    By sunilshiwankar in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 5th, 2014, 03:48 AM
  2. The Employee class
    By vercammen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2013, 02:27 AM
  3. Employee Class Exercise... Code will not run!!!!
    By jbarcus81 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: January 26th, 2012, 03:39 PM
  4. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM