PageRenderTime 746ms CodeModel.GetById 208ms app.highlight 338ms RepoModel.GetById 189ms app.codeStats 0ms

/src/mpv5/ui/misc/Calc.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 1125 lines | 511 code | 156 blank | 458 comment | 122 complexity | 2f5a03824c7eab4e88f9d80221b4f1ff MD5 | raw file
   1/*
   2 *  This file is part of YaBS.
   3 *
   4 *      YaBS is free software: you can redistribute it and/or modify
   5 *      it under the terms of the GNU General Public License as published by
   6 *      the Free Software Foundation, either version 3 of the License, or
   7 *      (at your option) any later version.
   8 *
   9 *      YaBS is distributed in the hope that it will be useful,
  10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 *      GNU General Public License for more details.
  13 *
  14 *      You should have received a copy of the GNU General Public License
  15 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
  16 */
  17package mpv5.ui.misc;
  18
  19/******************************************************************************
  20 *
  21 * Program Name:		Java Calculator Applet
  22 * Author:				Jason Elias
  23 * Last Modified:		April 12, 2001
  24 * Purpose:				Provide all of the basic functions of a calculator in
  25 *						a GUI environment -- an applet. Also illustrates how
  26 *						to multithread an applet with two different
  27 *						implementations to illustrate it: one with a Thread
  28 *						internally defined in the applet, and a Clock thread
  29 *						defined outside of the applet.
  30 *
  31 * Notes:				Feel free to use and improve this code, and I would love
  32 *						to see any improvements that are made.
  33 *
  34 *						Email:		jasonelias@hotmail.com
  35 *						Website:	www.geocities.com/entity05
  36 *
  37 *****************************************************************************/
  38
  39import java.awt.BorderLayout;
  40import java.awt.Color;
  41import java.awt.Event;
  42import java.awt.Font;
  43
  44import java.awt.event.ActionEvent;
  45import java.awt.event.ActionListener;
  46import java.awt.event.KeyEvent;
  47import java.awt.event.KeyListener;
  48import javax.swing.JButton;
  49import javax.swing.JFrame;
  50import javax.swing.JLabel;
  51import javax.swing.border.EtchedBorder;
  52
  53
  54public class Calc extends JLabel implements ActionListener,KeyListener {
  55
  56    private static final long serialVersionUID = 1L;
  57    int Counter;			//counts the number of digits entered
  58    double Result;			//the answer displayed, as well as the second
  59    //operator taken for an operation
  60    double Operand;			//the first number entered for an operation
  61    double Mem;				//the variable which holds whatever value the user
  62    //wants kept in "memory"
  63    boolean DecimalFlag;	//the 'flag' that will signify whether or not the
  64    //decimal button has been pressed
  65    boolean SignFlag;		//the 'flag' that will signify whether or not the
  66    //plus/minus button has been pressed
  67    boolean OperatorKey;	//the 'flag' that will signify whether or not any
  68    //operator button has been pressed
  69    boolean FunctionKey;	//the 'flag' that will signify whether or not any f
  70    //function button has been pressed
  71    int Operator;			//an integer value to indicate which operator
  72    //button was pressed
  73    char currchar;			//a character to hold the value of the key most
  74    //recently pressed
  75    String GrStatus;		//String to hold the status of various graphic
  76    //operations of the program
  77    String Status;			//String to hold the status of various parts
  78
  79    //This label will display all error messages
  80     JLabel DisplError = new  JLabel(" ",  JLabel.CENTER);
  81
  82    /* This label is just to the left of the Display Label lcdDisplay, and will
  83     * indicate whether or not a value is being held in the calculator's "memory"
  84     */
  85     JLabel LabelMem = new JLabel(" ",  JLabel.RIGHT);
  86
  87    /* This is the Display Label, which is declared as a label so the user will not
  88     * be able to enter any text into it to possibly crash the calculator
  89     */
  90     JLabel lcdDisplay = new  JLabel("0",  JLabel.RIGHT);
  91
  92    // This is the declaration of all numeric buttons to be used in the applet
  93     JButton button1 = new  JButton("1");
  94     JButton button2 = new  JButton("2");
  95     JButton button3 = new  JButton("3");
  96     JButton button4 = new  JButton("4");
  97     JButton button5 = new  JButton("5");
  98     JButton button6 = new  JButton("6");
  99     JButton button7 = new  JButton("7");
 100     JButton button8 = new  JButton("8");
 101     JButton button9 = new  JButton("9");
 102     JButton button0 = new  JButton("0");
 103
 104    /* This is the declaration of all operation buttons that are used in applet.
 105     * MPlus, MClear and MR are all memory functions.
 106     */
 107    JButton buttonMinus = new JButton("-");
 108    JButton buttonMultiply = new JButton("x");
 109     JButton buttonPlus = new  JButton("+");
 110     JButton buttonEquals = new  JButton("=");
 111     JButton buttonDivide = new  JButton("/");
 112     JButton buttonClear = new  JButton("C");
 113     JButton buttonDecimal = new  JButton(".");
 114     JButton buttonNegative = new  JButton("+/-");
 115     JButton buttonMPlus = new  JButton("M+");
 116     JButton buttonMClear = new  JButton("MC");
 117     JButton buttonMR = new  JButton("MR");
 118     JButton buttonPercent = new  JButton("%");
 119     JButton buttonOneOverX = new  JButton("1/X");
 120     JButton buttonSqr = new  JButton("xÂ?");
 121     JButton buttonSqrRoot = new  JButton("sqrt");
 122
 123    public static void main(String[] args) {
 124
 125        JFrame j = new JFrame();
 126        j.setLayout(new BorderLayout());
 127
 128        j.add(new Calc(), BorderLayout.CENTER);
 129        j.setSize(340, 300);
 130        j.pack();
 131        j.setVisible(true);
 132    }
 133
 134    /* *This the only method that is called explicitly -- every other method is
 135     * called depending on the user's actions.
 136     */
 137    public Calc() {
 138        //Allows for configuring a layout with the restraints of a grid or
 139        //something similar
 140        setLayout(null);
 141
 142        //This will resize the applet to the width and height provided
 143        setSize(340, 300);
 144//
 145//        //This sets the default font to Helvetica, plain, size 12
 146//        setFont(new Font("Helvetica", Font.PLAIN, 12));
 147
 148        /* Display Panel, which appears at the top of the screen. The label is
 149         * placed and sized with the setBounds(x,y,width,height) method, and the
 150         * font, foreground color and background color are all set. Then the
 151         * label is added to the layout of the applet.
 152         */
 153        lcdDisplay.setBounds(2, 15, 308, 30);
 154        lcdDisplay.setFont(new Font("Helvetica", Font.PLAIN, 20));
 155        lcdDisplay.setForeground(Color.black);
 156        lcdDisplay.setBackground(Color.WHITE);
 157        lcdDisplay.setBorder(new EtchedBorder());
 158        add(lcdDisplay);
 159
 160        /* Memory Panel, which appears just to the right of the Display Panel.
 161         * The label is placed and sized with the setBounds(x,y,width,height)
 162         * method, and the font, foreground color and background color are all
 163         * set. Then the label is added to the layout of the applet.
 164         */
 165        LabelMem.setBounds(310, 15, 20, 30);
 166        LabelMem.setFont(new Font("Helvetica", Font.PLAIN, 16));
 167        LabelMem.setForeground(new Color(65280));
 168        LabelMem.setBackground(Color.LIGHT_GRAY);
 169        LabelMem.setBorder(new EtchedBorder());
 170        add(LabelMem);
 171
 172        /* The following declarations initialize all of the Numberic Buttons
 173         * that will displayed on the applet.
 174         *
 175         * First, an ActionListener (which will capture events) is added to the
 176         * button, sending the applet as the argument
 177         *
 178         * Second, the button is placed and sized with the setBounds(x,y,width,
 179         * height) method.
 180         *
 181         * Then the default font is set for the button, and the button is added
 182         * to the layout of the applet.
 183         */
 184        button1.addActionListener(this);
 185        button1.setBounds(2, 65, 60, 34);
 186        button1.addKeyListener(this);
 187        add(button1);
 188
 189        button2.addActionListener(this);
 190        button2.setBounds(66, 65, 60, 34);
 191        button2.addKeyListener(this);
 192        add(button2);
 193
 194        button3.addActionListener(this);
 195        button3.setBounds(130, 65, 60, 34);
 196        button3.addKeyListener(this);
 197        add(button3);
 198
 199        button4.addActionListener(this);
 200        button4.setBounds(2, 104, 60, 34);
 201        button4.addKeyListener(this);
 202        add(button4);
 203
 204        button5.addActionListener(this);
 205        button5.setBounds(66, 104, 60, 34);
 206        button5.addKeyListener(this);
 207        add(button5);
 208
 209        button6.addActionListener(this);
 210        button6.setBounds(130, 104, 60, 34);
 211        button6.addKeyListener(this);
 212        add(button6);
 213
 214        button7.addActionListener(this);
 215        button7.setBounds(2, 142, 60, 34);
 216        button7.addKeyListener(this);
 217        add(button7);
 218
 219        button8.addActionListener(this);
 220        button8.setBounds(66, 142, 60, 34);
 221        button8.addKeyListener(this);
 222        add(button8);
 223
 224        button9.addActionListener(this);
 225        button9.setBounds(130, 142, 60, 34);
 226        button9.addKeyListener(this);
 227        add(button9);
 228
 229        button0.addActionListener(this);
 230        button0.setBounds(2, 180, 60, 34);
 231        button0.addKeyListener(this);
 232        add(button0);
 233
 234        /* The following declaration and initialization statements set up all of
 235         * the operation buttons on the applet.
 236         *
 237         * First, an ActionListener is added to each button with the "this"
 238         * keyword indicating that "this object" (the applet) is being referred to.
 239         *
 240         * Then the button is "reshaped" to a certain coordinate (x and y) as
 241         * well as new dimensions (width, height).
 242         *
 243         * Then the font is set for that button, and the arguments include:
 244         *		- String value representing the name of the font
 245         *		- a member of Font called BOLD which bolds the text
 246         *		- the size of the font as an integer
 247         *
 248         * Lastly, the button is added to the applet
 249         */
 250        buttonDecimal.addActionListener(this);
 251        buttonDecimal.setBounds(66, 180, 60, 34);
 252        buttonDecimal.addKeyListener(this);
 253        add(buttonDecimal);
 254
 255        buttonNegative.addActionListener(this);
 256        buttonNegative.setBounds(130, 180, 60, 34);
 257        buttonNegative.addKeyListener(this);
 258        add(buttonNegative);
 259
 260        buttonMinus.addActionListener(this);
 261        buttonMinus.setBounds(194, 104, 60, 34);
 262        buttonMinus.addKeyListener(this);
 263        add(buttonMinus);
 264
 265        buttonMultiply.addActionListener(this);
 266        buttonMultiply.setBounds(194, 142, 60, 34);
 267        buttonMultiply.addKeyListener(this);
 268        add(buttonMultiply);
 269
 270        buttonPlus.addActionListener(this);
 271        buttonPlus.setBounds(194, 65, 60, 34);
 272        buttonPlus.addKeyListener(this);
 273        add(buttonPlus);
 274
 275        buttonPercent.addActionListener(this);
 276        buttonPercent.setBounds(2, 230, 60, 34);
 277        buttonPercent.addKeyListener(this);
 278//        add(buttonPercent);
 279
 280        buttonOneOverX.addActionListener(this);
 281        buttonOneOverX.setBounds(66, 230, 60, 34);
 282        buttonOneOverX.addKeyListener(this);
 283        add(buttonOneOverX);
 284
 285        buttonSqr.addActionListener(this);
 286        buttonSqr.setBounds(130, 230, 60, 34);
 287        buttonSqr.addKeyListener(this);
 288        add(buttonSqr);
 289
 290        buttonSqrRoot.addActionListener(this);
 291        buttonSqrRoot.setBounds(194, 230, 60, 34);
 292        buttonSqrRoot.addKeyListener(this);
 293        add(buttonSqrRoot);
 294
 295        buttonEquals.addActionListener(this);
 296        buttonEquals.setBounds(269, 230, 60, 34);
 297        buttonEquals.addKeyListener(this);
 298        add(buttonEquals);
 299
 300        buttonDivide.addActionListener(this);
 301        buttonDivide.setBounds(194, 180, 60, 34);
 302        buttonDivide.addKeyListener(this);
 303        add(buttonDivide);
 304
 305        buttonClear.addActionListener(this);
 306        buttonClear.setBounds(269, 65, 60, 34);
 307        buttonClear.addKeyListener(this);
 308        buttonClear.setForeground(new Color(16711680));
 309        buttonClear.setBackground(new Color(12632256));
 310        add(buttonClear);
 311
 312        buttonMPlus.addActionListener(this);
 313        buttonMPlus.setBounds(269, 104, 60, 34);
 314        buttonMPlus.addKeyListener(this);
 315        buttonMPlus.setForeground(Color.blue);
 316        add(buttonMPlus);
 317
 318        buttonMClear.addActionListener(this);
 319        buttonMClear.setBounds(269, 142, 60, 34);
 320        buttonMClear.addKeyListener(this);
 321        buttonMClear.setForeground(Color.blue);
 322        add(buttonMClear);
 323
 324        buttonMR.addActionListener(this);
 325        buttonMR.setBounds(269, 180, 60, 34);
 326        buttonMR.addKeyListener(this);
 327        buttonMR.setForeground(Color.blue);
 328        add(buttonMR);
 329
 330        /* This is the initialization of the DisplayError label. It is placed and
 331         * sized with the setBounds(x,y,width,height) method, and then the font,
 332         * foreground color and background color are all set. Finally, the label
 333         * is added to the layout of the applet.
 334         */
 335        DisplError.setBounds(2, 278, 329, 15);
 336        DisplError.setFont(new Font("Dialog", Font.BOLD, 12));
 337        DisplError.setForeground(new Color(16711680));
 338        DisplError.setBackground(new Color(0));
 339        add(DisplError);
 340
 341
 342        Clicked_Clear();			//calls the Clicked_Clear method (C button)
 343
 344    }	//end of calc init method
 345
 346    /* The following integer values are being set as "final" because
 347     * they are going to be used for determining what button has been
 348     * pushed
 349     */
 350    public final static int OpMinus = 11,  OpMultiply = 12,  OpPlus = 13,  OpDivide = 15,  OpMPlus = 19,  OpMClear = 20,  OpMR = 21;
 351
 352    /* This method is called whenever anything needs to be displayed
 353     * in the error message field at the bottom of the calculator,
 354     * accepting a String as an argument
 355     */
 356    void DisplayError(String err_msg) {
 357        /* calls the setText method of the Label DisplError, sending
 358         * whatever string it received initially
 359         */
 360        DisplError.setText(err_msg);
 361    }
 362
 363    /* This method is called whenever a numeric button (0-9) is pushed. */
 364    public void NumericButton(int i) {
 365        DisplayError(" ");			//Clears the error message field
 366
 367        /* Declares a String called Display that will initialize to whatever
 368         * is currently displayed in the lcdDisplay of the calculator
 369         */
 370        String Display = lcdDisplay.getText();
 371
 372        /* Checks if an operator key has just been pressed, and if it has,
 373         * then the limit of 20 digits will be reset for the user so that
 374         * they can enter in up to 20 new numbers
 375         */
 376        if (OperatorKey == true) {
 377            Counter = 0;
 378        }
 379
 380        Counter = Counter + 1;		//increments the counter
 381
 382        /* This is a condition to see if the number currently displayed is zero OR
 383         * an operator key other than +, -, *, or / has been pressed.
 384         */
 385        if ((Display.equals("0")) || (Status.equals("FIRST"))) {
 386            Display = "";			//Do not display any new info
 387        }
 388
 389        if (Counter < 21) //if more than 20 numbers are entered
 390        {
 391            //The number just entered is appended to the string currently displayed
 392            Display = Display + String.valueOf(i);
 393        } else {
 394            //call the DisplayError method and send it an error message string
 395            DisplayError("Digit Limit of 20 Digits Reached");
 396        }
 397
 398        lcdDisplay.setText(Display);	//sets the text of the lcdDisplay
 399        //Label
 400
 401        Status = "VALID";				//sets the Status string to valid
 402        OperatorKey = false;			//no operator key was pressed
 403        FunctionKey = false;			//no function key was pressed
 404    }
 405
 406    /* This method is called whenever an operator button is pressed, and is
 407     * sent an integer value representing the button pressed.
 408     */
 409    public void OperatorButton(int i) {
 410        DisplayError(" ");			//Clears the error message field
 411
 412        /* Creates a new Double object with the specific purpose of retaining
 413         * the string currently on the lcdDisplay label, and then immediately
 414         * converts that string into a double-precision real number and then
 415         * gives that number to the variable Result.
 416         */
 417        Result = (new Double(lcdDisplay.getText())).doubleValue();
 418
 419        //If no operator key has been pressed OR a function has been pressed
 420        if ((OperatorKey == false) || (FunctionKey = true)) {
 421            switch (Operator) //depending on the operation performed
 422            {
 423                /* if the user pressed the addition button, add the two numbers
 424                 * and put them in double Result
 425                 */
 426                case OpPlus:
 427                    Result = Operand + Result;
 428                    break;
 429
 430                /* if the user pressed the subtraction button, subtract the two
 431                 * numbers and put them in double Result
 432                 */
 433                case OpMinus:
 434                    Result = Operand - Result;
 435                    break;
 436
 437                /* if the user pressed the multiplication button, multiply
 438                 * the two numbers and put them in double Result
 439                 */
 440                case OpMultiply:
 441                    Result = Result * Operand;
 442                    break;
 443
 444                /* if the user pressed the division button, check to see if
 445                 * the second number entered is zero to avoid a divide-by-zero
 446                 * exception
 447                 */
 448                case OpDivide:
 449                    if (Result == 0) {
 450                        //set the Status string to indicate an
 451                        //an error
 452                        Status = "ERROR";
 453
 454                        //display the word "ERROR" on the
 455                        //lcdDisplay label
 456                        lcdDisplay.setText("ERROR");
 457
 458                        /* call the DisplayError method and
 459                         * send it a string indicating an error
 460                         * has occured and of what type
 461                         */
 462                        DisplayError("ERROR: Division by Zero");
 463                    } else {
 464                        //divide the two numbers and put the
 465                        //answer in double Result
 466                        Result = Operand / Result;
 467                    }
 468            }
 469
 470            //if after breaking from the switch the Status string is not set
 471            //to "ERROR"
 472            if (!Status.equals("ERROR")) {
 473                Status = "FIRST";	/* set the Status string to "FIRST" to
 474                 * indicate that a simple operation was
 475                 * not performed
 476                 */
 477
 478                Operand = Result;	//Operand holds the value of Result
 479
 480                Operator = i;		/* the integer value representing the
 481                 * operation being performed is stored
 482                 * in the integer Operator
 483                 */
 484
 485                //The lcdDisplay label has the value of double Result
 486                //displayed
 487                lcdDisplay.setText(String.valueOf(Result));
 488
 489                //The boolean decimal flag is set false, indicating that the
 490                //decimal button has not been pressed
 491                DecimalFlag = false;
 492
 493                //The boolean sign flag is set false, indicating that the sign
 494                //button has not been pressed
 495                SignFlag = false;
 496
 497                //The boolean OperatorKey is set true, indicating that a simple
 498                //operation has been performed
 499                OperatorKey = true;
 500
 501                //The boolean FunctionKey is set false, indicating that a
 502                //function key has not been pressed
 503                FunctionKey = false;
 504
 505                DisplayError(" ");		//Clears the error message field
 506            }
 507
 508        }
 509
 510    }			//end of OperatorButton method
 511
 512    /* This is a method that is called whenever the decimal button is
 513     * pressed.
 514     */
 515    public void DecimalButton() {
 516        DisplayError(" ");		//Clears the error message field
 517
 518        /* Declares a String called Display that will initialize to whatever
 519         * is currently displayed in the lcdDisplay of the calculator
 520         */
 521        String Display = lcdDisplay.getText();
 522
 523        //if a simple operation was performed successfully
 524        if (Status .equals("FIRST")) {
 525            Display = "0";		//set Display string to character 0
 526        }
 527
 528        //If the decimal button has not already been pressed
 529        if (!DecimalFlag) {
 530            //appends a decimal to the string Display
 531            Display = Display + ".";
 532        } else {
 533            /* calls the DisplayError method, sending a string
 534             * indicating that the number already has a decimal
 535             */
 536            DisplayError("Number already has a Decimal Point");
 537        }
 538
 539        /* calls the setText method of the Label lcdDisplay and
 540         * sends it the string Display
 541         */
 542        lcdDisplay.setText(Display);
 543        DecimalFlag = true;		//the decimal key has been pressed
 544        Status = "VALID";		/* Status string indicates a valid
 545         * operation has been performed
 546         */
 547        OperatorKey = false;    //no operator key has been pressed
 548
 549    }	//end of the DecimalButton method
 550
 551    /* This method is called whenever the percent button is pressed
 552     */
 553    void PercentButton() {
 554        DisplayError(" ");		//clears the error message field
 555
 556        /* Declares a String called Display that will initialize to whatever
 557         * is currently displayed in the lcdDisplay of the calculator
 558         */
 559        String Display = lcdDisplay.getText();
 560
 561        /* if the Status string is not set to "FIRST" OR the Display string
 562         * does not currently hold the value "0"
 563         */
 564        if ((!Status.equals("FIRST")) || (!Display.equals("0"))) {
 565            /* Creates a new Double object with the specific purpose of retaining
 566             * the string currently on the lcdDisplay label, and then immediately
 567             * converts that string into a double-precision real number and then
 568             * gives that number to the variable Result.
 569             */
 570            Result = (new Double(lcdDisplay.getText())).doubleValue();
 571
 572            //divide the double Result by 100, getting the percentage
 573            Result = Result / 100;
 574
 575            /* call the setText method of Label lcdDisplay and send it the string
 576             * that represents the value in Result
 577             */
 578            lcdDisplay.setText(String.valueOf(Result));
 579            Status = "FIRST";		//
 580            OperatorKey = true;		//an operator key has been pressed
 581            FunctionKey = true;		//a function key has been pressed
 582        }
 583    }	//end of the PercentButton method
 584
 585    /* This method is called first when the calculator is initialized
 586     * with the init() method, and is called every time the "C" button
 587     * is pressed
 588     */
 589    void Clicked_Clear() {
 590        Counter = 0;				//sets the counter to zero
 591        Status = "FIRST";			//sets Status to FIRST
 592        Operand = 0;				//sets Operand to zero
 593        Result = 0;					//sets Result to zero
 594        Operator = 0;				//sets Operator integer to zero
 595
 596        DecimalFlag = false;		//decimal button has not been
 597        //pressed
 598
 599        SignFlag = false;			//sign button has not been pressed
 600
 601        OperatorKey = false;		//no operator button has been
 602        //pressed
 603
 604        FunctionKey = false;		//no function button has been
 605        //pressed
 606
 607        /* calls the setText method of Label lcdDisplay and sends
 608         * it the character "0"
 609         */
 610        lcdDisplay.setText("0");
 611        DisplayError(" ");			//clears the error message field
 612    }
 613
 614    /* This method is called whenever the sign button is pressed */
 615    void PlusMinusButton() {
 616        DisplayError(" ");			//clears the error message field
 617
 618        /* Declares a String called Display that will initialize to whatever
 619         * is currently displayed in the lcdDisplay of the calculator
 620         */
 621        String Display = lcdDisplay.getText();
 622
 623        /* if Status is not set to FIRST and the Display string does not
 624         * hold the value "0"
 625         */
 626        if ((!Status.equals("FIRST")) || (!Display.equals("0"))) {
 627            /* Creates a new Double object with the specific purpose of retaining
 628             * the string currently on the lcdDisplay label, and then immediately
 629             * converts that string into a double-precision real number and then
 630             * gives that number to the variable Result.
 631             */
 632            Result = (new Double(lcdDisplay.getText())).doubleValue();
 633
 634            //sets the double Result to it's negative value
 635            Result = -Result;
 636
 637            /* call the setText method of Label lcdDisplay and send it the string
 638             * that represents the value in Result
 639             */
 640            lcdDisplay.setText(String.valueOf(Result));
 641            Status = "VALID";		//sets Status string to VALID
 642            SignFlag = true;		//the sign button has been pressed
 643            DecimalFlag = true;		//a decimal has appeared
 644        }
 645    }	//end of the PlusMinusButton method
 646
 647    /* This method is called whenever the square button is pressed */
 648    void SqrButton() {
 649        DisplayError(" ");			//clears the error message field
 650
 651        /* Declares a String called Display that will initialize to whatever
 652         * is currently displayed in the lcdDisplay of the calculator
 653         */
 654        String Display = lcdDisplay.getText();
 655
 656        /* if Status is not set to FIRST and the Display string does not
 657         * hold the value "0"
 658         */
 659        if ((!Status.equals("FIRST")) || (!Display.equals("0")) ){
 660            /* Creates a new Double object with the specific purpose of retaining
 661             * the string currently on the lcdDisplay label, and then immediately
 662             * converts that string into a double-precision real number and then
 663             * gives that number to the variable Result.
 664             */
 665            Result = (new Double(lcdDisplay.getText())).doubleValue();
 666
 667            /* multiply the double Result by itself, effectively squaring
 668             * the number */
 669            Result = Result * Result;
 670
 671            /* call the setText method of Label lcdDisplay and send it the string
 672             * that represents the value in Result
 673             */
 674            lcdDisplay.setText(String.valueOf(Result));
 675
 676            Status = "FIRST";		//indicates this is the first time
 677            //this button has been pressed
 678
 679            OperatorKey = true;		//an operator button has been pressed
 680            FunctionKey = true;		//a function button has been pressed
 681        }
 682    }	//end of the SqrButton method
 683
 684    /* This method is called whenever the square button is pressed */
 685    void SqrRootButton() {
 686        DisplayError(" ");			//clears the error message field
 687
 688        /* Declares a String called Display that will initialize to whatever
 689         * is currently displayed in the lcdDisplay of the calculator
 690         */
 691        String Display = lcdDisplay.getText();
 692
 693        /* if Status is not set to FIRST and the Display string does not
 694         * hold the value "0"
 695         */
 696        if ((!Status.equals("FIRST")) || (!Display.equals( "0"))) {
 697            /* Creates a new Double object with the specific purpose of retaining
 698             * the string currently on the lcdDisplay label, and then immediately
 699             * converts that string into a double-precision real number and then
 700             * gives that number to the variable Result.
 701             */
 702            Result = (new Double(lcdDisplay.getText())).doubleValue();
 703
 704            /* Makes a call to the Math class method "sqrt", which produced the
 705             * square root and stores the value in Result
 706             */
 707            Result = Math.sqrt(Result);
 708
 709            /* call the setText method of Label lcdDisplay and send it the string
 710             * that represents the value in Result
 711             */
 712            lcdDisplay.setText(String.valueOf(Result));
 713
 714            Status = "FIRST";		//indicates this is the first time
 715            //this button has been pressed
 716
 717            OperatorKey = true;		//an operator button has been pressed
 718            FunctionKey = true;		//a function button has been pressed
 719        }
 720    }	//end of the SqrRootButton method
 721
 722    /* This method is called whenever the OneOverXButton is pressed */
 723    void OneOverXButton() {
 724        DisplayError(" ");			//clears the error message field
 725
 726        /* Declares a String called Display that will initialize to whatever
 727         * is currently displayed in the lcdDisplay of the calculator
 728         */
 729        String Display = lcdDisplay.getText();
 730
 731        /* if Status is not set to FIRST and the Display string does not
 732         * hold the value "0"
 733         */
 734        if ((!Status.equals( "FIRST")) || (!Display .equals("0"))) {
 735            /* Creates a new Double object with the specific purpose of retaining
 736             * the string currently on the lcdDisplay label, and then immediately
 737             * converts that string into a double-precision real number and then
 738             * gives that number to the variable Result.
 739             */
 740            Result = (new Double(lcdDisplay.getText())).doubleValue();
 741
 742            //divides one by the double Result
 743            Result = 1 / Result;
 744
 745            /* call the setText method of Label lcdDisplay and send it the string
 746             * that represents the value in Result
 747             */
 748            lcdDisplay.setText(String.valueOf(Result));
 749
 750            Status = "FIRST";		//indicates that this is the first time
 751            //this button has been pressed
 752
 753            OperatorKey = true;		//an operator key has been pressed
 754            FunctionKey = true;		//a function key has been pressed
 755        }
 756    }	//end of the OneOverXButton method
 757
 758    /* This method is called whenever one of the three memory buttons is pressed,
 759     * accepting an integer representing the button pressed as an argument
 760     */
 761    void MemoryButton(int i) {
 762        DisplayError(" ");			//clears the error message field
 763
 764        /* Declares a String called Display that will initialize to whatever
 765         * is currently displayed in the lcdDisplay of the calculator
 766         */
 767        String Display = lcdDisplay.getText();
 768
 769        //depending on the value sent representing the buttons
 770        switch (i) {
 771            /* if the M+ button is pressed, check if the Display string has the value
 772             * "0." OR just "0", but along with the second condition check if double
 773             * Mem holds the value zero
 774             */
 775            case OpMPlus:
 776                if (((Display.equals("0.")) || (Display.equals("0"))) && (Mem == 0)) {
 777                    //clears the LabelMem label
 778                    LabelMem.setText(" ");
 779                } else {
 780                    /* Creates a new double variable called temp, which accepts the
 781                     * double value of a new Double object retaining the string
 782                     * currently on the lcdDisplay label, and then immediately
 783                     * converts that string into a double-precision real number
 784                     */
 785                    double temp = (new Double(lcdDisplay.getText())).doubleValue();
 786
 787                    //set the Mem variable with that the sum of what's currently in
 788                    //Mem and the temp variable
 789                    Mem = Mem + temp;
 790
 791                    /* calls the setText method of the Label LabelMem, sending
 792                     * it the character "M"
 793                     */
 794                    LabelMem.setText("M");
 795                }
 796                break;
 797
 798            /* if the MR button is pressed, call the setText method of Label lcdDisplay,
 799             * sending it the string that represents what the value of Mem is
 800             */
 801            case OpMR:
 802                lcdDisplay.setText(String.valueOf(Mem));
 803                break;
 804
 805            /* if the MC button is pressed, set the Mem variable to zero */
 806            case OpMClear:
 807                Mem = 0;
 808
 809                //clears the LableMem field
 810                LabelMem.setText(" ");
 811                break;
 812        }
 813        Status = "FIRST";			//indicates that this is first time
 814        //button has been pressed
 815
 816        OperatorKey = true;			//an operator button has been pressed
 817
 818        //if the Mem variable has the value zero
 819        if (Mem == 0) {
 820            LabelMem.setText(" ");	//clear the LabelMem field
 821        }
 822    }	//end of the MemoryButton method
 823
 824    /* This was left commented out so that a shell would be ready if needed
 825    public void paint(Graphics g)
 826    {
 827
 828    }*/
 829    /***************************************************************************
 830     *
 831     * This is the overridden update method for this applet -- update is used to
 832     * eliminate the flicker that happens with repainting an image from scratch.
 833     *
 834     **************************************************************************/
 835//    public void update(Graphics g) {
 836//        /* The format of the coordinates below is:
 837//         *		x position on the applet
 838//         *		y position on the applet
 839//         *		width of the component
 840//         *		height of the component
 841//         */
 842//
 843//        //Sets the color to white and draw a rectangle around the applet
 844//        g.setColor(Color.white);
 845//        g.drawRect(0, 0, 420, 368);
 846//
 847//        //sets the color to black and draws lines down the east and south sides
 848//        //of the applet to make it appear three-dimensional
 849//        g.setColor(Color.black);
 850//        g.drawLine(419, 0, 419, 367);
 851//        g.drawLine(0, 367, 419, 367);
 852//
 853//        //sets the color to gray and draws lines right next to the black lines
 854//        //to enhance the three-dimensional effect
 855//        g.setColor(Color.gray);
 856//        g.drawLine(418, 1, 418, 366);
 857//        g.drawLine(1, 366, 418, 366);
 858//
 859//        //sets the color to gray and draws a rectangle around the lcdDisplay
 860//        //label to give it some depth
 861//        g.setColor(Color.gray);
 862//        g.drawRect(41, 14, 329, 31);
 863//
 864//        //sets the color to yellow and draws a rectangle around the DisplError
 865//        //label to give it some importance and help attract the eye to errors
 866//        //displayed there
 867//        g.setColor(Color.yellow);
 868//        g.drawRect(41, 277, 330, 16);
 869//
 870//
 871//    }
 872
 873    /************************************************************************
 874    /*
 875    /* This method is called whenever an action is performed on the applet,
 876    /* specifically whenever one of the buttons on the applet is pressed
 877    /*
 878     * /***********************************************************************
 879     * @param event 
 880     */
 881    public void actionPerformed(ActionEvent event) {
 882        /* Declares an object named "src" to represent the event.getSource()
 883         * method. This shortens the code that follows.
 884         */
 885        Object src = event.getSource();
 886
 887        /* Checks to see if the source of the event which src is representing
 888         * is a Button and can behave like a Button
 889         */
 890        if (src instanceof JButton) {
 891            /* Checks to see if Status string holds the value ERROR OR if the
 892             * source of the event is the buttonClear ("C") button
 893             */
 894            if ((!Status.equals( "ERROR")) || (src.equals(buttonClear))) {
 895                /* The following conditions check for numeric buttons that have
 896                 * been pressed, and then called the method NumericButton and
 897                 * send it an integer value depending on the value of the button
 898                 * pressed
 899                 */
 900                if (src == button1) {
 901                    NumericButton(1);
 902                }
 903                if (src == button2) {
 904                    NumericButton(2);
 905                }
 906                if (src == button3) {
 907                    NumericButton(3);
 908                }
 909                if (src == button4) {
 910                    NumericButton(4);
 911                }
 912                if (src == button5) {
 913                    NumericButton(5);
 914                }
 915                if (src == button6) {
 916                    NumericButton(6);
 917                }
 918                if (src == button7) {
 919                    NumericButton(7);
 920                }
 921                if (src == button8) {
 922                    NumericButton(8);
 923                }
 924                if (src == button9) {
 925                    NumericButton(9);
 926                }
 927                if (src == button0) {
 928                    NumericButton(0);
 929                }
 930
 931                /* The following conditions check for operator buttons that have
 932                 * been pressed, and then call the OperatorButton method, sending
 933                 * the corresponding integer value
 934                 */
 935                if (src == buttonMinus) {
 936                    OperatorButton(11);
 937                }
 938                if (src == buttonMultiply) {
 939                    OperatorButton(12);
 940                }
 941                if (src == buttonPlus) {
 942                    OperatorButton(13);
 943                }
 944                if (src == buttonEquals) {
 945                    OperatorButton(14);
 946                }
 947                if (src == buttonDivide) {
 948                    OperatorButton(15);
 949                }
 950
 951                /* This condition checks if the decimal button has been pressed,
 952                 * and then calls the DecimalButton method
 953                 */
 954                if (src == buttonDecimal) {
 955                    DecimalButton();
 956                }
 957
 958                /* This condition checks if the percent button has been pressed,
 959                 * and then calls the PercentButton method
 960                 */
 961                if (src == buttonPercent) {
 962                    PercentButton();
 963                }
 964
 965                /* This condition checks if the clear button has been pressed,
 966                 * and then calls the Clicked_Clear method
 967                 */
 968                if (src == buttonClear) {
 969                    Clicked_Clear();
 970                }
 971
 972                /* This condition checks if the Plus Minus Button has been
 973                 * pressed, and then calls the PlusMinusButton method
 974                 */
 975                if (src == buttonNegative) {
 976                    PlusMinusButton();
 977                }
 978
 979                /* This condition checks if the Square Button has been pressed,
 980                 * and then calls the SqrButton method
 981                 */
 982                if (src == buttonSqr) {
 983                    SqrButton();
 984                }
 985
 986                /* This condition checks if the Square Root Button has been
 987                 * pressed, and then calls the SqrRootButton method
 988                 */
 989                if (src == buttonSqrRoot) {
 990                    SqrRootButton();
 991                }
 992
 993                /* This condition checks if the One over X Button has been
 994                 * pressed, and then calls the OneOverXButton method
 995                 */
 996                if (src == buttonOneOverX) {
 997                    OneOverXButton();
 998                }
 999
1000                /* These conditions checks to see if any of the Memory Buttons
1001                 * have been pressed, and then calls the MemoryButton method and
1002                 * sends them the corresponding integer values
1003                 */
1004                if (src == buttonMPlus) {
1005                    MemoryButton(19);
1006                }
1007
1008                if (src == buttonMClear) {
1009                    MemoryButton(20);
1010                }
1011
1012                if (src == buttonMR) {
1013                    MemoryButton(21);
1014                }
1015            }
1016        }
1017    }			//end of actionPerformed method
1018
1019    /****************************************************************************
1020     *
1021     * The following method handles all of the key events that occurs when a user
1022     * enters values from the keyboard. It accepts an Event object and an integer
1023     * representing the key pressed as arguments.
1024     *
1025     * It should be noted that in order to accept keys entered to the lcdDisplay,
1026     * the applet must be in focus.
1027     *
1028     **************************************************************************
1029     * @param evt
1030     * @param key
1031     */
1032  
1033    public void keyD(KeyEvent evt, char key) {
1034        //assigns key value to currchar through typecasting key to a character
1035        currchar = key;
1036
1037        /* The following conditions check to see if a number key has been pressed
1038         * and calls the NumericButton method, sending it the appropriate integer
1039         * as an argument
1040         */
1041        if (currchar == '0') {
1042            NumericButton(0);
1043        }
1044        if (currchar == '1') {
1045            NumericButton(1);
1046        }
1047        if (currchar == '2') {
1048            NumericButton(2);
1049        }
1050        if (currchar == '3') {
1051            NumericButton(3);
1052        }
1053        if (currchar == '4') {
1054            NumericButton(4);
1055        }
1056        if (currchar == '5') {
1057            NumericButton(5);
1058        }
1059        if (currchar == '6') {
1060            NumericButton(6);
1061        }
1062        if (currchar == '7') {
1063            NumericButton(7);
1064        }
1065        if (currchar == '8') {
1066            NumericButton(8);
1067        }
1068        if (currchar == '9') {
1069            NumericButton(9);
1070        }
1071
1072        /* The following conditions check to see if any of the operator keys
1073         * which are on the number pad have been pressed. It then calls the
1074         * OperatorButton method, sending it the appropriate integer value as
1075         * an argument.
1076         *
1077         * It should be noted that the main functions checked for are those on
1078         * the number pad, but if they are pressed at other occurences on the
1079         * keyboard, they are still accepted.
1080         */
1081        if (currchar == '/') {
1082            OperatorButton(15);
1083        }
1084        if (currchar == '*') {
1085            OperatorButton(12);
1086        }
1087        if (currchar == '-') {
1088            OperatorButton(11);
1089        }
1090        if (currchar == '+') {
1091            OperatorButton(13);
1092        }
1093
1094        if (currchar == '=') {
1095            OperatorButton(14);
1096        }
1097
1098        //This condition is different from the others because it tests for the
1099        //Enter key being pressed, and this is pre-built into the Event evt
1100        //object
1101        if (currchar == Event.ENTER) {
1102            OperatorButton(14);
1103        }
1104
1105        /* This checks for the decimal key being pressed, either on the number
1106         * pad or the period key, and calls the DecimalButton method.
1107         */
1108        if (currchar == '.') {
1109            DecimalButton();
1110        }
1111
1112    }
1113
1114    public void keyTyped(KeyEvent evt) {
1115       keyD(evt, evt.getKeyChar());
1116    }
1117
1118    public void keyPressed(KeyEvent e) {
1119       
1120    }
1121
1122    public void keyReleased(KeyEvent e) {
1123       
1124    }
1125}