HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

An app that uses the quadratic formula to solve ax^2+bx+c=0 so long that the answers aren't imaginary

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theappanswerslongusesarenthatsolvequadraticformula

Problem

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(Doubl

Solution

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);

Code Snippets

//  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.");
//  output solution
ans1.setText(Double.toString(x1));
if (discriminant < epsilon) ans2.setText("Only one root.");
else ans2.setText(Double.toString(x2));
//  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);

Context

StackExchange Code Review Q#133870, answer score: 2

Revisions (0)

No revisions yet.