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

OBJECTIVES: Read data from the keyboard. Use arithmetic operators Use conditiona

ID: 3748462 • Letter: O

Question

OBJECTIVES:
Read data from the keyboard.
Use arithmetic operators
Use conditional logic (if, else, and else if)
PROGRAM DESCRIPTION:
For this program, you’ll prompt the user to enter the coefficients and constant term of a
quadratic equation, in standard form, and determine the number and type of solutions.
Then find and display the solution.
PROGRAM SPECIFICATIONS:
1. Create a new class called QuadraticSolver
2. Consider the code below
if (num > 0)
System.out.println("The number is positive!");
else if (num == 0)
System.out.print("The number is neither positive nor negative!");
else
System.out.print("The number is negative!");
3. Modify the example code by following the implementation below:
a. Declare a Scanner variable called input.
b. Declare three double variables called a, b, and c.
c. Prompt the user to enter a value for each constant and assign the value to a, b, and c.
d. Find the value of the discriminant (2 4) to determine and display the number
and type of solutions.
i. If 2 4 > 0, then there are two real solutions.
ii. If 2 4 = 0, then there is one real solution.
iii. If 2 4 < 0, then there are two complex solutions.
e. Find the solution using the quadratic formula ( =
±24
2
). Then display the
solution(s), using the printf method, rounded to two decimal places.
SAMPLE OUTPUT:
This program solves a quadratic equation in standard form
a*x^2 + b*x + c = 0
Please enter the following:
Quadratic coefficient a: 1
Linear coefficient b: 2
Constant c: 3
The quadratic equation has two complex solutions:
x = -1.0000 + 1.4142i
x = -1.0000 – 1.4142i
CODING STANDARDS:
1. Use meaningful variable names.
2. Remove the comments generated by NetBeans or any other IDE.
3. Write a comment that includes your name and the date of your last revision. In addition,
write comments that describe the contents of your code.
4. Your output should be user-friendly.
5. Your code must be well-organized and easy to read.

Explanation / Answer

import java.util.Scanner;

public class Main
{  
public static void main(String[] args)
{
        Scanner input=new Scanner(System.in);
        double a,b,c,d;
        System.out.println(" This program solves a quadratic equation in standard form ");
        System.out.println("a*x^2 + b*x + c = 0 ");


        System.out.println("Please enter the following: ");
        System.out.println("Quadratic coefficient a:");
        a=input.nextDouble();
        System.out.println("Linear coefficient b:");
        b=input.nextDouble();
        System.out.println("Constant c:");
        c=input.nextDouble();
        d=(b*2)-(4*a*c);//calculating D
        if(d>0)
        {
            System.out.println("This Equation has two real solutions");
            System.out.format(" X=%f",(-b+Math.sqrt(d))/(2*a));
            System.out.format(" X=%f",(-b-Math.sqrt(d))/(2*a));
        }
        else if(d==0)
        {
            System.out.println("This Equation has one real solutions");
            System.out.format(" X=%f",(-b+Math.sqrt(d))/(2*a));
        }
        else
        {
            System.out.println("This Equation has two complex solutions");
            System.out.format(" X=%f + i(%f)",(-b / (2 *a)),(Math.sqrt(-d) / (2 * a)));
            System.out.format(" X=%f - i(%f)",(-b / (2 *a)),(Math.sqrt(-d) / (2 * a)));
        }
}
}