Question
Need assistance with code that's defective. Something may be missing or in the incorrect spot. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class IncomeTaxSTART extends
Need assistance with code that's defective. Something may be missing or in the incorrect spot.
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class IncomeTaxSTART extends JFrame { private static final int WIDTH = 200; private static final int HEIGHT = 400; private JTextField incomeField; private JTextField statusField; private JTextField outputField; // These are the actual input data and output values private double grossIncome; private int numberOfExemptions; private char filingStatus; public IncomeTaxSTART() { setTitle("Tax Computations"); setSize(WIDTH, HEIGHT); setLayout(new FlowLayout()); setDefaultCloseOperation(EXIT_ON_CLOSE); createContents(); setVisible(true); } public void createContents() { JLabel incomeLabel = new JLabel("Enter Gross Income (as a decimal number): ");
JLabel exemptionsLabel = new JLabel("Enter Number of Exemptions (as an integer): "); JLabel statusLabel = new JLabel("Enter Filing Status Code ('s', 'S', 'm', 'M', 'c', 'C'): "); JButton computeButton = new JButton("Compute Taxs"); Listener listener = new Listener(); incomeField = new JTextField(10); // TODO 1: // create three JTextFields (exemptionsField, statusField, outputField with appropriate widths for exemptions, filing status, and output
add(incomeLabel); add(incomeField); add(exemptionsLabel); add(exemptionsField); add(statusLabel); add(statusField); add(computeButton); add(outputField);
// TODO 2: // do an addActionListener(listener) and attach it to the computeButton to get notified when it is pressed. computeButton. }
private class Listener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { double taxRate = 0.0, taxableIncome = 0.0, taxesOwed = 0.0; String outputString;
try { grossIncome = Double.parseDouble(incomeField.getText()); // TODO 3: Write this conversion for the exemptions field, almost as above. numberOfExemptions =; filingStatus = statusField.getText().charAt(0); System.out.println("Gross Income = " + grossIncome); System.out.println("Number of Exemptions = " + numberOfExemptions); System.out.println("Filing Status = " + filingStatus);
// Compute the taxable income from what you read in.
taxableIncome = grossIncome - 2000.00 * numberOfExemptions;
if (taxableIncome > 0.0) { // Has taxable income. switch (filingStatus) { case 's', 'S' -> { taxRate = 0.20; } case 'm', 'M' -> { taxRate = 0.25; } case 'c', 'C' -> { // Compute the taxes for cohabiting filers. // Checks have to be made, using the taxable income, to find the exact rate. if (taxableIncome < 20_000.00)
{ taxRate = 0.10; } else if (20_000.00 <= taxableIncome && taxableIncome <= 50_000.00) { taxRate = 0.15; } else { taxRate = 0.30; } } default -> { System.out.println(filingStatus + " is an invalid Filing Status Code"); } } // end of switch taxesOwed = taxableIncome * taxRate; outputString = "Tax Rate = " + taxRate + ", Taxable Income = " + taxableIncome + ", Taxes Owed = " + taxesOwed; // TODO 4: Put the outputString into the outputField text field.
outputField. } else { System.out.println("Taxable Income < 0.0"); } } catch (Exception exception) { System.out.println("Illegal input, try again "); } }
}
public static void main(String[] args) { new IncomeTaxSTART(); } }
Step by Step Solution
3.38 Rating (151 Votes )
There are 3 Steps involved in it
Step: 1
It seems like there are a few issues with your code Lets address them step by step 1 Creating JTextF...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started