• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difficulty computing correct final value from static variables/methods  RSS feed

 
Nick How
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm working on my first of many weekly assignments in a summer Java course and I was hoping someone could help me with this one issue. I'm trying to calculate the initial weekly salaries of two employee's (based on a simple pay-rate * hours worked equation), then add extra work hours to one of the employees, recalculate his new weekly salary, and finally sum up the total value of the two salaries combined. However, I'm having a problem acquiring the correct total salary value.

The program is correctly outputting employee1's salary, employee2's salary, and the revised salary for employee1 after adding 10 hours of work. Unfortunately, it's only returning the sum total from employee1 & employee2 before the hours are revised. I'm having a hard time solving this problem (I'm assuming it relates to incorrect usage of the static variable) and would really appreciate some guidance. Attached is the code from my two Java classes as well as the assignment's description/goal.

The ultimate goal of the assignment is to familiarize us with using static variables and methods. The assignment is worded as follows:

Write a Java program that can be used to calculate the weekly salary for hourly paid employees. The program initially accepts the employee’s id, the number of hours worked, and the hourly pay rate. An employee may work additional hours. When this happens, the additional number of hours must be added to the original amount, and the salary is re-calculated. The program must output on separate lines the employee’s id number, the number of hours worked, the hourly pay rate, and the salary, for each employee. In addition, the program must also output the total amount of money paid out by the company.

In designing your program consider the following:
(a) Write a class called Payroll to include, but not limited to:
• Instance variables for number of hours worked, and hourly rate of pay.
• Class variable for accumulating the weekly payroll.
• A mutator instance method that calculates an employee salary, and accumulates the weekly payroll.
• Constructor that accepts the employee’s id number, the number of hours worked, and the hourly rate per employee.

(b) Design a test class called TestPayroll that implements the Payroll class.


My Java class code:





Any advice is appreciated. Thank you!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick How wrote:Unfortunately, it's only returning the sum total from employee1 & employee2 before the hours are revised.


Look for all the places where you set or change the weekly totals. Now look at what code you execute when you revise hours. Notice how the two do not intersect? That is, you're not changing the weekly totals when you change the hours.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing, I am afraid. Why do you have those variables marked static in the first place?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!