2015-10-22 99 views
0

對Java來說很新,這是用於賦值的。基本上我想要做的是用戶輸入他們工作的小時數,他們的小時費率,他們的直線時間小時以及程序輸出的淨薪水。從子類的超類調用方法返回值爲0

我可以計算毛支付就好了,但分配要求我通過從超類調用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. 
+2

只提供需要的東西。不要轉儲所有的代碼。 [MCVE](http://stackoverflow.com/help/mcve)。 – YoungHobbit

+0

您的Payroll類完全錯誤 - 您不應該創建Pay對象,而是使用超類的方法。這個班有什麼要求?總體要求是什麼?你正在做出我認爲不好的假設。 –

回答

1

幾個問題。首先,你要創建一個工資對象:

Payroll objectTwo = new Payroll(); 

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

沒有給它的任何值,然後是驚訝,當其持有的0.1

的值應該在這裏使用一個單獨的對象,一個工資對象,而不是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對象,而是根據需要使用超級方法。爲了更好地幫助你,你必須告訴我們你的完成任務要求,因爲你對這個任務做出了錯誤的假設,我相信。

+0

得到它的工作,謝謝。 –

+0

@GuyFieri:希望你改變了工資單。 –