Sin, Cos, Tan problem

Hi i'm doing textbook problems, i'm practically new to java, and forgive my lack of math skills but I need help.
I need to write a program that prints the values of the function y=sin(x), z=cos(x), t=tan(x) for 'x' allowing the user to enter the starting degree, ending degree, and specifying the step increments. I'm not allowed to use math methods for calculations.
I can get most of it working just not the actual math calculations. Any help would be appreciated!

If you're not allowed to use the Math methods then you'll need to hand craft the sin, cos and tan functions yourself.
The Taylor Expansions for these babies could be the badgers you're after.
Try here for an introduction.
http://mathforum.org/library/drmath/view/53760.html

Similar Messages

  • Inverse sin cos and tan

    Hello,
    I was trying to use the the Math class in java but the sin cos and tan functions only take radians, not degrees and there doesn't seem to be any functions for finding the the inverse sin cos or tan. Is there another class for doing this or were sun just in a funny mood when they wrote it ?

    So you missed the methods "asin", "acos", "atan", "toRadians", and "toDegrees" in the API spec how?
    http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Math.html
    http://java.sun.com/j2se/1.4.1/docs/api/java/lang/StrictMath.html
    Note that the to* methods are fairly new additions to the API. Conversion between radians and degrees isn't hard anyway ... multip</B>r divide by 180/Math.PI.

  • ATAN2  geometric function and pasing DECFLOAT34 in Sin, cos and Tan.

    Hello,
    1) Is there any macro,Function module  or any way to use atan2 is a variation of the arctangent function in ABAP
    atan2 is available in C, C++, java but i don't seem to find it in ABAP.
    Further details : --- http://en.wikipedia.org/wiki/Atan2
    2) The current limitation for Sin , Cos and tan  geometric functions is Float.
    I am looking for a way to pass a double (DECFLOAT34) into SIn or Cos.
    I do not want to lose the accuracy by using Float.
    Can this be done ?
    Regards,
    Ajay Kulkarni

    Hello Alvaro,
    I have written the following code for atan2 accordingly but it is not as accurate as i wan't it to be.
    The results are not accurate.
    Regards,
    Ajay K
    method ATAN2.
      DATA :
           xsqr TYPE DECFLOAT34,
           ysqr TYPE DECFLOAT34,
           xysqr TYPE DECFLOAT34,
           xysqrt TYPE DECFLOAT34,
           tempa TYPE DECFLOAT34,
           tempb TYPE DECFLOAT34,
           tempc TYPE DECFLOAT34,
           x TYPE DECFLOAT34,
           y TYPE DECFLOAT34,
           atan2 TYPE f.
      data tempb_float TYPE f.
      data tempd_float TYPE f.
    x = iv_x.
    y = iv_y.
    if x = 0 and y = 0.
    ans not defined
    ELSE.
        xsqr = x * x .
        ysqr = y * y .
        xysqr = xsqr + ysqr.
        xysqrt = sqrt( xysqr ).
        tempa = xysqrt - x.
        tempb = tempa / y .
        tempb_float = tempb.
        tempd_float = atan( tempb_float ).
        tempc = 2 * tempd_float.
        ev_atan2 = tempc.
    ENDIF.
    endmethod.

  • Sin/cos

    hey I need my program to run faster and I used to just have sin/cos tables to get values from but I need more accuracy than that. However, I dont think I need as much accuracy as the built in java functions give. I forget the exact formula for it.. its something like 1+e^3/3!+e^5/5! etc.. does anyone know how far the java function goes?

    Don't bother writing your own trigonometric functions. If you think your program runs slower by calling them (and my guess is that you haven't tested that hypothesis yet), then just make a lookup table. Have entry 0 contain the sine of 0, entry 1 contain the sine of 0.01 (radians), and so on up to entry 628 containing the sine of 6.28 radians. This table of 629 entries covers the entire circle. You can fill in this table while your program is loading by calling the regular Math.sin method.
    Then when you need to calculate the sine of an angle, round it to the nearest 0.01 radians, multiply by 100, and use that integer as an array index to get the sine.

  • Native sin/cos functions

    hey I was curious how much faster I could make sin/cos functions at the expense of some accuracy. I used a taylor polynomial and found that to the 3rd degree, the results of my function were accurate to about 2 decimal points. By the 5th degree, they were pretty much perfect up till past the 8th decimal, unless youre looking for numbers close to pie/2, which is where the most inaccuracy occurs (still accurate to about the 6th decimal place). I tested this against the native Math.sin function, and found that mine was about twice as fast for 3rd degree, and about 1.5 times faster for 5th degree. However, I got some very strange results when I used large numbers in the calculations, where my results were 16X faster then the native ones. Does anyone know what they use to calculate sin and cos? heres my program and results:
    public class Math
         public static double PIE = 3.14159265358979323846;
         public static double sin(double x)
              if(abs(x) <= PIE)
                   return x-x*x*x/6+x*x*x*x*x/120-x*x*x*x*x*x*x/5040+x*x*x*x*x*x*x*x*x/362880;
              double t = x/PIE;
              x = PIE * (t-(int)t);
                   return x-x*x*x/6+x*x*x*x*x/120-x*x*x*x*x*x*x/5040+x*x*x*x*x*x*x*x*x/362880;
         public static double abs(double n)
              return n<0?-n:n;
         public static void main(String[] args)
              int trials = 10000000;
              long initTime = System.currentTimeMillis();
              long time1 = 0;
              long time2 = 0;
              for(int loop = 0; loop < trials; loop++)
                   java.lang.Math.sin(PIE/trials*loop*2);
              time1 = System.currentTimeMillis()-initTime;
              initTime = System.currentTimeMillis();
              for(int loop = 0; loop < trials; loop++)
                   sin(PIE/trials*loop*2);
              time2 = System.currentTimeMillis()-initTime;
              System.out.println("System runtime: "+time1+"\nMy runtime: "+time2);
    }System runtime: 2015
    My runtime: 1594
              for(int loop = 0; loop < trials; loop++)
                   java.lang.Math.sin(loop);
              for(int loop = 0; loop < trials; loop++)
                   sin(loop);
    ....System runtime: 16453
    My runtime: 1688
    I just thought that was a little strange and was curious if anyone knew what caused it?

         public static double PIE = 3.14159265358979323846;Math.PI isn't good enough for you?
    You can always look in the src.zip at the source code.
    Did you get these polynomial approximations out of Abramowitz and Stegun? If not, go find out what that is.
    Or are these Taylor series approximations that you derived for yourself? I don't think this is a good idea.
    How much expertise do you have in numerical analysis? Can you speak to convergence rate, roundoff, and errors in your formulation? If not, you shouldn't offer it as an alternative.
    I doubt that the folks who wrote the sine and cosine used in java.lang.Math were poor programmers. It's good for you to want to understand the details, but it's naive to think that your Taylor series approximation is such a radical improvement.
    %

  • Solaris 9 Fortran sin & cos

    We have a new server running Solaris 9.
    Our old server ran Solaris 6.
    We find that Fortran sin & cos values are now slightly different for given input values.
    Since both of the Solaris versions use IEEE arithmetic with the same rounding conventions, etc. what has changed??????

    We don't believe going from 32 bits to the 64 bits of Solaris 9 is relevant (and its only in addressing, not calculations - isn't it?).
    There are other differences as well, it was the f77 compiler on Solaris 6, now it is the f90 compiler with the -f77 flag.
    But the basic point is that with IEEE arithmetic and the same rounding conventions there is a correct real*4 value for sin(2.4504421).
    Using "WRITE(6,'(F10.7)') SIN(2.4504421)" on our old machine gave 0.63742417 (hex 3F232E3B) whereas our new machine gives 0.63742411 (hex 3F232E3A).
    One of these answers is WRONG - admittedly only in the final bit, but WRONG.

  • Sine wave frequency problem

    hello
    i have some part of my program that is not working well.
    the idea is to sample a 2V sine wave of various frequency and record down the max and min values
    by right i should get around +2 and -2 for my max and min values
    however at frequencies multiple of 5(5k,10k,15k...) i get a reading of 0.0628215 for my max and -0.0628215 for my min.all other frequencies have no such problem.
    i tried changing the sampling time and rate but i still get a close to 0 result for frequencies at multiples of 5.
    may i know what causes the problem and how can i resolve this?

    Hi
    It may sound silly - but do you acquire data fast enough?
    Just have a look at the Nyquist Theorem.
    Thomas
    Using LV8.0
    Don't be afraid to rate a good answer...

  • DrawOval( ) and Math.tan( ) problem

    Hello,
    I want to make a very simple animation: a circle moves with a 30 degree angle over the screen.
    Now my problem:
    When I calculate the new x and y coordinates for the circle I use the "Math.tan( )" method. But when I want to use those coordinates in the drawOval( ) method they should be INTEGERS. But then they will be rounded numbers and nothing will happen. (A horizontal animation will follow, because of the rounding of the numbers...). Can DOUBLES be used in a way in those draw methods? (So there won't be any loss of precision.)
    Thanks in advance...

    This is my source:
    import java.awt.*;
    import java.applet.*;
    public class Demo extends Applet
    public void paint(Graphics g)
    double x=10,y=300;
    for (int xadjust = 1;xadjust < 300;xadjust++)
    // a 45 degree angle
    x = x + xadjust;
    y = y - ((Math.tan((1/4)*Math.PI)) * x); // calculate new y coordinate
    g.drawOval( (int)x,(int)y,15,15);
    }

  • CoS template problem on DS5.2?

    Hi, Experts,
    I'm seeing this wierd problem that sometime ost of portal users just lost their portal attributes in LDAP. Here is our environment:
    Directory Server 5.2
    Access Manager 7.1
    Portal 7.1 u1
    And our Directory Server tree
    dc=company,dc=com -> o=ccc,dc=company,dc=com.
    We create filtered roles at o=ccc,dc=company,dc=com. Everytime a filtered role is created, portal service will be assigned to this role, which will create a CoS template at o=ccc,dc=company,dc=com level. We are doing the testing on the system and I have seen that portal attributes as virtual attributes at user level are lost for most of portal users. The effort I have tried to resolve the problem:
    1. Removed roles and CoS templates which has special characters. This once solved the problem. But the problem happened again after that. So it's not the only reason or root reason.
    2. Removed extra CoS templates. I found after the filtered roles were removed by an external tool, the corresponding CoS template still existed. So I removed the template manually. This once solved the problem. But again, it's not the only reason and the problem happened again after that.
    3. Shrink the number of filtered roles. I just removed most of the roles and corresponding costemplates. And it resolved the problem.
    The problem we are facing: I don't know why the problem happens and in exactly what situation it will happen. I could not re-produce the problem at this moment. But I'm afraid the problem will happen again after the system goes alive. The possible reason I can think of:
    Maybe that Directory Server has limitation on the number of CoS templates it can handle for each CoS definition?
    Again, I'm not sure if that is it. Has anybody seen this before?
    Thanks.

    You will need to contact Sun support organization to get a real help with this problem, as it is almost impossible to understand the Directory configuration and data without complete descriptions of the Roles, CoSTemplates and CosDefinitions (and the search requests that are working or are not working properly).
    Regards,
    Ludovic.

  • StrictMath sin & cos percision errors

    I was wondering if anyone is getting this problem too?
    double sinTheta=StrictMath.sin(StrictMath.PI);
    Now sinTheta should be 0, cuz sin(PI)= 0, in radians.
    But instead sinTheta is
    1.2246467991473532E-16.
    If anyone else is getting this, out of curiousity , how are you handling it? I was planning on making my own Math class which extends from StrictMath (if not then just a new class) which has a method sin(double angle), and if the value is very close to zero, then i will return a zero.
    Even though this all seems picky, I'm writing test harness for Matrix Rotation methods and they are failing only cuz StrictMath.sin is returning an inconsistent value.

    I was wondering if anyone is getting this problem
    too?
    double sinTheta=StrictMath.sin(StrictMath.PI);
    Now sinTheta should be 0, cuz sin(PI)= 0, in radians.
    But instead sinTheta is
    1.2246467991473532E-16.StrictMath.PI is not the actual number PI, but the closest IEEE floating point number to PI. Therefore, the sine of that number is not exactly zero. Floating point arithmetic is inherently imprecise and you cannot expect exact results from it.

  • I have a problem with my calculator :(

    hi all,
    i need some help with my calculator..after i wrote the code i discovered my mistake so i made some changes on the code..the problem is that it's still applying the old code !!
    ( i didn't forgot to compile it, & i closed all the browsers b4 trying it )
    i will appreciate ANY suggestion
    P.S : my mistake was with the operations buttons ( add, sub, multip, division ) & the equal button
    if it may help, this is the code ( the new one, then the old one )
    the new code:
    import java.awt.* ; // Container, FlowLayout
    import java.awt.event.* ; // ActionEvent, ActionListener
    import javax.swing.* ; // JApplet , JButton , JLabal, JTextField
    public class Calculator2 extends JApplet implements ActionListener {
    // graphical user interface components
    JTextField field ;
    JButton zero, one, two, three, four, five, six, seven, eight, nine, fraction, clear,
    add, sub, multip , division, equal ,sin , cos, tan ,log ,sqrt ,exp ;
    // variables
    String string = " " ; // to store what is in the text field
    String operation ; // to store the operation selected
    double operate ; // to store the result of the operation selected
    double operand1 , operand2 ; // the operands of the operation
    // set up GUI components
    public void init ()
    Container container = getContentPane ();
    container.setLayout ( new FlowLayout () );
    // create a text field
    field = new JTextField ( 17 );
    container.add ( field ) ;
    // create buttons
    clear = new JButton ( "C" ) ;
    clear.addActionListener ( this ) ;
    container.add ( clear );
    zero = new JButton ( "0" ) ;
    zero.addActionListener ( this ) ;
    container.add ( zero );
    one = new JButton ( "1" ) ;
    one.addActionListener ( this ) ;
    container.add ( one );
    two = new JButton ( "2" ) ;
    two.addActionListener ( this ) ;
    container.add ( two );
    three = new JButton ( "3" ) ;
    three.addActionListener ( this ) ;
    container.add ( three );
    four = new JButton ( "4" ) ;
    four.addActionListener ( this ) ;
    container.add ( four );
    five = new JButton ( "5" ) ;
    five.addActionListener ( this ) ;
    container.add ( five );
    six = new JButton ( "6" ) ;
    six.addActionListener ( this ) ;
    container.add ( six );
    seven = new JButton ( "7" ) ;
    seven.addActionListener ( this ) ;
    container.add ( seven );
    eight = new JButton ( "8" ) ;
    eight.addActionListener ( this ) ;
    container.add ( eight );
    nine = new JButton ( "9" ) ;
    nine.addActionListener ( this ) ;
    container.add ( nine );
    fraction = new JButton ( "." ) ;
    fraction.addActionListener ( this ) ;
    container.add ( fraction );
    add = new JButton ( "+" ) ;
    add.addActionListener ( this ) ;
    container.add ( add );
    sub = new JButton ( "-" ) ;
    sub.addActionListener ( this ) ;
    container.add ( sub );
    multip = new JButton ( "*" ) ;
    multip.addActionListener ( this ) ;
    container.add ( multip );
    division = new JButton ( "�" ) ;
    division.addActionListener ( this ) ;
    container.add ( division );
    sin = new JButton ( "sin" ) ;
    sin.addActionListener ( this ) ;
    container.add ( sin );
    cos = new JButton ( "cos" ) ;
    cos.addActionListener ( this ) ;
    container.add ( cos );
    tan = new JButton ( "tan" ) ;
    tan.addActionListener ( this ) ;
    container.add ( tan );
    log = new JButton ( "log" ) ;
    log.addActionListener ( this ) ;
    container.add ( log );
    sqrt = new JButton ( "sqrt" ) ;
    sqrt.addActionListener ( this ) ;
    container.add ( sqrt );
    exp = new JButton ( "exp" ) ;
    exp.addActionListener ( this ) ;
    container.add ( exp );
    equal = new JButton ( "=" ) ;
    equal.addActionListener ( this ) ;
    container.add ( equal );
    } // end of method init
    public void actionPerformed ( ActionEvent event )
         // button zero
         if ( event.getSource()== zero )
         string = string + "0" ;
         field.setText ( string ) ;
         // button one
         else if ( event.getSource()== one )
              string = string + "1" ;
              field.setText ( string ) ;
         // button two
         else if ( event.getSource()== two )
              string = string + "2" ;
              field.setText ( string ) ;
         // button three
         else if ( event.getSource()== three )
              string = string + "3" ;
              field.setText ( string ) ;
         // button four
         else if ( event.getSource()== four )
              string = string + "4" ;
              field.setText ( string ) ;
         // button five
         else if ( event.getSource()== five )
              string = string + "5" ;
              field.setText ( string ) ;
         // button six
         else if ( event.getSource()== six )
              string = string + "6" ;
              field.setText ( string ) ;
         // button seven
         else if ( event.getSource()== seven )
              string = string + "7" ;
              field.setText ( string ) ;
         // button eight
         else if ( event.getSource()== eight )
              string = string + "8" ;
              field.setText ( string ) ;
         // button nine
         else if ( event.getSource()== nine )
              string = string + "9" ;
              field.setText ( string ) ;
         // button fraction
         else if ( event.getSource()== fraction )
              string = string + "." ;
              field.setText ( string ) ;
         // button clear
         else if ( event.getSource()== clear )
              clear ();
         // button add
         else if ( event.getSource()== add )
              operand1 = Double.parseDouble ( string );
              operation = "+" ;
              clear ();
         // button sub
         else if ( event.getSource()== sub )
              operand1 = Double.parseDouble ( string );
              operation = "-" ;
              clear ();
         // button multip
         else if ( event.getSource()== multip )
              operand1 = Double.parseDouble ( string );
              operation = "*" ;
              clear ();
         // button division
         else if ( event.getSource()== division )
              operand1 = Double.parseDouble ( string );
              operation = "/" ;
              clear ();
         // button sin
         else if ( event.getSource()== sin )
              operate = Double.parseDouble ( string ) ;
              operate = Math.sin( operate );
              field.setText ( Double.toString ( operate ) ) ;
    // button cos
         else if ( event.getSource()== cos )
              operate = Double.parseDouble ( string ) ;
              operate = Math.cos( operate );
              field.setText ( Double.toString ( operate ) ) ;
         // button tan
         else if ( event.getSource()== tan )
              operate = Double.parseDouble ( string ) ;
              operate = Math.tan( operate );
              field.setText ( Double.toString ( operate ) ) ;
         // button log
         else if ( event.getSource()== log )
              operate = Double.parseDouble ( string ) ;
              operate = Math.log( operate );
              field.setText ( Double.toString ( operate ) ) ;
         // button sqrt
         else if ( event.getSource()== sqrt )
              operate = Double.parseDouble ( string ) ;
              operate = Math.sqrt( operate );
              field.setText ( Double.toString ( operate ) ) ;
         // button exp
         else if ( event.getSource()== exp )
              operate = Double.parseDouble ( string ) ;
              operate = Math.exp( operate );
              field.setText ( Double.toString ( operate ) ) ;
         // button equal
         else // if ( event.getSource()== equal )
              operand2 = Double.parseDouble ( string );
              if ( operation == "+" )
                   operate = operand1 + operand2 ;
              else if ( operation == "-" )
                   operate = operand1 - operand2 ;
              else if ( operation == "*" )
                   operate = operand1 * operand2 ;
              else if ( operation == "/" )
                   operate = operand1 / operand2 ;
              field.setText ( Double.toString ( operate ) ) ;
    } // end of method actionPerformed
    public void clear ()
         string = " ";
         field.setText ( string ) ;
    } // end of method clear
    } // end of class
    the old code which have the problem ( which i make the changes on ) :
         // button clear
         else if ( event.getSource()== clear )
              string = "";
              field.setText ( string ) ;
         // button add
         else if ( event.getSource()== add )
              string = string + "+" ;
              field.setText ( string ) ;
         // button sub
         else if ( event.getSource()== sub )
              string = string + "-" ;
              field.setText ( string ) ;
         // button multip
         else if ( event.getSource()== multip )
              string = string + "*" ;
              field.setText ( string ) ;
         // button division
         else if ( event.getSource()== division )
              string = string + "/" ;
              field.setText ( string ) ;
         // button equal
         else // if ( event.getSource()== equal )
              operate = Double.parseDouble ( string ) ;
              field.setText ( Double.toString ( operate ) ) ;
    thanks a lot :)

    Open Java console and press "x" (Clear cache)
    Disable caching in java paremeters.

  • Help Please I am new to programming Java1.3.1

    I am having the following problem with the appletviewer for the following code and screen print of error message. I would greatly aperciate any help i can get.Thanks Adera...
    my e-mail address is [email protected]
    Sorry the screen print will not copy into here so i will type it out.
    java.lang.ClassCastException:Calculator
    at sun.applet.AppletPanel.createApplet(AppletPanel.java:579)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java:515)
    at sun.applet.AppletPanel.run(AppletPanel.java:293)
    at java.lang.Thread.run(Thread.java:484)
    Here is my code..
    //Adera Currie Student Id# 31248
    //Course Title: Internet Programming
    //Due Date: August 13th, 2002
    //calculator.Java
    //Hilltop Library Calculator Java Project
    //import javax.swing.*;
    //import javax.swing.JOptionPane;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    //import java.awt.Container;
    public class Calculator extends JFrame implements ActionListener
    int chioce, i;
    //integer variable to hold stringnums in
    double calcone, calctwo;
    //variables for mouse clicks
    double numone, numtwo, total, holdnum, decimalCount;
    String calctot, calctot1, calctot2, negnum, holdnumonetwo;
    //declare text area
    TextField text1;
    //declare button array
    Button calcbutton[], clear;
    //string array to convert textfield values, string manipulation
    String textfield;
    String bpressedEvaluated;
    //declare panel object
    Panel bpanel,text2,Cpanel;
    //declare variable arrays to hold the name and numbers of the buttons that
    //will be created on the applet
    String bnames[] = {"7","8","9","/","Sin","4","5","6",
    "*","Cos","1","2","3","-","Tan",
    "0",".","=","+","Neg"};
    //arrays for holding the items to check for when the action click event is called
    String holdOperatorVisual[] = {"/","*","-","=","+"};
    String holdNumNames[] = {"0","1","2","3","4","5","6","7","8","9",".","Neg"};
    String holdSciFiVisuals[] = {"Sin","Cos","Tan"};
    //declare boolean values to keep track of numbers and operators either
    //in use or what should be terminated
    boolean operators,operators2;
    boolean firstnum;
    boolean secondnum;
    boolean numberpressed,decimalPressed,negPressed;
    public void init()
    //create the panels to hold applet objects
    bpanel = new Panel();
    text2 = new Panel();
    Cpanel = new Panel();
    //set up the applet layout for the buttons and such
    //BorderLayout layout = new BorderLayout(5,5);
    setLayout(new BorderLayout(5,5));
    //create a container object
    //Container c = this.getContentPane();
    //c.setLayout(new FlowLayout());
    //this.setLayout( new FlowLayout() );
    //set up the panels with layout manager
    bpanel.setLayout( new GridLayout (4,5));
    text2.setLayout(new GridLayout(1,1));
    Cpanel.setLayout(new GridLayout(1,1));
    //create my text field to hold the display for the user
    text1 = new TextField(20);
    //make it so the user cannot enter text from the keyboard
    text1.setEditable(false);
    text1.setBackground(Color.cyan);
    text2.add(text1);
    //add teh panel to the container
    add(text2, BorderLayout.NORTH);
    //instantiate button object
    calcbutton = new Button[bnames.length];
         for ( int i = 0; i < bnames.length; i++ )
    calcbutton[i] = new Button(bnames);
    calcbutton[i].addActionListener(this);
    //(new ActionListener()
         calcbutton[i].setBackground(Color.cyan);
    //add the new button from teh array into the panel for buttons
    bpanel.add(calcbutton[i]);
    //add the button panel to the container object
    add(bpanel, BorderLayout.CENTER);
    //create the clear button to be displayed at the bottom of the screen
    clear = new Button("Clear");
    //add the action listener for this object
    clear.addActionListener(this);
    clear.setBackground(Color.cyan);
    //add the clear button to the panel for the clear button
    Cpanel.add(clear);
    //add the clear button panel to the container
    add(Cpanel, BorderLayout.SOUTH);
         public void actionPerformed(ActionEvent e)
         String bpressed = (e.getActionCommand());
         checkButtonPressed(bpressed);
         if(bpressedEvaluated=="notFound")
    //JOptionPane.showMessageDialog(null, "Invalid button selection!",
    // "System Message", JOptionPane.INFORMATION_MESSAGE);
    public void checkButtonPressed(String valueIn)
              String me;
              String takeValueIn = valueIn;
              double tot=0;
              String tat;
              for (i=0;i<holdNumNames.length;i++)
         if(takeValueIn == holdNumNames[i])
         //if there is a second operator in memory, then clear the contents of the
         //text box and start over adding in new numbers from the user
         if (takeValueIn == ".")
         if (decimalPressed == true)
    //JOptionPane.showMessageDialog(null, "This function cannot be used with any other operatorsdecimalPressed== " + decimalPressed,"System Message.", JOptionPane.INFORMATION_MESSAGE);
    break;
    else
    decimalPressed = true;
    if(takeValueIn == "Neg")
         if(numberpressed == false && negPressed == false)
    negPressed = true;
    takeValueIn = "-";
    else
    break;
    if (operators2 == true)
         String nothing;
         nothing = "";
         text1.setText(nothing);
         operators2 = false;
    //if there is no data in the text box, then set this number as the new text
         if (text1.getText().length() == 0)
    text1.setText(takeValueIn);
    //if there is text contained in the text field, then append this number to
    //to the text field and set the new text view for the user
    else if (text1.getText().length() != 0)
    holdnumonetwo = text1.getText();
    holdnumonetwo += takeValueIn;
    text1.setText(holdnumonetwo);
    numberpressed = true;
    bpressedEvaluated = "Found";
    break;
    for (i=0;i<holdOperatorVisual.length;i++)
    if(takeValueIn == holdOperatorVisual[i])
    if (takeValueIn == "=")
    if (operators == true)
    //convert text to number two for calculation
    numtwo = Double.parseDouble(text1.getText());
    //do the math
    if(calctot1=="-")
    tot = numone-numtwo;
    else if(calctot1=="+")
    tot = numone+numtwo;
    else if(calctot1=="/")
    tot = numone/numtwo;
    else if(calctot1=="*")
    tot = numone*numtwo;
    //convert total to string
    tat = String.valueOf(tot);
    //set the visual value to the screen for the user
    text1.setText(tat);
    //update the new number one to be used in the next calculation
    numone = tot;
    decimalPressed = false;
    negPressed = false;
    numberpressed = false;
    break;
    else
    if (operators != true && text1.getText().length()!= 0)
    calctot1 = takeValueIn;
    numone = Double.parseDouble(text1.getText());
    String t;
    t = "";
    text1.setText(t);
    firstnum = true;
    operators = true;                                    decimalPressed = false;
    negPressed = false;                               numberpressed = false;                                    break;
    else if (operators == true && text1.getText().length()!= 0)
    {                                                                                                                                                     calctot2 = takeValueIn;                                                                                                                                                     numtwo = Double.parseDouble(text1.getText());                                                                                                                                                     //do the math                                                                                                                                                if(calctot1=="-")                                                                                                                                                       {                                                                                                                                                     tot = numone-numtwo;                                                                                                                                                    }                                    else if(calctot1=="+")                                    {                                                                                                                                                        tot = numone+numtwo;                                                                                                                                                        }                                    else if(calctot1=="/")                                    {                                                                                                                                                            tot = numone/numtwo;                                                                                                                                                            }                                    else if(calctot1=="*")                                    {                                                                                                                                                                tot = numone*numtwo;                                                                                                                                                               }                                    //convert total to string                                    tat = String.valueOf(tot);                                    //set the visual value to the screen for the user                                    text1.setText(tat);                               //update the new number one to be used in the next calculation                               numone = tot;                               operators2 = true;                               decimalPressed = false;                               negPressed = false;                               }                               calctot1 = calctot2;                               //set the flags                               firstnum = true;                               decimalPressed = false;                               negPressed = false;                               numberpressed = false;                               bpressedEvaluated = "found";                               break;                               }                          }                     }                          for(i=0;i<holdSciFiVisuals.length;i++)                     {                                                                                                                                                         if(takeValueIn == holdSciFiVisuals[i])                     {                                                                                                                                                        if (text1.getText().length()!= 0 && operators != true)                                                                                                                                                            {                                                                                                                                                               double s=0;                                                                                                                                                                numone = Double.parseDouble(text1.getText());                                                                                                                                                            if(takeValueIn == "Sin")                                                                                                                                                                 s = Math.sin(numone);                                                                                                                                                            if(takeValueIn == "Cos")                                                                                                                                                                  s = Math.cos(numone);                                                                                                                                                            if(takeValueIn == "Tan")                                                                                                                                                                  s = Math.tan(numone);                                                                                                                                                                  firstnum = true;                                                                                                                                                                  String ch;                                                                                                                                                                  ch = String.valueOf(s);                                                                                                                                                                  text1.setText(ch);                                                                                                                                                                  operators2 = true;                                                                                                                                                            }                     else if (operators == true)                     {                                                                                                                                                                 //JOptionPane.showMessageDialog(null, "This function cannot be used with any other operators",                                                                                                                                                          //"System Message", JOptionPane.INFORMATION_MESSAGE);                                                                                                                                                                     break;                                                                                                                                                               }                          bpressedEvaluated = "found";                          decimalPressed = false;                          negPressed = false;                          numberpressed = false;                     }                }                          bpressedEvaluated = "notFound";                     if(takeValueIn == "Clear")
    //reset all the values that are either presently in use, or that will be the
    //first values to be used after the text field is cleared, to start from square one
    numone = 0;
    numtwo = 0;
    //set the flags
    firstnum = false;
    secondnum = false;
    operators = false;
    operators2 = false;
    decimalPressed = false;
    negPressed = false;
    numberpressed = false;
    //declare string to help clear the text field
    String cn;
    cn = "";
    //set the text field to nothing, an empty string
    text1.setText(cn);
         //execute application
         public static void main (String args[])
         Calculator application = new Calculator();
    //     addMouseListener(this);
         application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Adera Currie Student Id# 31248
    //Course Title: Internet Programming
    //Due Date: August 13th, 2002
    //calculator.Java
    //Hilltop Library Calculator Java Project
    //import javax.swing.*;
    //import javax.swing.JOptionPane;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    //import java.awt.Container;
    public class Calculator extends JFrame implements ActionListener
         int chioce, i;
         //integer variable to hold stringnums in
         double calcone, calctwo;
         //variables for mouse clicks
         double numone, numtwo, total, holdnum, decimalCount;
         String calctot, calctot1, calctot2, negnum, holdnumonetwo;
         //declare text area
         TextField text1;
         //declare button array
         Button calcbutton[], clear;
         //string array to convert textfield values, string manipulation
         String textfield;
         String bpressedEvaluated;
         //declare panel object
         Panel bpanel,text2,Cpanel;
         //declare variable arrays to hold the name and numbers of the buttons that
         //will be created on the applet
         String bnames[] = {"7","8","9","/","Sin","4","5","6",
    "*","Cos","1","2","3","-","Tan",
    "0",".","=","+","Neg"};
         //arrays for holding the items to check for when the action click event is called
         String holdOperatorVisual[] = {"/","*","-","=","+"};
         String holdNumNames[] = {"0","1","2","3","4","5","6","7","8","9",".","Neg"};
         String holdSciFiVisuals[] = {"Sin","Cos","Tan"};
         //declare boolean values to keep track of numbers and operators either
         //in use or what should be terminated
         boolean operators,operators2;
         boolean firstnum;
         boolean secondnum;
         boolean numberpressed,decimalPressed,negPressed;
         public Calculator()
              //create the panels to hold applet objects
              bpanel = new Panel();
              text2 = new Panel();
              Cpanel = new Panel();
              //set up the applet layout for the buttons and such
              //BorderLayout layout = new BorderLayout(5,5);
              getContentPane().setLayout(new BorderLayout(5,5));
              //create a container object
              //Container c = this.getContentPane();
              //c.setLayout(new FlowLayout());
              //this.setLayout( new FlowLayout() );
              //set up the panels with layout manager
              bpanel.setLayout( new GridLayout (4,5));
              text2.setLayout(new GridLayout(1,1));
              Cpanel.setLayout(new GridLayout(1,1));
              //create my text field to hold the display for the user
              text1 = new TextField(20);
              //make it so the user cannot enter text from the keyboard
              text1.setEditable(false);
              text1.setBackground(Color.cyan);
              text2.add(text1);
              //add teh panel to the container
              getContentPane().add(text2, BorderLayout.NORTH);
              //instantiate button object
              calcbutton = new Button[bnames.length];
              for ( int i = 0; i < bnames.length; i++ )
                   calcbutton[0] = new Button(bnames[0]);
                   calcbutton[0].addActionListener(this);
                   //(new ActionListener()
                   calcbutton[0].setBackground(Color.cyan);
                   //add the new button from teh array into the panel for buttons
                   bpanel.add(calcbutton[0]);
              //add the button panel to the container object
              getContentPane().add(bpanel, BorderLayout.CENTER);
              //create the clear button to be displayed at the bottom of the screen
              clear = new Button("Clear");
              //add the action listener for this object
              clear.addActionListener(this);
              clear.setBackground(Color.cyan);
              //add the clear button to the panel for the clear button
              Cpanel.add(clear);
              //add the clear button panel to the container
              getContentPane().add(Cpanel, BorderLayout.SOUTH);
              pack();
              show();
         public void actionPerformed(ActionEvent e)
              String bpressed = (e.getActionCommand());
              checkButtonPressed(bpressed);
              if(bpressedEvaluated=="notFound")
                   //JOptionPane.showMessageDialog(null, "Invalid button selection!",
                   // "System Message", JOptionPane.INFORMATION_MESSAGE);
         public void checkButtonPressed(String valueIn)
              String me;
              String takeValueIn = valueIn;
              double tot=0;
              String tat;
              for (i=0;i<holdNumNames.length;i++)
                   if(holdNumNames.equals(takeValueIn + ""))
                        //if there is a second operator in memory, then clear the contents of the
                        //text box and start over adding in new numbers from the user
                        if(takeValueIn == ".")
                             if (decimalPressed == true)
                                  //JOptionPane.showMessageDialog(null, "This function cannot be used with any other operatorsdecimalPressed== " + decimalPressed,"System Message.", JOptionPane.INFORMATION_MESSAGE);
                                  break;
                             else
                                  decimalPressed = true;
                        if(takeValueIn == "Neg")
                             if(numberpressed == false && negPressed == false)
                                  negPressed = true;
                                  takeValueIn = "-";
                             else
                                  break;
                        if (operators2 == true)
                             String nothing;
                             nothing = "";
                             text1.setText(nothing);
                             operators2 = false;
                        //if there is no data in the text box, then set this number as the new text
                        if(text1.getText().length() == 0)
                             text1.setText(takeValueIn);
                        //if there is text contained in the text field, then append this number to
                        //to the text field and set the new text view for the user
                        else if (text1.getText().length() != 0)
                             holdnumonetwo = text1.getText();
                             holdnumonetwo += takeValueIn;
                             text1.setText(holdnumonetwo);
                        numberpressed = true;
                        bpressedEvaluated = "Found";
                        break;
              for (i=0;i<holdOperatorVisual.length;i++)
                   if(holdOperatorVisual.equals(takeValueIn+""))
                        if (takeValueIn == "=")
                             if (operators == true)
                                  //convert text to number two for calculation
                                  numtwo = Double.parseDouble(text1.getText());
                                  //do the math
                                  if(calctot1=="-")
                                       tot = numone-numtwo;
                                  else if(calctot1=="+")
                                       tot = numone+numtwo;
                                  else if(calctot1=="/")
                                       tot = numone/numtwo;
                                  else if(calctot1=="*")
                                       tot = numone*numtwo;
                                  //convert total to string
                                  tat = String.valueOf(tot);
                                  //set the visual value to the screen for the user
                                  text1.setText(tat);
                                  //update the new number one to be used in the next calculation
                                  numone = tot;
                                  decimalPressed = false;
                                  negPressed = false;
                                  numberpressed = false;
                             break;
                        else
                             if (operators != true && text1.getText().length()!= 0)
                                  calctot1 = takeValueIn;
                                  numone = Double.parseDouble(text1.getText());
                                  String t;
                                  t = "";
                                  text1.setText(t);
                                  firstnum = true;
                                  operators = true; decimalPressed = false;
                                  negPressed = false; numberpressed = false; break;
                             else if (operators == true && text1.getText().length()!= 0)
                             { calctot2 = takeValueIn; numtwo = Double.parseDouble(text1.getText()); //do the math if(calctot1=="-") { tot = numone-numtwo; } else if(calctot1=="+") { tot = numone+numtwo; } else if(calctot1=="/") { tot = numone/numtwo; } else if(calctot1=="*") { tot = numone*numtwo; } //convert total to string tat = String.valueOf(tot); //set the visual value to the screen for the user text1.setText(tat); //update the new number one to be used in the next calculation numone = tot; operators2 = true; decimalPressed = false; negPressed = false; } calctot1 = calctot2; //set the flags firstnum = true; decimalPressed = false; negPressed = false; numberpressed = false; bpressedEvaluated = "found"; break; } } } for(i=0;i<holdSciFiVisuals.length;i++) { if(takeValueIn == holdSciFiVisuals) { if (text1.getText().length()!= 0 && operators != true) { double s=0; numone = Double.parseDouble(text1.getText()); if(takeValueIn == "Sin") s = Math.sin(numone); if(takeValueIn == "Cos") s = Math.cos(numone); if(takeValueIn == "Tan") s = Math.tan(numone); firstnum = true; String ch; ch = String.valueOf(s); text1.setText(ch); operators2 = true; } else if (operators == true) { //JOptionPane.showMessageDialog(null, "This function cannot be used with any other operators", //"System Message", JOptionPane.INFORMATION_MESSAGE); break; } bpressedEvaluated = "found"; decimalPressed = false; negPressed = false; numberpressed = false; } } bpressedEvaluated = "notFound"; if(takeValueIn == "Clear")
                                       //reset all the values that are either presently in use, or that will be the
                                       //first values to be used after the text field is cleared, to start from square one
                                       numone = 0;
                                       numtwo = 0;
                                       //set the flags
                                       firstnum = false;
                                       secondnum = false;
                                       operators = false;
                                       operators2 = false;
                                       decimalPressed = false;
                                       negPressed = false;
                                       numberpressed = false;
                                       //declare string to help clear the text field
                                       String cn;
                                       cn = "";
                                       //set the text field to nothing, an empty string
                                       text1.setText(cn);
         //execute application
         public static void main (String args[])
              Calculator application = new Calculator();
              // addMouseListener(this);
              application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

  • Calling Report Parameter in Row Formula - Report Painter

    hello
    i have a unique situation in which 11 months out of the year i need to call a certain "cell" and then in December I need to call another "cell"
    i figured if that is possible I would be able to do this one of two ways:
    IF "Eval Period" <> 12 THEN "Y021" ELSE "Y023" - this would result in a single line solution
    IF "Eval Period" <> 12 THEN "Y021" ELSE 0
    IF "Eval Period" = 12 THEN "Y023" ELSE 0
    the later formula would be split into two lines.
    i am a big excel and VBA user (where this would be possible with some trick coding), but i am slowly learning the art of report painter. Is this possible. I know you can define a row or column with a variable, but can you call it out in a formula?
    thanks

    Hi Adam01,
    I am not sure if I have understood your problem, but I am going to comment one thing in case it is helful for you.
    If you press F1 in the u201CFormulaLineu201D when you are editing the formula, you can read:
    Formula syntax in the Report Writer
    A formula consists of operands, operators, numbers, and parentheses.
    The following operands are possible:
    Row numbers (for example, Y001 for row 001)
    Column numbers (for example, X001 for column 001)
    Cells (for example, Z001 for a specific total in a report)
    Value variables (for example, '1PERIK' for the value variable 1PERIK)
    You can enter numbers directly, with or without a decimal point (for example, 100 or 2.5).
    Example (percentage difference):
    ( X001 - X002 ) * 100 / X001
    In addition to basic arithmetic operations + - * / the following operators are supported:
    Operator Example
    ABS   Absolute value ABS(-10) = 10
    DIV   Quotient-whole no. div. 10 DIV 3 = 3
    MOD   Remainder-whole no. div. 10 MOD 3 = 1
    SQRT  Square root SQRT(9) = 3
    INT   Truncation to integer INT(3.9) = 3
    TRUNC Truncation to integer TRUNC(3.9) = 3
    ROUND Rounding ROUND(3.5) = 4
    EXP   Exponential function EXP(1) = 2.71828
    LOG   Logarithm LOG(10) = 2.3025
    SIN, COS, TAN  Trigonomic functions (length of a curve)
    Example (difference of absolute values):
    ABS(X001) - ABS(X002)
    Logical Expressions:
    IF c1 THEN f1 ELSE f2
    where c1 is a comparison condition, f1 and f2 are formulas.
    In comparison conditions, the following operators are allowed:
    < less than
    <= less than or equal to
    > greater than
    >= greater than or equal to
    = equal to
    Example (determine maximum value):
    IF Y001 > Y002 THEN Y001 ELSE Y002
    For instance you can write:
      IF '8A-PER' < 12 THEN Y023 ELSE Y021
    Best regards,
    Paco

  • Trig etc functions on fractions?

    Fraction.java with trig methods?
    I've been playing with geospatial stuff for a while now, and I've been striking a lot of problems with the inherent inaccuracies of floating point number representations, especially in complex-calculated values.
    Here's a simple but pertinent example:
    class TheSumOfSevenSevethsIsNotOne
      public static void main(String[] args) {
        double f = 1.0/7.0;
        double sum = 0.0;
        for (int i=0; i<7; i++) {
          sum += f;
        System.out.println("sum = "+sum);
    // OUTPUT:
    // sum = 0.9999999999999998
    // not 1 as you might expectThe numbers I'm storing are latitudes and longitudes in degrees, so the max_value is just 360, but the requirement is that lat/lon must be accurate to 9 decimal places, which equates to about +/- 0.6 millimetres, which is (apparently) close enough to be regarded as "millimetre accuracy" by cartographers, even though total ambiguity is 1.2 mm.
    So three digits, plus six digits, is only nine digits, right?... and the humble int can store a tad more than 9 digits...
                                     123.123 456
    Integer.MAX_VALUE = (2^31)-1 = 2,147,483,647So I got to thinking... How would it be if I stored all lat/lons in the database as integers (multiplied by a million)? and did all my calculations rounded (not truncated) to the nearest 1. I could even save a few hundred million bytes that way... But that still leaves the same ole ambiguity around the actual calculations, many of which involve division ;-(.
    So I got to thinking maybe I could use fractions? How would a Fraction.java look?
    I googled around and found some great stuff, including:<ul>
    <li>[Diane Kramers Fraction.java|http://aleph0.clarku.edu/~djoyce/cs101/Resources/Fraction.java]
    <li>[Working with Fractions in Java|http://www.merriampark.com/fractions.htm] (includes BigFraction.java - very handy)
    <li>[Doug Leas Fraction.java|http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/misc/Fraction.java]
    </ul>
    But I haven't found anything which implements the basic trigonometry functions like sin, cos, tan, cot (and whatever else)... but [Daves Short Trig Course|http://www.clarku.edu/~djoyce/trig/] might give me the understanding required to do so... nor does any existing Fraction.java (which I've found so far) implement handy things like x^y, log, modulo (and whatever else) ... and not being a mathematician myself, I'm not overly eager to implement them... I'd never be sure I'd done the job properly.
    Please is anyone aware of any such implementations, even partial ones? Or could someone perhaps be persuaded to do part(s) of it just for the challenge?
    Cheers all. Keith.

    Sir Turing Pest,
    I do geospatial stuff amost exclusively and I dont understand this post.
    Why not just use doubles? The "error" you're seeing is so trivial.
    Put into perspective, if you were positioning something from the
    Sun to Pluto you'd be off by half a millimeter.
    How do you figure that you only need 9 digits?
    ignore 360 degrees. the circumference of the earth is 41k km and you need
    accuracy to .6 mm = 11 places
    (40 075.02 kilometers) / (.5 millimeters) = 80,150,040,000I didn't figure it, our local GIS expert did, based mainly on the size of an integer,
    When I do the math I get to 9 decimal places == about 1.11 mm, and that's allways rounded (not truncated) to 9 decimal places which I think means our points are accurate to plus or minus about 0.6 mm.... but I'm just a humble computer programmer, NOT a mathemetician or a geospatial expert.
    We're storing lat/lon to 9 decimal places... So 1 is 1 degree.
                   40,075.160000000     kilometers     circumference of the earth
    equals     40,075,160.000000000     meters        circumference of the earth
    equals        111,319.888888889     meters        meters per degree at the equator
    equals              0.000000001     degrees         storage accurracy
    equals              0.000111320     meters         
    equals              1.113198889     millemeters     storage accurracy in millimeters
    So sure you established that aggregate double addition comes out "wrong".
    Then dont do it, lol. Dont waste your time trying to invent new number
    storage. Just try to write your algorithms so you don't do aggregate addition as much.We try not to aggregate calculated values... but there are certain algorithms, like the reverse/mercator transforms where it's unavoidable... So opportunities for improvement is this area a likely to be NOT very cost effective, ie: bigger than Ben Hur, harder than a bulls azz, and uglier than an extreme closeup of my scotum... My main concern has been (rightly or wrongly) the inherent inaccuracy in our storage of numbers.... thinking that improvements in this area just might be cost-effective, and therefore doable.
    Iterestingly... we had a tree-clearing case kicked out of court recenctly because we couldn't define the boundaries of the national park in question to the satisfaction of the court... a "satisfaction level" which was based on our own "millimeter accuracy" definition of the required accuracy of survey data, which is based on international standards for GIS. ie: It's a bit of sore spot around the office at the moment, and I'm trying to do some bluddy thing about it... I'm just at a bit of a loss as to exactly what, without throwing literally millions of dollars at the problem to upgrade ALL our systems to 11 decimal places (or better). I'm in stress city.
    Cheers. Keith.

  • URGENT Help With Scientific Calculator!

    Hi everybody,
    I designed a calculator, and I need help with the rest of the actions. I know I need to use the different Math methods, but I tried tried that and it didn't work. Also, it needs to work as an applet and application, and in the applet, the buttons don't appear in order, how can I fix that?
    I will really appreciate your help with this program, I can't get it to work and I'm frustrated, I need to finish this for next Tuesday 16th. Please e-mail me at [email protected].
    Below is the code for the calcualtor.
    Thanks a lot!
    -Maria
    // calculator
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    public class calculator extends JApplet implements
    ActionListener
      private JButton one, two, three, four, five, six, seven,
      eight, nine, zero, dec, eq, plus, minus, mult, div, clear,
      mem, mrc, sin, cos, tan, asin, acos, atan, x2, sqrt, exp, pi, percent;
      private JLabel output, blank;
      private Container container;
      private String operation;
      private double number1, number2, result;
      private boolean clear = false;
      //GUI
      public void init()
        container = getContentPane();
        //Title
        //super("Calculator");
        JPanel container = new JPanel();     
        container.setLayout( new FlowLayout( FlowLayout.CENTER
        output = new JLabel("");     
        output.setBorder(new MatteBorder(2,2,2,2,Color.gray));
        output.setPreferredSize(new Dimension(1,26));     
        getContentPane().setBackground(Color.white);     
        getContentPane().add( "North",output );     
        getContentPane().add( "Center",container );
        //blank
        blank = new JLabel( "                    " );
        container.add( blank );
        //clear
        clear = new JButton( "CE" );
        clear.addActionListener(this);
        container.add( clear );
        //seven
        seven = new JButton( "7" );
        seven.addActionListener(this);
        container.add( seven );
        //eight
        eight = new JButton( "8" );
        eight.addActionListener(this);
        container.add( eight );
        //nine
        nine = new JButton( "9" );
        nine.addActionListener(this);
        container.add( nine );
        //div
        div = new JButton( "/" );
        div.addActionListener(this);
        container.add( div );
        //four
        four = new JButton( "4" );
        four.addActionListener(this);
        container.add( four );
        //five
        five = new JButton( "5" );
        five.addActionListener(this);
        container.add( five );
        //six
        six = new JButton( "6" );
        six.addActionListener(this);
        container.add( six );
        //mult
        mult = new JButton( "*" );
        mult.addActionListener(this);
        container.add( mult );
        //one
        one = new JButton( "1" );
        one.addActionListener(this);
        container.add( one );
        //two
        two = new JButton( "2" );
        two.addActionListener(this);
        container.add( two );
        //three
        three = new JButton( "3" );
        three.addActionListener(this);
        container.add( three );
        //minus
        minus = new JButton( "-" );
        minus.addActionListener(this);
        container.add( minus );
        //zero
        zero = new JButton( "0" );
        zero.addActionListener(this);
        container.add( zero );
        //dec
        dec = new JButton( "." );
        dec.addActionListener(this);
        container.add( dec );
        //plus
        plus = new JButton( "+" );
        plus.addActionListener(this);
        container.add( plus );
        //mem
        mem = new JButton( "MEM" );
        mem.addActionListener(this);
        container.add( mem );   
        //mrc
        mrc = new JButton( "MRC" );
        mrc.addActionListener(this);
        container.add( mrc );
        //sin
        sin = new JButton( "SIN" );
        sin.addActionListener(this);
        container.add( sin );
        //cos
        cos = new JButton( "COS" );
        cos.addActionListener(this);
        container.add( cos );
        //tan
        tan = new JButton( "TAN" );
        tan.addActionListener(this);
        container.add( tan );
        //asin
        asin = new JButton( "ASIN" );
        asin.addActionListener(this);
        container.add( asin );
        //acos
        acos = new JButton( "ACOS" );
        cos.addActionListener(this);
        container.add( cos );
        //atan
        atan = new JButton( "ATAN" );
        atan.addActionListener(this);
        container.add( atan );
        //x2
        x2 = new JButton( "X2" );
        x2.addActionListener(this);
        container.add( x2 );
        //sqrt
        sqrt = new JButton( "SQRT" );
        sqrt.addActionListener(this);
        container.add( sqrt );
        //exp
        exp = new JButton( "EXP" );
        exp.addActionListener(this);
        container.add( exp );
        //pi
        pi = new JButton( "PI" );
        pi.addActionListener(this);
        container.add( pi );
        //percent
        percent = new JButton( "%" );
        percent.addActionListener(this);
        container.add( percent );
        //eq
        eq = new JButton( "=" );
        eq.addActionListener(this);
        container.add( eq );
        //Set size and visible
        setSize( 190, 285 );
        setVisible( true );
    public static void main(String args[]){
        //execute applet as application
         //applet's window
         JFrame applicationWindow = new JFrame("calculator");
    applicationWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         //applet instance
         calculator appletObject = new calculator();
         //init and start methods
         appletObject.init();
         appletObject.start();
      } // end main
      public void actionPerformed(ActionEvent ae)
        JButton but = ( JButton )ae.getSource();     
        //dec action
        if( but.getText() == "." )
          //if dec is pressed, first check to make shure there
    is not already a decimal
          String temp = output.getText();
          if( temp.indexOf( '.' ) == -1 )
            output.setText( output.getText() + but.getText() );
        //clear action
        else if( but.getText() == "CE" )
          output.setText( "" );
          operation = "";
          number1 = 0.0;
          number2 = 0.0;
        //plus action
        else if( but.getText() == "+" )
          operation = "+";
          number1 = Double.parseDouble( output.getText() );
          clear = true;
          //output.setText( "" );
        //minus action
        else if( but.getText() == "-" )
          operation = "-";
          number1 = Double.parseDouble( output.getText() );
          clear = true;
          //output.setText( "" );
        //mult action
        else if( but.getText() == "*" )
          operation = "*";
          number1 = Double.parseDouble( output.getText() );
          clear = true;
          //output.setText( "" );
        //div action
        else if( but.getText() == "/" )
          operation = "/";
          number1 = Double.parseDouble( output.getText() );
          clear = true;
          //output.setText( "" );
        //eq action
        else if( but.getText() == "=" )
          number2 = Double.parseDouble( output.getText() );
          if( operation == "+" )
            result = number1 + number2;
          else if( operation == "-" )
            result = number1 - number2;
          else if( operation == "*" )
            result = number1 * number2;
          else if( operation == "/" )
            result = number1 / number2;       
          //output result
          output.setText( String.valueOf( result ) );
          clear = true;
          operation = "";
        //default action
        else
          if( clear == true )
            output.setText( "" );
            clear = false;
          output.setText( output.getText() + but.getText() );
    }

    Multiple post:
    http://forum.java.sun.com/thread.jsp?forum=31&thread=474370&tstart=0&trange=30

Maybe you are looking for