Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need help to check on this calculator app building in adroidstudio Multiplication button not working Here is my code below :- MainActivity.java package com.kean.calculator; import
Need help to check on this calculator app building in adroidstudio
Multiplication button not working
Here is my code below :-
MainActivity.java
package com.kean.calculator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView mNumberDisplay; private SimpleExpression mExpression; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); mNumberDisplay = (TextView) findViewById(R.id.textView1); mExpression = new SimpleExpression(); } public void goAC (View view){ mNumberDisplay.setText(R.string.zero); mExpression.clearOperands(); } public void goOperand (View view) { String val= (String) mNumberDisplay.getText(); String digit = (String) view.getContentDescription(); if (val.charAt(0) == '0') mNumberDisplay.setText(digit); else mNumberDisplay.setText(mNumberDisplay.getText() + digit); } public void goOperator (View view){ String operator = (String) view.getContentDescription(); String val = (String) mNumberDisplay.getText(); mExpression.setOperand1(Integer.parseInt(val)); mExpression.setOperator(operator); mNumberDisplay.setText(R.string.zero); } public void goCompute (View view){ String val = (String) mNumberDisplay.getText(); mExpression.setOperand2(Integer.parseInt(val)); mNumberDisplay.setText(mExpression.getValue().toString()); } }
-------------------------------------------------------------------------------------------------------------------------
Simple Expression.java
package com.kean.calculator; /** * Created by ademola on 11/21/17. */ public class SimpleExpression { private int mOperand1; private int mOperand2; private String mOperator; private int mResult; public SimpleExpression(){ mOperand1 = 0; mOperand2 = 0; mOperator = "+"; mResult = 0; } public void clearOperands(){ mOperand1 = 0; mOperand2 = 0; } public void setOperand1 (int v){ mOperand1 = v; } public void setOperand2 (int v){ mOperand2 =v; } public void setOperator (String s){ mOperator = s; } public Integer getValue(){ computeValue(); return mResult; } private void computeValue(){ mResult = 0; if (mOperator.contentEquals("+")) mResult = mOperand1 + mOperand2; else if (mOperator.contentEquals("-")) mResult = mOperand1 - mOperand2; else if (mOperator.contentEquals("*")) mResult = mOperand1 * mOperand2; else if (mOperator.contentEquals("/") && mOperand2 != 0) mResult = mOperand1 / mOperand2; else mResult = mOperand1 % mOperand2; } }
--------------------------------------------------------------------------------------
xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="0,1,2,3"> <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_span="4" android:background="@color/orange_cream" android:gravity="right" android:text="0" android:textSize="70sp"/> TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="match_parent" android:minHeight="7dp"> <Button android:id="@+id/buttonAC" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:layout_span="2" android:background="@color/blue_dust" android:contentDescription="@string/ac" android:minHeight="70dp" android:onClick="goAC" android:text="@string/ac" /> <Button android:id="@+id/buttonPer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/percent" android:minHeight="70dp" android:onClick="goOperator" android:text="@string/percent" /> <Button android:id="@+id/buttonDiv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/div" android:minHeight="70dp" android:onClick="goOperator" android:text="@string/div" /> TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_height="match_parent" android:minHeight="70dp"> <Button android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/seven" android:minHeight="70dp" android:onClick="goOperand" android:text="@string/seven" /> <Button android:id="@+id/button8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/eight" android:minHeight="70dp" android:onClick="goOperand" android:text="@string/eight" /> <Button android:id="@+id/button9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/nine" android:minHeight="70dp" android:onClick="goOperand" android:text="@string/nine" /> <Button android:id="@+id/buttonMult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/mult" android:minHeight="70dp" android:onClick="goOperator" android:text="@string/mult"/> TableRow> <TableRow android:id="@+id/tableRow4" android:layout_width="match_parent" android:layout_height="match_parent" android:minHeight="70dp"> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/four" android:minHeight="70dp" android:onClick="goOperand" android:text="@string/four" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/five" android:minHeight="70dp" android:onClick="goOperand" android:text="@string/five" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/six" android:minHeight="70dp" android:onClick="goOperand" android:text="@string/six" /> <Button android:id="@+id/buttonSub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/subtract" android:minHeight="70dp" android:onClick="goOperator" android:text="@string/subtract" /> TableRow> <TableRow android:id="@+id/tableRow5" android:layout_width="match_parent" android:layout_height="match_parent" android:minHeight="70dp"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/one" android:minHeight="70dp" android:onClick="goOperand" android:text="@string/one" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/two" android:minHeight="70dp" android:onClick="goOperand" android:text="@string/two" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/three" android:minHeight="70dp" android:onClick="goOperand" android:text="@string/three" /> <Button android:id="@+id/buttonAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/gray" android:contentDescription="@string/add" android:minHeight="70dp" android:onClick="goOperator" android:text="@string/add" /> TableRow> <TableRow android:id="@+id/tableRow6" android:layout_width="match_parent" android:layout_height="match_parent" android:minHeight="70dp"> <Button android:id="@+id/button0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:layout_span="2" android:background="@color/gray" android:contentDescription="@string/zero" android:minHeight="70dp" android:onClick="goOperand" android:text="@string/zero" /> <Button android:id="@+id/buttonEq" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:layout_span="2" android:background="@color/blue_dust" android:contentDescription="@string/equal" android:minHeight="70dp" android:onClick="goCompute" android:text="@string/equal" /> TableRow> TableLayout>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started