|
| 1 | +import javax.swing.JOptionPane; |
| 2 | +//A dialog calculator program. |
| 3 | +public class Cal { |
| 4 | + int input; // Class variable |
| 5 | + double x, y, z; // Class variable |
| 6 | + |
| 7 | + public void Menu(){//Class Menu method |
| 8 | + JOptionPane.showMessageDialog(null, "A Simple Calculator using Dialog." ); |
| 9 | + input = Integer.parseInt(JOptionPane.showInputDialog("\t Menu: \n 1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Division \n 5. Exit \n Enter an option: ")); |
| 10 | + } |
| 11 | + |
| 12 | + public void Add(){//Class Addition Method. |
| 13 | + JOptionPane.showMessageDialog(null, "\t Addition"); |
| 14 | + x = Integer.parseInt(JOptionPane.showInputDialog("Enter First Number: ")); |
| 15 | + y = Integer.parseInt(JOptionPane.showInputDialog("Enter Second Number: ")); |
| 16 | + z = x+y; |
| 17 | + JOptionPane.showMessageDialog(null, x + " + " + y + " = " + (z)); |
| 18 | + } |
| 19 | + |
| 20 | + public void Sub(){//Class Subtraction method |
| 21 | + JOptionPane.showMessageDialog(null, "\t Subtraction"); |
| 22 | + x = Integer.parseInt(JOptionPane.showInputDialog("Enter First Number: ")); |
| 23 | + y = Integer.parseInt(JOptionPane.showInputDialog("Enter Second Number: ")); |
| 24 | + z = x-y; |
| 25 | + JOptionPane.showMessageDialog(null, x + " - " + y + " = " + (z)); |
| 26 | + } |
| 27 | + |
| 28 | + public void Mul(){//Class Multiplication method |
| 29 | + JOptionPane.showMessageDialog(null, "\t Addition"); |
| 30 | + x = Integer.parseInt(JOptionPane.showInputDialog("Enter First Number: ")); |
| 31 | + y = Integer.parseInt(JOptionPane.showInputDialog("Enter Second Number: ")); |
| 32 | + z = x*y; |
| 33 | + JOptionPane.showMessageDialog(null, x + " x " + y + " = " + (z)); |
| 34 | + } |
| 35 | + |
| 36 | + public void Div(){//Class Division method. |
| 37 | + JOptionPane.showMessageDialog(null, "\t Addition"); |
| 38 | + x = Integer.parseInt(JOptionPane.showInputDialog("Enter First Number: ")); |
| 39 | + y = Integer.parseInt(JOptionPane.showInputDialog("Enter Second Number: ")); |
| 40 | + z = x/y; |
| 41 | + //Validating the division method, incase user input for y = 0; |
| 42 | + if(y ==0){ |
| 43 | + JOptionPane.showMessageDialog(null, "Division by zero is invalid!"); |
| 44 | + } |
| 45 | + else{ |
| 46 | + JOptionPane.showMessageDialog(null, x + " / " + y + " = " + (z)); |
| 47 | + } |
| 48 | + } |
| 49 | + public void exit() {//A class method to quit the program. |
| 50 | + JOptionPane.showMessageDialog(null, "You closed the program, goodbye!"); |
| 51 | + } |
| 52 | + public static void main(String[] args) { |
| 53 | + int next; |
| 54 | + |
| 55 | + do{//A do-while loop for program reusability. |
| 56 | + |
| 57 | + Cal h = new Cal();//An object for the class. |
| 58 | + h.Menu();//Calling te menu Menu. |
| 59 | + //A list of conditions to validate user inputs for menu options. |
| 60 | + if (h.input==1){ |
| 61 | + h.Add(); |
| 62 | + } |
| 63 | + else if (h.input==2){ |
| 64 | + h.Sub(); |
| 65 | + } |
| 66 | + else if (h.input==3){ |
| 67 | + h.Mul(); |
| 68 | + } |
| 69 | + else if (h.input==4){ |
| 70 | + h.Div(); |
| 71 | + } |
| 72 | + else if (h.input==5) { |
| 73 | + h.exit(); |
| 74 | + } |
| 75 | + else{//An else statement for error handling. |
| 76 | + JOptionPane.showMessageDialog(null, "Your input was invalid. Please try again."); |
| 77 | + } |
| 78 | + |
| 79 | + //Global variable for program continuation. |
| 80 | + next = Integer.parseInt(JOptionPane.showInputDialog(null, "Do you want to continue: (1.Yes / 2.No)")); |
| 81 | + if(next == 2){h.exit();}//for quitting when a user choise is 2(No). |
| 82 | + |
| 83 | + }while(next == 1);//Continue while user input is yes for next. |
| 84 | + } |
| 85 | +} |
0 commit comments