In: Computer Science
Write a Java program that solves a set of quadratic equations as per Requirement #1 (without using global variables), and reads an integer number entered in the command line, as follows: > java program_name input_number and depending on the value of this number does the following:
1) If the number entered is positive, the program shall solve (running in a loop) as many quadratic equations of the following form, as the input_number tells:
a * x2 + b * x + c = 0
where coefficients a, b and c are double values, entered from the keyboard as per Requirement #3.
2) If the input number entered in the command line is not positive, the program shall print the following message to the screen and exit:
A positive value shall be entered in the command line.
3) The values of coefficients a, b and c, as listed in Requirement #1, shall be read from the keyboard by the program with the following message on the screen, each time a new coefficient is needed:
Please enter the next coefficient of a quadratic equation:
4) If the coefficient a is a zero, the program shall display the following message on the screen (first line) repeating the request for another value of a (second line):
Coefficient a cannot be a zero. Enter the correct value. Please enter the next coefficient of a quadratic equation:
5) When the program solves the quadratic equation (calculates the roots), as per Requirement #1, the following output shall be displayed on the screen:
<one empty line>
Quadratic equation with the following coefficients
a: <value>; b: <value>; c: has
the following roots: <root1>and <root2>
6) In case the quadratic equation for given coefficients does not have roots, the program shall print the following message, and continue operation for the next set of coefficients:
<one empty line>
Quadratic equation with the following coefficients:
a: <value>; b: <value>; c: <value> does not have roots
Code is Given Below:
============================
import java.util.Scanner;
public class Quadratic {
public static void main(String[] args) {
//creating scanner object to get
input from user
Scanner scn=new
Scanner(System.in);
//asking user to input
System.out.println("Quadratic
Equation solver");
System.out.print("Enter Positive
number: ");
int n=scn.nextInt();
//cheking if n is negative
if(n<=0) {
System.out.println("A positive value shall be entered in the
command line.");
return;
}
int i=1;
//loop will run n times
while(n>=i) {
//asking user
for input
System.out.print("Please enter the next coefficient of a quadratic
equation: ");
double
a=scn.nextDouble();
if(a==0) {
System.out.println("Coefficient a cannot be a
zero. Enter the correct value. ");
continue;
}
System.out.print("Please enter the next coefficient of a quadratic
equation: ");
double
b=scn.nextDouble();
System.out.print("Please enter the next coefficient of a quadratic
equation: ");
double
c=scn.nextDouble();
//finding
discreminant
double d=b*b -
4*a*c;
//checking if
equation has root or not
if(d<0)
{
System.out.println("a: <"+a+">; b:
<"+b+">; c: <"+c+"> does not have roots");
i++;
System.out.println();
continue;
}
//finding
roots
double root1 =
(-b + Math.sqrt(d))/2*a;
double root2 =
(-b - Math.sqrt(d))/2*a;
//displaying
result
System.out.println("a: <"+a+">; b: <"+b+">; c:
<"+c+"> has the following root");
System.out.println("<"+root1+"> and
<"+root2+">");
i++;
System.out.println();
}
}
}
Output:
===========
Code Snapshot:
==================