How do I get these values to display as the output instead of "0.0"?

129 Views Asked by At

So When I run this it gives me the grosspay but its just 0.0 when I do it... and I need to know how to get it to show the actual output.

import javax.swing.JOptionPane;


public class Payroll_demo

{
   public static void main(String[] args)

    {

    String input;

    Payroll A = new Payroll();

    input = JOptionPane.showInputDialog(null, "What is your name?");

    A.setName(input);

    input = JOptionPane.showInputDialog(null, "What is your ID number?");

    int input_b = Integer.parseInt(input);

    A.setID(input_b);

    input = JOptionPane.showInputDialog(null, "What is your hourly pay rate?");

    A.setPayRate(input);

    input = JOptionPane.showInputDialog(null, "How many hours did you work?");

    A.setHours(input);

    JOptionPane.showMessageDialog(null, A.Gross());


  }


}

I think it may be a constructor problem but I honestly have no clue. I feel as if ive tried just about everything but there has to be something...

public class Payroll

{

private String name;

private int ID;

private double payrate;

private double hours;

private double grosspay;


    public void setName(String nam)

    {

        String Name = nam;


    }

    public void setID(int id)

    {

        int ID = id;


    }

    public void setPayRate(String pay)

    {

        double PayRate = Double.parseDouble(pay);


    }

    public void setHours(String hou)

    {

        double Hours = Double.parseDouble(hou);


    }

    public String getName()
    {

        return name;

    }

    public int getID()
    {

        return ID;

    }

    public double getPayRate()
    {

        return payrate;

    }

    public double getHours()
    {

        return hours;

    }


    public double Gross()
    {

        double Gross = hours * payrate;

        grosspay = Gross;

        return grosspay;

    }






}
1

There are 1 best solutions below

0
On

How about instead of re-declaring your variables...

public void setName(String nam)
{
    String Name = nam;
}

public void setID(int id)
{
    int ID = id;
}

public void setPayRate(String pay)
{
    double PayRate = Double.parseDouble(pay);
}

public void setHours(String hou)
{
    double Hours = Double.parseDouble(hou);
}

You use the classes instance fields instead...

public void setName(String nam)
{
    name = nam;
}

public void setID(int id)
{
    ID = id;
}

public void setPayRate(String pay)
{
    payRate = Double.parseDouble(pay);
}

public void setHours(String hou)
{
    hours = Double.parseDouble(hou);
}