0

我正在做一个项目并且卡住了一段时间,我想做两个对象,一个是 t2001,另一个是 t2009。我为他们创建了TaxFiling的构造函数,但我仍然收到一个红色下划线错误,说找不到符号?有谁知道我做错了什么。

package ProgrammingAssignment;

import java.util.Scanner;

/**
 *
 * @author Joe
 */
public class Tax {
    public static final int SINGLE_FILER = 0;
    public static final int MARRIED_JOINTLY_OR_QUALIFYING_WIDOW = 1;
    public static final int MARRIED_SEPARATELY = 2;
    public static final int HEAD_OF_HOUSEHOLD = 3;

    private int filingStatus;
    private int[][] brackets;
    private double[] rates;
    private double taxableIncome;

    public Tax(int filingStatus, int[][] brackets, double[] rates, double taxableIncome) {
        this.filingStatus = filingStatus;
        this.brackets = brackets;
        this.rates = rates;
        this.taxableIncome = taxableIncome;
    }

    public double getTax() {
        double tax;
        if (taxableIncome <= brackets[filingStatus][0]) {
            return Math.round(taxableIncome * rates[0]);
        }

        tax = brackets[filingStatus][0] * rates[0];

        for (int i = 1; i < brackets[filingStatus].length; i++) {

            if (taxableIncome > brackets[filingStatus][i]) {
                tax += (brackets[filingStatus][i] - brackets[filingStatus][i - 1]) * rates[i];

            } else {
                return Math.round(tax + (taxableIncome - brackets[filingStatus][i - 1]) * rates[i]);
            }
        }
        return Math.round(tax + (taxableIncome - brackets[filingStatus][4]) * rates[5]);
    }

    public void TaxFiling(int taxYear, int[][] brackets, double[] rates) {
        int Year = taxYear;
        int[][] bracket = brackets;
        double[] rate = rates;
    }

    public static void main(String[] args) {
        int taxYear;
        int sIncome;
        int eIncome;
        int stepVal;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a tax year: ");
        taxYear = input.nextInt();
        if (taxYear != 2009 && taxYear != 2001) {
            System.out.println("Please Enter a Valid Year!");
            System.exit(1);
        }
        System.out.println("Enter the starting income value: ");
        sIncome = input.nextInt();
        System.out.println("Enter ending income value: ");
        eIncome = input.nextInt();
        if (eIncome < sIncome) {
            System.out.println("Ending Income Cannot Be Less Then Starting");
            System.exit(2);
        }
        System.out.println("Enter a step value: ");
        stepVal = input.nextInt();
        if (stepVal < 0) {
            System.out.println("Please Enter a Positive Step Value!");
            System.exit(3);
        }
        System.out.println(taxYear + " Tax Table");
        System.out.println("Income range: " + sIncome + " to " + eIncome);

        int[][] brackets2009 = new int[][]{
            // stat 0 single 
            {8350, 33950, 82250, 171550, 372950},
            // stat 1 Married Filing Jointly 
            {16700, 67900, 137050, 208850, 372950},
            // stat 2 Married Filing Separately 
            {8350, 33950, 68525, 104425, 186475},
            // stat 3 Head of Household 
            {11950, 45500, 117450, 190200, 372950}};
        int[][] brackets2001 = new int[][]{
            // stat 0 single
            {27050, 65550, 136750, 297350},
            // stat 1 married joint 
            {45200, 109250, 166500, 297350},
            // stat 2 married separate
            {22600, 54625, 83250, 148675},
            // stat 3 head of household
            {36250, 93650, 15150, 297350}
        };

        double[] rates2009 = new double[]{0.10, 0.15, 0.25, 0.28, 0.33, 0.35};
        double[] rates2001 = new double[]{.15, .275, .305, .355, .391};
        TaxFiling t2001 = new TaxFiling(2001, brackets2001, rates2001);
        TaxFiling t2009 = new TaxFiling(2009, brackets2009, rates2009);

        if (taxYear == 2001) {
            DisplayTaxTable(t2001, sIncome, eIncome, stepVal);
        } else if (taxYear == 2009) {
            DisplayTaxTable(t2009, sIncome, eIncome, stepVal);
        }

    }

