Power function introduction into calculator

I have this Java code but I do not know how to introduce power, sin, cos or other functions in it. I looked for it various websites but no luck finding it

public class calculator extends Activity {

 private WebView mWebView;
 private StringBuilder mMathString;
 private ButtonClickListener mClickListener;

 /** Called when the activity is first created. */
 public void onCreate(Bundle savedInstanceState) {

  // Create the math string
  mMathString = new StringBuilder();

  // Enable javascript for the view
  mWebView = (WebView) findViewById(R.id.webview);

  // Set the listener for all the buttons
  mClickListener = new ButtonClickListener();
  int idList[] = { R.id.button0, R.id.button1, R.id.button2,
    R.id.button3, R.id.button4, R.id.button5, R.id.button6,
    R.id.button7, R.id.button8, R.id.button9, R.id.buttonLeftParen,
    R.id.buttonRightParen, R.id.buttonPlus, R.id.buttonPlus,
    R.id.buttonMinus, R.id.buttonDivide, R.id.buttonTimes,
    R.id.buttonDecimal, R.id.buttonBackspace, R.id.buttonClear, R.id.buttonPow };

  for(int id : idList) {
   View v = findViewById(id);


 private void updateWebView() {

  StringBuilder builder = new StringBuilder();

  builder.append("<script type=\"text/javascript\">document.write('");
  builder.append("document.write('<br />=' + eval(\"");

  mWebView.loadData(builder.toString(), "application/xhtml", "UTF-8");

 private class ButtonClickListener implements OnClickListener {

  public void onClick(View v) {
   switch (v.getId()) {
   case R.id.buttonBackspace:
    if(mMathString.length() > 0)
   case R.id.buttonClear:
    if(mMathString.length() > 0)
     mMathString.delete(0, mMathString.length());
    mMathString.append(((Button) v).getText());




could someone say how to introduce power function?


To answer the question I think you're really wanting to know, which is how does THAT code calculate math functions without using Java's math package...

That code you are looking at prints a bunch of javascript to an HTML page, including eval("your math expression") with the entered mathmatical expression within the eval, and that eval line is what is actually doing the mathmatical evaluation, and it is doing so using javascript, not Java to actually do the math.

http://javascriptsource.com/math-related/5-function.html has example of doing powers using javascript's eval function...you would have to modify the javascript generated by the app in order to evaluate powers in that way.

You will probably want to look at Java's Math package. It has lots of math functions including the ones you are looking for. You may need to add an import of import java.lang.Math; at the beginning of the file you use the Math functions in, if the import is not already there.

For example, double answer = Math.pow(firstnum,secondnum); is the function to raise one number to the power of another, or double answer = Math.sin(anum) to give the sin of a value in the variable anum, and so on.

As you read through the javadoc for that class you will see additional functions that should supply the rest of the "scientific" calculator features you want.

Keep in mind also that integers and doubles do have limits to their storage capacities, so you may want to consider how to handle overflow if someone decides to do something crazy like 100,000^100,000,000. If having enough precision for really really large numbers is important to you, you may also want to look into classes such as BigDecimal or BigInteger.

You need to use java Math API. Take a look at this tutorial.

Java has these functions built-in in the java.lang.Math class as static methods. You can cal them just like this:

Power, using Math.pow(double, double)

double number = 3.42d;
double exponent = 5.14d;
double power = Math.pow(number, exponent);

Square Root, using Math.sqrt(double)

double shortSide1 = 4.0d;
double shortSide2 = 3.0d;
double longSide = Math.sqrt(shortSide1 * shortSide1 + shortSide2 * shortSide2);
// All credits goto Pythagoras. 

Sin, cos, tan

double angle = 2.13d;
double sin = Math.sin(angle);
double cos = Math.cos(angle);
double tan = Math.tan(angle);

Arcsin, arccos, arctan

double value = 0.13d;
double asin = Math.asin(value);
double acos = Math.acos(value);
double atan = Math.atan(value);

All these functions are Native (not written in Java), which mean that they will perform as good as possible on your platform.

Need Your Help

Struggling with has_many :through

ruby-on-rails foreign-keys associations

Suppose I have 3 Models like this (not sure if this is correct):

Closing and Opening Twitter Bootstrap Modals Quickly Causing Issue

javascript jquery twitter-bootstrap

I have a situation where I have a model open and when I close it and open it very quickly I am seeing a really very odd behavior like backdrop is not disappearing and modal is not showing.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.