Please give some advice for my simple calculator application

0 Walker Cheung · July 2, 2014
Hi guys, wrote a calculator application, this application would run but it crash sometime, 
It is possible I could have some advice to improve this app?

Thank you so much for your help!  


package com.gagson.calculator;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.DecimalFormat;

public class Calculation extends Activity {

//Valuables
double calculation = 0;
double saved_calculation = 0;
double decimal=0;

//Counters
int zero_check=0;
int i= 0;
int sw = 0;
int dec_check=0;

//String
String cal_display="";

//Buttons
Button one, two, three, four, five, six, seven, eight, nine, zero, plus, sub, mult, divi, del, ac, dot, ans;
TextView TextView;

//Main Body
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator_outlook_a);


//Button Reference
one = (Button) findViewById(R.id.number_one);
two = (Button) findViewById(R.id.number_two);
three = (Button) findViewById(R.id.number_three);
four = (Button) findViewById(R.id.number_four);
five = (Button) findViewById(R.id.number_five);
six = (Button) findViewById(R.id.number_six);
seven = (Button) findViewById(R.id.number_seven);
eight = (Button) findViewById(R.id.number_eight);
nine = (Button) findViewById(R.id.number_nine);
zero = (Button) findViewById(R.id.number_zero);
dot = (Button) findViewById(R.id.dot);
del = (Button) findViewById(R.id.delete);
ac = (Button) findViewById(R.id.allclear);
plus = (Button) findViewById(R.id.plus);
sub = (Button) findViewById(R.id.sub);
mult = (Button) findViewById(R.id.mult);
divi = (Button) findViewById(R.id.divi);
ans = (Button) findViewById(R.id.ans);
TextView = (TextView) findViewById(R.id.TextView);

one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal_display=cal_display+"1";
TextView.setText(cal_display);
/**if(dec_check == 0){
if(i == 0){
calculation = calculation* 0 + 1;
}else if(i > 0 && i < 10){
calculation = calculation* 10 + 1;
}
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(0);
TextView.setText(""+calculation);
i++;
}else if (dec_check>0 && dec_check<10){
decimal = 1*Math.pow(0.1,dec_check);
calculation= calculation + decimal;
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(dec_check);
TextView.setText(""+df.format(calculation));
dec_check++;
}else{
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(dec_check);
TextView.setText(""+df.format(calculation));
}*/
}
});
two.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal_display=cal_display+"2";
TextView.setText(cal_display);
}
});
three.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal_display=cal_display+"3";
TextView.setText(cal_display);
}
});
four.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal_display=cal_display+"4";
TextView.setText(cal_display);
}
});
five.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal_display=cal_display+"5";
TextView.setText(cal_display);
}
});
six.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal_display=cal_display+"6";
TextView.setText(cal_display);
}
});
seven.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal_display=cal_display+"7";
TextView.setText(cal_display);
}
});
eight.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal_display=cal_display+"8";
TextView.setText(cal_display);
}
});
nine.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal_display=cal_display+"9";
TextView.setText(cal_display);
}
});
zero.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal_display=cal_display+"0";
TextView.setText(cal_display);
}
});
plus.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dec_check=0;
if(cal_display!=""){
saved_calculation=Double.parseDouble(cal_display);
cal_display="";
sw=1;}
else{
sw=1;
}
/**calculation = 0;
i=0;
dec_check=0;
*/
}
});
sub.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dec_check=0;
if(cal_display!=""){
saved_calculation=Double.parseDouble(cal_display);
cal_display="";
sw=2;}
else{
sw=2;
}
}
});
mult.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dec_check=0;
if(cal_display!=""){
saved_calculation=Double.parseDouble(cal_display);
cal_display="";
sw=3;}
else{
sw=3;
}

}
});
divi.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dec_check=0;
if(cal_display!=""){
saved_calculation=Double.parseDouble(cal_display);
cal_display="";
sw=4;}
else{
sw=4;
}

}
});

del.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (cal_display==""){
TextView.setText("0");
}else{
cal_display = cal_display.substring(0, cal_display.length() - 1);
TextView.setText(cal_display);
}
}
});
ac.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal_display="";
calculation=0;
saved_calculation=0;
sw=0;
i=0;
dec_check=0;
TextView.setText("");
TextView.setHint("0.");
}
});
dot.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(dec_check !=1){
if(cal_display==""){
cal_display=0+".";
TextView.setText(cal_display);
dec_check=1;
}else{
cal_display=cal_display+".";
TextView.setText(cal_display);
dec_check=1;
}
}else{
TextView.setText(cal_display);
}
/**if (dec_check==0){
dec_check++;
TextView.setText(""+calculation);
}else{
TextView.setText(""+calculation);
}*/
}
});
ans.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub


switch(sw){
case 0:
if (cal_display =="" cal_display =="."){
calculation=0;
zero_check=1;
}else if (saved_calculation!=0 && cal_display == ""){
calculation=saved_calculation;
}else{
calculation=Double.parseDouble(cal_display);}
break;
case 1:
calculation=Double.parseDouble(cal_display);
calculation= saved_calculation + calculation;
break;
case 2:
calculation=Double.parseDouble(cal_display);
calculation= saved_calculation - calculation;
break;
case 3:
calculation=Double.parseDouble(cal_display);
calculation= saved_calculation * calculation;
break;
case 4:
calculation=Double.parseDouble(cal_display);
calculation= saved_calculation / calculation;
break;
}
cal_display=String.valueOf(calculation);
saved_calculation=calculation;
if (zero_check==1){
TextView.setText("");
TextView.setHint("0.");
zero_check=0;
}else if (dec_check == 0){
DecimalFormat df = new DecimalFormat("#.#");
df.setMaximumFractionDigits(0);
TextView.setText(""+df.format(calculation)+".");
}else{
TextView.setText(""+calculation);
}
cal_display="";
sw=0;
calculation = 0;
dec_check=0;
}
});
}
}


 

Post a Reply

Replies

Oldest  Newest  Rating
0 Walker Cheung · July 3, 2014
Well, This time I tested and seems it wont crash but there has problem for decimal places.
For example, if I calculate 100/3, I get 33 and there has no remainder displayed. 

Moreover, If I want to add exception, where should I put the try and catch? Should I worry about the lifecycle? 
0 Walker Cheung · July 4, 2014
sorry for the confuse, 
if I calculate 100 divide by 3, I want it has 33.33333
I should say have it in a decimal, right?
0 William Chamberlain · July 5, 2014
When you perform the calculation, you're using integer types by default, and these do not include decimal precision. You have to use primitive types float or double to output the proper decimal value, so 100.0f/30.0f with the result being of type float.
  • 1

Java / Android Development

107,243 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator