advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

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;
   }
}

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Microsoft RIA Development Center
 IBM Rational Resource Center
 Destination .NET
XML error: not well-formed (invalid token) at line 33
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Linux Player Xandros Grabs Storied Rival Linspire
Hey Enterprise: Here Comes the 3G iPhone
MySpace Opens Profile Portability API
Microsoft Jumps Into Virtualization Fray
Eclipse Ganymede Makes It Easier for Devs
Open Source Nokia a Threat to Microsoft, Google?
Salesforce, Google Head for 2nd on Apps
HP Open Sources Unix File System for Linux
Red Hat Opens Its Network to Space
Novell: openSUSE for All Linux Users

Beyond XML and JSON: YAML for Java Developers
Mastering the Windows Mobile Emulators
Avaya AE Services Provide Rapid Telephony Integration with Facebook
Featured Algorithm: Intel Threading Building Blocks: parallel_reduce
Getting Started with Windows Live Admin Center
Eight Key Practices for ASP.NET Deployment
Java ME User Interfaces: Do It with LWUIT!
Talking VPro: Transcript
Bringing Semantic Technology to the Enterprise
Persisting Data in Your Windows Mobile Device

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES