Advertisement
boxglue

Java Calc (1of2)

Jul 17th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. //package calculator;   //can't get this to work.
  2.  
  3.  
  4. /**
  5.  * This class is the part of the calculator program.
  6.  * It does all of the processing of the data after each button is pressed.
  7.  * The other part of the program is in the class CalcGUI.
  8.  * @author me */
  9. public class CalcMain {
  10.  
  11.     /**
  12.     * name of calculator GUI object
  13.     */ 
  14.     private static CalcGUI myCalc;
  15.     /**
  16.     * current operator
  17.     */
  18.     private static char op;
  19.     /**
  20.     * registers or variables for storing the numbers that have been entered
  21.     */
  22.     private static double x,y;
  23.    
  24.     /** flag to indicate data entry */ 
  25.     private static boolean dataEntry = false;  
  26.     //flag for decimal entry
  27.     /** flag to track how many decimal places we're working with.<br>
  28.     * 1 = none<br>
  29.     * 0 = decimal key has just been pressed<br>
  30.     * -1, -2, -3 ... where the next number is placed in the display
  31.     */
  32.     private static int decimal = 1;
  33.  
  34.     /**
  35.     * main function creates CalcGUI. That's it.
  36.     * @param args   command-line arguments (not used)
  37.     */
  38.     public static void main(String args[]) {
  39.         myCalc = new CalcGUI();
  40.     }
  41.  
  42.     /**
  43.     * Processes each button as it is pressed.<br>
  44.     * <b>Called from:</b> CalcGUI<br>
  45.     * <b>Calls:</b> setDisplay() in CalcGUI to set the display of the calculator<br>
  46.     * <b>Assumptions:</b> each each button has unique first character which is used to identify it.
  47.     * @param s  the string that comprises the button text  
  48.     */
  49.     static void sendButton(String s) {
  50.        
  51.         // myCalc.setDisplay("5.22");
  52.        
  53.         char c = (char)s.charAt(0);
  54.         // System.out.println(c);
  55.         switch (c) {
  56.             case 'C':   //clear
  57.                 myCalc.setDisplay("0.0");
  58.                 x=0.0;
  59.                 dataEntry=false;
  60.                 break;
  61.             case '.':
  62.                 decimal=0;
  63.                 break;
  64.             case '1': case '2': case '3': case '4': case '5':
  65.             case '6': case '7': case '8': case '9': case '0':
  66.                 if (dataEntry) {
  67.                     if (decimal < 1) {
  68.                         decimal--;
  69.                         if (x > 0)
  70.                             x = x + Character.getNumericValue(c) * Math.pow(10,decimal);
  71.                         else
  72.                             x = x - Character.getNumericValue(c) * Math.pow(10,decimal);
  73.                     } else
  74.                         if (x > 0)
  75.                             x = x*10.0 + Character.getNumericValue(c);
  76.                         else
  77.                             x = x*10.0 - Character.getNumericValue(c);
  78.                 }                  
  79.                 else {
  80.                     x = Character.getNumericValue(c);                  
  81.                 }
  82.                 dataEntry=true;
  83.                 myCalc.setDisplay(x);              
  84.                 break;
  85.             case '\u00B1':  // +/-
  86.                 x = -1 * x;
  87.                 // System.out.println(x + "CHS");
  88.                 myCalc.setDisplay(x);
  89.                 break;
  90.             case '\u221A':
  91.                 x = Math.sqrt(x);   //results in NaN if sqrt of negative number
  92.                 myCalc.setDisplay(x);
  93.                 dataEntry=false;
  94.                 break;
  95.             case '+': case '-': case 'x':
  96.                 op = c;
  97.                 y=x;
  98.                 dataEntry = false;
  99.                 break;
  100.             case '\u00F7':
  101.                 op = '/';
  102.                 y=x;
  103.                 dataEntry = false;
  104.                 break;
  105.             case '=':
  106.                 //perform calculation
  107.                 switch (op) {
  108.                 case '+':
  109.                     x = y + x;
  110.                     break;
  111.                 case '-':
  112.                     x = y - x;
  113.                     break;
  114.                 case 'x':
  115.                     x = y * x;
  116.                     break;
  117.                 case '/':
  118.                     x = y / x;  //divide by zero results in Infinity. Nice.
  119.                     break;
  120.                 }
  121.                 myCalc.setDisplay(x);
  122.                 dataEntry=false;
  123.                 break;
  124.             default: //for testing
  125.                 //myCalc.setDisplay(s);
  126.         }
  127.         if (!dataEntry) decimal = 1;
  128.         // System.out.println(x);
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement