Create a flowchart for this Java Program
Create a flowchart for this Java Program
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.JOptionPane;
public class Calculator extends Frame implements ActionListener {
Label fn, sn, op;
TextField tf1, tf2, tf3;
Button c;
Calculator() {
fn = new Label("First Number:");
fn.setBounds(60, 70, 110, 20);
sn = new Label("Second Number:");
sn.setBounds(60, 100, 110, 20);
op = new Label("Operator:");
op.setBounds(60, 130, 110, 20);
tf1 = new TextField();
tf1.setBounds(180, 70, 150, 20);
tf2 = new TextField();
tf2.setBounds(180, 100, 150, 20);
tf3 = new TextField();
tf3.setBounds(180, 130, 150, 20);
c = new Button("Compute");
c.setBounds(173, 170, 163, 30);
c.addActionListener(this);
add(fn);add(sn);add(op);add(tf1);add(tf2);add(tf3);add(c);
setSize(400, 250);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try {
double n1 = Double.parseDouble(tf1.getText());
double n2 = Double.parseDouble(tf2.getText());
double r;
char ch = tf3.getText().charAt(0);
switch (ch) {
case '+':
r = n1 + n2;
JOptionPane.showMessageDialog(null, "The sum is: " + r);
break;
case '-':
r = n1 - n2;
JOptionPane.showMessageDialog(null, "The difference is: " + r);
break;
case '*':
r = n1 * n2;
JOptionPane.showMessageDialog(null, "The product is: " + r);
break;
case '/':
r = n1 / n2;
JOptionPane.showMessageDialog(null, "The quotient is: " + r);
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Operator");
break;
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
public static void main(String[] args) {
new Calculator();
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images