Question: public class Worker { private double hourlyRate; private double hoursWorked; private double earnings; public Worker ( double rate, double hours ) { hourlyRate = rate;

public class Worker
{
private double hourlyRate;
private double hoursWorked;
private double earnings;
public Worker(double rate, double hours)
{
hourlyRate = rate;
hoursWorked = hours;
}
private void calculateEarnings()
{
double earnings =0.0;
earnings += hourlyRate * hoursWorked;
}
public double getEarnings()
{
calculateEarnings();
return earnings;
}
}
The following code segment appears in a method in a class other than Worker. The code segment is intended to print the value 800.0, but instead prints a different value because of an error in the Worker class.
Worker bob = new Worker(20.0,40.0);
System.out.println(bob.getEarnings());
The variable earnings in the calculateEarnings method is a local variable.
The local variable earnings declared in the calculateEarnings method has the same name as the private variable earnings. The local variable is assigned the intended amount, but the private variable is never updated. The private variable is the one printed by the getEarnings method.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!