简体   繁体   English

从子类的超类调用方法返回值0

[英]Calling a method from the superclass from subclass returns a value of 0

Pretty new to Java, this is for an assignment. Java很新,这是一项任务。 Basically what I'm trying to do is the user inputs the amount of hours they've worked, their hourly rate, and their straight time hours and the program outputs their net pay. 基本上,我要尝试的是用户输入工作时间,小时费率和工作时间,然后程序输出净工资。

I can calculate the gross pay just fine but the assignment requires me to calculate net pay within a subclass by calling the calc_payroll and tax methods from the superclass but it keeps returning a value of zero. 我可以很好地计算总薪水,但是分配要求我通过从超类调用calc_payroll和tax方法来计算子类内的净薪水,但它始终返回零值。 I thought maybe there was something wrong with my tax method so I tried returning the gross pay from the subclass but it still returned zero. 我以为自己的税法可能出了问题,所以我尝试从子类中退还总工资,但仍然返回零。

I'm really stumped here, can anyone help? 我真的很困惑,有人可以帮忙吗?

Superclass: 超类:

class Pay
{

   private float hoursWorked;
   private float rate;
   private int straightTimeHours;

   public double calc_payroll()
   {
      double straightTimePay = rate * straightTimeHours;
      double excessPay = (rate * 1.33) * (hoursWorked - straightTimeHours);
      double grossPay = straightTimePay + excessPay;

      return grossPay;
   }

   public double tax(double a)
   {
      double taxRate;
      double netPay;

      if(a <= 399.99)
         taxRate = 0.08;
      else if(a > 399.99 && a <= 899.99)
         taxRate = 0.12;
      else
         taxRate = 0.16;

      netPay = a - (a * taxRate);

      return netPay;
   }

   public void setHours(float a)
   {
      hoursWorked = a;
   }

   public float getHours()
   {
      return hoursWorked;
   }

   public void setRate(float a)
   {
      rate = a;
   }

   public float getRate()
   {
      return rate;
   }

   public void setHrsStr(int a)
   {
      straightTimeHours = a;
   }

   public int getHrsStr()
   {
      return straightTimeHours;
   }

}

Subclass: 子类:

class Payroll extends Pay
{

   public double calc_payroll()
   {
      Pay getVariables = new Pay();

      double getGrossPay = getVariables.calc_payroll();
      double finalNetPay = getVariables.tax(getGrossPay);

      return finalNetPay; //This returns a value of zero
      //return getGrossPay; This also returns a value of zero
   }

}

Main Method: 主要方法:

import java.util.*;

class Assign2A
{

   public static void main(String args[])
    {
      float userHours;
      float userRate;
      int userStraight;
      Scanner userInput = new Scanner(System.in);

      System.out.println("I will help you calculate your gross and net pay!");
      System.out.println("Please enter the number of hours you have worked: ");
      userHours = Float.valueOf(userInput.nextLine());
      System.out.println("Please enter your hourly pay rate: ");
      userRate = Float.valueOf(userInput.nextLine());
      System.out.println("Please enter the number of straight hours required: ");
      userStraight = Integer.parseInt(userInput.nextLine());

      Pay object = new Pay();
      object.setHours(userHours);
      object.setRate(userRate);
      object.setHrsStr(userStraight);

      Payroll objectTwo = new Payroll();

      System.out.println("========================================");
      System.out.println("Your gross pay is: ");
      System.out.println("$" + object.calc_payroll());
      System.out.println("Your net pay is: ");
      System.out.println("$" + objectTwo.calc_payroll());
      System.out.println("Thank you, come again!");
   }

}

Typical Output: 典型输出:

 ----jGRASP exec: java Assign2A

I will help you calculate your gross and net pay!
Please enter the number of hours you have worked: 
500
Please enter your hourly pay rate: 
25
Please enter the number of straight hours required: 
100
========================================
Your gross pay is: 
$15800.0
Your net pay is: 
$0.0
Thank you, come again!

 ----jGRASP: operation complete.

Several issues. 几个问题。 For one, you're creating a Payroll object: 首先,您要创建一个薪资对象:

  Payroll objectTwo = new Payroll();

  //.....
  System.out.println("$" + objectTwo.calc_payroll());

Not giving it any value, and then are surprised when it holds a value of 0. 没有给它任何值,然后当它的值为0时会感到惊讶。

You should use one single object here, a Payroll object, not a Pay object, fill it with valid data, and call both methods on this single object. 您应该在这里使用一个对象,一个薪资对象,而不是一个薪资对象,用有效数据填充它,并在该对象上调用这两种方法。

Secondly, your Payroll class is completely wrong. 其次,您的薪资类完全错误。 You have: 你有:

class Payroll extends Pay {

    public double calc_payroll() {
        Pay getVariables = new Pay();

        double getGrossPay = getVariables.calc_payroll();
        double finalNetPay = getVariables.tax(getGrossPay);

        return finalNetPay; // This returns a value of zero
        // return getGrossPay; This also returns a value of zero
    }

}

But it should not create a Pay object but rather use the super methods as needed. 但它不应创建Pay对象,而应根据需要使用super方法。 To better help you with this, you will have to tell us your complete assignment requirements because you're making wrong assumptions about the assignment, I believe. 我相信,为了更好地帮助您,您将不得不告诉我们您的完整作业要求,因为您对作业做出了错误的假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM