// Simple calculator using char. // Program features user-friendly, useful messages and prompts. // Think - INPUT - PROCESSING - OUTPUT. import javax.swing.JOptionPane; public class SimpleCalculator { public static void main(String[]args) { String operatorS; char operatorC; double firstNumber = 0.0, secondNumber = 0.0, total = 0.0; // INPUT // String values converted to double values firstNumber = Double.parseDouble(JOptionPane.showInputDialog("Enter the first number")); secondNumber = Double.parseDouble(JOptionPane.showInputDialog("Enter the second number")); operatorS = JOptionPane.showInputDialog("Enter the operator" +"\n" + "+ to add" + "\n" + "- to subtract" + "\n" + "* to multiply" + "\n" + "/ to divide."); // PROCESSING // Converts String to char.A;so ensures data validation of the operator. operatorC = operatorS.charAt(0); if (operatorC == '+') total = firstNumber + secondNumber; else if (operatorC == '-') total = firstNumber - secondNumber; else if (operatorC == '*') total = firstNumber * secondNumber; else if (operatorC == '/') total = firstNumber / secondNumber; else { // System exit if the operator is bad. JOptionPane.showMessageDialog(null, "Unrecognisable operator." + "\n" + "Terminate program."); System.exit(0); } // OUTPUT JOptionPane.showMessageDialog(null, "The answer to " + firstNumber + " " + operatorC + " " + secondNumber + " is " + total); } }