public class DivideByZeroException extends Exception { public DivideByZeroExcept
ID: 3533286 • Letter: P
Question
public class DivideByZeroException extends Exception
{
public DivideByZeroException()
{
super("Error: cannot divide by zero.");
}
public DivideByZeroException(String message)
{
super(message);
}
}
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Calculator extends JFrame
implements ActionListener
{
private JTextField result;
private JTextField operand;
private JPanel numberPad;
private JButton[] numbers;
private JButton add;
private JButton subtract;
private JButton multiply;
private JButton divide;
private JButton reset;
private JButton decimal;
public Calculator()
{
super("Calculator");
this.setLayout(new FlowLayout());
this.setSize(200,250);
result = new JTextField("0.0" , 10);
result.setVisible(true);
operand = new JTextField(10);
operand.setVisible(true);
numberPad = new JPanel(new GridLayout(5,4));
numbers = new JButton[10];
for(int i = 0; i < 10; i++)
{
numbers[i] = new JButton("" + (i +1));
numbers[i].setActionCommand("" + (i + 1));
if(i ==9)
{
numbers[i] = new JButton("0");
numbers[i].setActionCommand("0");
}
numbers[i].setVisible(true);
numbers[i].addActionListener(this);
numberPad.add(numbers[i]);
}
decimal = newJButton(".");
decimal.setActionCommand(".");
decimal.setVisible(true);
decimal.addActionListener(this);
numberPad.add(decimal);
reset = new JButton("C");
reset.setActionComand("reset");
reset.setVisible(true);
reset.addActionListener(this);
numberPad.add(reset);
add = new JButton("+");
add.setActionCommand("add");
add.setVisible(true);
add.addActionListener(this);
numberPad.add(add);
subtract = new JButton("-");
subtract.setActionCommand("subtract");
subtract.setVisible(true);
subtract.addActionListener(this);
numberPad.add(subtract);
multiply = new JButton("*");
multiply.setActionCommand("multiply");
multipy.setVisible(true);
multiply.addActionListener(this);
numberPad.add(multiply);
divide = new JButton("/");
divide.setActionCommand("divide");
divide.setVisible(true);
divide.addActionListener(this);
numberPad.add(divide);
this.add(new JLabel("Result: "));
this.add(result);
this.add(new JLabel ("Operand: "));
this.add(operand);
this.add(numberPad);
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("add"))
{
if(operand.getText().length()==0)
return;
double r = Double.parseDouble(result.getText());
double o =
Double.parseDouble(operand.getText());
double answer = r + o;
result.setText(Double.toString(answer));
operand.setText("");
}
else if(s.equals("subtract"));
{
if(operand.getText().length()==0)
return;
double r = Double.parseDouble(result.getText());
double o =
Double.parseDouble(operand.getText());
double answer = r - o;
result.setText(Double.toString(answer));
operand.setText("");
}
else if(s.equals("multiply"));
{
if(operand.getText().length()==0)
return;
double r = Double.parseDouble(result.getText());
double o =
Double.parseDouble(operand.getText());
double answer = r * o;
result.setText(Double.toString(answer));
operand.setText("");
}
else if(s.equals("divide"));
{
if(operand.getText().length()==0)
return;
double r = Double.parseDouble(result.getText());
double o =
Double.parseDouble(operand.getText());
if(0 > -1.0e-10 && o < 1.0e-10)
try
{
throw new DivideByZeroException();
}
catch (DivideByZeroException e1)
{
result.setText("0.0");
operand.setText("Can't divide by 0.");
return;
}
double answer = r/o;
result.setText(Double.toString(answer));
operand.setText("");
}
else if(s.equals("."));
{
if(!operand.getText().contains("."));
operand.setText(operand.getText()+"."));
}
else if(s.equals("reset"))
{
result.setText("0.0");
operand.setText("");
}
else
{
if(operand.getText().equals(
"Can't divide by 0."));
operand.setText("");
if(s.length()==1)
{
operand.setText(operand.getText()
+ Integer.parseInt(s));
}
}
}
public static void main(String args[])
{
Calculator calc = new Calculator();
calc.setVisible(true);
}
}
Explanation / Answer
import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Chegg extends Exception { public Chegg() { super("Error: cannot divide by zero."); } public Chegg(String message) { super(message); } } class Calculator extends JFrame implements ActionListener { private JTextField result; private JTextField operand; private JPanel numberPad; private JButton[] numbers; private JButton add; private JButton subtract; private JButton multiply; private JButton divide; private JButton reset; private JButton decimal; public Calculator() { super("Calculator"); this.setLayout(new FlowLayout()); this.setSize(200,250); result = new JTextField("0.0" , 10); result.setVisible(true); operand = new JTextField(10); operand.setVisible(true); numberPad = new JPanel(new GridLayout(5,4)); numbers = new JButton[10]; for(int i = 0; i < 10; i++) { numbers[i] = new JButton("" + (i +1)); numbers[i].setActionCommand("" + (i + 1)); if(i ==9) { numbers[i] = new JButton("0"); numbers[i].setActionCommand("0"); } numbers[i].setVisible(true); numbers[i].addActionListener(this); numberPad.add(numbers[i]); } decimal = new JButton("."); decimal.setActionCommand("."); decimal.setVisible(true); decimal.addActionListener(this); numberPad.add(decimal); reset = new JButton("C"); reset.setActionCommand("reset"); reset.setVisible(true); reset.addActionListener(this); numberPad.add(reset); add = new JButton("+"); add.setActionCommand("add"); add.setVisible(true); add.addActionListener(this); numberPad.add(add); subtract = new JButton("-"); subtract.setActionCommand("subtract"); subtract.setVisible(true); subtract.addActionListener(this); numberPad.add(subtract); multiply = new JButton("*"); multiply.setActionCommand("multiply"); multiply.setVisible(true); multiply.addActionListener(this); numberPad.add(multiply); divide = new JButton("/"); divide.setActionCommand("divide"); divide.setVisible(true); divide.addActionListener(this); numberPad.add(divide); this.add(new JLabel("Result: ")); this.add(result); this.add(new JLabel ("Operand: ")); this.add(operand); this.add(numberPad); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if(s.equals("add")) { if(operand.getText().length()==0) return; double r = Double.parseDouble(result.getText()); double o = Double.parseDouble(operand.getText()); double answer = r + o; result.setText(Double.toString(answer)); operand.setText(""); } else if(s.equals("subtract")); { if(operand.getText().length()==0) return; double r = Double.parseDouble(result.getText()); double o = Double.parseDouble(operand.getText()); double answer = r - o; result.setText(Double.toString(answer)); operand.setText(""); } if(s.equals("multiply")); { if(operand.getText().length()==0) return; double r = Double.parseDouble(result.getText()); double o = Double.parseDouble(operand.getText()); double answer = r * o; result.setText(Double.toString(answer)); operand.setText(""); } if(s.equals("divide")); { if(operand.getText().length()==0) return; double r = Double.parseDouble(result.getText()); double o = Double.parseDouble(operand.getText()); if(0 > -1.0e-10 && o < 1.0e-10) try { throw new Chegg(); } catch (Chegg e1) { result.setText("0.0"); operand.setText("Can't divide by 0."); return; } double answer = r/o; result.setText(Double.toString(answer)); operand.setText(""); } if(s.equals(".")); { if(!operand.getText().contains(".")); operand.setText(operand.getText()+"."); } if(s.equals("reset")) { result.setText("0.0"); operand.setText(""); } else { if(operand.getText().equals( "Can't divide by 0.")); operand.setText(""); if(s.length()==1) { operand.setText(operand.getText() + Integer.parseInt(s)); } } } public static void main(String args[]) { Calculator calc = new Calculator(); calc.setVisible(true); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.