Results 1 to 3 of 3

Thread: java calculator

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2002
    Location
    Philippines
    Posts
    877

    java calculator

    im trying to compile this java calc code using sun wireless toolkit 2.5 but it gives me alot of errors

    see attachment

    thanks
    Attached Files Attached Files

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: java calculator

    Of course it will give you a lot of errors, because it's not JME code it's an Applet
    Code:
    import java.applet.* ;
    import java.util.* ;
    
    import java.awt.* ;
    import java.awt.event.* ;
    
    public class CalculatorE
        extends Applet
    {
    
      private boolean isApplet = false ;
    
      private boolean lastKeyWasFunction = true ;
    
      private double displayHold = 0 ;
    
      private Frame f ;
    
      private Hashtable theKeys = new Hashtable() ;
    
      private Label display ;
    
      private Panel pMain ;
    
      private String lastFunction = "" ;
    
      private Toolkit tk = null ;
    
      private KeyListener keyListener = new KeyListener() ;
    
      public static void main ( String[] args )
      {
    
        CalculatorE calc = new CalculatorE() ;
        calc.isApplet = false ;
        calc.tk = Toolkit.getDefaultToolkit() ;
    
        calc.f = new Frame( "Calculator" ) ;
        // add Window closer
        calc.f.addWindowListener( new WindowAdapter()
        {
          public void windowClosing ( WindowEvent e )
          {
    	System.exit( 0 ) ;
          }
          // close anon inner class
        } ) ;
    
        // add Key Listener to frame
        calc.f.addKeyListener( calc.keyListener ) ;
    
        // create panel which will be shared by Frame and Applet
        calc.pMain = new Panel() ;
        calc.pMain.setLayout( new BorderLayout() ) ;
        calc.pMain.setBackground( Color.lightGray ) ;
        calc.pMain.addKeyListener( calc.keyListener ) ;
    
        // add shared panel to Frame
        calc.f.add( calc.pMain, BorderLayout.CENTER ) ;
    
        calc.makeGUI() ;
      }
    
      public void init ()
      {
        isApplet = true ;
    
        setLayout( new BorderLayout() ) ;
        setBackground( Color.lightGray ) ;
        this.addKeyListener( keyListener ) ;
    
        // create the shared panel
        pMain = new Panel() ;
        pMain.setLayout( new BorderLayout() ) ;
        pMain.setBackground( Color.lightGray ) ;
        pMain.addKeyListener( keyListener ) ;
    
        // add the shared panel to the applet
        add( pMain, BorderLayout.CENTER ) ;
    
        makeGUI() ;
      }
    
      private void makeGUI ()
      {
    
        // add display
        display = new Label( "0", Label.RIGHT ) ;
        pMain.add( display, BorderLayout.NORTH ) ;
    
        Panel numPanel = new Panel() ;
        numPanel.setBackground( pMain.getBackground() ) ;
        numPanel.addKeyListener( keyListener ) ;
        numPanel.setLayout( new GridLayout( 4, 3 ) ) ;
    
        Panel funcPanel = new Panel() ;
        funcPanel.setBackground( numPanel.getBackground() ) ;
        funcPanel.addKeyListener( keyListener ) ;
        funcPanel.setLayout( new GridLayout( 4, 2 ) ) ;
    
        // create number buttons
        if ( true )
        {
          ButtonListener bl = new ButtonListener( 0 ) ;
    
          for ( int i = 0 ; i < 12 ; i++ )
          {
    	Button b = new Button( "" + i ) ;
    	b.setForeground( Color.blue ) ;
    	b.addActionListener( bl ) ;
    	b.addKeyListener( keyListener ) ;
    
    	// handle blank and '.' buttons
    	if ( i == 10 )
    	{
    	  b.setLabel( "+/-" ) ;
    	}
    	if ( i == 11 )
    	{
    	  b.setLabel( "." ) ;
    	}
    	theKeys.put( b.getLabel(), b ) ;
          }
        }
    
        // create function keys
        if ( true )
        {
          ButtonListener bl = new ButtonListener( 1 ) ;
    
          StringTokenizer st = new StringTokenizer( "/ C * CE + none - =", " " ) ;
          while ( st.hasMoreTokens() )
          {
    	Button b = new Button( st.nextToken() ) ;
    	b.setForeground( Color.red ) ;
    	b.addActionListener( bl ) ;
    	b.addKeyListener( keyListener ) ;
    
    	// disable unneded 'b' key
    	if ( b.getLabel().equals( "none" ) )
    	{
    	  b.setVisible( false ) ;
    	}
    	// add the key to theKeys array
    	theKeys.put( b.getLabel(), b ) ;
    	// add the key to the function panel
    	funcPanel.add( b ) ;
          }
        }
    
        // place number buttons on numPanel
        for ( int i = 1 ; i <= 9 ; i++ )
        {
          numPanel.add( ( Button ) theKeys.get( "" + i ) ) ;
        }
        numPanel.add( ( Button ) theKeys.get( "+/-" ) ) ;
        numPanel.add( ( Button ) theKeys.get( "0" ) ) ;
        numPanel.add( ( Button ) theKeys.get( "." ) ) ;
    
        pMain.add( numPanel, BorderLayout.CENTER ) ;
        pMain.add( funcPanel, BorderLayout.EAST ) ;
    
        if ( !isApplet )
        {
          f.pack() ;
          Dimension screensize = tk.getScreenSize() ;
          // set size
          f.setSize( screensize.width / 5, screensize.height / 4 ) ;
          // center form
          f.setLocation( ( screensize.width - f.getWidth() ) / 2,
    		     ( screensize.height - f.getHeight() ) / 2 ) ;
          f.setResizable( false ) ;
        }
    
        pMain.setVisible( true ) ;
        setVisible( true ) ;
    
        if ( isApplet == false )
        {
          f.setVisible( true ) ;
        }
      }
    
      private double getDisplayValue ()
      {
        double d = 0.0 ;
        String s = display.getText() ;
        try
        {
          // Double.parseDouble doesn't work in 1.1v applets
          d = Double.valueOf( s ).doubleValue() ;
        }
        catch ( NumberFormatException e )
        {
          d = 0.0 ;
        }
        return d ;
      }
    
      private void handleFuncKey ( Button b )
      {
        String lbl = b.getLabel() ;
    
        if ( "=CCE".indexOf( lbl ) > -1 )
        {
          if ( lbl.equals( "C" ) )
          {
    	displayHold = 0d ;
    	lastFunction = "" ;
    	display.setText( "0" ) ;
          }
          if ( lbl.equals( "CE" ) )
          {
    	display.setText( "0" ) ;
    	lastKeyWasFunction = true ;
    	return ;
          }
          if ( lbl.equals( "=" ) )
          {}
        }
    
        if ( lastFunction.equals( "+" ) )
        {
          display.setText( "" + ( displayHold + getDisplayValue() ) ) ;
        }
        if ( lastFunction.equals( "-" ) )
        {
          display.setText( "" + ( displayHold - getDisplayValue() ) ) ;
        }
        if ( lastFunction.equals( "*" ) )
        {
          display.setText( "" + ( displayHold * getDisplayValue() ) ) ;
        }
        if ( lastFunction.equals( "/" ) )
        {
          String r ;
          try
          {
    	r = "" + ( displayHold / getDisplayValue() ) ;
          }
          catch ( ArithmeticException ae )
          {
    	r = "divison error" ;
    	displayHold = 0d ;
          }
          display.setText( r ) ;
        }
    
        lastKeyWasFunction = true ;
        lastFunction = lbl ;
        displayHold = getDisplayValue() ;
      }
    
      private void handleNumKey ( Button b )
      {
        String lbl = b.getLabel() ;
    
        if ( lastKeyWasFunction )
        {
          display.setText( "" ) ;
        }
    
        if ( lbl.equals( "." ) || lbl.equals( "+/-" ) )
        {
          if ( lbl.equals( "." ) )
          {
    	if ( display.getText().indexOf( "." ) == -1 )
    	{
    	  display.setText( display.getText() + "." ) ;
    	}
          }
          else
          {
    	display.setText( "" + getDisplayValue() * -1 ) ;
          }
        }
        else
        {
          display.setText( display.getText() + lbl ) ;
        }
    
        lastKeyWasFunction = false ;
      }
    
      private class ButtonListener
          implements ActionListener
      {
        // buttonType == 0 : number key listener
        // buttonType == 1 : function key listener
        private int listenerType = 0 ;
    
        ButtonListener ( int listenerType )
        {
          this.listenerType = listenerType ;
        }
    
        public void actionPerformed ( ActionEvent e )
        {
          Button b = ( Button ) e.getSource() ;
    
          if ( listenerType == 0 )
          {
    	handleNumKey( b ) ;
          }
          else
          {
    	handleFuncKey( b ) ;
          }
        }
      }
    
      private class KeyListener
          extends KeyAdapter
      {
        // better to use Keystroke class, but it's not supported in v1.1
        private String lastKey = "" ;
    
        public void keyPressed ( KeyEvent e )
        {
          Button b = null ;
          String targ = "NUMPAD" ;
    
          String key = e.getKeyText( e.getKeyCode() ) ;
          key = key.toUpperCase() ;
    
          if ( key.indexOf( targ ) > -1 )
          {
    	key = key.substring( key.indexOf( targ ) + 1 + targ.length() ) ;
          }
    
          // handle SHIFTed keys
          if ( lastKey.equals( "SHIFT" ) )
          {
    	if ( key.equals( "8" ) )
    	{
    	  key = "*" ;
    	}
    	if ( key.equals( "=" ) )
    	{
    	  key = "+" ;
    	}
          }
    
          if ( key.equals( "ENTER" ) )
          {
    	key = "=" ;
          }
          if ( key.equals( "MINUS" ) )
          {
    	key = "-" ;
          }
    
          b = ( Button ) theKeys.get( key ) ;
    
          if ( b != null )
          {
    	if ( "0123456789.".indexOf( key ) > -1 )
    	{
    	  handleNumKey( b ) ;
    	}
    	else
    	{
    	  handleFuncKey( b ) ;
    	}
          }
    
          lastKey = key ;
        }
      }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: java calculator

    Short version: you can't compile just any arbitrary code using the ME. You have to specifically write applications for it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width