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: Simple Employee Class Not Passing Tests

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Employee Class Not Passing Tests

    I have written a simple Employee class that is suppose to pass the tests that have been given to us but the tests are failing and I can't see why. The methods are doing what they are suppose to be doing as you can see from the main class. This was suppose to be a simple 15 minute exercise but I have no idea why the program isn't passing the tests. Could anyone shed some light for me?

    I have included all three classes the Employee, EmployeeTest and the Main.

    * Class to represent an employee in an organisation as
     * part of a payroll application
     */
    package comp125;
     
    /**
     * @author ;a;a;a
     *
     */
    public class Employee {
     
    	// the name of the employee
    	private String name_;
     
    	// the annual salary in dollars
    	private double annualSalary_;
     
    	// the bank account number
    	private String bankAccountNumber_;
     
     
    	/** 
    	 * Return the weekly pay for this employee
    	 * @return the weekly pay in dollars
    	 */
    	public double weeklyPay() {	
    		double weekly = (annualSalary_ /52.0);	
    		return weekly;
    	}
     
    	public void setAnnualSalary_(double annualSalary_) {
    		this.annualSalary_ = annualSalary_;
    	}
     
    	/**
    	 * Promote an employee, this adds 10% to the annual salary
    	 */
    	public void promote() {
    		double bonus =  (10 * annualSalary_) / 100;
    		annualSalary_ = annualSalary_ + bonus;		
    	}
     
    	public String getName_() {
    		return name_;
    	}
     
    	public void setName_(String name_) {
    		this.name_ = name_;
    	}
     
    	public double getAnnualSalary_() {
    		return annualSalary_;
    	}
     
     
    	public String getBankAccountNumber_() {
    		return bankAccountNumber_;
    	}
     
    	public void setBankAccountNumber_(String bankAccountNumber_) {
    		this.bankAccountNumber_ = bankAccountNumber_;
    	}
     
    }

    * Tests for the Employee class
     * COMP125 mixed class week 3 2011
     */
    package comp125;
     
    import static org.junit.Assert.*;
     
    import org.junit.Test;
     
    /**
     * @author lalala
     *
     */
    public class EmployeeTest {
     
    	/** 
    	 * test that the weekly pay is correct given the
    	 * annual salary
    	 */
    	@Test
    	public void testEmployeeWeeklyPay() {
     
    		Employee emp = new Employee();
     
    		// Test a few different salaries
     
    		emp.setAnnualSalary(50000.0);
    		assertEquals(50000.0/52, emp.weeklyPay(), 0.01);
     
    		emp.setAnnualSalary(1000.0);
    		assertEquals(1000.0/52, emp.weeklyPay(), 0.01);
     
    		// What happens if we don't pay anything?
    		emp.setAnnualSalary(0);
    		assertEquals(0.0, emp.weeklyPay(), 0.01);
    	}
     
    	/**
    	 * test the promotion method which should 
    	 * increase the annual salary by 10%
    	 */
    	@Test
    	public void testEmployeePromotion() {
     
    		Employee emp = new Employee();
    		emp.setAnnualSalary(10000.0);
     
    		// promotion adds 10%
    		emp.promote();
     
    		assertEquals(11000.0, emp.getAnnualSalary(), 0.01);
     
    	}
     
    }

    package comp125;
     
    public class Main {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    		Employee one = new Employee();	
    		one.setAnnualSalary_(50000.0);
    		one.weeklyPay();
    		System.out.println("This is weekly pay: " + one.weeklyPay());
    		System.out.println("This is before promote: " + one.getAnnualSalary_());
    		one.promote();			
    		System.out.println("This is after promote: " +one.getAnnualSalary_());
     
     
    	}
     
    }


  2. #2
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Simple Employee Class Not Passing Tests

    you got any exception or when you struck the process?
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Employee Class Not Passing Tests

    Found what happened. When I auto generated the Setters and Getters in Eclipse the method names were identical to the instance variables and parameters!

    The working class below.

    * Class to represent an employee in an organisation as
     * part of a payroll application
     */
    package comp125;
     
    /**
     * @author ;a;a;a
     *
     */
    public class Employee {
     
    	// the name of the employee
    	private String name_;
     
    	// the annual salary in dollars
    	private double annualSalary_;
     
    	// the bank account number
    	private String bankAccountNumber_;
     
     
    	/** 
    	 * Return the weekly pay for this employee
    	 * @return the weekly pay in dollars
    	 */
    	public double weeklyPay() {	
    		double weekly = (annualSalary_ /52.0);	
    		return weekly;
    	}
     
    	public void setAnnualSalary(double amount) {
    		this.annualSalary_ = amount;
    	}
     
    	/**
    	 * Promote an employee, this adds 10% to the annual salary
    	 */
    	public void promote() {
    		double bonus =  (10 * annualSalary_) / 100;
    		annualSalary_ = annualSalary_ + bonus;		
    	}
     
    	public String getName() {
    		return name_;
    	}
     
    	public void setName(String name) {
    		this.name_ = name;
    	}
     
    	public double getAnnualSalary() {
    		return annualSalary_;
    	}
     
     
    	public String getBankAccountNumber() {
    		return bankAccountNumber_;
    	}
     
    	public void setBankAccountNumber_(String bankAccountNumber) {
    		this.bankAccountNumber_ = bankAccountNumber;
    	}
     
    }

  4. #4
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Simple Employee Class Not Passing Tests

    Quote Originally Posted by floorplay View Post
    Found what happened. When I auto generated the Setters and Getters in Eclipse the method names were identical to the instance variables and parameters!
    Are you told about this methods?

    public String getName() {
    		return name_;
    	}
     
    	public void setName(String name) {
    		this.name_ = name;
    	}
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Employee Class Not Passing Tests

    Seems to be working ok. Posted the main class and the output. Is there something wrong with the code you posted above?

    OUTPUTS:
    Employees name: Humpty Dumpty
    Your account number is: 34543534
    Weekly pay is: 961.5384615384615
    This is before promote: 50000.0
    This is after promote: 55000.0
    public class Main {
     
    	public static void main(String[] args) {
     
    		Employee one = new Employee();	
    		one.setAnnualSalary(50000.0);
    		one.setName("Humpty Dumpty");			
    		System.out.println("Employees name: " +one.getName());
    		one.setBankAccountNumber("34543534");
    		System.out.println("Your account number is: " + one.getBankAccountNumber());
    		System.out.println("Weekly pay is: " + one.weeklyPay());
    		System.out.println("This is before promote: " + one.getAnnualSalary());
    		one.promote();			
    		System.out.println("This is after promote: " +one.getAnnualSalary());		
     
    	}
    }

Similar Threads

  1. The Employee class
    By vercammen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2013, 02:27 AM
  2. Simple Employee Payment Program
    By djl1990 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 21st, 2012, 07:53 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