Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class named QuadraticEquation for a quadratic equation ax 2 + bx + c =

ID: 3836556 • Letter: D

Question

Design a class named QuadraticEquation for a quadratic equation ax2 + bx + c = 0.

The class contains:

- Private data fields a, b, and c that represents three coefficients.

-A constructor for the arguments for a. b, and c

-Three methods for a, b, and c

-A method named getDiscriminant() that returns the discriminant, which is b2-4ac

-The method named getRoot1() and getRoot2() for returning two roots of the equation

These methods are useful only if the discriminant is nonnegative. Let these methods return 0 if the discriminant is negative.

Write a test program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display the two roots. If the discriminant is 0, display the one root. Otherwise, display "The equation has no roots."

  

T1 b 2a.

Explanation / Answer

import java.lang.*;
class QuadraticEquation
{
    int a;
    int b;
    int c;
    public QuadraticEquation(int a, int b, int c)
    {
       this.a = a;
       this.b = b;
       this.c = c;
    }
    void setA(int a) { this.a = a; }
    void setB(int b) { this.b = b; }
    void setC(int c) { this.c = c; }
   
    int getA() { return a; }
    int getB() { return b; }
    int getC() { return c; }
   
    int getDiscriminant()
    {
       return b*b - 4*a*c;
    }
    double getRoot1()
    {
       if(getDiscriminant() < 0)
           return 0;
       return (-b + Math.sqrt(getDiscriminant())) / (2*a);
    }
    double getRoot2()
    {
       if(getDiscriminant() < 0)
           return 0;
       return (-b - Math.sqrt(getDiscriminant())) / (2*a);
    }
}

And the test class is:

import java.util.*;
class TestQuadraticEquation
{
    public static void main(String[] args)
    {
       int a, b, c;
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the a, b, c co-efficients separated by space: ");
       a = sc.nextInt();
       b = sc.nextInt();
       c = sc.nextInt();
       QuadraticEquation qe = new QuadraticEquation(a, b, c);
       System.out.println("The roots of the quadratic equation are: " + qe.getRoot1() + " " + qe.getRoot2());
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote