★ wanayoo — archive 1999 http://javaboutique.internet.com/PocketCalc/source.htmlNouvelle recherche | Portail wanayoo
internet.com logo The Java Boutique

 APPLETS
by Category
by Date
by Name
Hall of Fame
Archive
Submit
Source Code
Servlets and JSP
 ARTICLES
Tutorials
Reviews
Glossary
 FORUM
FAQ
Users Poll
Discussion Group
Contact Us
 OTHER RESOURCES
Custom Applets
Java@Work
Java News
Jini Watch
Affiliate Programs
Top Searches
chat - counter
menu - countdown

internet.com
Internet News
Internet Stocks/VC
Internet Technology
Windows Internet Tech.
Linux/Open Source
Web Developer
ECommerce/Marketing
ISP Resources
ASP Resources
Wireless Internet
Downloads
Internet Resources
Internet Lists
International

Search internet.com
Advertising Info
Corporate Info

internet.commerce
Be a Commerce Partner
Computer Help
Survey your eMarket
Find a Consultant
Freelancer Exchange
Free virtual office
Get e-Biz Intell.
IT Products and Svcs
Barter Service
Hosting Services
Shopping Cart

WebDeveloper Network
BrowserWatch
Developer Forums
Developer News
JavaBoutique
JavaScriptSource
ScriptSearch
SearchEngineWatch
StreamingMediaWorld
The WDVL
WebDeveloper.com
WebDevelopersJournal WebReference.com
The XML Files
 [ Looking for a Site Map? ]

PocketCalc


/////////////////////////////////////////////////////////////////////
// 
// PocketCalc emulates a pocket calculator of the simplest
// kind. It was Mikael Bonnier's first Java program.
//
//     PocketCalc v1.0b1 is Freeware
//     Commercial Distribution Restricted                 
//     Copyright (C) 1995 by Mikael Bonnier, Lund, Sweden.
//
//
// It was developed using JDK-beta2 on Windows 95.
//
// Suggestions, improvements, and bug-reports
// are always welcome to:
//                  Mikael Bonnier
//                  Osten Undens gata 88
//                  S-227 62  LUND
//                  SWEDEN
//                             
// Or use my internet addresses:
//                  mikaelb@df.lth.se
//                  http://www.df.lth.se/~mikaelb
// 
// Mikael Bonnier
/////////////////////////////////////////////////////////////////////

import java.awt.*;
import java.applet.Applet;

public class PocketCalc extends Applet 
{
   TextField txtDisp;
   public final int OP_NONE = 0;
   public final int OP_ADD = 1;
   public final int OP_SUB = 2;
   public final int OP_MUL = 3;
   public final int OP_DIV = 4;
   public final int OP_NEG = 5;
   public final int OP_SQRT = 6;
   public final int OP_EQ = 7;
   public final int OP_C = 8;
   public final int OP_AC = 9;
   public final int DECSEP = -1;

   String msDecimal;
   int mnOp = OP_NONE;
   boolean mbNewNumber = true;
   boolean mbDecimal = false;
   double mdReg = 0.0;

   public void init() 
   {
      CalcButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
      CalcButton btnDecSep, btnNeg, btnSqrt, btnPlus, btnMinus;
      CalcButton btnTimes, btnDiv, btnEqual, btnClear, btnAllClear;

      setLayout(null);
      setFont(new Font("Helvetica", Font.PLAIN, 14));
      setBackground(new Color(0xFF, 0x80, 0x00));

      btn0 = new CalcButton("0", OP_NONE, 0);
      add(btn0);
      btn0.reshape(64, 144, 96, 24);

      btn1 = new CalcButton("1", OP_NONE, 1);
      add(btn1);
      btn1.reshape(64, 112, 40, 24);

      btn2 = new CalcButton("2", OP_NONE, 2);
      add(btn2);
      btn2.reshape(120, 112, 40, 24);

      btn3 = new CalcButton("3", OP_NONE, 3);
      add(btn3);
      btn3.reshape(176, 112, 40, 24);

      btn4 = new CalcButton("4", OP_NONE, 4);
      add(btn4);
      btn4.reshape(64, 80, 40, 24);

      btn5 = new CalcButton("5", OP_NONE, 5);
      add(btn5);
      btn5.reshape(120, 80, 40, 24);

      btn6 = new CalcButton("6", OP_NONE, 6);
      add(btn6);
      btn6.reshape(176, 80, 40, 24);

      btn7 = new CalcButton("7", OP_NONE, 7);
      add(btn7);
      btn7.reshape(64, 48, 40, 24);

      btn8 = new CalcButton("8", OP_NONE, 8);
      add(btn8);
      btn8.reshape(120, 48, 40, 24);

      btn9 = new CalcButton("9", OP_NONE, 9);
      add(btn9);
      btn9.reshape(176, 48, 40, 24);	

      btnDecSep = new CalcButton("·", OP_NONE, DECSEP);
      add(btnDecSep);
      btnDecSep.reshape(176, 144, 40, 24);	

      btnNeg = new CalcButton("+/-", OP_NEG, 0);
      add(btnNeg);
      btnNeg.reshape(8, 48, 40, 24);	

      btnSqrt = new CalcButton("Sqrt", OP_SQRT, 0);
      add(btnSqrt);
      btnSqrt.reshape(8, 80, 40, 24);	

      btnPlus = new CalcButton("+", OP_ADD, 0);
      add(btnPlus);
      btnPlus.reshape(232, 112, 40, 56);	

      btnMinus = new CalcButton("-", OP_SUB, 0);
      add(btnMinus);
      btnMinus.reshape(288, 112, 40, 24);	

      btnTimes = new CalcButton("×", OP_MUL, 0);
      add(btnTimes);
      btnTimes.reshape(232, 80, 40, 24);	

      btnDiv = new CalcButton("÷", OP_DIV, 0);
      add(btnDiv);
      btnDiv.reshape(288, 80, 40, 24);	

      btnEqual = new CalcButton("=", OP_EQ, 0);
      add(btnEqual);
      btnEqual.reshape(288, 144, 40, 24);	

      btnClear = new CalcButton("C", OP_C, 0);
      add(btnClear);
      btnClear.reshape(8, 112, 40, 24);	

      btnAllClear = new CalcButton("AC", OP_AC, 0);
      add(btnAllClear);
      btnAllClear.reshape(8, 144, 40, 24);	

      txtDisp = new TextField("0", 80);
      txtDisp.setEditable(false);
      add(txtDisp);
      txtDisp.reshape(64, 8, 268, 27);
      String sOneTenth = (new Double(0.1)).toString();
      msDecimal = sOneTenth.substring(sOneTenth.length()-2).substring(0, 1);
   }

