You are on page 1of 9

COMP 1786 Logbook

1. Basic Information

Fill in the information in the table below

1.1 Student name Nguyen Thai Manh

1.2 Who did you work with? Note that for Name:
logbook exercises you are allowed to
work with one other person as long as Login id:
you give their name and login ID and
both contribute to the work.

1.3 Which Exercise is this? Tick as  Exercise 1


appropriate.  Exercise 2
 Exercise 3

1.4 How well did you complete the  I tried but couldn't complete it
exercise? Tick as appropriate.  I did it but I feel I should have done better
 I did everything that was asked
 I did more than was asked for
1.5 Briefly explain your answer to question
1.4.

Without any explanation/justification, your


scores will be deducted.

2. Exercise answer
2.1 Screenshots demonstrating what you achieved.
Paste screenshots here. Add an explanation of what each screenshot demonstrates
2.2 Code that you wrote
Copy and paste the relevant code here. Actual code, not screenshots.
You need to add a brief explanation.
import androidx.appcompat.app.AppCompatActivity;
import org.mariuszgromada.math.mxparser.*;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

I have import mXparser to calculate and Toast to report errors


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display=findViewById(R.id.input);
display.setShowSoftInputOnFocus(false);

display.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
if(getString(R.string.display).equals(display.getText().toString()))
display.setText("");
}
});
}
I use “display=findViewById(R.id.input)” to refer to an “EditText” in the layout by finding the “input” id and
assigning it to the “display” variable for interaction. The line “display.setShowSoftInputOnFocus(false)” hides the
virtual phone's default keyboard. Then in the code snippet “display.setOnClickListener(new
View.OnClickListener() {...})”, “OnClickListener” is set for “display”. This means that when the user clicks on
“display” (EditText), the code in the “onClick” method will be executed. In the “onClick” method, the check line
“if(getString(R.string.display).equals(display.getText().toString()))” checks whether the current content of
“display” matches whether the string was previously defined with the name “display”. If this condition is true,
the contents of “display” will be removed and replaced with an empty string.

private void inputNumber(String numToAdd) {


String oldNum = display.getText().toString();
int mouseClick = display.getSelectionStart();
String leftStr = oldNum.substring(0, mouseClick);
String rightStr = oldNum.substring(mouseClick);
String newInput = leftStr + numToAdd + rightStr;

if (newInput.contains("/0")) {
Toast.makeText(this, "Division by zero is not allowed",
Toast.LENGTH_SHORT).show();
return;
}
display.setText(newInput);
display.setSelection(mouseClick + 1);
}

The “inputNumber” code snippet takes a number or character and adds them to an EditText field. If the added
string contains "/0", it displays a Toast message reporting a division by zero error. Otherwise, it updates the
EditText field and moves the cursor to the next position.

public void zero(View view){


inputNumber("0");
}
public void one(View view){
inputNumber("1");
}
public void two(View view){
inputNumber("2");
}
public void three(View view){
inputNumber("3");
}
public void four(View view){
inputNumber("4");
}
public void five(View view){
inputNumber("5");
}
public void six(View view){
inputNumber("6");
}
public void seven(View view){
inputNumber("7");
}
public void eight(View view){
inputNumber("8");
}
public void nine(View view){
inputNumber("9");
}
public void add(View view){
inputNumber("+");
}
public void multi(View view){
inputNumber("*");
}
public void minus(View view){
inputNumber("-");
}
public void divide(View view){
inputNumber("/");
}
This code defines a series of public methods in the application. Each of these methods is triggered by the click
event of a corresponding View (user interface). Generally these methods are used to add numbers and operators
(+, -, *, /) to the EditText field when the user clicks on the corresponding Views on the Android application's user
interface. The inputNumber method is called to perform the job of adding numbers or characters.

