1. Member
Join Date
Oct 2008
Posts
2
Rep Power
0

## Trouble with method

Hi im having problems completing the following part of the exercise -

Employees earn £2 for each widget they assemble. If employees work hard and complete the assembly of more than 10 widgets, they get a bonus of £10If they assemble more than 20 widgets they get an additional bonus of £15. If they assemble more than 30 widgets they get an additional bonus of £25.

Write a method called:
public int calculateBonus()
which will return the employee’s bonus payment.
Hint: You should use a local variable inside this method to hold the value of the bonus as you
calculate it.
heres my code, the method in question is the last one.

Java Code:
```/**
* Write a description of class Employee here.
*
* @version (a version number or a date)
*/
public class Employee
{
// The age of the employee
private int age;

// The number of days holiday the employee is entitled to
private int numHolidays;

// The name of this employee
private String name;

// The payscale of this employee
private int payscale;

// Employees widgets
private int numWidgets;

/**
* Constructor for objects of class Employee
*/
public Employee(String givenName, int givenAge)
{
// All new employees start at payscale 1
payscale = 1;

// all new employees are entitled to 10 days holiday
numHolidays = 10;

// The age is set to the age we are given
age = givenAge;

// The name is the name we are given
name = givenName;

// Employees assemble a number of widgets during the day
numWidgets = 0;

}

/**
* Print out the details of the employee on four lines with the following format:
* Name: John Smith
* Age: 24
* Payscale: 2
* Holiday entitlement: 13
*/
public void printDetails(int y)
{
// prints out the details of this employee
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Payscale: " + payscale);
System.out.println("Holiday entitlement: " + numHolidays);
}

// ****************************************************
// write all the other new methods below this line
/**
* Returns age of Employee
*/

public int getAge()
{
return age;
}
/**
* Returns name of Employee
*/
public String getName()
{
return name;
}

/**
* Returns Employees payscale
*/

public int getPayscale()
{
return payscale;
}

/**
* Returns Employees Holiday
*/

public int getHolidayEntitlement()
{
return numHolidays;
}

/**
*  Sets a new name for Employee
*/

public void setName (String newName)
{
this.name = newName;
}

/**
*  Sets employees Holiday Entitlement
*/

public void setHolidayEntitlement (int newHolidayEntitlement)

{
this.numHolidays = newHolidayEntitlement;
}

/**
*  Promotes Employee if payscale is less than 5
*/

public void promote()
{
if (payscale < 5){
payscale = payscale +1;}

}

/**
* Adds a year to employees age and increases employees holiday by 2
*/

public void birthday()
{
age = age +1;
numHolidays = numHolidays +2;
}

/**
* Employees Widgets
*/

public void finishWidget ()
{
numWidgets = numWidgets +1;
}

public int getWidgetCount ()
{
return numWidgets;
}

public void resetWidgetCount()
{
numWidgets = 0;

}

/**
* Employees salarys
*/

public int calculateDailySalary()
{
int dailySalary;
dailySalary = 50 + payscale *5;
return dailySalary;
}
/**
* Calulates an employees bonus
*/
public int calculateBonus()
{   int bonus;
bonus = 0;
int earnings = 2 * getWidgetCount();

if(numWidgets >10){
bonus = 10;

}

if (numWidgets >20){
bonus = 15;
}

if (numWidgets >30){
bonus = 25;
}
return bonus;
}

}```

It compiles fine however my tutor created a test program so that we could check our work as we went through it and the error im getting is -

junit.framework.AssertionFailedError: Calculate bonus after finishing 1 widgets expected:<2> but was:<0>
at EmployeeTest.testBonusPayments(EmployeeTest.java:1 45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
how can i correct this?

2. Where is line 145 in the EmployeeTest source? What's in it?
Can you post it? Or put some comments around it making it easy to find.

Add some println() statements to the code to show values and execution flow.

How are you executing the code? I'm not familiar with the first line of the error message.

3. ## hhhmmm.. could junit be wrong?

I'm not familiar with junit (only heard of it). From the output shown, I can only deduct the following:
• the assigment is wrong and there should be a bonus for < 10 widgets or
• junit test case is wrong and is expecting a bonus for < 10 widgets or
• junit test case output is wrong (not printing the correct test case results)

I haven't reviewed the whole program, so there could be something wrong with it.
and... (IMHO) if I would have written the calculateBonus() method, I would have passed the number of widgets as an argument to the method (but aparently that's not part of the assignment).

Also... Norm's suggestion is about putting println() in your code is excellent (that's what I do when I get a funky results from my programs).

CJSL

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•