As every conscientious algebra student knows the quadratic f

As every conscientious algebra student knows, the quadratic formula gives two solutions to each quadratic solution of the form ax^2 + bx + c = 0 (where a is non-zero). The solutions are given by r = -b Squareroot b^2 - 4ac/2a a. Using Java\'s built-in Math.pow and Math- squareroot methods, write a method called printRealRoots that takes parameters for the coefficients a, b, and c. The method should first test the value of b^2 - 4ac to determine whether it is positive, zero, or negative: If positive, then there are two real roots, and the method should calculate and print both roots and return the integer two (meaning that two real roots were printed). If zero, then there is one real root and the method should calculate and print that root and return the integer one (meaning that one real root was printed). If negative, then there are no real roots and the method should print \"No real roots!\" and return the integer zero (meaning that there are no real roots). b. Within the same source code file as your solution for pin (a), write a main method that prompts the user to enter values for a, b, and c that describe a parabola with two real roots. Your main method should then call your method from part (a), passing as arguments the values of a, b, and c entered by the user. Continue prompting the user for new values of a, b, and c and calling the method from part (a) until the value returned by the method from part (a) equals two (meaning that two real roots were printed).

Solution

QuadraticEquationRoots.java

import java.util.Scanner;

public class QuadraticEquationRoots {

   public static void main(String[] args) {
       //Declaring variables
       int a = 0,b = 0,c = 0,num;
      
       //Scanner class object is used read the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
      
      
       //This loop continues to execute until the method returns 2
       while(true)
       {
           //Getting the number \'a\' entered by the user
           System.out.print(\"\ Enter Number a:\");
           a=sc.nextInt();
          
           //Getting the number \'b\' entered by the user
           System.out.print(\"Enter Number b:\");
           b=sc.nextInt();
          
           //Getting the number \'c\' entered by the user
           System.out.print(\"Enter Number c:\");
           c=sc.nextInt();  
          
       //calling the method by passing the a,b,c as inputs  
       num=printRealRoots(a,b,c);
      
       /*checking the return value of the method is 2 or not
       * if the method returned 2,Then program will exit.
       * If not,program will continue to call the method printRealRoots()
       */
       if(num==2)
           break;
       else
           continue;
       }

   }

   /* This method will calculate the roots of the quadratic equation
   * Params : a,b,c
   * Return : integer
   */
   private static int printRealRoots(int a, int b, int c) {
      
      
       //Declaring variables
       double rootx=0,rooty=0;int val=0;
       double discriminate=Math.pow(b, 2)-4*a*c;
      
       double part1=Math.sqrt(Math.pow(b, 2)-4*a*c);
       //If the discriminate is positive calculate the two real roots an return 2 to the caller
       if(discriminate>0)
       {
             
       //calculating the two real roots
       rootx=((-b+part1)/(2*a));
       rooty=((-b-part1)/(2*a));
      
       //Displaying the two real roots
       System.out.printf(\"The Two roots are %.2f %.2f \",rootx,rooty);
       val=2;
       }
       //If the discriminate is equal to zero calculate the single root an return 1 to the caller
       else if(discriminate==0)
       {
          
       //calculating the single root  
       rootx=   ((-b+part1)/(2*a));
      
       //Displaying the single root
       System.out.println(\"The Equation has one real root :\"+rootx);
       val=1;
       }
       //If the discriminate is less than zero return 0 to the caller
       else if(discriminate<0)
       {
           //Displaying the message
           System.out.println(\"::No Real Roots::\");
           val=0;
       }
       return val;
      
   }

}

_________________________________________

Output:


Enter Number a:3
Enter Number b:2
Enter Number c:5
::No Real Roots::

Enter Number a:2
Enter Number b:8
Enter Number c:8
The Equation has one real root :-2.0

Enter Number a:1
Enter Number b:-3
Enter Number c:-4
The Two roots are 4.00 -1.00

_________________________Thank You

 As every conscientious algebra student knows, the quadratic formula gives two solutions to each quadratic solution of the form ax^2 + bx + c = 0 (where a is no
 As every conscientious algebra student knows, the quadratic formula gives two solutions to each quadratic solution of the form ax^2 + bx + c = 0 (where a is no
 As every conscientious algebra student knows, the quadratic formula gives two solutions to each quadratic solution of the form ax^2 + bx + c = 0 (where a is no

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site