Reputation: 53
Until three weeks ago I knew nothing about developing applications for android, and with very basic Java background.
However, I took up the interest, and I'm now tying to develop a simple application that performs basic arithmetic operations.
What I would like the application to do is take in figures input by the user, up to 20 pairs, multiply each pair separately, and then add the results. I have gotten through the multiplication of the pairs, but can't get onto adding up the results. This is what I have so far for the multiplication part, and it works okay....
public class CalcAlgorithm extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
final String LOG_TAG = "MainScreen";
super.onCreate(savedInstanceState);
setContentView(R.layout.calc_algorithm);
final EditText value1=(EditText) findViewById(R.id.value1);
final EditText value2=(EditText) findViewById(R.id.value2);
final EditText value3=(EditText) findViewById(R.id.value3);
final EditText value4=(EditText) findViewById(R.id.value4);
final EditText value5=(EditText) findViewById(R.id.value5);
final EditText value6=(EditText) findViewById(R.id.value6);
final EditText value7=(EditText) findViewById(R.id.value7);
final EditText value8=(EditText) findViewById(R.id.value8);
final EditText value9=(EditText) findViewById(R.id.value9);
final EditText value10=(EditText) findViewById(R.id.value10);
final TextView result=(Button) findViewById (R.id.multiplyValues);
final TextView result2=(Button) findViewById (R.id.multiplyValues2);
final TextView result3=(Button) findViewById (R.id.multiplyValues3);
final TextView result4=(Button) findViewById (R.id.multiplyValues4);
final TextView result5=(Button) findViewById (R.id.multiplyValues5);
final TextView result6=(Button) findViewById (R.id.addButton);
Button multiplyButton = (Button)findViewById(R.id.multiplyValues);
multiplyButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
try {
int val1=Integer.parseInt(value1.getText().toString());
int val2=Integer.parseInt(value2.getText().toString());
Integer answer = val1*val2;
result.setText(answer.toString());
} catch (Exception e) {
Log.e(LOG_TAG, "Failed to multiply numbers",e);
}
}
});
Button multiplyButton2 = (Button) findViewById(R.id.multiplyValues2);
multiplyButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
int val3 = Integer.parseInt(value3.getText().toString());
int val4 = Integer.parseInt(value4.getText().toString());
Integer answer = val3 * val4;
result2.setText(answer.toString());
} catch (Exception e) {
Log.e(LOG_TAG, "Failed to multiply numbers",e);
}
}
});
Button multiplyButton3 = (Button) findViewById(R.id.multiplyValues3);
multiplyButton3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
int val5 = Integer.parseInt(value5.getText().toString());
int val6 = Integer.parseInt(value6.getText().toString());
Integer answer = val5 * val6;
result3.setText(answer.toString());
} catch (Exception e) { Log.e(LOG_TAG, "Failed to multiply numbers",e);}}
});
Button multiplyButton4 = (Button) findViewById(R.id.multiplyValues4);
multiplyButton4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
int val7 = Integer.parseInt(value7.getText().toString());
int val8 = Integer.parseInt(value8.getText().toString());
Integer answer = val7 * val8;
result4.setText(answer.toString());
} catch (Exception e) { Log.e(LOG_TAG, "Failed to multiply numbers",e);}}
});
Button multiplyButton5 = (Button) findViewById(R.id.multiplyValues5);
multiplyButton5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
int val9 = Integer.parseInt(value9.getText().toString());
int val10 = Integer.parseInt(value10.getText().toString());
Integer answer = val9 * val10;
result5.setText(answer.toString());
} catch (Exception e) {
Log.e(LOG_TAG,"Failed to multiply number",e);
}
}
});
Is this the right path? And, i realize that there is a way of summarising the code into fewer lines and not as i have it. Could someone please show me how I can go about having fewer lines of code, and then adding up the results all at once?
After adding the values, I'd again like to perform some more (basic) operations on the results, but i think after I have known how to add the results, I should be able to figure my way from there.
**Perhaps i should point out that i would require the addition to be done once separately i.e on clicking of an added button (TotalSum,maybe) separately and not progressively as the multiplication goes on, such that the user can see the multiplied results for each pair of figures entered, and the total of all paired results on clicking a separate button..
It would also be of immense help if i could get links to some books/documentation/videos that could help with arithmetic functions in writing android applications.
Any help will be greatly appreciated guys. :)
Upvotes: 0
Views: 2471
Reputation: 35
package com.example.calc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText num1_edt=(EditText)findViewById(R.id.num1_edt);
final EditText num2_edt=(EditText)findViewById(R.id.num2_edt);
final Button add_btn=(Button)findViewById(R.id.add_btn);
findViewById(R.id.sub_btn);
findViewById(R.id.mul_btn);
findViewById(R.id.div_btn);
add_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int a=Integer.parseInt(num1_edt.getText().toString());
int b=Integer.parseInt(num2_edt.getText().toString());
Integer n=a*b;
Toast.makeText(getApplicationContext(),"Your Num Is:"+n.toString(),Toast.LENGTH_LONG).show();
}
});
}
}
Upvotes: 0
Reputation: 342
You could add a calculateResultSum()
method, which you call at the end of each onClick method with does something like
void calculateResultSum() {
Integer res1 = Integer.parse(result.getText.toString());
// .. get all the other results
Integer finalResult = res1 + res2 + res2 + res4 + res5;
//do something with the result
}
BUT your approach is very redundant, i.e. you have the same code in all of you onClick methods. This is considered bad code style, you should try to extract the actual processing of the numbers into one single method and call this method from each of the listeners, for example
void addNumbers(TextView tv1, TextView tv2, TextViev res){
try {
Integer val1=Integer.parseInt(value1.getText().toString());
Integer val2=Integer.parseInt(value2.getText().toString());
Integer answer = val1*val2;
res.setText(answer.toString());
} catch (Exception e) { Log.e(LOG_TAG, "Failed to multiply numbers",e);}}
}
and set the onClicks to
multiplyButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
addNumbers(value1,value2,result);
calculateResultSum();
});
with the corresponding views for each Button. Hope I could help.
Upvotes: 1
Reputation: 2766
If I understood correctly, here is the solution.
Create a class variable like private int total=0;
and in each setOnClickListener
add your answer
s to this variable.
Integer answer = val1*val2;
total += answer;
Also instead of using 5 Button
and 10 TextView
s you can do this with simply one Button
and one TextView
storing results in another TextView
but you said you are practicing, if it is just to practice it should be alright.
Upvotes: 0