I'm a college student in my first year of my bachelor in IT. I recently decided to try some app development with Android Studio and Java. For my first project, I made a basic calculator app.
Here you can see how it looks:
The calculator app:
- can take multiple digits and floating point numbers as input
- has operator precendence
- can add, substract, divide and mutiply
My main idea was to have an ArrayList
in wich every number and every operator gets stored.
Then for the calculation, I searched the ArrayList
for the operator and took the numbers that are to left and to the right of it, and replaced those with the answer of those.
Here is the code of my mainActivity.java
:
package com.example.calculatormk2;
public class MainActivity extends AppCompatActivity {
private ArrayList input = new ArrayList();
private StringBuilder number = new StringBuilder();
private boolean calculationDone = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onNumberClick(View v) {
number.append(getBtnTxt(v));
updateView(v);
}
public void onOperatorClick(View v) {
addNumberToInput(number);
addOperatorToInput(v);
updateView(v);
}
public void onEqualsClick(View v) {
addNumberToInput(number);
calculate(input);
clearInput();
}
public void calculate(ArrayList i) {
while (i.contains("x") || i.contains("÷")) {
int indexOperatorTimes = i.indexOf("x");
int indexOperatorDivideBy = i.indexOf("÷");
int indexOperatorMain;
if (indexOperatorTimes < indexOperatorDivideBy && indexOperatorTimes != -1) {
indexOperatorMain = indexOperatorTimes;
} else {
indexOperatorMain = indexOperatorDivideBy;
}
if (indexOperatorDivideBy == -1) {
indexOperatorMain = indexOperatorTimes;
}
double firstNumber;
double secondNumber;
String operator = i.get(indexOperatorMain).toString();
double answer;
if (i.get(indexOperatorMain - 1) instanceof Integer) {
firstNumber = (int) i.get(indexOperatorMain - 1);
} else {
firstNumber = (double) i.get(indexOperatorMain - 1);
}
if (i.get(indexOperatorMain + 1) instanceof Integer) {
secondNumber = (int) i.get(indexOperatorMain + 1);
} else {
secondNumber = (double) i.get(indexOperatorMain + 1);
}
if (operator.equals("x")) {
answer = firstNumber * secondNumber;
} else {
answer = firstNumber / secondNumber;
}
i.remove(indexOperatorMain - 1);
i.remove(indexOperatorMain - 1);
i.remove(indexOperatorMain - 1);
i.add(indexOperatorMain - 1, answer);
System.out.println(i);
}
while (i.contains("+") || i.contains("-")) {
int indexOperator1 = i.indexOf("+");
int indexOperator2 = i.indexOf("-");
int indexOperatorDef;
if (indexOperator1 < indexOperator2 && indexOperator1 != -1) {
indexOperatorDef = indexOperator1;
} else {
indexOperatorDef = indexOperator2;
}
if (indexOperator2 == -1) {
indexOperatorDef = indexOperator1;
}
double firstNumber;
double secondNumber;
String operator = i.get(indexOperatorDef).toString();
double answer;
if (i.get(indexOperatorDef - 1) instanceof Integer) {
firstNumber = (int) i.get(indexOperatorDef - 1);
} else {
firstNumber = (double) i.get(indexOperatorDef - 1);
}
if (i.get(indexOperatorDef + 1) instanceof Integer) {
secondNumber = (int) i.get(indexOperatorDef + 1);
} else {
secondNumber = (double) i.get(indexOperatorDef + 1);
}
if (operator.equals("+")) {
answer = firstNumber + secondNumber;
} else {
answer = firstNumber - secondNumber;
}
i.remove(indexOperatorDef - 1);
i.remove(indexOperatorDef - 1);
i.remove(indexOperatorDef - 1);
i.add(indexOperatorDef - 1, answer);
}
calculationDone = true;
TextView t = findViewById(R.id.mainOutput);
String s = i.get(0).toString();
t.setText(s);
}
public void addOperatorToInput(View v) {
input.add(getBtnTxt(v));
}
public void addNumberToInput(StringBuilder s) {
if (s.toString().contains(".")) {
input.add(Double.parseDouble(s.toString()));
} else {
input.add(Integer.parseInt(s.toString()));
}
clearNumber();
}
public String getBtnTxt(View v) {
return ((Button) v).getText().toString();
}
public void clearNumber() {
number.delete(0, number.length());
}
public void clearInput() {
input.clear();
}
@SuppressLint("SetTextI18n")
public void updateView(View v) {
TextView mainOutput = findViewById(R.id.mainOutput);
if (calculationDone) {
mainOutput.setText("");
calculationDone = false;
}
mainOutput.setText(mainOutput.getText() + getBtnTxt(v));
}
}
In total it took me about 10 hours to make this and I'm pretty happy with it (because I didn't expect to get it to even work) even though there is a lot of double code. There is also a lot of code which only exists to cast the numbers to the right datatype. I realised late when working on the app that the get()
function of an ArrayList
returns the item as an object and it gave a lot of trouble retrieving something out of it. Looking back, I probably should have done this differently but I went into this project without a proper plan and just winged it.
I really would appreciate some feedback. You can be honest about it, even if it's garbage.
If, by any chance, anyone wants to take a look at the full project. Here is the GitHub page: https://github.com/PhilipNousPXL/Calculator-mk2.git