WAP in java to execute the following program.Need the complete source code and the program must include the use of user defined functions as per the question.
Answers
Quadratic Equation Solver - Java
Question:
The roots of a quadratic equation of the form A + B + C = 0 are given by the formulae:
Using user defined functions, write a program in Java to compute the two roots for any values of A, B and C. The program should print "No solution" if
Answer:
We will use Scanner to take user input for the values of A, B and C. We will store them as double data type to account for all possible types of real numerical values.
After this, we have four user defined functions created in the program:
1) static double discriminant(double A, double B, double C)
This function takes A, B and C as input and returns the Discriminant .
2) static boolean areRootsImaginary(double D)
This function takes D as the input parameter and returns true if
This is the case of No solution. It returns false if the roots are real.
3) static double root1(double A, double B, double D)
It returns the first root:
4) static double root2(double A, double B, double D)
It returns the second root:
After this we have the main() function where we take the User Input and call the other four user defined functions to generate the required output.
The Java Program Code
import java.util.Scanner; //Importing Scanner
public class QuadraticEquation //Creating Class
{
//Function to calculate Discriminant
static double discriminant(double A, double B, double C)
{
double D = (B*B)-4*A*C;
return D;
}
//Function to check if roots are imaginary
//It returns true if B^2-4AC < 0
static boolean areRootsImaginary(double D)
{
if(D < 0)
return true;
else
return false;
}
//Function to compute the first root
static double root1(double A, double B, double D)
{
double n1 = ((-1*B)+Math.sqrt(D))/(2*A);
return n1;
}
//Function to compute the second root
static double root2(double A, double B, double D)
{
double n2 = ((-1*B)-Math.sqrt(D))/(2*A);
return n2;
}
//The MAIN Function
public static void main(String args[])
{
//Create Scanner Object
Scanner sc = new Scanner(System.in);
System.out.println("Standard Form of Quadratic Equation in n is:");
System.out.println("An^2 + Bn + C = 0 \n");
//Take User Input for A, B and C
System.out.print("Enter the value of A: ");
double A = sc.nextDouble();
System.out.print("Enter the value of B: ");
double B = sc.nextDouble();
System.out.print("Enter the value of C: ");
double C = sc.nextDouble();
//Calculate Discriminant by calling function
double D = discriminant(A, B, C);
System.out.println(); //Printing a Blank Line for Aesthetics
//Validate Existence of Solution
//Call Function to check if roots are imaginary
//If true, then no real solution exists
if(areRootsImaginary(D))
{
System.out.println("No Solution.");
}
else //Condition of Real Roots
{
//Call the root computing functions and print the roots
System.out.println("First Root n1 = "+root1(A, B, D));
System.out.println("Second Root n2 = "+root2(A, B, D));
}
}
}
Program Output
Here are three different runs of the program:
- First Run
Standard Form of Quadratic Equation in n is:
An^2 + Bn + C = 0
Enter the value of A: 4.56
Enter the value of B: -5.3
Enter the value of C: 1.24
First Root n1 = 0.8376443135293584
Second Root n2 = 0.32463638822502755
- Second Run
Standard Form of Quadratic Equation in n is:
An^2 + Bn + C = 0
Enter the value of A: 1
Enter the value of B: -6
Enter the value of C: 9
First Root n1 = 3.0
Second Root n2 = 3.0
- Third Run
Standard Form of Quadratic Equation in n is:
An^2 + Bn + C = 0
Enter the value of A: 2
Enter the value of B: 4
Enter the value of C: 7
No Solution.
Screenshots of the Java Program Code and the Output Runs have been attached.