Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.0 kB
2
Indexable
Never
package ru.samsung.itschool.mdev.homework;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    public EditText objectA, objectB, objectC;
    public TextView res;

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

        res = (TextView) findViewById(R.id.res);
    }

    private void printRoots(double root1, double root2) {
        res.setText(root1 + " " + root2);
    }
    private void printRoots(double root) {
        res.setText(String.valueOf(root));
    }
    private void printRoots(boolean hasRoots) {
        res.setText(hasRoots ? "any" : "none");
    }

    public void solve(View v) {
        double a = Double.parseDouble(((EditText)findViewById(R.id.a)).getText().toString());
        double b = Double.parseDouble(((EditText)findViewById(R.id.b)).getText().toString());
        double c = Double.parseDouble(((EditText)findViewById(R.id.c)).getText().toString());


        if (a == 0) {
            if (b == 0) {
                printRoots(c == 0);
                return;
            }
            printRoots(-c/b);
            return;
        }

        double root1, root2;
        double discriminant = Math.pow(b, 2) - 4 * a * c;
        if (discriminant < 0) {
            printRoots(false);
            return;
        }
        if (discriminant == 0) {
            root1 = -b/2*a;
            printRoots(root1);
            return;
        }
        root1 = (-b-Math.sqrt(discriminant))/(2*a);
        root2 = (-b+Math.sqrt(discriminant))/(2*a);
        if (root1 == -0.0) root1 = 0.0;
        if (root2 == -0.0) root2 = 0.0;
        printRoots(root1, root2);
    }
}
Leave a Comment