Can anyone correct this Java Program? I need to allows the user to enter the amo
ID: 3569343 • Letter: C
Question
Can anyone correct this Java Program? I need to allows the user to enter the amount of a bill, a tax rate, and percentage for a tip and then calculate the total amount owed. I am getting the wrong calculations when I run the program. Here are my 3 applications
//PayBillGUI
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
/*GUI class PayBillGUI*/
public class PayBillGUI extends JFrame implements ActionListener
{
/*declares the required components*/
//JLabel jlb_Amount, jlb_Tax, jlb_Tip, jlb_Total;
//JButton jbtn_Calculate, jbtn_Exit;
//JTextField jtf_Amount, jtf_Tax, jtf_Tip;
static JLabel jlb_Amount, jlb_Tax, jlb_Tip, jlb_Total;
static JButton jbtn_Calculate, jbtn_Exit;
static JTextField jtf_Amount, jtf_Tax, jtf_Tip;
JPanel panel=new JPanel(new FlowLayout());
JPanel panel1=new JPanel(new GridLayout(3,2));
JPanel panel2=new JPanel(new GridLayout(1,2,5,5));
/*declares the variable double*/
double total=0.00;
/*constructor*/
public PayBillGUI()
{
/*Initializes the objects*/
jlb_Amount=new JLabel("Total Due");
jlb_Tax=new JLabel("Tax Rate");
jlb_Tip=new JLabel("Tip Rate");
jlb_Total=new JLabel("Total Owed $"+total);
jtf_Amount=new JTextField(10);
jtf_Tax=new JTextField(10);
jtf_Tip=new JTextField(10);
jbtn_Calculate=new JButton("Calculate");
jbtn_Exit=new JButton("Exit");
/*adds the components to the desired panels*/
panel1.add(jlb_Amount);
panel1.add(jtf_Amount);
panel1.add(jlb_Tax);
panel1.add(jtf_Tax);
panel1.add(jlb_Tip);
panel1.add(jtf_Tip);
panel.add(panel1);
panel.add(jlb_Total);
panel2.add(jbtn_Calculate);
panel2.add(jbtn_Exit);
panel.add(panel2);
/*adds the panel to the frame*/
add(panel);
/*adds ActionListeners to the button*/
jbtn_Calculate.addActionListener(this);
jbtn_Exit.addActionListener(this);
/*sets the default close operation for the frame*/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/*actionPerformed method that computes the required functions when particular buttons are clicked*/
public void actionPerformed(ActionEvent e)
{
/*the below code will be executed when the Calculate button is clicked*/
if(e.getSource()==jbtn_Calculate)
{
double amount=0;
double tax=0;
double tip=0;
try
{
/*checks the number format of the text*/
if(isDouble(jtf_Amount.getText()))
{
amount=Double.parseDouble(jtf_Amount.getText());
/*checks whether the input value is less than zero, if it is it throws an InvalidAmountException*/
if(amount<0){
throw new InvalidAmountException();
}
}
/*checks for the number format of the text*/
if(isDouble(jtf_Tax.getText()))
{
tax=Double.parseDouble(jtf_Tax.getText());
if(tax<0){
throw new InvalidAmountException();
}
}
if(isInteger(jtf_Tip.getText()))
{
tip=Integer.parseInt(jtf_Tip.getText());
if(tip<0){
throw new InvalidAmountException();
} }
/*computes the tax before tip*/
double taxWithoutTip=tax*amount/100;
/*calculates the tip for the amount*/
double tipAmount=tip*amount/100;
/*calculates the total amount*/
total=amount+taxWithoutTip+tipAmount;
/*sets the output up to 2 decimals*/
DecimalFormat df = new DecimalFormat("0.##");
jlb_Total.setText("Total Owed $"+df.format(total/100));
}
/*catch blocks*/
catch(NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null,"Amount entered is invalid.");
}
catch(InvalidAmountException iae)
{
new InvalidAmountException();
}
}
/*application closes if Exit button is clicked*/
if(e.getSource()==jbtn_Exit)
{
System.exit(0);
}
}
/*checks whether input is double or not*/
boolean isDouble(String str) {
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
/*check whether input is integer or not*/
boolean isInteger(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
//InvalidAmountException class
import javax.swing.JOptionPane;
/*InvalidAmountException customized class*/
public class InvalidAmountException extends Throwable
{
InvalidAmountException()
{
JOptionPane.showMessageDialog(null,"Amount entered is invalid.");
}
}
//PayBillApplication
public class PayBillApplication /*main class*/
{
/*main method*/
public static void main(String args[])
{
/*creates object to the PayBillGUI class*/
PayBillGUI frame=new PayBillGUI();
frame.setTitle("Pay the Bill");
frame.setVisible(true);
frame.setSize(250, 200);
}
}
Explanation / Answer
which language ?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.