我是java的新手,我正在创建一个程序,我使用重载方法计算各种金额(税金,总薪酬等),然后显示它。我已经完成了它的大部分内容(我认为),但现在我需要在同一个包中创建第二个类,我将值设置为第一个程序以查看它是否有效。我不知道如何在涉及重载方法时调用该方法,我尝试使用get和set但我不知道如何在重载方法中应用它。如果我的术语关闭,我很抱歉,我还是很新!
package payrolllibrary;
public class PayrollLibrary {
public static float calculatePay(float hours, float rates)
{System.out.println("Pay: " + rates*hours);
return 0;
}
public static float calculatePay(float hours, float rates, float multiplier)
{System.out.println("Pay: " + rates*hours*multiplier);
return 0;
}
public static float calculateGrossPay(float regularPay, float overtimePay, float shift2Pay, float shift3Pay, float weeklyPay)
{float grossPay;
grossPay = regularPay + overtimePay + shift2Pay + shift3Pay + weeklyPay;
System.out.println("Gross Pay: " + grossPay);
return 0;
}
public static float calculatedSocialSecurityTax(float grossPay, float ytdEarnings, float ytdSocialSecurity)
{
float calculateSocialSecurityTax = 0;
float calculateGrossPay = 0;
if(ytdEarnings > 118500)
{calculateSocialSecurityTax = 0;}
if (ytdEarnings+calculateGrossPay < 118500)
{calculateSocialSecurityTax = calculateGrossPay*.062f;}
if (ytdEarnings+calculateGrossPay > 118500)
{if (calculateGrossPay*.062 <= 118500)
{calculateSocialSecurityTax = 118500;}
}
{System.out.println("Social Security Tax: " + calculateSocialSecurityTax);
return 0;
}
}
public static float calculateMedicareTax(float grossPay)
{float medicareTax;
medicareTax = grossPay*.0145f;
System.out.println("Medicare Tax: " + medicareTax);
return 0;
}
public static float calculateStateTax(float grossPay, float stateWithholding)
{System.out.println("State Tax: " + grossPay*stateWithholding);
return 0;
}
public static float calculateNetPay(float grossPay, float federalWithholding, float socialSecurityWithholding, float medicareWithholding, float stateWithholding, float deduction1Amount, float deduction2Amount, float deduction3Amount)
{float calculateNetPay;
calculateNetPay = grossPay-federalWithholding-socialSecurityWithholding-medicareWithholding-stateWithholding-deduction1Amount-deduction2Amount-deduction3Amount;
System.out.println("Net Pay: " + calculateNetPay);
return 0;
}
public static float calculateDeduction(float grossPay, char deductionCode, float deductionValue)
{if (deductionCode == 'N')
{deductionValue = 0;}
else if (deductionCode == 'F')
{deductionValue = deductionValue;}
else if (deductionCode == 'P')
{deductionValue = deductionValue*grossPay;}
System.out.println("Deduction: " + deductionValue);
return 0;
}
}
package PayrollLibrary;
public class TestPayroll {
public static void main(String[] args) {
PayrollLibrary Person = new PayrollLibrary();
Person.setRate(99.99);
Person.setHours(99);
Person.setMultiplier(999);
Person.setRegularPay(999);
Person.setOvertimePay(999);
Person.setShift2Pay(999);
Person.setShift3Pay(999);
Person.setWeekendPay(999);
Person.setYtdEarnings(999);
Person.setYtdSocialSecurity(999);
Person.setStateWithholding(999);
Person.setDeduction1Amount(999);
Person.setDeduction2Amount(999);
Person.setDeduction3Amount(999);
Person.setDeductionCode('F');
Person.setDeductionAmount(999);
}
}
答案 0 :(得分:0)
那些setXXX()
不会因为他们的英语含义而设置这些值。它们只是方法的名称。要设置值,您必须对它们进行编码。所以在PayrollLibrary
你应该有
public class PayrollLibrary {
float rate;
float hours;
......
public void setRate(float inRate) {
this.rate = inRate;
}
public void setHours(float inHours) {
this.hours = inHours;
}
然后在您的计算方法中,如果您要使用的是rate
和hours
,则不必将它们传递给方法,只需:
public float calculatePay() {
return rate*hours;
}
所以在main()
中,请执行此操作:
PayrollLibrary person = new PayrollLibrary();
person.setRate(99.99f);
person.setHours(99.0f);
要获得上述setXXX
之后的付款权,请执行以下操作:
float thePay = person.calculatePay();
请注意Java约定:类名以大写字母开头,变量和方法名称以小写字母开头。
希望它有所帮助。