Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //QuadraticEquation.java MrG 2011.1024
- public class QuadraticEquation
- {
- private int a;
- private int b;
- private int c;
- /**
- Constructor: instantiate the integer coefficients of the equation
- @param a coefficient of x^2
- @param b coefficient of x^1
- @param c coefficient of x^0
- */
- public QuadraticEquation(int a, int b, int c)
- {
- this.a = a;
- this.b = b;
- this.c = c;
- }
- /**
- Accessor: calculates the discriminant
- @return b^2-4ac
- */
- public int getDisc()
- {
- return b*b - 4*a*c;
- }
- /**
- Accessor: calculates one root
- @return (-b+sqrt(d))/(2a)
- */
- public double getRoot1()
- {
- return (-b+Math.sqrt(getDisc()))/(2*a);
- }
- /**
- Accessor: calculates another root
- @return (-b-sqrt(d))/(2a)
- */
- public double getRoot2()
- {
- return (-b-Math.sqrt(getDisc()))/(2*a);
- }
- /**
- Accessor: true iff roots are real
- @return getDisc()>=0
- */
- public boolean hasSolutions()
- {
- if(getDisc()<0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement