
(Algebra: solve quadratic equations) The two roots of a quadratic equation ax2 + bx + c = 0 can be obtained using the following formula:
b2 − 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots.
Write a
Note you can use Math. pow(x, 0.5) to compute

Algebra: solve quadratic equation
Program Plan:
- Import the required packages.
- Create a class Exercise
- Define the main method.
- Define the scanner input and prompt user to enter the value of a,b,c.
- Get the input.
- Calculate the discriminant value.
- Condition to validate the discriminant value.
- After validation the value gets of the root gets calculated based on the condition.
- Display the result of roots.
The below program is used to solve the quadratic equation:
Explanation of Solution
Program:
//import the required packages
import java.util.Scanner;
//define the class exercise
public class Exercise
{
public static void main(String[] args)
{
//scanner input
Scanner input = new Scanner(System.in);
/*prompt user to enter the value of the a,b and c*/
System.out.print("Enter a, b, c: ");
//get value of a
double a = input.nextDouble();
//get value of b
double b = input.nextDouble();
//get value of c
double c = input.nextDouble();
//equation to calculate the discriminant
double discriminant = b * b - 4 * a * c;
//condition to validate the discriminant value
if (discriminant < 0)
{
//display result
System.out.println("The equation has no real roots");
}
//condition to validate the discriminant value
else if (discriminant == 0)
{
//equation to calculate the root value
double r1 = -b / (2 * a);
//display result
System.out.println("The equation has one root " + r1);
}
//condition to validate the discriminant value
else
{
// (discriminant > 0)
//equation to calculate the root value
double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
//equation to calculate the root value
double r2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);
//display result
System.out.println("The equation has two roots " + r1 + " and " + r2);
}
}
}
Enter a, b, c: 1
2.0
1
The equation has one root -1.0
Additional Output 1:
Enter a, b, c: 1.0
3
1
The equation has two roots -0.3819660112501051 and -2.618033988749895
Additional Output 2:
Enter a, b, c: 1
2
3
The equation has no real roots
Want to see more full solutions like this?
Chapter 3 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Starting Out With Visual Basic (8th Edition)
Management Information Systems: Managing The Digital Firm (16th Edition)
SURVEY OF OPERATING SYSTEMS
INTERNATIONAL EDITION---Engineering Mechanics: Statics, 14th edition (SI unit)
Concepts Of Programming Languages
Problem Solving with C++ (10th Edition)
- I need to make a parallel version of this sequential code.arrow_forwardBenefits of using arrays as instance variables: What are the advantages of incorporating arrays as instance variables within a class? Initializing and managing arrays: How do you initialize and manage arrays within class constructors and mutators (setters)? Example of using arrays as instance variables: Share an example where you have used arrays as instance variables and discuss its application in a real-world scenario. Common mistakes with arrays as instance variables: What are some common mistakes to avoid when working with arrays as instance variables? Information hiding violations: What is the potential violation of information hiding when using arrays as instance variables? How can this be resolved?arrow_forwardDo you think that computers should replace teachers? Give three references with your answer.arrow_forward
- Is online learning or face to face learning better to teach students around the around the world? Give reasons for your answer and provide two references with your response. What are benefits of both online learning and face to face learning ? Give two references with your answer. How does online learning and face to face learning affects students around the world? Give two references with your answer.arrow_forwardExplain Five reasons if computers should replace teachers. Provide three references with your answer. List three advantages and three disadvantages face to face learning and online learning may have on children. Provide two references with your answer.arrow_forwardYou were requested to design IP addresses for the following network using the address block 10.10.10.0/24. Specify an address and net mask for each network and router interfacearrow_forward
- For the following network, propose routing tables in each of the routers R1 to R5arrow_forwardFor the following network, propose routing tables in each of the routers R1 to R5arrow_forwardUsing R language. Here is the information link. http://www.cnachtsheim-text.csom.umn.edu/Kutner/Chapter%20%206%20Data%20Sets/CH06PR18.txtarrow_forward
- Using R languagearrow_forwardHow can I type the Java OOP code by using JOptionPane with this following code below: public static void sellCruiseTicket(Cruise[] allCruises) { //Type the code here }arrow_forwardDraw a system/level-0 diagram for this scenario: You are developing a new customer relationship management system for the BEC store, which rents out movies to customers. Customers will provide comments on new products, and request rental extensions and new products, each of which will be stored into the system and used by the manager for purchasing movies, extra copies, etc. Each month, one employee of BEC will select their favorite movie pick of that week, which will be stored in the system. The actual inventory information will be stored in the Entertainment Tracker system, and would be retrieved by this new system as and when necessary. Example of what a level-0 diagram looks like is attached.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTCOMPREHENSIVE MICROSOFT OFFICE 365 EXCEComputer ScienceISBN:9780357392676Author:FREUND, StevenPublisher:CENGAGE L



