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

As every conscientious algebra student knows, the quadratic formula gives the so

ID: 3855559 • Letter: A

Question

As every conscientious algebra student knows, the quadratic formula gives the solutions to quadratic equations of the form ax^2 + bx + c = 0. Not many people are aware that a formula also exists to solve cubic equations of the form x^3 + ax^2 + bx + c = 0. This formula is called Cardano's formula, after the Italian mathematician who allegedly derived it several hundred years ago. The formula says that the solution^1 is given by x = u - p/3u - a/3, where p = b - a^2/3 q = c + 2a^3 - 9ab/27 u = 3 Squareroot - q/2 + Squareroot q^2/4 + p^3/27 a. Using Java's built-in Math.pow and Math. squareroot methods, write a method that takes parameters for the coefficients a, b, and c, then returns the solution to the cubic equation. I suggest starting by calculating the values of p, q, and u, and storing them into appropriate variables. b. Within the same source code file as your solution for (a), write a main method that allows the user to enter values for a, b, and c, then calls your method from (a) and displays the result. If you're mathematically inclined, you may be wondering what Cardano was smoking, because this formula gives only one value of x. "But dude!" you say. "A cubic equation should have THREE roots'" It's possible to find the other roots too, but don't worry about it for the purposes of this assignment.

Explanation / Answer

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