Question: m trying to add try and catch when the user inputs a negative number or blank. for this java code. package gui; import java.awt.BorderLayout;

m trying to add try and catch when the user inputs a negative number or blank." " for this java code.

package gui;

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat;

import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField;

public class Normal extends JFrame { private static final long serialVersionUID = 1L; private static final double COUNTY_TAX = 0.02; private static final double STATE_TAX = 0.04; private static final int WINDOW_WIDTH = 320; private static final int WINDOW_HEIGHT = 105; private static JPanel panel; private static JTextField totalSalesTextField; private static JButton calcButton; private static JLabel messageLabel;

public Normal() { JFrame window = new JFrame(); window.setTitle("Monthly Sales Tax Calculator"); window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); window.setLayout(new BorderLayout()); buildPanel();

window.add(panel); window.setVisible(true); }

private void buildPanel() { messageLabel = new JLabel("Please enter the total sales " + "for the month");

totalSalesTextField = new JTextField(10); calcButton = new JButton("Calculate"); calcButton.addActionListener(new CalcButtonListener());

panel = new JPanel(); panel.add(messageLabel); panel.add(totalSalesTextField); panel.add(calcButton); }

private class CalcButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) { String input; double totalSales; double countyTax; double stateTax; double stateCountyTotal; String message; DecimalFormat df = new DecimalFormat("#0.00"); input = totalSalesTextField.getText();

totalSales = Double.parseDouble(input);

countyTax = totalSales * COUNTY_TAX; stateTax = totalSales * STATE_TAX; stateCountyTotal = stateTax + countyTax; message = "The amount of county sales tax is $" + df.format(countyTax) + " The amount of state sales tax is $" + df.format(stateTax) + " The total sales tax is $" + df.format(stateCountyTotal); JOptionPane.showMessageDialog(null, message); } } public static void main(String[] args)throws Exception { Normal taxObject = new Normal();

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!