-1

This was really hard to word since I'm not too sure what I should be asking but basically the task was to make a weekly pay calculator (ignoring taxes) that calculates overtime and regular pay

I have the following class

public class Hourly extends Employee {
private static double HourlyWage;
private static double Worked;

public Hourly(int Id, String Name, double HourlyWage, int Worked) {
    super(Name, Id);
    Hourly.HourlyWage = HourlyWage;
    Hourly.Worked = Worked;
}
 protected double WeeklyPay() {
    if (Worked> 40) {
        return (Worked - 40) * (HourlyWage * 1.5);
    } else {
        return HourlyWage * Worked;
    }
 }
 
       
@Override
public String toString() {
return "ID: " + Id + ", Name: " + Name + ", Hourly Wage: " + HourlyWage + ", Hours Worked: " + Worked;
}

and the following constructor

public static void main(String[] args) {
Hourly A = new Hourly (1111, "John Smith", 15.25, 40);
System.out.println(A);
Hourly B  = new Hourly (2222, "Oliver Thomas", 15.50, 60);
System.out.println(B);
System.out.println("Weekly pay for Hourly worker: " + A.WeeklyPay());
System.out.println("Weekly pay for Hourly worker: " + B.WeeklyPay());

the output I get from this is:

ID: 1111, Name: John Smith, Hourly Wage: 15.25, Hours Worked: 40.0

ID: 2222, Name: Oliver Thomas, Hourly Wage: 15.5, Hours Worked: 60.0

and

Weekly pay for Hourly worker: 465.0

Weekly pay for Hourly worker #2: 465.0

Obviously, it doesn't consider the number of hours worked or the hourly pay how would I fix this?

If there is any further detail I need to provide let me know, thanks!

Kevin
  • 9
  • 2
  • Take care of java namning conventions. variable names sould start with lower case character – Jens Jan 14 '23 at 21:00
  • D mot make your variables static. There are equals for every instance of the class – Jens Jan 14 '23 at 21:00
  • 1
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jan 14 '23 at 21:02
  • `static` variables get SHARED across all instances of the class. So if you change it from one place, all other places will see that change to. If you want each instance of `Employee` to have their own separate, independent copies of `HourlyWage` and `Worked` them REMOVE the `static` keyword. – Idle_Mind Jan 14 '23 at 22:26

1 Answers1

-2

All I had to do was instead of having

  Hourly.HourlyWage = HourlyWage;
  Hourly.Worked = Worked;

I needed to change it to

 this.hourlyWage = hourlyWage;
 this.worked = worked;

Also, the math was incomplete and to calculate the total weekly with overtime the correct equation is

(worked - 40) * (hourlyWage * 1.5) + (hourlyWage * worked)

And it magically worked

Kevin
  • 9
  • 2
  • the formatting looks different because someone commented about it so I changed it all throughout my project from an uppercase at the start of the declared variable to a lowercase at the start – Kevin Jan 14 '23 at 21:20
  • 2
    that is simply not *magically worked*. If the variables are static, all instances will get the same result – Jens Jan 14 '23 at 21:44