    public static void displayTaxTable(Tax tObj, int incomeStart, int incomeEnd, int incomeStep) {
        String s1 = "Taxable Income";
        String s2 = "Single";
        String s3 = "Married Joint";
        String s4 = "Married Separate";
        String s5 = "Head of house";
        System.out.printf(
                "%-20s%-12s%-4s%21s%16s\n", s1, s2, s3, s4, s5);
            for (int i = 50000;
                    i <= 60000; i += 1000) {
                System.out.printf("%4d%19.0f%16.0f%16.0f%20.0f\n", i,
                        new Tax(Tax.SINGLE_FILER, brackets2009, rates2009, i).getTax(),
                        new Tax(Tax.MARRIED_JOINTLY_OR_QUALIFYING_WIDOW, brackets2009, rates2009, i).getTax(),
                        new Tax(Tax.MARRIED_SEPARATELY, brackets2009, rates2009, i).getTax(),
                        new Tax(Tax.HEAD_OF_HOUSEHOLD, brackets2009, rates2009, i).getTax()
                );

            }
            for (int i = 50000;
                    i <= 60000; i += 1000) {
                System.out.printf("%4d%19.0f%16.0f%16.0f%20.0f\n", i,
                        new Tax(Tax.SINGLE_FILER, brackets2001, rates2001, i).getTax(),
                        new Tax(Tax.MARRIED_JOINTLY_OR_QUALIFYING_WIDOW, brackets2001, rates2001, i).getTax(),
                        new Tax(Tax.MARRIED_SEPARATELY, brackets2001, rates2001, i).getTax(),
                        new Tax(Tax.HEAD_OF_HOUSEHOLD, brackets2009, rates2001, i).getTax()
                );

            }

        }
    }
4

4 回答 4

0

TaxFiling不是构造函数,它是Tax类内部的方法,您需要为其创建一个不同的类Taxfiling并创建一个构造函数,例如:

public class TaxFiling { 

        public TaxFiling(int taxYear, int[][] brackets, double[] rates) { 

        } 

}
于 2016-04-11T19:57:04.863 回答
0
public Tax(int taxYear, int[][] brackets, double[] rates) {
    int Year = taxYear;
    int[][] bracket = brackets;
    double[] rate = rates;

}

这就是我将它更改为它并且它可以工作但是当我尝试将对象传递到 displayTaxTable 时我得到更多错误。

public static void displayTaxTable(Tax tObj, int incomeStart, int incomeEnd, int stepVal) {
    String s1 = "Taxable Income";
    String s2 = "Single";
    String s3 = "Married Joint";
    String s4 = "Married Separate";
    String s5 = "Head of house";
    System.out.printf(
            "%-20s%-12s%-4s%21s%16s\n", s1, s2, s3, s4, s5);
        for (int i = incomeStart;
                i <= incomeEnd; i += stepVal) {
            System.out.printf("%4d%19.0f%16.0f%16.0f%20.0f\n", i,
                    new Tax(Tax.SINGLE_FILER, bracket, rate, i).getTax(),
                    new Tax(Tax.MARRIED_JOINTLY_OR_QUALIFYING_WIDOW, bracket, rate, i).getTax(),
                    new Tax(Tax.MARRIED_SEPARATELY, bracket, rate, i).getTax(),
                    new Tax(Tax.HEAD_OF_HOUSEHOLD, bracket, rate, i).getTax()
            );

        }

    }
于 2016-04-11T20:08:11.683 回答
0

如果你写“new TaxFiling(...)”,这意味着你调用了一个构造函数。但在您的代码中,TaxFiling 是一种方法。

三个选项: 1)TaxFiling使用字段taxYear, bracketsdouble rates. 制作一个构造函数:

public TaxFiling(int taxYear, int[][] brackets, double[] rates) {
    //set taxYear, brackets, rates etc
}

2)使方法taxFiling(方法以小写开头!)返回一个Tax对象,如下所示:

public Tax taxFiling(int taxYear, int[][] brackets, double[] rates) {  
    return new Tax(/* set your data here */);
}

3) 使 TaxFiling 成为 Tax 类中的构造函数。但是,所有构造函数的名称必须与它们所在的类完全相同,如下所示:

public Tax(int taxYear, int[][] brackets, double[] rates) {
    //set taxYear, brackets, rates etc
}

请注意,在displayTaxTable方法中,您不要使用传递的 tObj。好好利用它!使用 getter 从该对象获取信息

于 2016-04-11T19:59:39.640 回答
0

您正在尝试为您的类TaxFiling内部声明一个构造函数。需要它自己的单独的类文件,或者需要在类内部的私有类中声明。TaxTaxFilingTax

于 2016-04-11T19:53:56.213 回答