【问题标题】:Calling a method from the superclass from subclass returns a value of 0从子类调用超类的方法返回值 0
【发布时间】:2015-10-22 11:22:43
【问题描述】:

Java 新手,这是一个作业。基本上我要做的是用户输入他们工作的小时数、他们的小时费率和他们的直接工作时间,然后程序输出他们的净工资。

我可以很好地计算总工资,但作业要求我通过调用超类中的 calc_payroll 和 tax 方法来计算子类中的净工资,但它一直返回零值。我想我的纳税方法可能有问题,所以我尝试从子类返回总工资,但它仍然返回零。

我真的被难住了,有人可以帮忙吗?

超类:

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;
   }

}

子类:

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
   }

}

主要方法:

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!");
   }

}

典型输出:

 ----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.

【问题讨论】:

  • 只提供需要的东西。不要转储所有代码。 MCVE.
  • 你的 Payroll 类是完全错误的——你不应该让它创建一个 Pay 对象,而应该使用超类的方法。这个班的要求是什么?总体要求是什么?我认为你做出了错误的假设。

标签: java inheritance


【解决方案1】:

几个问题。首先,您正在创建一个 Payroll 对象:

  Payroll objectTwo = new Payroll();

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

不给它任何值,然后当它保持值为 0 时感到惊讶。

你应该在这里使用一个单一的对象,一个 Payroll 对象,而不是一个 Pay 对象,用有效数据填充它,并在这个单一对象上调用这两个方法。

其次,您的 Payroll 类完全错误。你有:

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
    }

}

但它不应该创建 Pay 对象,而是根据需要使用超级方法。为了更好地帮助您,您必须告诉我们您的完整作业要求,因为我相信您对作业做出了错误的假设。

【讨论】:

  • @GuyFieri:希望你改变了工资单。
猜你喜欢
  • 2011-04-05
  • 1970-01-01
  • 2011-10-24
  • 2012-04-18
  • 1970-01-01
相关资源
最近更新 更多