How to make a scientific calculator?

  • Replies:1
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Mar 13, 2016 8:08:59 AM via Website

Hello Everyone.

I am learning android and I was asked to make an android application(A scientific calculator) and submit it to my teacher by Tuesday.

I want to make a Scientific calculator.. my layout.xml file is ready but need some help with .java file.

I already made a simple calculator app which had some bugs. This time i want to be the first in the class i have a good knowledge of using buttons and handling event on them but i am weak in java i want some help in java.

I am looking for someone who have a good knowledge of android and java or if you have source code for a scientific calculator and explain me that. I have searched a lot on google about this but i don't understand those codes. Finally, I thought to ask an expert here. I know you guys are really good in this work.

Please HELP ME!
Email. dp.divyanshpagaria@gmail.com

  • Forum posts: 269

Mar 13, 2016 9:11:40 PM via Website

Hi.

There is MainActivity code of simple calculator and it will be good sample for you.

package com.example.calculator

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

final int MENU_RESET_ID = 1;
final int MENU_QUIT_ID = 2;

EditText etNum1;
EditText etNum2;

Button btnAdd;
Button btnSub;
Button btnMult;
Button btnDiv;

TextView tvResult;

String oper = "";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    etNum1 = (EditText) findViewById(R.id.etNum1);
    etNum2 = (EditText) findViewById(R.id.etNum2);

    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnSub = (Button) findViewById(R.id.btnSub);
    btnMult = (Button) findViewById(R.id.btnMult);
    btnDiv = (Button) findViewById(R.id.btnDiv);

    tvResult = (TextView) findViewById(R.id.tvResult);

    btnAdd.setOnClickListener(this);
    btnSub.setOnClickListener(this);
    btnMult.setOnClickListener(this);
    btnDiv.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    float num1 = 0;
    float num2 = 0;
    float result = 0;

    if (TextUtils.isEmpty(etNum1.getText().toString())
            || TextUtils.isEmpty(etNum2.getText().toString())) {
        return;
    }

    num1 = Float.parseFloat(etNum1.getText().toString());
    num2 = Float.parseFloat(etNum2.getText().toString());

    switch (v.getId()) {
        case R.id.btnAdd:
            oper = "+";
            result = num1 + num2;
            break;
        case R.id.btnSub:
            oper = "-";
            result = num1 - num2;
            break;
        case R.id.btnMult:
            oper = "*";
            result = num1 * num2;
            break;
        case R.id.btnDiv:
            oper = "/";
            result = num1 / num2;
            break;
        default:
            break;
    }

    tvResult.setText(num1 + " " + oper + " " + num2 + " = " + result);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, MENU_RESET_ID, 0, "Reset");
    menu.add(0, MENU_QUIT_ID, 0, "Quit");
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case MENU_RESET_ID:
            etNum1.setText("");
            etNum2.setText("");
            tvResult.setText("");
            break;
        case MENU_QUIT_ID:
            finish();
            break;
    }
    return super.onOptionsItemSelected(item);
}

}