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

Write a program that generates random numbers representing the three coefficient

ID: 3745591 • Letter: W

Question

Write a program that generates random numbers representing the three coefficients a, b, and c of the quadratic equation. The values of a should include numbers 1 through 10, the values of b and c should include numbers -5 through 5. The value of the discriminant will determine if the root of the equation will contain two real roots, one real root, or two complex roots. Print the values of a, b, and c. Print the value of the discriminant.

Print an appropriate message for each case. If two real roots are calculated, print the values appropriately. If only one real root is calculated, print a message that only the one root is produced and print the value.

Your program should not calculate the roots if the coefficients will produce complex roots based on the value of the discriminant. Simply print a message that the roots are complex.

Requirements:

Include the required heading as stated in Creating and Submitting Projects in Eclipse

Use of internal documentation

Use the DecimalFormat class to format the roots. There should always be a digit to the left of the decimal point and at least 1 digit to the right of the decimal point. Print up to three decimal places if the digits are not zero. See sample output below.

Use the pow() method and sqrt() method from the Math class

Use the Random class to assign values to a, b, and c

Sample Output:

The coefficients are:

a = 4 b = 4 c = 1

The discriminant is 0.0

Values produce only one root: -0.5

The coefficients are:

a = 9 b = 1 c = 0

The discriminant is 1.0

The roots are: 0.0 and -0.111

The coefficients are:

a = 2 b = 2 c = 2

The discriminant is -12.0

The roots are complex

Explanation / Answer

//Class Roots is nested under Roots/src where Roots is the project name

//created under Eclipse

import java.text.DecimalFormat;

import java.util.Random;

import java.lang.Math;

public class Roots {

public static void main(String[] args){

Random rand = new Random();

//rand.nextInt(i) generates random Ints between 0 to i-1 (both included)

//generating 'a' between 1 to 10 (both 1 and 10 included)

int a = 1 + rand.nextInt(10);

//generating 'b' between -5 to 5 (both -5 and 5 included)

int b = -5 + rand.nextInt(11);

//generating 'c' between -5 to 5 (both -5 and 5 included)

int c = -5 + rand.nextInt(11);

System.out.println("The coefficients of are: ");

System.out.println("a = "+a+" b = "+b+" c = "+c);

//Calculating the discriminant

// D = b^2 - 4*a*c

double dis = Math.pow((double)b,2) - 4*(double)a*c;

System.out.println("The discriminant is " + dis);

//Required formating for printing the roots

DecimalFormat df = new DecimalFormat("0.0##");

if(dis < 0.0) {

System.out.println("The roots are complex");

}

else if(dis == 0.000) {

double root = (-1.0*(double)b)/(2*(double)a);

System.out.println("Values produce only one root: " + df.format(root));

  

}

else {

double root1,root2;

root1 = (-1.0*(double)b + Math.sqrt(dis))/(2*a);

root2 = (-1.0*(double)b - Math.sqrt(dis))/(2*a);

System.out.println("The roots are: "+ df.format(root1) + " and "+df.format(root2));

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote