Tristinia
Tristinia

Reputation: 13

Issue passing arguements to constructor in different public class

I am having difficulty passing the hours and hourlyWage arguments to the constructor in the Paycheck class. The issue is as follows:

symbol: variable hours
location : class Paycheck

It repeats for every instances of hours or hourly wage in public class Paycheck.

The code is as follows

import java.util.Scanner;

public class PayDayCalculator {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Hourly wage: ");
        double hourlyWage = in.nextDouble();
        System.out.println("Hours worked: ");
        double hours = in.nextDouble();

        Paycheck paycheck = new Paycheck(hourlyWage, hours);
        System.out.println("Pay: " + paycheck.getPay());
    }
}

public class Paycheck {
    private double pay = 0;
    private double overtime = 0;
    private double overtimePay = 0;

    /*double hours;
    double hourlyWage; */
    Paycheck(double hourlyWage, double hours) {
        setPay(0);
    }

    public void setPay(double newPay) {
        if (hours > 40) {
            overtime = hours % 40;
            hours = hours - overtime;
        }
        overtimePay = hourlyWage * 1.5;
        pay = (hours * pay) + (overtime * overtimePay);
    }

    public double getPay() {
        return pay;
    }
}

Upvotes: 1

Views: 81

Answers (3)

Bill the Lizard
Bill the Lizard

Reputation: 405745

Your setPay method is referring to hours and hourlyWage, which are parameters passed in to the constructor, making them local only to the constructor. They are not available to all methods in the class. Those need to be uncommented at the class level if you want all methods to have access to them.

double hours;
double hourlyWage;

Paycheck(double hourlyWage, double hours) {
    this.hourlyWage = hourlyWage;
    this.hours = hours;
    setPay(0);
}

Upvotes: 0

CodeBlue
CodeBlue

Reputation: 15389

hours and hourlyWage are not defined!

Uncomment this part -

/*double hours;
double hourlyWage; */

Upvotes: 0

AlexR
AlexR

Reputation: 115328

You have commented out member variable hours:

/*double hours;
double hourlyWage; */

but still try to refer to it, e.g.:

if (hours > 40) {
    overtime = hours%40;
    hours = hours - overtime;
}

If you need this variable - uncomment it.

Upvotes: 1

Related Questions