0

I wanted to create a simple quadratic equation solver but when I run it, it gets caught at the first if statement of each button. Any ideas?

public class MainActivity extends AppCompatActivity {
    Button button1, button2;
    String a_temp;
    EditText b_temp;
    EditText c_temp;
    TextView tv1, tv2;
    Double a,b,c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        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();
            }
        });

        a_temp = ((EditText)findViewById(R.id.editText)).getText().toString();
        if (a_temp.equals("")) {
            a = Double.valueOf(0);
        } else {
            a = Double.valueOf(a_temp);
        }

        b_temp = (EditText)findViewById(R.id.editText2);
        if (!b_temp.getText().toString().equals("")) {
            b = Double.parseDouble(b_temp.getText().toString());
        }else {
            b = Double.valueOf(0);
        }

        c_temp = (EditText)findViewById(R.id.editText3);
            if (!c_temp.getText().toString().equals("")) {
            c = Double.parseDouble(c_temp.getText().toString());
        }else {
            c = Double.valueOf(0);
        }

        tv1 = (TextView) findViewById(R.id.textView2);
        tv2 = (TextView) findViewById(R.id.textView3);

        button1 = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (a == 0) {
                    tv1.setText("That's not a quadratic equation");
                }else {
                    if (b*b - 4*a*c < 0) {
                        tv1.setText("This quadratic equation has no real roots");
                    } else {
                        double root1 = (-b + Math.sqrt(Math.pow(b,2) - 4 * a * c))/(2*a);
                        double root2 = (-b - Math.sqrt(Math.pow(b,2) - 4 * a * c))/(2*a);
                        double result = Math.max(root1,root2);
                        tv1.setText(String.valueOf(result));
                    }
                }
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (a == 0) {
                    tv2.setText("That's not a quadratic equation");
                }else {
                    if (b * b - 4 * a * c < 0) {
                        tv2.setText("This quadratic equation has no real roots");
                    } else {
                        double root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
                        double root2 = (-b - Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
                        double result = Math.min(root1, root2);
                        tv2.setText(" " + result);
                    }
                }
            };
        });
    }

XML code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Type the operands of the quadratic equation you want to solve"
    android:id="@+id/textView"
    android:layout_marginTop="39dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Root #1"
    android:id="@+id/button"
    android:layout_marginTop="60dp"
    android:layout_below="@+id/editText3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Root #2"
    android:id="@+id/button2"
    android:layout_marginTop="69dp"
    android:layout_alignTop="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/textView2"
    android:layout_alignBottom="@+id/button"
    android:layout_alignTop="@+id/button"
    android:layout_alignRight="@+id/textView"
    android:layout_alignEnd="@+id/textView"
    android:layout_toRightOf="@+id/button"
    android:layout_toEndOf="@+id/button"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="false" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/textView3"
    android:layout_alignBottom="@+id/button2"
    android:layout_toRightOf="@+id/button2"
    android:layout_alignTop="@+id/button2"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number|numberSigned"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_marginTop="32dp"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/textView2"
    android:layout_toStartOf="@+id/textView2" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number|numberSigned"
    android:ems="10"
    android:id="@+id/editText2"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/textView2"
    android:layout_toStartOf="@+id/textView2" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number|numberSigned"
    android:ems="10"
    android:id="@+id/editText3"
    android:layout_below="@+id/editText2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/textView2"
    android:layout_toStartOf="@+id/textView2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="x^2"
    android:id="@+id/textView4"
    android:layout_alignTop="@+id/editText"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignStart="@+id/textView2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="x"
    android:id="@+id/textView5"
    android:layout_below="@+id/editText"
    android:layout_alignLeft="@+id/textView4"
    android:layout_alignStart="@+id/textView4" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="=0"
    android:id="@+id/textView6"
    android:layout_alignBottom="@+id/editText3"
    android:layout_alignLeft="@+id/textView5"
    android:layout_alignStart="@+id/textView5" />
</RelativeLayout>
Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
Giannis B
  • 3
  • 1

2 Answers2

0

You are comparing double values with ==. The problem is that it will likely not be equal 0, because of the minor error it has when storing double values. For example, it will be something like 0.000000001 or -0.00000001. Instead, compare like this:

 if (Math.abs(a) < 0.0000001) { ... }

Also you forgot to divide your roots by 2 * a.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
0

So the main problem I see is, that you are assigning your values for a, b and c in the onCreate method of your activity class.

This means, as soon as your activity is created, i.e. before the user had any time to input any data into your EditText fields, you are trying to read said non-existing input - i.e. 0.

This is why in each onClick method, the first if() clause which checks for zero-values, returns true. Because the values are zero.

What you should do is move this part of each variable:

    if (!c_temp.getText().toString().equals("")) {
        c = Double.parseDouble(c_temp.getText().toString());
    }else {
        c = Double.valueOf(0);
    }

into your OnClickListener classes, i.e. into the onClick methods, to "refresh" the values you are using in your calculations based on the current user input.

damian
  • 2,001
  • 20
  • 38