Can you indicate what will go in the command window and what will go in the scri
ID: 3811443 • Letter: C
Question
Can you indicate what will go in the command window and what will go in the script window. ThanksThe roots of a quadratic equation ax2 bx given by the formula c 0 are 4 ac 2a write a function that take as input the coefficients a, b,c and returns as output the roots x1,x2 o the quadratic equation. Let x1 be the smaller of the two roots. -If a is zero, print a warning message user of the situation. But, then solve the linear informing the equation for the single root and set x2 NaN If b2 4ac is negative, print a warning message informing the user of the situation. But, then solve the equation anyways to find and return the complex roots. If b 4ac is zero, set x2 equal to the unique root x1. If any of a, b, c are not scalar numbers, print an error message informing the user of the situation and set x x2 NaN
Explanation / Answer
import java.util.InputMismatchException;
import java.util.Scanner;
public class QuadEquation
{
public static void main(String[] args)
{
//Declared required variables for quadratic equation
int a, b, c;
double x1, x2, d;
Scanner sc = new Scanner(System.in);
//Taken Input from user to find roots of quadratic equation
try
{
System.out.println("Enter values for quadratic equation as :ax^2 + bx + c");
System.out.print("Enter value of a:");
a = sc.nextInt();
System.out.print("Enter value of b:");
b = sc.nextInt();
System.out.print("Enter value of c:");
c = sc.nextInt();
System.out.println("Given quadratic equation:"+a+"x^2 + "+b+"x + "+c);
//Calculated value of d
d = b * b - 4 * a * c;
// Warning for a=0
if(a==0)
{
System.out.println("Warning: You have enterd value of a is Zero");
}
// Conditions checked for different values of d
if(d > 0)
{
System.out.println("Roots are real and unequal");
x1 = ( - b + Math.sqrt(d))/(2*a);
x2 = (-b - Math.sqrt(d))/(2*a);
System.out.println("First root is:"+x1);
System.out.println("Second root is:"+x2);
}
else if(d == 0)
{
System.out.println("Roots are real and equal");
x1 = (-b+Math.sqrt(d))/(2*a);
System.out.println("Root: "+x1);
}
else
{
System.out.println("Roots are imaginary");
x1 = -b/(2*a);
x2 = Math.sqrt(-d)/(2*a);
System.out.println
("Root 1 = "+x1+" + i "+x2);
System.out.println
("Root 2 = "+x2+" - i "+x2);
}
// If scalar values are entered
}
catch(InputMismatchException e)
{
System.out.println("You have enterd non scalar value of coefficient");
System.out.println("Root1= NaN and Root2= NaN");
}
}
}