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

Write a method called quadratic that solves quadratic equations and prints their

ID: 642778 • Letter: W

Question

Write a method called quadratic that solves quadratic equations and prints their roots. Recall that a quadratic equation is a polynomial equation in terms of a variable x of the form a x2 + b x + c = 0.The formula for solving a quadratic equation is:

the code i came up with works except it wants a return in int not double:

public double quadratic(int a, int b, int c) {
    int root1 = (-b + Math.sqrt(Math.pow(b,2) - 4 * a * c) / 2 * a);
    return(root1);
}

Write a method called quadratic that solves quadratic equations and prints their roots. Recall that a quadratic equation is a polynomial equation in terms of a variable x of the form a x2 + b x + c = 0.The formula for solving a quadratic equation is: x = -b plus minus root b^2 - 4ac/2a the code i came up with works except it wants a return in int not double: public double quadratic(int a, int b, int c) { int root1 = (-b + Math.sqrt(Math.pow(b,2) - 4 * a * c) / 2 * a); return(root1); }

Explanation / Answer

//Note : Cast the value from double to int

//Loss of precision (decimal values of a result value).

//There are two roots for a quadratic equation

    //Returns the first root value
   public int quadraticRoot1(int a, int b, int c)
   {
       // cast the final double value to int. There may be loss of precision.
        int root1 = (int)(-b + Math.sqrt(Math.pow(b,2) - 4 * a * c)) / 2 * a;
        return(root1);
   }
  
   //Returns the second root value
   public int quadraticRoot2(int a, int b, int c)
   {
       // cast the final double value to int. There may be loss of precision.
        int root2 = (int)(-b - Math.sqrt(Math.pow(b,2) - 4 * a * c)) / 2 * a;
        return(root2);
   }

---------------------------------------------------------------------------------------------------------

//Testing the methods using java program


//Java program that tests the method quadraticRoot1
//and quadraticRoot2 for three values a,b and c
//QuadraticEquation.java
public class QuadraticEquation
{
   public static void main(String[] args)
   {
      
       //set values for a,b and c of a equation
       int a=1;
       int b=-3;
       int c=-4;
  
      
       //Note : roots depends on the descrimination value
       //Descrimination = pefect square, two real solutions exists
       //Descrimination = 0, One solution exists      
       //Descrimination > 0, Two real solutions exists
       //Descrimination < 0, Two imaginary solutions exists
       //Descrimination = Positive but not a perfect squre,
       //Two irrational solutions exists
      
        //Calling two static methods quadraticRoot1 and quadraticRoot2 with a,b and c values as

       //arguments and returns the root values
       System.out.println("Root 1 : "+quadraticRoot1(a, b, c));
       System.out.println("Root 2 : "+quadraticRoot2(a, b, c));
      
   }
  
   //Returns the first root value
   public static int quadraticRoot1(int a, int b, int c)
   {
       // cast the final double value to int. There may be loss of precision.
        int root1 = (int)(-b + Math.sqrt(Math.pow(b,2) - 4 * a * c)) / 2 * a;
        return(root1);
   }
  
   //Returns the second root value
   public static int quadraticRoot2(int a, int b, int c)
   {
       // cast the final double value to int. There may be loss of precision.
        int root2 = (int)(-b - Math.sqrt(Math.pow(b,2) - 4 * a * c)) / 2 * a;
        return(root2);
   }
}


----------------------------------------------------------------------------------------------------

Sample output:

Root 1 : 4
Root 2 : -1

Hope this helps you

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