Write a tip calculator application. Use grid layout. Include panels in the progr
ID: 3654556 • Letter: W
Question
Write a tip calculator application. Use grid layout. Include panels in the program to display the amount of the order, tip, split of each person tipping, and the results which include the tip amount, total amount, tip per person, and the total per person. Also add buttons to calculate and reset the data entered.Please be sure to include a main method along with different classes to go along with it. The amount should be at the top followed by the tip, then the split of each person tipping (1-10), then the buttons (calculate and reset), followed by the results.Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.text.DecimalFormat;
import java.util.EventObject;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.plaf.basic.BasicBorders.ButtonBorder;
public abstract class TipCalc extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final Color Gray = null;
private JButton cmdEnter ;
private JButton cmdReSet;
private JTextField lcd;
private JTextField lcd2;
private JFrame mainframe;
@SuppressWarnings("deprecation")
public TipCalc(){
mainframe = new JFrame("Tip Calculator");
cmdEnter = new JButton("Enter Total");
cmdReSet = new JButton("Clear");
lcd = new JTextField();
lcd2 = new JTextField();
Container c = mainframe.getContentPane();
c.setLayout (new FlowLayout());
c.add(cmdEnter);
c.add(cmdReSet);
cmdEnter.setMnemonic('E');
cmdReSet.setMnemonic('R');
mainframe.setSize(150,300);
mainframe.addWindowListener(new WindowAdapter(){
@SuppressWarnings("unused")
public void windowClosing(windowEvent e){System.exit(0);}});
Color magenta = null;
Color Black = null;
//create and register and single button event handler
ButtonBorder bhandler = new ButtonBorder(Gray,magenta, Black,Gray); //Instantiate the handler
cmdEnter.addActionListener( (ActionListener) bhandler); //register the handler
cmdReSet.addActionListener ((ActionListener) bhandler); //register the handler
mainframe.show();
}
class ButtonHandler implements ActionListener{
@SuppressWarnings("null")
public void actionPerformed(ActionEvent e){
EventObject r = null;
if (e.getSource()==cmdEnter)
JOptionPane.showInputDialog(null,"Enter SubTotal.","Event Handler Message",JOptionPane.QUESTION_MESSAGE);
else if (r.getSource() == cmdReSet)
JOptionPane.showInputDialog(null,"Reset!!","Event Handler Message",JOptionPane.INFORMATION_MESSAGE);}
public void main(String[]args)
{
int a;
int b;
double subTotal;
double gratuity;
double total;
String x;
lcd = JOptionPane.showInputDialog(null,"Enter subtotal","Value for A",JOptionPane.INFORMATION_MESSAGE);
a = Integer.parseInt(x);
x = JOptionPane.showInputDialog(null,"Enter Gratuity","Value for B",JOptionPane.QUESTION_MESSAGE);
b = Integer.parseInt(x);
subTotal = a;
gratuity = b;
total = subTotal + gratuity;
total = Math.round(total*100)/10;
System.out.print("The total is" + total);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.