Question

I am trying to link a methods from one class to a main Tax Calculator program. I am using a Tax class with my methods. THe

Am I doing something wrong so the main program cannot call the methods from the Tax class? I have a feeling that its something with my namings.

The main pre-made code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;

import HW5.Tax;

public class TaxCalculator extends JApplet{
private final JButton btnAmount= new JButton("<html>Tax<br/>Amount</html>");
private final JButton btnMarginal= new JButton("<html>Marginal<br/>Tax Rate</html>");
private final JButton btnAverage= new JButton("<html>Average<br/>Tax Rate</html>");
private final JButton btnTaxable= new JButton("<html>Taxable<br/>Income</html>");
private final JButton btnCalculate= new JButton("<html>Calculate<br/>Taxable Amount</html>");
private final JLabel lblOutput = new JLabel("Result");
private final JLabel lblDummy1 = new JLabel(" ");
private final JLabel lblDummy2 = new JLabel(" ");
private  JFormattedTextField tfTaxable ;
private final JRadioButton radSingle = new JRadioButton("Single", true);
private final JRadioButton radMarried = new JRadioButton("Married");
private  JFormattedTextField tfResult;
JPanel contentPanel = new JPanel(new BorderLayout());
JPanel mainPanel = new JPanel(new GridLayout(3,3,4,4));
JPanel buttonPanel = new JPanel(new GridLayout(1,4,3,4));
private  NumberFormat amountFormat;

private final int SINGLE=1, MARRIED=2;
private int status=SINGLE;

public void init() {
    initFormatter();
    tfTaxable = new JFormattedTextField(amountFormat);
    tfResult = new JFormattedTextField(amountFormat);

    mainPanel.add(new JLabel("Taxable Income:"));
    mainPanel.add(tfTaxable); mainPanel.add(lblDummy1);

    mainPanel.add(new JLabel("Filing Status:"));
    ButtonGroup bg = new ButtonGroup();
    bg.add(radSingle);
    bg.add(radMarried);
    JPanel radioPanel = new JPanel(new GridLayout(1,2,3,4));
    radioPanel.add(radSingle); radioPanel.add(radMarried);
    mainPanel.add(radioPanel); mainPanel.add(lblDummy2);

    mainPanel.add(lblOutput);
    mainPanel.add(tfResult); tfResult.setEditable(false);
    mainPanel.add(btnCalculate);
    btnCalculate.setVisible(false); 
    btnCalculate.setEnabled(false);

    contentPanel.add(mainPanel, BorderLayout.NORTH);

    buttonPanel.add(btnAmount);
    buttonPanel.add(btnMarginal);
    buttonPanel.add(btnAverage);
    buttonPanel.add(btnTaxable);
    contentPanel.add(buttonPanel, BorderLayout.SOUTH);
    contentPanel.setBorder(new EmptyBorder(10, 10, 10, 10) );
    contentPanel.setBorder(
            BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),"2012 Tax Year")); 

    this.add(contentPanel);
    this.setVisible(true);

    btnAmount.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            toggleUI(false);
            lblOutput.setText("Tax amount: ");
            double income = ((Number)tfTaxable.getValue()).doubleValue();
            double tax = Tax.getTaxAmount(income, status);
            tfResult.setValue(new Double(tax));

        }
    });

    btnMarginal.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            toggleUI(false);
            lblOutput.setText("Marginal Tax Rate (%): ");
            double income = ((Number)tfTaxable.getValue()).doubleValue();
            double marginalRate = 100*Tax.getMarginalRate(income, status);
            tfResult.setValue(new Double(marginalRate));
        }
    });

    btnAverage.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            toggleUI(false);
            lblOutput.setText("Average Tax Rate (%): ");
            double income = ((Number)tfTaxable.getValue()).doubleValue();
            double averageRate = 100*Tax.getAverageRate(income, status);
            tfResult.setValue(new Double(averageRate));
            /*tfResult.setText(
                    String.format("%,.2f", //change the following method
                            100*Tax.getTaxAmount(getInput(tfTaxable),status) ));*/
        }
    });

    btnTaxable.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            lblOutput.setText("After tax amount: ");
            toggleUI(true);
            tfResult.requestFocus();
            tfTaxable.setText("");
        }
    });

    btnCalculate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            double postTax = ((Number)tfResult.getValue()).doubleValue();
            double preTax = Tax.getPreTaxAmount(postTax, status);
            tfTaxable.setValue(new Double(preTax));

        }
    });


    radSingle.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TaxCalculator.this.status=TaxCalculator.this.SINGLE;
        }
    });

    radMarried.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TaxCalculator.this.status=TaxCalculator.this.MARRIED;
        }
    });

}


private void toggleUI(boolean getPreTax) {
    tfResult.setEditable(getPreTax);
    btnCalculate.setEnabled(getPreTax);
    btnCalculate.setVisible(getPreTax);
}

private void initFormatter() {
    amountFormat = NumberFormat.getNumberInstance();
    //percentFormat = NumberFormat.getNumberInstance();
    amountFormat.setMinimumFractionDigits(2);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new JFrame("Tax Calculator");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            TaxCalculator applet = new TaxCalculator();
            frame.setContentPane(applet);//.getContentPane().add(applet, BorderLayout.CENTER);
            applet.init();
            applet.start();
            //frame.setSize(700, 500);
            frame.pack();
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            frame.setLocation((d.width - frame.getSize().width) / 2,
              (d.height - frame.getSize().height) / 2);
            frame.setVisible(true);
        }
    });

}
}

the methods that I use in a separate Tax calss are:

getTaxAmount:

public static double getTaxAmount(double income, int status){
    if (status == 1){
        if (income < 8700) return 8700 * .10;
        if (income < 35350) return 8700 * .10 + (income - 8700) * .15;
        if (income < 85650) return 8700 * .10 + (35350 - 8700) * .15 + (income - 35350) * .25;
        if (income < 178650) return 8700 * .10 + (35350 - 8700) * .15 + (85650 - 35350) * .25 + (income - 85650) * .28;
        if (income < 335000) 
            return 8700 *.10 + (35350 - 8700) * .15 + (75000 - 35350) * .25 + (115000 - 85650) * .28 + (income - 178650) * .33;
        else
            return income * .35;
        }
    else{}
        return income;

getAverageRate

    public static double getAverageRate(double income, int status){
        double averageRate;
        if(status == 1){
            if (income < 8700) 
                return ((income * .10) / income);
            if (income < 35350) 
                return ((8700 * .10 + (income - 8700) * .15) / income);
            if (income < 75000) 
                return ((8700 * .10 + (35350 - 8700) * .15 + (income - 35350) * .20) / income);
            if (income < 115000) 
                return ((8700 * .10 + (35350 - 8700) * .15 + (75000 - 35350) * .20 + (income - 75000) * .25) / income);
            if (income < 335000) 
                return ((8700 *.10 + (35350 - 8700) * .15 + (75000 - 35350) * .20 + (115000 - 75000) * .25 + (income - 115000) * .30) / income);
            else{}
                return averageRate;     


        }           


    return averageRate;
    }   
Was it helpful?

Solution

if your methods are in a separate class file from your main method then the reason is because static is local scope only. take the static out of the method signatures and you should be fine. Or if you move the methods to the same class file as the main method keep the static modifiers and it should still work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top