rjsantiago0001

ComplexNumbersGUIDriver

Feb 20th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. /*
  2.  * Ricardo Santiago
  3.  * 2/20/16
  4.  * CSC-112
  5.  * ComplexGuiDriver
  6.  */
  7. import javax.swing.JOptionPane;
  8.  
  9. public class ComplexGuiDriver {
  10.    
  11.     public static void main(String[]args){
  12.         final String title = "Complex Numbers GUI";
  13.         System.out.println("Running " + title +". Have fun!!!");
  14.          
  15.        
  16.         do {
  17.             double real, imag;
  18.             String temp;
  19.             temp = JOptionPane.showInputDialog(null, "Enter a real number", title, JOptionPane.INFORMATION_MESSAGE);
  20.             if (temp == null)
  21.                 break;
  22.             real = Integer.parseInt(temp);
  23.             temp= JOptionPane.showInputDialog(null, "Enter an imaginary number", title, JOptionPane.PLAIN_MESSAGE);
  24.             if (temp == null)
  25.                 break;
  26.             imag = Integer.parseInt(temp);
  27.             Complex a = new Complex(real,imag);
  28.            
  29.             //enter 2nd real number
  30.                 temp = JOptionPane.showInputDialog(null, "Enter a real number", title, JOptionPane.INFORMATION_MESSAGE);
  31.                 if (temp == null)
  32.                     break;
  33.                 real = Integer.parseInt(temp);
  34.                 temp= JOptionPane.showInputDialog(null, "Enter an imaginary", title, JOptionPane.PLAIN_MESSAGE);
  35.                 if (temp == null)
  36.                     break;
  37.                 imag = Integer.parseInt(temp);
  38.                 Complex c = new Complex(real,imag);
  39.                
  40.                 Complex ans = null;
  41.                 String options[] = {"Add","Subtract","Multiply","Divide","Absolute Value","Negate","Conjugate","Distance",
  42.                         "Equality","Greater than","Less Than","Quit"};
  43.                 int option = JOptionPane.showOptionDialog(null,"Choose Operation", title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,null, options,11);
  44.                 String output = "("+a+")";
  45.                 String out = "";
  46.                 switch (option){
  47.                 case 0:
  48.                     ans = a.add(c);
  49.                     output += " + ";
  50.                     break;
  51.                 case 1:
  52.                     ans = a.subtract(c);
  53.                     output += " + ";
  54.                     break;
  55.                 case 2:
  56.                     ans = a.multiply(c);
  57.                     output += " * ";
  58.                     break;
  59.                 case 3:
  60.                     ans = a.divide(c);
  61.                     output += " / ";
  62.                     break;
  63.                 case 4:
  64.                     out += " |" +a+ "|  = " +a.abs()+" \n |"+c+"| = "+c.abs()+"";
  65.                     break;
  66.                 case 5:
  67.                     out += "negate "+a+" = "+a.negate()+"\nnegate "+c+" = "+c.negate();
  68.                     break;
  69.                 case 6:
  70.                     out += "conjugate of "+a+" = "+a.conjugate()+"\nconjugate of "+c+" = "+c.conjugate();
  71.                     break;
  72.                 case 7:
  73.                     out += "distance between ("+a+") and ("+c+") = "+a.distance(c);
  74.                     break;
  75.                 case 8:
  76.                     out += a+" == "+c+" = "+ a.equals(c);
  77.                     break;
  78.                 case 9:
  79.                     out +=  a+" > "+c+" = "+ a.greaterThan(c);
  80.                     break;
  81.                 case 10:
  82.                     out +=  a+" < "+c+" = "+ a.lessThan(c);
  83.                     break;
  84.                    
  85.                 default:
  86.                     break;
  87.             }
  88.                 if (option > 11)
  89.                     break;
  90.                
  91.                 if (option <= 3){
  92.                     output +="("+ c +")"+ " = " + ans;
  93.                      JOptionPane.showMessageDialog(null,  output, title, JOptionPane.PLAIN_MESSAGE);
  94.                
  95.                 }else{
  96.                 if (option >= 4)
  97.                 JOptionPane.showMessageDialog(null,  out, title, JOptionPane.PLAIN_MESSAGE);
  98.                 }
  99.                
  100.                
  101.                 option = JOptionPane.showConfirmDialog(null,"Do you want to do this again?", title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
  102.                 if (option != JOptionPane.YES_OPTION)
  103.                     break;
  104.         } while(true);
  105.         }
  106. }
Add Comment
Please, Sign In to add comment