Results 1 to 3 of 3
Thread: Trouble with method
- 10-19-2008, 06:40 PM #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 -
heres my code, the method in question is the last one.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.
Java Code:/** * Write a description of class Employee here. * * @author (your name) * @version (a version number or a date) */ public class Employee { // insert your fields below the appropriate comments // 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 -
how can i correct this?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)
- 10-19-2008, 08:40 PM #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.
- 10-19-2008, 09:05 PM #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).
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Got some trouble with JComboBox
By hungleon88 in forum AWT / SwingReplies: 16Last Post: 09-15-2008, 11:26 AM -
Trouble will calling a method
By jonsamwell in forum New To JavaReplies: 9Last Post: 08-22-2008, 10:16 PM -
having some trouble
By Unknown1369 in forum New To JavaReplies: 13Last Post: 07-21-2008, 11:52 PM -
Trouble in Runing
By kavithaprabhaker in forum New To JavaReplies: 5Last Post: 05-13-2008, 05:59 AM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks