Here's my assignment question:

**Develop a Payroll class that consists of two user-defined methods, namely calculateNetPay() and printOutput() methods. The calculateNetPay() method is used to calculate the employee's net pay, as the formula follows:

Net pay = gross pay - state tax and federal tax

In the same class, print the net pay value in the printOutput() method.

Then, develop a PayrollDemo class which consists of the main method. Inside the main method, prompt a user to insert employer ID, gross pay, state tax, and federal tax. Create an object to invoke calculateNetPay() and printOutput() methods. Your program should produce the following output:

Enter your employee ID number: 2150
Enter your gross pay: $ 4000
Enter your State Tax: $ 300
Enter your Federal Tax: $500

Net pay is: $3200.0**

So, here's MY trial of coding for the Payroll class:

public class Payroll
{
    public double grossPay;
    public double stateTax;
    public double federalTax;

    public double calculateNetPay()
    {
        return grossPay - (stateTax + federalTax);
    }

    public double printOutput()
    {
        System.out.println(" Net Pay is: " + y02);
    }
}

but with one error here:

Payroll.java:14: error: cannot find symbol
        System.out.println(" Net Pay is: " + y02);
                                             ^
  symbol:   variable y02
  location: class Payroll
1 error

AND here's MY trial coding for the PayrollDemo class:

import java.util.Scanner;
public class PayrollDemo
{

    public static void main(String[] args)
    {

    int employeeID, grossPay, stateTax, federalTax;
    double y01, y02;

    Scanner keyboard = new Scanner(System.in);
    System.out.println(" Enter your employee ID number: ");
    employeeID = keyboard.nextInt();
    System.out.println(" Enter your Gross pay: ");
    grossPay = keyboard.nextInt();
    System.out.println(" Enter your State Tax: ");
    stateTax = keyboard.nextInt();
    System.out.println(" Enter your Federal Tax: ");
    federalTax = keyboard.nextInt();

    Payroll taxes = new Payroll();

    y01 = taxes.calculateNetPay();
    y02 = taxes.printOutput();

    }
}

And the error that occured when I tried compiling this PayrollDemo class is the same with the error I got for the Payroll class.

So, It'd be great if someone could shed some light on this matter, thanks in advance!

dat's correct error shown to you..
As you are calling printOutput() method with object taxes, but this method doesn't have variable y02.
So how could it print it..?

and also you have written like "y02 = taxes.printOutput();"
this means printOutput() method returns some double value that is stored in y02 variable.

but in printOutput() method there is no return statement.

so check it out. try to improve this thing first.

otherwise modify your class as follows

public class Payroll
{
    public double grossPay;
    public double stateTax;
    public double federalTax;
    public double calculateNetPay()
    {
        return grossPay - (stateTax + federalTax);
    }
    public void printOutput(double yo2)
    {
        System.out.println(" Net Pay is: " + y02);
    }
}

and call printOutput() method by passing yo1 value as you called in your program like as follows
// change those lines in your program

y01 = taxes.calculateNetPay();
taxes.printOutput(y01);

happy coding
radha krishna .p

Okay, I re-did my coding as radha krishna told me. Here's my first class code:

public class Payroll
{
    public double grossPay;
    public double stateTax;
    public double federalTax;

    public double calculateNetPay()
    {
        return grossPay - (stateTax + federalTax);
    }

    public void printOutput(double y02)
    {
        System.out.println(" Net Pay is: " + y02);
    }
}

And no errors occured. And I did the second class, here:

import java.util.Scanner;
public class PayrollDemo
{

    public static void main(String[] args)
    {

    int employeeID, grossPay, stateTax, federalTax;
    double y01, y02;

    Scanner keyboard = new Scanner(System.in);
    System.out.println(" Enter your employee ID number: ");
    employeeID = keyboard.nextInt();
    System.out.println(" Enter your Gross pay: ");
    grossPay = keyboard.nextInt();
    System.out.println(" Enter your State Tax: ");
    stateTax = keyboard.nextInt();
    System.out.println(" Enter your Federal Tax: ");
    federalTax = keyboard.nextInt();

    Payroll taxes = new Payroll();

    y02 = taxes.calculateNetPay();
   taxes.printOutput(y02);

    }
}

And I got NO error. BUT, the coding doesn't seem to calculate the output for the Net pay. You can see the UNCALCULATED final output here:

 Enter your employee ID number: 
