Sunday, 2 June 2013

Create a java program to solve quadratic eqations

Create a java program to solve quadratic eqations

Solving a quadratic equation
The program must have two methods quadraticEquationRoot1 which takes as input 3 doubles, representing a,b,c and returns the larger of the two roots and quadraticEquationRoot 2 which takes as input 3 doubles, representing a,b, and c (in that order) and returns the smaller of the two roots.
We assume that the numbers a,b,c are chosen so that the square root is never the square root of a negative number
I have the following written down so far. I am not sure on how to introduce the second method
public static void main(string args[]){
}

public static double quadraticEquationRoot1(int a, int b, int c) (){
}

if(Math.sqrt(Math.pow(b, 2) - 4*a*c) == 0)
{
   return -b/(2*a);
}else
{
   int root1, root2;
   root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
   root2 = (-b - Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
   return Math.max(root1, root2);
}
}

No comments:

Post a Comment