/ Published in: Java
                    
                                        
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
                Notes: GUI in XML(not here), Eclipse Android plug-in, some images not here
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
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
switch (view.getId()) {
}
//initialize all the buttons
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++) {
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
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);
.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;
.valueOf(multiply(inputValue))); //do addition calculations
inputNums.setText("");//clear the textfield
}
if(subtractButton.isPressed() ){
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;
.valueOf(divide(inputValue))); //do division calculations
inputNums.setText("");//clear the textfield
}
if (factorsButton.isPressed()){
}
}
// 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
{
// 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 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;
total=resultValue-subtractNum;
numOfSubtract++;
return total;
}
private double divide(double divideNum)
{
numOfDivide++;
return resultValue/divideNum;
}
{
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
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                