• Register
1 vote
175 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,240 points

Related questions

0 votes
1 answer 5 views
5 views
Problem: What I'm trying to do here is to count the vowels in an arraylist of strings. What I did may not be right, but that's not my problem for now.Arrays cannot be resolved to a variable. Bonjour; Je suis d&eacute;butante en Java.what m&eacute;thodes declared in Arrays
asked 1 day ago Ali 4.4k points
0 votes
1 answer 10 views
10 views
Problem: I am developing a dynamic web project (RESTful jersey) under Eclipse IDE. Under src/my/demo/service folder I have CarService.java class public class CarService { ... }
asked Feb 1 Ali 4.4k points
0 votes
1 answer 7 views
7 views
Problem: The method public boolean mergesWith(Tile moving) returns true if this and moving tiles have the same value. But when I check if they are the same by doing the following: if(this.value == temp.value){ return true; } then it shows me error on temp.value saying that value cannot be resolved or is not a field. How can I fix that?
asked 4 days ago Ali 4.4k points
0 votes
1 answer 14 views
14 views
What causes this error message?
asked Dec 17, 2020 TeamScript 13.5k points
0 votes
1 answer 14 views
14 views
the problem I want a solution to this problem and she always confronts me
asked Feb 6 ahmedadel9090 2.6k points
0 votes
1 answer 9 views
9 views
Any help or guidance would be much appreciated! Thank you
asked Dec 16, 2020 TeamScript 13.5k points
0 votes
1 answer 35 views
35 views
How I solve this? Help me.
asked Dec 13, 2020 TeamScript 13.5k points
0 votes
1 answer 2 views
2 views
Problem: array cannot be resolved to a variable
asked 2 days ago Hira 2.2k points
0 votes
2 answers 5 views
5 views
Problem: Running a maven web project in eclipse was giving errors on jsp pages:javax.servlet.jsp.pagecontext cannot be resolved to a typejavax.servlet.jsp.jspexception Running a Maven web project in eclipse was giving errors on jsp pages: javax.servlet.jsp.PageContext cannot be resolved to a type javax.servlet.jsp.JspException cannot be resolved to a type
asked 2 days ago Ali 4.4k points
0 votes
1 answer 7 views
7 views
what else do I need to check? please help.
asked Feb 17 TeamScript 13.5k points