Programming Examples

Java program to create a class qurd to solve the quadratic equation


A Class quad contains the following data members and member function s to find the roots of quadratic equation.

Class Name : quad

Data Members:

a,b,c(float) x,y(float)

Member Function/Method:

quad(float,float,float): constructor to assign values to the data members

float discriminant(): to return the discriminant [b1-4ac]

void root_equal(): to display the root if both roots are equal.

void image(): to display the root if roots are imaginary.

void root_real(): to display the two real, unequal roots.

void root(): to call other appropriate functions to find the solutions of the problem.

Solution

import java.util.*;
class quad
{
    float a,b,c,x1,x2;
    quad(float k1,float k2,float k3)
    {
        
        a=k1;
        b=k2;
        c=k3;
    }
    float discriminant()
    {
        float dis;
        dis=(b*b)-(4*a*c);
        return dis;
    }
    void root_equal()
    {
        float d=discriminant();
        if(d==0)
        {
            System.out.println("ROOTS ARE EQUAL:"+x1+" "+x2);
        }
    }
    void image()
    {
        float d=discriminant();
        if(d<0)
        {
            System.out.println("ROOTS ARE IMAGINARY:"+x1+" "+x2);
        }
    }
    void root_real()
    {
        float d=discriminant();
        if(d>0)
        {
            System.out.println("ROOTS ARE REAL:"+x1+" "+x2);
        }
    }
    void root()
    {
        float d=discriminant();
        x1=((-b)+(float)Math.sqrt(d))/(2*a);
        x2=((-b)-(float)Math.sqrt(d))/(2*a);
    }
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 3 no");
        float a=sc.nextFloat();
        float b=sc.nextFloat();
        float c=sc.nextFloat();
        quad ob=new quad(a,b,c);
        ob.root();
        ob.root_equal();
        ob.image();
        ob.root_real();
    }
}
Output

Enter 3 no

1

7

3

ROOTS ARE REAL:-0.45861864 -6.5413814

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz