Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. package com.example.nofar.myapplication; //Targil 2 misevaha ribohit
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10.  
  11.  
  12.  
  13. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  14. EditText etA;
  15. EditText etB;
  16. EditText etC;
  17.  
  18. Button btnFindRoots;
  19. TextView tvMessage;
  20. TextView tvX1;
  21. TextView tvX2;
  22.  
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28.  
  29. etA= (EditText) findViewById(R.id.editTextA);
  30. etB= (EditText) findViewById(R.id.editTextB);
  31. etC= (EditText) findViewById(R.id.editTextC);
  32.  
  33. tvMessage= (TextView) findViewById(R.id.textViewMessage);
  34. tvX1= (TextView) findViewById(R.id.textViewX1);
  35. tvX2= (TextView) findViewById(R.id.textViewX2);
  36. btnFindRoots= (Button) findViewById(R.id.buttonFindRoots);
  37. btnFindRoots.setOnClickListener(this);
  38.  
  39. }
  40.  
  41. @Override
  42. public void onClick(View view) {
  43.  
  44. try {
  45. double a=Double.parseDouble(etA.getText().toString());
  46. double b=Double.parseDouble(etB.getText().toString());
  47. double c=Double.parseDouble(etC.getText().toString());
  48.  
  49. double d=Math.pow(b,2)-4*a*c;
  50. if(d<0){
  51. tvMessage.setText("No roots");
  52. tvX1.setText("");
  53. tvX2.setText("");
  54. }else{
  55. if(d==0){
  56. tvMessage.setText("One Root");
  57. double x =(double) -b/2*a;
  58. tvX1.setText(x+"");
  59. tvX2.setText(x+"");
  60. }
  61.  
  62. if(d>0){
  63. tvMessage.setText("Two Root");
  64. double x1 =(double) (-b+Math.sqrt(d))/2*a;
  65. double x2 =(double) (-b-Math.sqrt(d))/2*a;
  66. tvX1.setText(x1+"");
  67. tvX2.setText(x2+"");
  68.  
  69. }
  70. }
  71.  
  72. } catch (NumberFormatException e) {
  73. //Error message
  74. Toast.makeText(this,"Error input values!. Please try again.",Toast.LENGTH_LONG).show();
  75. tvX1.setText("");
  76. tvX2.setText("");
  77. tvMessage.setText("");
  78.  
  79. }
  80.  
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement