Revision: 46982
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at May 28, 2011 09:33 by ehrenb
Initial Code
package newProject.test;
//Code by ehrenb
import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.RadioButton;
import android.widget.Toast;
import java.util.ArrayList;
import android.widget.*;
//import java.util.ArrayList;
//needs error checking for number parsing**
public class myMenu extends Activity {
private EditText inputNums;
private TextView resultBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputNums = (EditText) findViewById(R.id.input);
resultBox = (TextView) findViewById(R.id.results);
// requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
}
//int[] storage = new int[10];
//ArrayList<Double> storage=new ArrayList<Double>();
double numToCalc=0;
// This method is called at button click because we assigned the name to the
// "On Click property" of the button
public void myClickHandler(View view) {
switch (view.getId()) {
}
//initialize all the buttons
Button addButton = (Button) findViewById(R.id.addButton);
Button multButton = (Button) findViewById(R.id.multButton);
Button subtractButton = (Button) findViewById(R.id.subtract);
Button divideButton = (Button) findViewById(R.id.divideButton);
Button factorsButton = (Button) findViewById(R.id.factorsButton);
if (inputNums.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return; //return is a MUST...force close without
//parse for numbers, not just a blank input
}
//Code by ehrenb
// check for non-numeric values
//abstracted from:
//src = http://www.ehow.com/how_7718014_error-check-fields-java.html
for(int i= 0; i < inputNums.getText().length(); i++) {
if(!Character.isDigit(inputNums.getText().charAt(i)))
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
double inputValue = Double.parseDouble(inputNums.getText().toString());
if (addButton.isPressed()) {
//storage.add(inputValue); //add parsed double to arraylist
//numToCalc = inputValue;
//inputNums.setText(inputValue.);
//inputNums.append("+"); //add + to the textView
//permNum=add(inputValue);
resultBox.setText(String
.valueOf(add(inputValue))); //do addition calculations
inputNums.setText("");//clear the textfield
}
if (multButton.isPressed()){
numToCalc = inputValue;
//add parsed double to arraylist
//inputNums.setText(inputValue.);
//inputNums.append("+"); //add + to the textView
//storage.add(inputValue);
//storage.add(inputValue);
//permNum=resultValue;
resultBox.setText(String
.valueOf(multiply(inputValue))); //do addition calculations
inputNums.setText("");//clear the textfield
}
if(subtractButton.isPressed() ){
resultBox.setText(String.valueOf(subtract(inputValue))); //do addition calculations
inputNums.setText("");//clear the textfield
}
// Switch to the other button
// if (fahrenheitButton.isChecked()) {
// fahrenheitButton.setChecked(false);
// celsiusButton.setChecked(true);
// } else {
// fahrenheitButton.setChecked(true);
// celsiusButton.setChecked(false);
// }
if (divideButton.isPressed()){
numToCalc = inputValue;
resultBox.setText(String
.valueOf(divide(inputValue))); //do division calculations
inputNums.setText("");//clear the textfield
}
if (factorsButton.isPressed()){
resultBox.setText(String.valueOf(findFactors(inputValue)));
}
}
// add
int numOfAdds=0;
private double add(double addNum) {
//int temp = storage.size();
double total =0;
// if (temp == 0) // if array is empty, do nothing
// {
//
// }
// else
{
double resultValue = Double.parseDouble(resultBox.getText().toString());
double inputValue = Double.parseDouble(inputNums.getText().toString());
// total=numToCalc+resultValue;
total = addNum+resultValue;
//permNum=total;
// for (int i=0; i<temp; i++) //loop adds up every item in the array
// {
// total = total +storage.get(i);
//
// }
// storage.clear();//standard template for calc functions: clear the arrayList, then add
// storage.add(total);
}
numOfAdds++;
return (total);
}
int multiplicationInit=1;
int numOfMultis=0;//FIX - counts amt of multiplications done in the program
private double multiply(double multiplyNum)
{
double inputValue = Double.parseDouble(inputNums.getText().toString());
double resultValue = Double.parseDouble(resultBox.getText().toString());
double product=0;
product=multiplyNum*resultValue;
if (numOfMultis==0 && resultValue==0 && numOfAdds==0 && numOfSubtract==0 && numOfDivide==0)
//if it is absolutely the first calculation <<FIX!! If it is BOTH: The first Calculation & the resultBox=0 > it is ABSOLUTELY the first calculation.
//previous problem: if(numOfMultis==0) only checks to see when the first multiplication is, not what the values are
//still testing
//seek to optimize this later--IE- no number of calculations counted
{
product = multiplyNum*multiplicationInit;//workaround
}
else //assuming we have already done something to the resultValue
//then we dont need to worry about the case of multiplying product*0
{
product = multiplyNum*resultValue;//multiply normally
}
numOfMultis++;
return product;
}
int numOfSubtract=0;
int numOfDivide=0;
private double subtract(double subtractNum)
{
double total =0;
double resultValue = Double.parseDouble(resultBox.getText().toString());
total=resultValue-subtractNum;
numOfSubtract++;
return total;
}
private double divide(double divideNum)
{
double resultValue = Double.parseDouble(resultBox.getText().toString());
numOfDivide++;
return resultValue/divideNum;
}
private String findFactors(double factNum)
{
ArrayList tempArray = new ArrayList();
for(int i = 1; i <= factNum/2; i++)
{
if(factNum % i == 0)// if the remainder of num5 and a number is 0..it is a factor!
tempArray.add(i);
//System.out.print(i + " ");
}
return tempArray.toString();
}
//MULTIPLCATION NOTES:
//**Calculations are done off of the resultValue
//Since the result value has to initially be 0, we must compensate
//and make resultValue=1 in the case that the user multiplies first
//instead of add/sub/div
//If we were to keep resultValue=0, we would run into a problem where; if multiplying first(assuming resultValue hasnt been touched), our calculation would be forced to =0
// because by default, resultValue must=0. IE 1*0=0
//goals:maintain universal resultValue
//problems: if subtracting say 3-3=0. If we attempt to multiply after that, we will encounter an issue b/c it will force the 0 to be a 1.
//Solution: compensate again in the functions that require it (force 1 to be a 0?)
//above problem fixed
//Code by ehrenb
}
Initial URL
Initial Description
This is a current side project. I make changes (mainly adding new things) about once a week (when I have time). Notes: GUI in XML(not here), Eclipse Android plug-in, some images not here
Initial Title
Basic Android Calculator
Initial Tags
java, android
Initial Language
Java