Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this is my code so far then it says to do this, and I can figure it out nActivit

ID: 3719384 • Letter: T

Question

this is my code so far then it says to do this, and I can figure it out

nActivityjava × ontent main.xm tends AppCompatActivity I public class HainActivity ex private Button addButton: private EditText input private TextView ansverText private Button subtractButton: private Button multiplyButton private Button divideButton: private EditText input2: 1 Override protected void onCreate (Bundle savedInstanceState) super.onCreate (savedInstanceState) setContentView (R. layout.activity main) Toolbar toolbar (Toolbar) findViewById (R.id. too lbar) : addButton(Button) findViewById (R.1d. addButtan) subtractButton (Button) findviewByld (R.id. subtractButton): multiplyButton (Button) rindViewById (R.id.nultiplyButton) divideButton= (Button) findieuById(R.1d.divideButtan); inputi (EditText)findViewById (R.1d. inputi) input2 (EditText) findViewById (R.id. input:2): answerText (Textview) findViewById (R.id. answer Tesrt): getSupportActionBar (toolbar FloatingActionButton fab. (FloatingActionButton) findViewById(R.?d. fab); fab·?eeOnClicklistener (new view. CnClickListener() { 0verr1de public void onciick (Vie view) t Snackber.make(viex, text: "Replace with your own action, Snackbar. LENGTH LONG) setAction( text: "Action, listener null) show override public boolean onCreateoptionsMenu (Menu menu) Inflete the nenu: this adds items to the action ber i it s prosen getHenuInflater () inflate (R.menu.menu nain, menu): return true: override public boolean onoptionsItenSelected (MenuIten item) t Handle action bar iten clicks here. The açtion bar E11 // autonatically handie clicks on the Hone/Dp bucton, ap 2ong as you specify a parent activity in Androdteni fest.m2 int id item.getItenId): //noinspection Simplifiableirstatenent 1? {id "-R.?d.ac tiansettings) [ - return true: return super.onoptionsreenSelected (iten) MainActivity addButton

Explanation / Answer

// implements View.OnClickListener

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

               private Button addButton, subtractButton, multiplyButton, divideButton;

               private EditText input1, input2;

               private TextView answerText;

              

              

               @Override

               protected void onCreate(Bundle savedInstanceState)

               {

                              super.onCreate(savedInstanceState);

                              setContentView(R.layout.activty_main);

                              Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);

                              addButton = (Button)findViewById(R.id.addButton);

                              subtractButton =(Button)findViewById(R.id.subtractButton);

                              multiplyButton =(Button)findViewById(R.id.multiplyButton);

                              divideButton = (Button) findViewById(R.id.divideButton);

                             

                              // add this as listener for each button

                              addButton.setOnClickListener(this);

                              subtractButton.setOnClickListener(this);

                              multiplyButton.setOnClickListener(this);

                              divideButton.setOnClickListener(this);

                             

                              input1 = (EditText)findViewById(R.id.input1);

                              input2 = (EditText)findViewById(R.id.input2);

                             

                              answerText = (TextView) findViewById(R.id.answerText);

                              setSupportActionBar(toolbar);

                             

                              FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);

                             

                              fab.setOnClickListener(new View.OnClickListener(){

                                             @Override

                                             public void onClick(View view)

                                             {

                                                            Snackbar.make(view,"Replace with your own action",Snackbar.LENGTH_LONG).setAction("Action",null).show();

                                             }

                              });

                             

               }

              

               @Override

               public boolean onCreateOptionsMenu(Menu menu)

               {

                              getMenuInflater().inflate(R.menu.menu_main, menu);

                              return true;

               }

              

               @Override

               public boolean onOptionsItemSelected(MenuItem item)

               {

                              int id = item.getItemId();

                              if(id == R.id.action_settings)

                              {

                                             return true;

                              }

                             

                              return super.onOptionsItemSelected(item);

               }

              

               @Override

    public void onClick(View v) {

              

                              // get the text from the input fields

                              double a = Double.parseDouble(input1.getText().toString());

                              double b = Double.parseDouble(input2.getText().toString());

                              if(v == addButton)

                              {

                                             c = a+b; // calculate the sum

                                             answerText.setText(c+""); // convert to string and set the text on the answerText TextView

                              }else if(v == subtractButton)

                              {

                                             c = a-b; // calculate the difference

                                             answerText.setText(c+""); // convert to string and set the text on the answerText TextView

                              }else if(v == multiplyButton)

                              {

                                             c = a*b; // calculate multiplication

                                             answerText.setText(c+""); // convert to string and set the text on the answerText TextView

                              }else if(v == divideButton)

                              {

                                             if(b!= 0) // check divide by 0

                                             {

                                                            c = a/b;

                                                            answerText.setText(c+""); // convert to string and set the text on the answerText TextView

                                             }else{

                                                            answerText.setText("Infinity"); // convert to string and set the text on the answerText TextView

                                             }

                              }

                             

               }

              

              

}