Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create an application named TaxCalc.java that allows you to enter the amount of

ID: 3882859 • Letter: C

Question

Create an application named TaxCalc.java that allows you to enter the amount of a purchase and then displays the amount of sales tax on that purchase. Use a slider to adjust the tax rate between 0 percent and 10 percent. This is what I have but there are about 4 errors that I can't figure out how to fix. Please help!!

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.text.DecimalFormat;

public class ScrollableTaxCalculator extends JFrame
{
private JLabel l1, l2, l3;
private JTextField costA; //Price Amount
private JTextField sTax;
private JTextField totalSales;
private JPanel ppanel;
private JPanel spanel;
private JPanel tpanel;
private JPanel sliderPanel;
private JSlider slider;

public ScrollableTaxCalculator()
{
setTitle("Scrollable Tax Calculator");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

l1 = new JLabel("Enter the amount of Price Amount: ");
l2 = new JLabel("Sales Tax on the purchase: ");
l3 = new JLabel("Total Sales:");

costA = new JTextField("0.0", 10);
costA.setEditable(true);
  
sTax = new JTextField("0.0", 10);
sTax.setEditable(false);
  
totalSales = new JTextField("0.0", 10);
totalSales.setEditable(false);

slider = new JSlider(JSlider.HORIZONTAL, 0, 10, 0);
slider.setMajorTickSpacing(1);
slider.setMinorTickSpacing(1);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.addChangeListener(new SliderListener());

ppanel = new JPanel();
ppanel.add(l1);
ppanel.add(costA);
  
spanel = new JPanel();
spanel.add(l2);
spanel.add(sTax);
  
tpanel = new JPanel();
tpanel.add(l3);
tpanel.add(totalSales);
  
sliderPanel = new JPanel();
sliderPanel.add(slider);

setLayout(new GridLayout(4, 1));

add(ppanel);
add(spanel);
add(tpanel);
add(sliderPanel);

pack();
setVisible(true);
}

private class SliderListener implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
double price, tax, total;
DecimalFormat fmt = new DecimalFormat("0.0");

price = Double.parseDouble(costA.getText());
tax = slider.getValue();
total = price * (1 + (tax/100));
  
costA.setText(Double.toString(price));
sTax.setText(fmt.format(tax));
totalSales.setText(fmt.format(total));
  
}
}

public static void main(String[] args)
{
new ScrollableTaxCalculator();
}
}

Explanation / Answer

Okay, Let's Start

First of all, you need to have a Java Development Kit Installed on your System.

Now the name of the file should be same as the name of the class having the main method.

So, the file name should be TaxCalc.java, and the class having the main method should also be named TaxCalc.

So replacing the name of the class ScrollableTaxCalculator to TaxCalc, and replacing it wherever necessary.

Code: -

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.text.DecimalFormat;
public class TaxCalc extends JFrame
{
private JLabel l1, l2, l3;
private JTextField costA; //Price Amount
private JTextField sTax;
private JTextField totalSales;
private JPanel ppanel;
private JPanel spanel;
private JPanel tpanel;
private JPanel sliderPanel;
private JSlider slider;
public TaxCalc()
{
setTitle("Scrollable Tax Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1 = new JLabel("Enter the amount of Price Amount: ");
l2 = new JLabel("Sales Tax on the purchase: ");
l3 = new JLabel("Total Sales:");
costA = new JTextField("0.0", 10);
costA.setEditable(true);
  
sTax = new JTextField("0.0", 10);
sTax.setEditable(false);
  
totalSales = new JTextField("0.0", 10);
totalSales.setEditable(false);
slider = new JSlider(JSlider.HORIZONTAL, 0, 10, 0);
slider.setMajorTickSpacing(1);
slider.setMinorTickSpacing(1);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.addChangeListener(new SliderListener());
ppanel = new JPanel();
ppanel.add(l1);
ppanel.add(costA);
  
spanel = new JPanel();
spanel.add(l2);
spanel.add(sTax);
  
tpanel = new JPanel();
tpanel.add(l3);
tpanel.add(totalSales);
  
sliderPanel = new JPanel();
sliderPanel.add(slider);
setLayout(new GridLayout(4, 1));
add(ppanel);
add(spanel);
add(tpanel);
add(sliderPanel);
pack();
setVisible(true);
}

private class SliderListener implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
double price, tax, total;
DecimalFormat fmt = new DecimalFormat("0.0");
price = Double.parseDouble(costA.getText());
tax = slider.getValue();
total = price * (1 + (tax/100));
  
costA.setText(Double.toString(price));
sTax.setText(fmt.format(tax));
totalSales.setText(fmt.format(total));
  
}
}
public static void main(String[] args)
{
new TaxCalc();
}
}

Now, locate the file using Comand Prompt (cmd) and use javac TaxCalc.java to compile the code, and then java TaxCalc to run the compiler File.

This is all for the question.

Thank You for using Chegg...!!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote