Android : Calculer aX2+bX+c

Créer une application qui permet de calculer les solutions de l'équation du deuxième degrée aX2+bx+c

1.Layout

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="25dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="6"

android:background="#fff">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:text="F(x)="/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:id="@+id/a"
android:background="#f00"
android:text="a"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:text="x²"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:background="#f00"
android:id="@+id/b"
android:text="b"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:text="x"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:background="#f00"
android:id="@+id/c"
android:text="c"/>

</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
android:id="@+id/begal"
android:text="="/>

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/eqsolution"
android:text="Solution"/>
</LinearLayout>

2.Java



import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class EquationSolution extends AppCompatActivity implements View.OnClickListener {

EditText a,b,c;
Button begal;
TextView eqsolution;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_equation_solution);

//liaison entre les objets graphiques les objets java
a=findViewById(R.id.a);
b=findViewById(R.id.b);
c=findViewById(R.id.c);
begal=findViewById(R.id.begal);
eqsolution=findViewById(R.id.eqsolution);

//ecouter les boutons
begal.setOnClickListener(this);
}

@Override
public void onClick(View view) {
//si on a cliqué sur egale
if(view.getId()==begal.getId())
{
if(!a.getText().toString().isEmpty() &&
!b.getText().toString().isEmpty() &&
!c.getText().toString().isEmpty() )
{
float aa=Float.parseFloat(a.getText().toString());
float bb=Float.parseFloat(b.getText().toString());
float cc=Float.parseFloat(c.getText().toString());

//calculer Delta
float delta=bb*bb + (4*aa*cc);
if(delta>0)
{
double x1=(-bb- Math.sqrt(delta))/(2*aa);
double x2=(-bb+ Math.sqrt(delta))/(2*aa);
eqsolution.setText("x1="+x1+"\n x2="+x2);

}
else if(delta==0)
{
double x1=-bb/(2*aa);
eqsolution.setText("x1="+x1);

}
else {
eqsolution.setText("pas de solution dans N");
}
}
else
{
eqsolution.setText("Erreur il faut donnée a,b et c");
}


}
}
}