public void equal(View view){


String userEqual = display.getText().toString();
userEqual = userEqual.replaceAll("÷","/");
userEqual = userEqual.replaceAll("×","*");
Expression exp = new Expression(userEqual);
String result = String.valueOf(exp.calculate());
display.setText(result);
display.setSelection(result.length());
}
public void clear(View view) {
display.setText("");
}
Equal method: This method calculates the result of the mathematical expression entered in the EditText
(`display`) field. Before calculating, it replaces the characters "÷" and "×" with "/" and "*" to ensure correct
calculation. It then uses a class called `Expression` to perform the calculation and display the results on the user
interface.
Clear method: This method simply clears the entire content in the EditText (`display`) field by setting it to an
empty string. This helps users delete any mathematical expressions that are displayed.

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="@string/CALCULATOR"
android:textAlignment="center"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
First I have a TextView to display the text Calculator
Then I have 1 tableLayout and in the tableLayout there are 4 tableRows, in each tableRow there are 4 buttons
and each button represents a separate function.
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input"
app:layout_constraintVertical_bias="0.95">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:id="@+id/seven"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="seven"
android:text="@string/seven"
android:textColor="@color/white"
android:textSize="24sp" />

<Button
android:id="@+id/eight"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="eight"
android:text="@string/eight"
android:textColor="@color/white"
android:textSize="24sp" />

<Button
android:id="@+id/nine"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="nine"
android:text="@string/nine"
android:textColor="@color/white"
android:textSize="24sp" />

<Button
android:id="@+id/divide"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="divide"
android:text="@string/divide"
android:textColor="@color/white"
android:backgroundTint="@color/orange"
android:textSize="24sp" />
</TableRow>
In the above code line, I use a tableRow to contain 4 buttons, the numbers 7, 8, 9 and the division sign, and each
button is used for me to call onClick.
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/four"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="four"
android:text="@string/four"
android:textColor="@color/white"
android:textSize="24sp" />

<Button
android:id="@+id/five"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="five"
android:text="@string/five"
android:textColor="@color/white"
android:textSize="24sp" />

<Button
android:id="@+id/six"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="six"
android:text="@string/six"
android:textColor="@color/white"
android:textSize="24sp" />

<Button
android:id="@+id/multi"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="multi"
android:text="@string/multi"
android:textColor="@color/white"
android:backgroundTint="@color/orange"
android:textSize="24sp" />
</TableRow>
In the above line of code, I use a tableRow to contain 4 buttons, the numbers 4, 5, 6 and the multiplication sign,
each button is used to call onClick.
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/one"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="one"
android:text="@string/one"
android:textColor="@color/white"
android:textSize="24sp" />

<Button
android:id="@+id/two"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="two"
android:text="@string/two"
android:textColor="@color/white"
android:textSize="24sp" />

<Button
android:id="@+id/three"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="three"
android:text="@string/three"
android:textColor="@color/white"
android:textSize="24sp" />
<Button
android:id="@+id/minus"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="minus"
android:text="@string/minus"
android:textColor="@color/white"
android:backgroundTint="@color/orange"
android:textSize="24sp" />
</TableRow>
In the above line of code, I use a tableRow to contain 4 buttons, the numbers 1, 2, 3 and the minus sign, each
button is used to call onClick.
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/clear"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="clear"
android:text="@string/clear"
android:textColor="@color/white"
android:backgroundTint="@color/orange"
android:textSize="24sp" />

<Button
android:id="@+id/zero"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="zero"
android:text="@string/zero"
android:textColor="@color/white"
android:textSize="24sp" />

<Button
android:id="@+id/equal"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="equal"
android:text="@string/equal"
android:textColor="@color/white"
android:backgroundTint="@color/orange"
android:textSize="24sp" />

<Button
android:id="@+id/add"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="add"
android:text="@string/add"
android:textColor="@color/white"
android:backgroundTint="@color/orange"
android:textSize="24sp" />
</TableRow>
</TableLayout>
In the above line of code, I use a tableRow to contain 4 buttons, with the number 0, the letter C and the equal
sign, each button is used to call onClick.
<EditText
android:id="@+id/input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:autofillHints="false"
android:inputType="none"
android:text="@string/display"
android:textAlignment="textEnd"
android:textSize="32sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:ignore="LabelFor" />
Finally I use an editText to display the number that the user enters to calculate and print out the result after
calculation

You might also like