*6.15(金融应用:打印税表)程序清单3-5给出了计算税款的程序。使用税款的程序。使用下面的方法头编写一个计算税款的方法:
public static double computeTax(int status,double taxableIncome)
使用这个方法编写程序,打印可征税收人从50000美元到60000美元,收入间隔为50美元的所有以下婚姻状态的纳税表,如下所示:
Taxable Single Married Joint Married Head of
Income or Qualifying Separate House hold
Widow(er)
------------------------------------------------------------------------------------------
50000 8688 6665 8688 7353
50050 8700 6673 8700 7365
......
59950 11175 8158 11175 9840
60000 11188 8165 11188 9853
提示:使用Math.round(即Math.round(computeTax(status,tabableIncome)))将税收舍入为整数。
*6.15(Financial application: print a tax table) Listing 3.5 gives a program to compute tax. Write a method for computing tax using the following header:
public static double computeTax(int status, double taxableIncome)
Use this method to write a program that prints a tax table for taxable income from $50,000 to $60,000 with intervals of $50 for
all the following statuses:
Taxable Single Married Joint Married Head of
Income or Qualifying Separate House hold
Widow(er)
------------------------------------------------------------------------------------------
50000 8688 6665 8688 7353
50050 8700 6673 8700 7365
......
59950 11175 8158 11175 9840
60000 11188 8165 11188 9853
Hint: round the tax into integers using Math.round (i.e., Math .round(computeTax(status, taxableIncome))).
下面是参考答案代码:
public class PrintATaxTableQuestion15 {
public static void main(String[] args) {
printTableHead();
for(int i = 50000;i <= 60000;i += 50)
{
System.out.printf("%d\t\t", i);
System.out.printf("%d\t\t", Math.round(computeTax(0,i)));
System.out.printf("%d\t\t\t", Math.round(computeTax(1,i)));
System.out.printf("%d\t\t", Math.round(computeTax(2,i)));
System.out.printf("%d\n", Math.round(computeTax(3,i)));
}
}
public static double computeTax(int status,double taxableIncome)
{
double tax = 0;
if (status == 0)
{ // Compute tax for single filers
if (taxableIncome <= 8350)
tax = taxableIncome * 0.10;
else if (taxableIncome <= 33950)
tax = 8350 * 0.10 + (taxableIncome - 8350) * 0.15;
else if (taxableIncome <= 82250)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15
+(taxableIncome - 33950) * 0.25;
else if (taxableIncome <= 171550)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15
+(82250 - 33950) * 0.25 + (taxableIncome - 82250) * 0.28;
else if (taxableIncome <= 372950)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
(taxableIncome - 171550) * 0.33;
else
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
(372950 - 171550) * 0.33 + (taxableIncome - 372950) * 0.35;
}
else if (status == 1)
{// Compute tax for married file jointly or qualifying widow(er)
if (taxableIncome <= 16700)
tax = taxableIncome * 0.10;
else if (taxableIncome <= 67900)
tax = 16700 * 0.10 + (taxableIncome - 16700) * 0.15;
else if (taxableIncome <= 137050)
tax = 16700 * 0.10 + (67900 - 16700) * 0.15
+(taxableIncome - 67900) * 0.25;
else if (taxableIncome <= 208850)
tax = 16700 * 0.10 + (67900 - 16700) * 0.15
+(137050 - 67900) * 0.25 + (taxableIncome - 137050) * 0.28;
else if (taxableIncome <= 372950)
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 +
(137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 +
(taxableIncome - 208850) * 0.33;
else
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 +
(137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 +
(372950 - 208850) * 0.33 + (taxableIncome - 372950) * 0.35;
}
else if (status == 2)
{ // Compute tax for married separately
if (taxableIncome <= 8350)
tax = taxableIncome * 0.10;
else if (taxableIncome <= 33950)
tax = 8350 * 0.10 + (taxableIncome - 8350) * 0.15;
else if (taxableIncome <= 68525)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15
+(taxableIncome - 33950) * 0.25;
else if (taxableIncome <= 104425)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15
+(68525 - 33950) * 0.25 + (taxableIncome - 68525) * 0.28;
else if (taxableIncome <= 186475)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 +
(taxableIncome - 104425) * 0.33;
else
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 +
(186475 - 104425) * 0.33 + (taxableIncome - 186475) * 0.35;
}
else if (status == 3)
{ // Compute tax for head of household
if (taxableIncome <= 11950)
tax = taxableIncome * 0.10;
else if (taxableIncome <= 45500)
tax = 11950 * 0.10 + (taxableIncome - 11950) * 0.15;
else if (taxableIncome <= 117450)
tax = 11950 * 0.10 + (45500 - 11950) * 0.15
+(taxableIncome - 45500) * 0.25;
else if (taxableIncome <= 190200)
tax = 11950 * 0.10 + (45500 - 11950) * 0.15
+(117450 - 45500) * 0.25 + (taxableIncome - 117450) * 0.28;
else if (taxableIncome <= 372950)
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +
(117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 +
(taxableIncome - 190200) * 0.33;
else
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +
(117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 +
(372950 - 190200) * 0.33 + (taxableIncome - 372950) * 0.35;
}
else
return -1; // that means status is invalid
return tax;
}
public static void printTableHead()
{
System.out.println("Taxable\t\tSingle\t\tMarried Joint\t\tMarried\t\tHead of");
System.out.println("Income\t\t\t\tor Qualifying\t\tSeparate\tHouse hold");
System.out.println("\t\t\t\tWidow(er)");
System.out.print("-----------------------------------------");
System.out.print("-----------------------------------------\n");
}
}
运行效果:
注:编写程序要养成良好习惯
1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)
5.普通变量,方法名要小驼峰,类名要大驼峰,常量要使用全部大写加上下划线命名法
6.要学习相应的代码编辑器的一些常用快捷键,如:快速对齐等等