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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: Employee Class Exercise... Code will not run!!!!

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Employee Class Exercise... Code will not run!!!!

    I got what seems to be a simple exercise with a simple answer.. However, I can't get this code to run properly!! I'm just starting a course at my local community college and am using Java How to Program Edition 9 and am stuck.. Here is the exercise.. Sorry for the huge first post but I have been at this for hours and have hit a wall...

    3.14 (Employee Class) Create a class called Employee that includes three instance variables-a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test application named EmployeeTest that demonstrates class Employee's capabilities. Create two Employee objects and display each object's yearly salary. Then give each Employee a 10% raise and display each Employee's yearly salary again.
    Here is what I have in my code...

    Employee.java
     
    //This class is called Employee and works with name and salary
    //James
     
    public class Employee
    {
    	private String firstName;
    	private String lastName;
    	private double monthlySalary;
     
    	public Employee()
    	{
    		firstName = null;
    		lastName = null;
    		monthlySalary = 0.0;
    	}
     
    	public String getfirstName()
    	{
    		return firstName;
    	}
     
    	public String getlastName()
    	{
    		return lastName;
    	}
     
    	public double getSalary()
    	{
    		return monthlySalary;
    	}
     
    	public void setfirstName(String first)
    	{
    		firstName = first;
    	}
     
    	public void setlastName(String last)
    	{
    		lastName = last;
    	}
     
    	public void setSalary(double salary)
    	{
    		monthlySalary = salary;
    	}
     
    }
    Now here is my EmployeeTest.java
     
    //This is a test file.
    //James
     
    import java.util.Scanner;
     
    public class EmployeeTest
    {
    	public static void main( String args[] )
    	{
    		Employee employee1 = new Employee();
    		Employee employee2 = new Employee();
     
    		Scanner input = new Scanner( System.in );
    		String first;
    		String last;
    		double salary;
     
    		System.out.print( "Enter the first name of the first employee: " );
    		first = input.next();
    		employee1.setfirstName( first );
     
    		System.out.print( "Enter the last name of the first employee: " );
    		last = input.next();
    		employee1.setlastName( last );
     
    		System.out.print( "Enter the first employee's monthly salary: " );
    		salary = input.nextDouble();
    		employee1.setSalary( salary );
     
    		System.out.print( "Enter the first name of the second employee: " );
    		first = input.next();
    		employee2.setfirstName( first );
     
    		System.out.print( "Enter the last name of the second employee: " );
    		last = input.next();
    		employee2.setlastName( last );
     
    		System.out.print( "Enter the second employee's monthly salary: " );
    		salary = input.nextDouble();
    		employee2.setSalary( salary );
     
     
    		System.out.printf( "Now displaying employees' full names and annual salary.\n");
    		System.out.printf( employee1.getfirstName()," ", employee1.getlastName(), "	 ", employee1.getSalary() * 12, "\n" );
    		System.out.printf( employee2.getfirstName()," ", employee2.getlastName(), "	 ", employee2.getSalary() * 12, "\n" );
     
    		System.out.printf( "Now applying a 10% raise to each employee and displaying the updated information.\n");
    		System.out.printf( employee1.getfirstName()," ", employee1.getlastName(), "	 ", employee1.getSalary() * 12, "\n" );
    		System.out.printf( employee2.getfirstName()," ", employee2.getlastName(), "	 ", employee2.getSalary() * 12, "\n" );
     
     
    	}
    }

    Now when I try to run I get this specific error: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Employee cannot be resolved to a type

    What have I done wrong here??


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Employee Class Exercise... Code will not run!!!!

    I can't seem to find anything wrong with your code.
    Try and recompile / build your project, in-case you're using outdated class files.

    Note though; " 10% " within a printf() method is going to cause conflict due to '%' being used to denote a type of argument is to be inserted.
    Stick to println for that one.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    Check out this page, it describes the error you're getting. Are your Employee.class and Employee.java files in the same folder as your EmployeeTest.class and EmployeeTest.java files?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    I fixed it!! I clicked on the syntax error and then clicked to "Fix project setup" .. Now it will run.. BUT ... it isn't running correctly ... not at all...


    after I run.. I get an output of this

    Enter the first name of the first employee: James
    Enter the last name of the first employee: Smith
    Enter the first employee's monthly salary: 1200.00
    Enter the first name of the second employee: Mike
    Enter the last name of the second employee: Smith
    Enter the second employee's monthly salary: 1300
    Now displaying employees' full names and annual salary.
    JamesMikeNow applying a 10% raise to each employee and displaying the updated information.

    JamesMike
    This doesn't look right at all.... Back to square one!! There is no way it's complete.. I don't have a 10% raise.. I haven't answered all the questions.. I feel like I'm gonna cry!! JEESH!!
    Last edited by jbarcus81; January 25th, 2012 at 05:35 PM.

  5. #5
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    My first suggestion: replace all your printf()'s with print()'s or println()'s. The way you are using printf() makes your code more difficult to understand, not easier. Plus, I think the printf is messing up your display... Try that, and see what happens.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    Quote Originally Posted by snowguy13 View Post
    My first suggestion: replace all your printf()'s with print()'s or println()'s. The way you are using printf() makes your code more difficult to understand, not easier. Plus, I think the printf is messing up your display... Try that, and see what happens.
    Ok.. so I changed lines 43 - 49 to this...

     
    		System.out.println( "Now displaying employees' full names and annual salary.\n");
    		System.out.println( employee1.getfirstName()," ", employee1.getlastName(), "	 ", employee1.getSalary() * 12, "\n" );
    		System.out.println( employee2.getfirstName()," ", employee2.getlastName(), "	 ", employee2.getSalary() * 12, "\n" );
     
    		System.out.println( "Now applying a 10% raise to each employee and displaying the updated information.\n");
    		System.out.println( employee1.getfirstName()," ", employee1.getlastName(), "	 ", employee1.getSalary() * 12, "\n" );
    		System.out.println( employee2.getfirstName()," ", employee2.getlastName(), "	 ", employee2.getSalary() * 12, "\n" );

    But I get errors again...

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method println(String) in the type PrintStream is not applicable for the arguments (String, String, String, String, double, String)
    The method println(String) in the type PrintStream is not applicable for the arguments (String, String, String, String, double, String)
    The method println(String) in the type PrintStream is not applicable for the arguments (String, String, String, String, double, String)
    The method println(String) in the type PrintStream is not applicable for the arguments (String, String, String, String, double, String)

  7. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Employee Class Exercise... Code will not run!!!!

    I assume he will have already replaced the prinf() with println() as he would have run into runtime errors using % in the code.
    Not that you should have known that ofcourse.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Employee Class Exercise... Code will not run!!!!

    That issue is because you're attempting to add multiple parameters into the println() method, which only accepts 1 String.
    Replace the comma with a '+'
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    Quote Originally Posted by newbie View Post
    That issue is because you're attempting to add multiple parameters into the println() method, which only accepts 1 String.
    Replace the comma with a '+'
    Please excuse my noobness but which comma? All of them? I think I understand, I just want to be clear..

  10. #10
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Employee Class Exercise... Code will not run!!!!

    Yes all of them; each comma denotes a new input argument, and println() only accepts one String input.

    e.g. public void addNumbers(int numOne, int numTwo) << 2 inputs
    public void setName(String name) << 1 input.

    If you call setName like setName("String one", "String two"), you're attempting to place 2 arguments not the 1, meaning it's invalid.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  11. #11
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    Quote Originally Posted by newbie View Post
    Yes all of them; each comma denotes a new input argument, and println() only accepts one String input.

    e.g. public void addNumbers(int numOne, int numTwo) << 2 inputs
    public void setName(String name) << 1 input.

    If you call setName like setName("String one", "String two"), you're attempting to place 2 arguments not the 1, meaning it's invalid.
    I really appreciate the help!!!

    		System.out.println( "Now displaying employees' full names and annual salary.\n");
    		System.out.println( employee1.getfirstName() + " " + employee1.getlastName() + "	 " + employee1.getSalary() * 12 + "\n" );
    		System.out.println( employee2.getfirstName() + " " + employee2.getlastName() + "	 " + employee2.getSalary() * 12 + "\n" );
     
    		System.out.println( "Now applying a 10% raise to each employee and displaying the updated information.\n");
    		System.out.println( employee1.getfirstName() + " " + employee1.getlastName() + "	 " + employee1.getSalary() * 12 + "\n" );
    		System.out.println( employee2.getfirstName() +" " + employee2.getlastName() + "	 " + employee2.getSalary() * 12 + "\n" );

    It works... BUT, it's not calculating the 10% raise.. The same salary is showing in the output.. here is what I get out of it...

    Enter the first name of the first employee: Mike
    Enter the last name of the first employee: Smith
    Enter the first employee's monthly salary: 1500
    Enter the first name of the second employee: Ben
    Enter the last name of the second employee: Man
    Enter the second employee's monthly salary: 1600
    Now displaying employees' full names and annual salary.

    Mike Smith 18000.0

    Ben Man 19200.0

    Now applying a 10% raise to each employee and displaying the updated information.

    Mike Smith 18000.0

    Ben Man 19200.0
    I want to figure this out and then dissect it and understand it... What a dang process!!

  12. #12
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    It's not calculating the 10% raise because you haven't told it to.

    This is how you calculate annual salary (this is correct):

    employee2.getSalary() * 12

    This is how you calculate the 10% raise (incorrect):

    employee2.getSalary() * 12

    Do you see the problem?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  13. #13
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    Quote Originally Posted by snowguy13 View Post
    It's not calculating the 10% raise because you haven't told it to.

    This is how you calculate annual salary (this is correct):

    employee2.getSalary() * 12

    This is how you calculate the 10% raise (incorrect):

    employee2.getSalary() * 12

    Do you see the problem?
    Been a long evening!! LoL.. Let me see here...

  14. #14
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    Now I have figured out how to calculate the extra 10% that they will get per year.. but I can't figure out how to show a total... I have this..


    		System.out.println( "Now displaying employees' full names and annual salary.\n");
    		System.out.println( employee1.getfirstName() + " " + employee1.getlastName() + "	 " + employee1.getSalary() * 12 +"\n" );
    		System.out.println( employee2.getfirstName() + " " + employee2.getlastName() + "	 " + employee2.getSalary() * 12 +"\n" );
     
    		System.out.println( "Now applying a 10% raise to each employee and displaying additional yearly income.\n");
    		System.out.println( employee1.getfirstName() + " " + employee1.getlastName() + "	 " + employee1.getSalary() * 0.10 +"\n" );
    		System.out.println( employee2.getfirstName() + " " + employee2.getlastName() + "	 " + employee2.getSalary() * 0.10 +"\n" );

    This will output...

    Enter the first name of the first employee: Ben
    Enter the last name of the first employee: Smith
    Enter the first employee's monthly salary: 1500
    Enter the first name of the second employee: Mike
    Enter the last name of the second employee: Smith
    Enter the second employee's monthly salary: 1600
    Now displaying employees' full names and annual salary.

    Ben Smith 18000.0

    Mike Smith 19200.0

    Now applying a 10% raise to each employee and displaying the updated information.

    Ben Smith 150.0

    Mike Smith 160.0
    Am I wrong here? Maybe it would look better if I could show the total?

  15. #15
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    Okay! You are so very close!

    So, you're multiplying the annual salary by .1 to get 10% of the total. This is right, except you want a 10% raise. Think about this: 100% of their salary, PLUS a 10%-of-their-salary raise. So, what percent of the salary do you need?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  16. #16
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Employee Class Exercise... Code will not run!!!!

    Oh oh I know this one... please sir... pick me... oohh ohh...youhooo.. !
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  17. #17
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    Oh oh I know this one... please sir... pick me... oohh ohh...youhooo.. !
    XD

    I wonder if he's figured it out...?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  18. #18
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    Nope.. haven't figured it out.. I am still most likely overlooking it!! This seems so simple yet I can't get my head around it.. I've tried a couple of things but they haven't worked... Grrr!!

  19. #19
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    Am I any closer? I'm about to bang my head on the desk...

     
    		System.out.println( "Now applying a 10% raise to each employee and displaying additional yearly income.\n");
    		System.out.println( employee1.getfirstName() + " " + employee1.getlastName() + "     " + employee1.getSalary() + salary * 0.10 +"\n" );
    		System.out.println( employee2.getfirstName() + " " + employee2.getlastName() + "     " + employee2.getSalary() + salary * 0.10 +"\n" );

  20. #20
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    You guys have me soooo close I can taste it!! But I am going to sleep on it.. I'm gonna throw what I have to my professor and see what she can clear up that I'm not seeing.. I greatly appreciate the help so far and will appreciate continued help form these forums as I just don't seem to be getting Java at all.. I'm still new and it's only my 4th week of class.. I just need to read, comprehend, and implement!

  21. #21
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Employee Class Exercise... Code will not run!!!!

    Let me jump into this discussion too as it seems interesting though. I guess i've been late as OP has already been helped out and that's great.
    Well, will any of you let me know why did you suggest using println() instead of printf()?

  22. #22
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Employee Class Exercise... Code will not run!!!!

    Because he was using a '%' char in one of the printf(), which would have thrown an error as he wasn't using it appropriately. Advising him to use println() was far more efficient that explaining the proper syntax for printf()
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  23. #23
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    Hmm... ^ that's a better reason than mine. I just don't like printf(). xD
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  24. #24
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Employee Class Exercise... Code will not run!!!!

    Quote Originally Posted by newbie View Post
    Because he was using a '%' char in one of the printf(), which would have thrown an error as he wasn't using it appropriately. Advising him to use println() was far more efficient that explaining the proper syntax for printf()
    Yeah, right. I just thought, that might be there are certain reasons which i might not know.

  25. #25
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Employee Class Exercise... Code will not run!!!!

    I figured it out!! I can't believe how close I was and how simple the solution is... But I got it and it's functioning 100%!!! Thanks for the help!! But I'll be back no doubt!!


    		System.out.println( "Now applying a 10% raise to each employee and displaying the new total annual salary.\n");
    		System.out.println( employee1.getfirstName() + " " + employee1.getlastName() + "     " + employee1.getSalary() * 1.10 * 12 +"\n" );
    		System.out.println( employee2.getfirstName() + " " + employee2.getlastName() + "     " + employee2.getSalary() * 1.10 * 12 +"\n" );

Page 1 of 2 12 LastLast

Similar Threads

  1. Employee class won't set salary and number values
    By JeremiahWalker in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 7th, 2012, 06:01 PM
  2. weekly hours for each employee
    By sircamolate in forum Collections and Generics
    Replies: 18
    Last Post: September 1st, 2011, 06:09 PM
  3. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM
  4. Program to read & write the Employee Records
    By arvindk.vij in forum Member Introductions
    Replies: 1
    Last Post: February 11th, 2011, 01:19 AM
  5. Program to read & write the Employee Records
    By arvindk.vij in forum Java Theory & Questions
    Replies: 1
    Last Post: February 11th, 2011, 01:19 AM