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 5 of 5

Thread: Employee class won't set salary and number values

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

    Default Employee class won't set salary and number values

    I'm new to Java and I'm writing a sample code wherein a user inputs employee info (name, position and monthly salary--which also calculates annual salary by doing monsal * 12). My set constructors work fine for the employee's name, but It won't give me the numbers. IT always shows up as 0.0 Also, employee2's name won't take input. Hereis my code.

    Employee.java
    //Jeremiah A. Walker
    //Create a class called Employee that includes three instance variables--Name(String), Position(String) and
    //Monthly Salary(double)
     
    public class Employee 
    {
    	private String name; //string name.  I prefer to use a single name field as opposed to two or three.
    	private String position;//position in the company; their job
    	private double monsal;// the employee's monthly salary
    	private double anusal;//annual salary
    	private double raise;//10% raise to monthly salary
     
    	//Sets the employee's name for us
    	public void setname(String empname)
    	{
    		name = empname;
    	}//end setname
     
    	//returns name value for us to use
    	public String getname()
    	{
    		return name;
    	}//end getname
     
    	//sets position for us
    	public void setposition(String emppos)
    	{
    		position = emppos;
    	}//end setposition
     
    	//returns position value for us to use
    	public String getposition()
    	{
    		return position;
    	}//end getposition
     
    	//set monthly salary for us.  If monthly salary is negative or zero, it will default to zero
    	public double setmonsal(double msal)
    	{
    		if (monsal > 0.0)
    			monsal = msal;
    		return monsal;
    	}//end setmonsal
     
    	//returns monthly salary for us
    	public double getmonsal()
    	{
    		return monsal;
    	}//end getmonsal
     
    	//Multiplies monsal by 12 to give us anusal(annual salary)
    	public double setanusal(double asal)
    	{
    		asal = monsal * 12;
    		anusal = asal;
    		return anusal;
    	}//end setanusal
     
    	//returns the value so we can use it.
    	public double getanusal()
    	{
    		return anusal;
    	}//end getanusal
     
    }//end class Employee

    EmployeeTest.Java
    //Jeremiah A. Walker
    //EmployeeTest-- Test application for employee class
    import java.util.*;
     
    public class EmployeeTest
    {
    	//main method
    	public static void main(String[] args)
    	{
    		Employee Employee1 = new Employee();
    		Employee Employee2 = new Employee();
    		//Scanner to get input from user
    		Scanner input = new Scanner(System.in);
    		String name; //employee's name
    		String position;//employee's actual job
    		double monsal; // employee's monthly pay
    		double anu;// annual salary
    		double Empraise1;
    		double Empraise2;
     
    		//Asks the user to input name for employee1
    		System.out.print("Enter name for employee1:  ");//prompt
    		name = input.nextLine();//user input for name
    		System.out.print("Name entered was " + name + ".\n\n");//message to user that input was recieved
    		Employee1.setname(name);//sets name for employee1
     
    		//Asks the user to input position for employee1
    		System.out.print("Enter position for employee1:  ");//prompt
    		position = input.nextLine(); //user input for position
    		System.out.print("Position entered for " + name + " was " + position + ".\n\n");//message to user that input was recieved
    		Employee1.setposition(position); //sets position for employee1
     
    		//Asks the user to input monthly salary for employee1
    		System.out.print("Enter monthly salary for employee1: ");//prompt
    		monsal =  input.nextDouble();//user input for monthly salary
    		System.out.print("Monthly Salary entered for "  + name + " was " + "$" + monsal + ".\n\n" ); //message to user that input was recieved
    		Employee1.setmonsal(monsal);//sets monthly salary for employee1
    		Employee1.getanusal();
    		Empraise1 = Employee1.getmonsal() * 0.1 + Employee1.getmonsal();
     
    		//Asks the user to input name for employee2
                    System.out.print("Enter name for employee2:  ");//prompt
                    name = input.nextLine();//user input for name
                    System.out.print("Name entered was " + name + ".\n\n");//message to user that input was recieved
                    Employee2.setname(name);//sets name for employee1
     
    		//Asks the user to input position for employee2
                    System.out.print("Enter position for employee2:  ");//prompt
                    position = input.nextLine(); //user input for position
                    System.out.print("Position entered for " + name + "was " + position + ".\n\n");//message to user$
                    Employee2.setposition(position); //sets position for employee1
     
    		//Asks the user to input monthly salary for employee2
                    System.out.print("Enter monthly salary for employee2: ");//prompt
                    monsal =  input.nextDouble();//user input for monthly salary
                    System.out.print("Monthly Salary entered for "  + name + " was " + "$" + monsal + ".\n\n" ); //mes$
                    Employee2.setmonsal(monsal);//sets monthly salary for employee1
    		Employee2.getanusal();
    		Empraise2 = Employee2.getmonsal() * 0.1 + Employee2.getmonsal();
     
    		//displays information we put in
    		System.out.print("Employee1: " + Employee1.getname() + "\n" );
    		System.out.print("Position: " + Employee1.getposition() + "\n" );
    		System.out.print("Monthly Salary: " + Employee1.getmonsal() + "\n"  );
    		System.out.print("Annual Salary: " + Employee1.getanusal() + "\n"  );
    		System.out.print("Monthly Salary + 10% raise: " + Empraise1 + "\n"  );
     
                    System.out.print("Employee2: " + Employee2.getname()  + "\n" );
                    System.out.print("Position: " + Employee2.getposition()  + "\n" );
                    System.out.print("Monthly Salary: " + Employee2.getmonsal()  + "\n" );
                    System.out.print("Annual Salary: " + Employee2.getanusal()  + "\n" );
                    System.out.print("Monthly Salary + 10% raise: " + Empraise2  + "\n" );
     
     
    	}
    }//end class

    Output
    Enter name for employee1: Jor-El
    Name entered was Jor-El.

    Enter position for employee1: CEO
    Position entered for Jor-El was CEO.

    Enter monthly salary for employee1: 100,000
    Monthly Salary entered for Jor-El was $100000.0.

    Enter name for employee2: Name entered was . [It gives me the prompt, but it doesn't let me input]

    Enter position for employee2: CTO
    Position entered for was CTO.

    Enter monthly salary for employee2: 10203.23
    Monthly Salary entered for was $10203.23.

    Employee1: Jor-El
    Position: CEO
    Monthly Salary: 0.0
    Annual Salary: 0.0
    Monthly Salary + 10% raise: 0.0
    Employee2:
    Position: CTO
    Monthly Salary: 0.0
    Annual Salary: 0.0
    Monthly Salary + 10% raise: 0.0

    I don't know what's going on. Hoping you all can help. Thanks a lot

    ~J. Walker
    Last edited by JeremiahWalker; January 5th, 2012 at 08:44 PM.


  2. #2
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Employee class won't set salary and number values

    Note to make myself right or wrong but do you think perhaps having a clearly defined constructor in the employee class could make your initialization easier? Do you need to declare the same variables twice? In the Employee class and in the main method? I tried looking through your code but couldn't see the point. I think you can simplify your work by creating a constructor in the Employee class, then after instantiating your Objects, use the scanner to prompt the user for data which should have the parametrized method from the Employee class as well. To print out the entire data to the console, just use the output method. Hope this helps

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Employee class won't set salary and number values

    Thanks for the suggestion, can you show me the code?

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Employee class won't set salary and number values

    Some begginer's comments on the "Employee" class. I think that the set methods should be void and define that the two variables cannot be negative.
    I suggest an expression like:
     anusal = (anusal < 0) ? 0 : asal;
    The calculations should be in the get methods and i suppose that it would be better to have a constructor in your class.
    About the "EmployeeTest" class. I don't know what you are asked to do, but an iterative procedure would be better for implementing your tasks, so that you can take the same functionality for as many "employees" as you want.

  5. #5
    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 won't set salary and number values

    	//set monthly salary for us.  If monthly salary is negative or zero, it will default to zero
    	public double setmonsal(double msal)
    	{
    		if (monsal > 0.0)
    			monsal = msal;
    		return monsal;
    	}//end setmonsal

    You're checking if your instance variable is > 0.0 instead of checking the parametrised input, and as the instance variable is yet to be defined, it uses a default value of 0.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Character Values Inside of Number Values
    By bgroenks96 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 2nd, 2011, 08:27 PM
  2. Prime Number Program for class
    By chachunga in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 22nd, 2011, 12:05 AM
  3. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM
  4. [SOLVED] Using values set in one class in another
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 23rd, 2011, 10:34 PM
  5. [SOLVED] Class constructor default values
    By srs in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2010, 09:51 PM