3
\$\begingroup\$

I'm a beginner to Java and Android programming, and I wrote this app in order to get some practice. It works, but it's most likely not as good or efficient as it could be, since I'm new. I'm hoping to get some feedback or maybe some revisions to help me learn.

MainActivity.java:

//  this app can't:
//  accept blank values for A, B, or C
//  display answers as fractions
//  find answers including imaginary numbers
//  show work or factor

package com.example.example.factortrinomials;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onButtonClick(View v) {

        //  declare variables
        EditText aNum = (EditText)findViewById(R.id.aBox);
        EditText bNum = (EditText)findViewById(R.id.bBox);
        EditText cNum = (EditText)findViewById(R.id.cBox);

        TextView ans1 = (TextView)findViewById(R.id.x1Box);
        TextView ans2 = (TextView)findViewById(R.id.x2Box);

        //  input
        int a = Integer.parseInt(aNum.getText().toString());
        int b = Integer.parseInt(bNum.getText().toString());
        int c = Integer.parseInt(cNum.getText().toString());


        //  process solution
        double x1 = 0;
        double x2 = 0;
        boolean imaginary = false;

        double temp1 = (b * b) - (4 * a * c);
        if (temp1 >= 0) {
            temp1 = Math.sqrt(temp1);
            x1 = (-b + temp1) / (2 * a);
            x2 = (-b - temp1) / (2 * a);
        }
        else { //if answer is imaginary...
            imaginary = true;
        }


        //  output solution
        if (!imaginary) {
            ans1.setText(Double.toString(x1));
            if (x1 != x2) ans2.setText(Double.toString(x2));
            else ans2.setText("Only one root.");
        }
        else {
            ans1.setText("Sorry, the roots contain imaginary");
            ans2.setText("numbers. I can't solve that trinomial.");
        }
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.todd.factortrinomials.MainActivity">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="75dp"
        android:text="Find Roots"
        android:id="@+id/button"
        android:onClick="onButtonClick"
        android:enabled="true"
        android:clickable="true"
        android:textSize="20dp"
        android:layout_below="@+id/frameLayout3"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="15dp"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:id="@+id/frameLayout"></FrameLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="x"
        android:id="@+id/textView2"
        android:textSize="30dp"
        android:layout_below="@+id/textView6"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="x"
        android:id="@+id/textView5"
        android:textSize="30dp"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:id="@+id/x1Box"
        android:textSize="30dp"
        android:layout_above="@+id/x2Box"
        android:layout_toRightOf="@+id/textView10"
        android:layout_toEndOf="@+id/textView10" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:id="@+id/x2Box"
        android:textSize="30dp"
        android:layout_below="@+id/textView7"
        android:layout_alignLeft="@+id/x1Box"
        android:layout_alignStart="@+id/x1Box" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Roots:"
        android:id="@+id/textView6"
        android:layout_below="@+id/frameLayout"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="30dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/textView7"
        android:textSize="20dp"
        android:layout_above="@+id/textView5"
        android:layout_toLeftOf="@+id/textView10"
        android:layout_toStartOf="@+id/textView10" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:id="@+id/textView8"
        android:textSize="20dp"
        android:layout_alignBottom="@+id/textView5"
        android:layout_toRightOf="@+id/textView5"
        android:layout_toEndOf="@+id/textView5" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=": "
        android:id="@+id/textView9"
        android:textSize="30dp"
        android:layout_above="@+id/textView10"
        android:layout_toRightOf="@+id/textView7"
        android:layout_toEndOf="@+id/textView7" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=": "
        android:id="@+id/textView10"
        android:textSize="30dp"
        android:layout_below="@+id/x1Box"
        android:layout_toRightOf="@+id/textView8"
        android:layout_toEndOf="@+id/textView8" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="15dp"
        android:id="@+id/frameLayout2"
        android:layout_below="@+id/x2Box"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Example: For x^2+4x-32, enter 1, 4, and -32"
        android:id="@+id/textView3"
        android:textSize="15dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/aBox"
        android:gravity="right"
        android:hint="A"
        android:textSize="30dp"
        android:layout_below="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/bBox"
        android:gravity="center"
        android:hint="B"
        android:textSize="30dp"
        android:layout_above="@+id/frameLayout3"
        android:layout_toRightOf="@+id/textView15"
        android:layout_toEndOf="@+id/textView15" />

    <EditText
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/cBox"
        android:gravity="center"
        android:hint="C"
        android:textSize="30dp"
        android:layout_below="@+id/textView3"
        android:layout_toRightOf="@+id/textView4"
        android:layout_toEndOf="@+id/textView4" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:text="x"
        android:id="@+id/textView"
        android:textSize="30dp"
        android:layout_alignBottom="@+id/aBox"
        android:layout_toRightOf="@+id/aBox"
        android:layout_toEndOf="@+id/aBox"
        android:gravity="top" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:text="x+"
        android:id="@+id/textView4"
        android:textSize="30dp"
        android:layout_above="@+id/frameLayout3"
        android:layout_toRightOf="@+id/bBox"
        android:layout_toEndOf="@+id/bBox"
        android:gravity="top" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:text=" = 0"
        android:id="@+id/textView12"
        android:textSize="30dp"
        android:layout_above="@+id/frameLayout3"
        android:layout_toRightOf="@+id/cBox"
        android:layout_toEndOf="@+id/cBox"
        android:gravity="top" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="15dp"
        android:id="@+id/frameLayout3"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:text="2"
        android:id="@+id/textView13"
        android:textSize="15dp"
        android:gravity="top"
        android:layout_alignTop="@+id/textView"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:text="+"
        android:id="@+id/textView15"
        android:textSize="30dp"
        android:gravity="top"
        android:layout_alignTop="@+id/textView4"
        android:layout_toRightOf="@+id/textView13"
        android:layout_toEndOf="@+id/textView13" />


</RelativeLayout>
\$\endgroup\$
  • \$\begingroup\$ It would be more precise to say that you are solving a quadratic equation. A "trinomial" could consist of any three terms. \$\endgroup\$ – 200_success Jul 4 '16 at 21:23
  • \$\begingroup\$ Looks like you are using the quadratic equation to solve a function of the form \$a{x}^2+b{x}+c=0\$. You should state the problem more clearly, because "trinomial" is not an accurate description. \$\endgroup\$ – JS1 Jul 4 '16 at 22:03
  • \$\begingroup\$ Okay, thanks. Hopefully this title is better. \$\endgroup\$ – Todd Jul 4 '16 at 22:57
  • 1
    \$\begingroup\$ double discriminant = b * b - 4 * a * c; and double discSqrt = Math.sqrt(discriminant); might make clearer separate variable names than reusing temp1. I'd also move the root-finding out into a separate function that returns an int[]. \$\endgroup\$ – Ry- Jul 4 '16 at 23:15
  • 1
    \$\begingroup\$ My answer to a different question on solving the quadratic equation applies here too. \$\endgroup\$ – Peter Taylor Jul 5 '16 at 10:43
2
\$\begingroup\$

This is just some basic advice but it is too long to put into a comment.

Naming

  • The variable temp should be named discriminant as that is its formal name.

Coding

  • Do not let errors or undesired cases propagate through the code. Handle them once and only once if you can. Doing so makes your code more readable.

    //  process solution
    double discriminant = (b * b) - (4 * a * c);
    if (discriminant < 0) {
        // 
        ans1.setText("Sorry, both roots are complex numbers.");
        ans2.setText("I can not solve that quadratic equation.");
        return;
    }
    
    double rootDiscriminant = Math.sqrt(discriminant);
    x1 = (-b + rootDiscriminant) / (2 * a);
    x2 = (-b - rootDiscriminant) / (2 * a);
    
    //  output solution
    ans1.setText(Double.toString(x1));
    if (x1 != x2) ans2.setText(Double.toString(x2));
    else ans2.setText("Only one root.");
    

Numerical methods

  • You usually do not want to directly compare two doubles for equality. Instead you would test if your discriminant was less than some small value called an epsilon. You define your epsilon to be as small as you want it to be.

    //  output solution
    ans1.setText(Double.toString(x1));
    if (discriminant < epsilon) ans2.setText("Only one root.");
    else ans2.setText(Double.toString(x2));
    

    However, since you are not trying to develop a linear algebra library I would not worry about this now. The bottom line is that you do not want the user to see two answer strings that are equal.

    //  output solution
    String s1 = Double.toString(x1);
    String s2 = Double.toString(x2);
    
    ans1.setText(s1);
    if (s1.equals(s2)) ans2.setText("Only one root.");
    else ans2.setText(s2);
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.