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 Workerdouble rate, double hours
hourlyRate rate;
hoursWorked hours;
private void calculateEarnings
double earnings ;
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 but instead prints a different value because of an error in the Worker class.
Worker bob new Worker;
System.out.printlnbobgetEarnings;
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
