Professional Documents
Culture Documents
1. Basic Information
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.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.
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;
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.
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.
<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