2150
 Enter your Gross pay: 
4000
 Enter your State Tax: 
300
 Enter your Federal Tax: 
500
 Net Pay is: 0.0

The net pay is supposed to be calculated and I'msupposed to get a Net pay output of 3200.0

Can you exlpain to me a bit more about your last comment, when you said:

and call printOutput() method by passing yo1 value as you called in your program like as follows
// change those lines in your program

y01 = taxes.calculateNetPay();
taxes.printOutput(y01);

Because I don't understand what needs to be changed there, radha krishna. Thanks in advance!

You created/initiated a class instance but did not associate it with input values you got from a user. As a result, it uses its initial values (primitive data type is set to 0 when you declare a variable with the data type).

There are many ways to solve your problem. One is to implement a constructor which takes all variables from the outside and assigns to your class's variables.

In Java, when you do not declare a constructor for a class, it will automatically use the "default" constructor for you.

// I will try to explain each line of code/syntax for you
public class Payroll {      // <-- class name declaration
  public double grossPay;   // \
  public double stateTax;   //  :- these are class variables
  public double federalTax; // /

  // You don't have a constructor, so Java will automatically use
  // the method below for you.
  // Then you can initiate a class instance as...
  //   Payroll taxes = new Payroll();
  // But it does nothing but initiate an instance containing all default
  // value you have set in the class.
  public Payroll() { }


  // However, you could have multiple constructors.
  // One example of another constructor taking an argument type of double
  // and assign it to a class's variable -- grossPay
  // How to initiate this constructor is as follows:
  //    Payroll taxes = new Payroll(grossPay);
  public Payroll(double v) {
    grossPay = v;
  }

  // Now you should modify the constructor to take all variables at once.


  // Other public methods are below...
  ...
}

One last comment from me is about your variable naming. It is good to have all naming in the way you did -- self explanation. I just want to point out that the variables you declared in main() method are NOT the same as the variables inside your class. They are in 2 different scopes. You may learn more about variable scope later on in your class (I hope).

y02 = taxes.printOutput(); from main: printOutput must return something and it's not. y01 is a double in main so the printOutput must return a number.

Also y02 is visible just in main, not in printOutput from payRoll class. Seems you need it in both Payroll and PayrollDemo. Make it visible in Payroll by importing PayrollDemo in Payroll.

In Payroll type

import PayrollDemo_package.PayrollDemo;
Then create a new instance of PayrollDemo like :

Payroll obj = new PayrollDemo(); 
double y02 = obj.getY02();

y02 = taxes.printOutput(); from main: printOutput must return something and it's not. y01 is a double in main so the printOutput must return a number.

@kyupa.suria, could you please read the last OP post? What you mentioned in your post may confuse the OP because the OP has already updated the code. In other words, the taxes.printOutput() now is a void type and does not need to return anything (and it shouldn't unless it returns a String but not recommended).

Also, all input values from a user will never be visible to anything in the class except inside the main() method. As I mention earlier, there are many ways to pass the values to Payroll class, but I suggested implementing another constructor. If you are going to suggest another solution, please feel free. But please do not reply to an old version of the code.

hai UNDER-18 FG,

you did one thing wrong for caliculating netPay value that is

you have taken input from user thats right but you haven't assigned those values to the Payroll class variables.

without doing that how can you caliculate netpay ? the above two posts are explaining same thing

for the solution do one thing

after payroll object is created assign user input values to the payroll class variables
(i think you know how the process is)
but i will provide for you

<instance-name>.variable_name = appropriate_value
do for remaining var's also

after all that,call caliculateNetpay()

then you will get the output as you wished.........

Instead of assigning values like this <instance-name>.variable_name = appropriate_value.
It is better to have a constructor and pass all the values to the constructor while you create the object.
Otherwise create getter and setter methods for all the class veariables.
Then call the setter methods to assign the value to the class variables and use getter methods to use the assigned value of the variables.
This is all about object oriented approach in java.

Fo this example the constructor is can be defined like this

 public Payroll(double grossPay, double stateTax, double federalTax){
    this.grossPay=grossPay;
    this.stateTax=stateTax;
    this.federalTax=federalTax;
 }

and while object can be created like this

 Payroll payroiObject=new Payroll(10000,1000,500);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.