• Register
1 vote
508 views

Problem:

I'm a complete beginner to Java, and after learning some of the basics, I decided to do some practice problems. I am solving problems from many voluntary websites. I've searched for solutions on various forums, but none of them seem to be having the same problem, and my problem literally new and unknown to me. Here is my code:

public class SalaryCalc {
    private int hoursWorked;
    public SalaryCalc(String name, int hours, double hoursRate) {
        nameOfEmployee = name;
        hoursWorked = hours;
        ratePHr = hoursRate;
    }
    public void setHoursWorked() {
        hoursWorked = hours;
    }
    public double calculateSalary() {
        if (hoursWorked <= 40) {
            totalSalary = ratePHr * (double) hoursWorked;
        }
        if (hoursWorked > 40) {
            salaryAfter40 = hoursWorked - 40;
            totalSalary = (ratePHr * 40)
                + (ratePHr * 1.5 * salaryAfter40);
        }
        return totalSalary;
    }
}

I am trying to make a program to calculate the earnings of my workers. But the program doesn’t run and throws an error.

"Variable name" cannot be resolved to a variable.

Any Idea? How can I solve the problem?

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

The error you are getting is produced this block

public void setHoursWorked(){
        hoursWorked = hours;
    }

Look inside the block, you haven’t declared the hours inside the hoursWorked()  method. It is not declared as a variable, not passed as a parameter or an argument not even being used as a class member in your program. That’s the reason, the compiler produces the error.

13 9 6
94,260 points

Related questions

0 votes
1 answer 42 views
42 views
What causes this error message?
asked Dec 17, 2020 TeamScript 13.5k points
0 votes
1 answer 3 views
3 views
Problem: Why it is android r cannot be resolved to a variable?
asked Apr 13 jamuna1 30.2k points
0 votes
1 answer 8 views
8 views
Problem: In eclipse, I created a project from a source and now it is noticing errors - "R cannot be resolved from a variable". From what I found here, I had cleaned up and rebuilt the project, but still the R file doesn't show up in the / gen folder. Any ideas?
asked Apr 5 sasha 16.2k points
0 votes
1 answer 4 views
4 views
Problem: In Eclipse, I've created a project from a source and now it shows errors - "R cannot be resolved to a variable". From what I found here, I had cleared and rebuilt the project, but still, the R file doesn't appear in the /gen folder. Any ideas?
asked Mar 30 ummesalma 29.2k points
0 votes
1 answer 31 views
31 views
Problem: array cannot be resolved to a variable
asked Feb 24 Hira 2.2k points
0 votes
1 answer 99 views
99 views
the problem I want a solution to this problem and she always confronts me
asked Feb 6 ahmedadel9090 3.1k points
0 votes
1 answer 23 views
23 views
Any help or guidance would be much appreciated! Thank you
asked Dec 16, 2020 TeamScript 13.5k points
0 votes
1 answer 244 views
244 views
How I solve this? Help me.
asked Dec 13, 2020 TeamScript 13.5k points
0 votes
1 answer 6 views
6 views
Problem: I am getting that error: R cannot be resolved to a variable.What should I do?
asked Apr 14 sumaiya simi 43.9k points
1 vote
1 answer 33 views
33 views
Problem : I am new in java and I am getting the following error after importing a project in Eclipse: The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files However, I have set the path as C:\Program Files\Java\jdk1.6 ... Kepler, through Window &raquo; Preferences &raquo; Java &raquo; Installed JREs. Any ideas on what I need to do to fix this? Thanks!
asked Apr 9 Sabbir Hossain 14.6k points