   public static void main(String args[]) 
   {
      Frame frm = new Frame("Pocket Calculator");
      PocketCalc ex1 = new PocketCalc();
      ex1.init();
      frm.add("Center", ex1);
      frm.pack();
      frm.resize(395, 179);
      frm.show();
   }

   public void append(int nValue) 
   {
      String sDigit;

      if(nValue == DECSEP)
         if(!mbDecimal)
         {
            if(mbNewNumber)
            {
               txtDisp.setText("0");
               mbNewNumber = false;
            }
            mbDecimal = true;
            sDigit = msDecimal;
         }
         else
            return; 
      else
         sDigit = (new Integer(nValue)).toString();
      if(mbNewNumber)
      {
         txtDisp.setText(sDigit);
         mbNewNumber = false;
      }
      else
         txtDisp.setText(txtDisp.getText() + sDigit);
      repaint();
   }

   public void doOp(int nNewOp)
   {
      double dDisp;

      dDisp = (new Double(txtDisp.getText())).doubleValue();
      switch(nNewOp)
      {
      case OP_NEG:
      case OP_SQRT:
      case OP_C:
      case OP_AC:
         switch(nNewOp)
         {
         case OP_NEG:
            txtDisp.setText((new Double(-dDisp)).toString());
            break;
         case OP_SQRT:
            txtDisp.setText((new Double(Math.sqrt(dDisp))).toString());
            mbNewNumber = true;
            mbDecimal = false;
            break;
         case OP_C:
            mbNewNumber = true;
            mbDecimal = false;
            txtDisp.setText("0");
            break;
         case OP_AC:
            mnOp = OP_NONE;
            mbNewNumber = true;
            mbDecimal = false;
            mdReg = 0.0;
            txtDisp.setText("0");
            break;
         }
         break;
      case OP_ADD:
      case OP_SUB:
      case OP_MUL:
      case OP_DIV:
      case OP_EQ:
         switch(mnOp)
         {
         case OP_ADD:
            mdReg = mdReg + dDisp;
            break;
         case OP_SUB:
            mdReg = mdReg - dDisp;
            break;
         case OP_MUL:
            mdReg = mdReg * dDisp;
            break;
         case OP_DIV:
            mdReg = mdReg / dDisp;
            break;
         case OP_EQ:
         case OP_NONE:
            mdReg = dDisp;
            break;
         }
         mnOp = nNewOp;
         mbNewNumber = true;
         mbDecimal = false;
         txtDisp.setText((new Double(mdReg)).toString());
         break;
      }
   }
}

class CalcButton extends Button
{
   int mnOp;
   int mnValue;

   CalcButton(String sText, int nOp, int nValue)
   {
      super(sText);
      mnOp = nOp;
      mnValue = nValue;   
   }

   public boolean action(Event evt, Object arg) 
   {
      PocketCalc par = (PocketCalc)getParent();

      if(mnOp == par.OP_NONE)
         par.append(mnValue);
      else
      {
         par.doOp(mnOp);
      }
      return true;
   }
}


Browse More Great Applets ...


Applet Index
(sorted alphabetically)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z #s
The Java Source
(applets w/source code)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z


Let internet.com work for you!



Copyright © 1999 internet.com Corporation
All Rights Reserved. Legal Notices.