Advertisement
calcpage

C5X1_QuadraticEquation.java

Nov 14th, 2011
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. //QuadraticEquation.java    MrG 2011.1024
  2. public class QuadraticEquation
  3. {
  4.     private int a;
  5.     private int b;
  6.     private int c;
  7.  
  8.     /**
  9.     Constructor: instantiate the integer coefficients of the equation  
  10.     @param a coefficient of x^2
  11.     @param b coefficient of x^1
  12.     @param c coefficient of x^0
  13.     */
  14.     public QuadraticEquation(int a, int b, int c)
  15.     {
  16.         this.a = a;
  17.         this.b = b;
  18.         this.c = c;
  19.     }
  20.  
  21.     /**
  22.     Accessor: calculates the discriminant
  23.     @return b^2-4ac
  24.     */
  25.     public int getDisc()
  26.     {
  27.         return b*b - 4*a*c;
  28.     }
  29.  
  30.     /**
  31.     Accessor: calculates one root
  32.     @return (-b+sqrt(d))/(2a)
  33.     */
  34.     public double getRoot1()
  35.     {
  36.         return (-b+Math.sqrt(getDisc()))/(2*a);
  37.     }
  38.  
  39.     /**
  40.     Accessor: calculates another root
  41.     @return (-b-sqrt(d))/(2a)
  42.     */
  43.     public double getRoot2()
  44.     {
  45.         return (-b-Math.sqrt(getDisc()))/(2*a);
  46.     }
  47.  
  48.     /**
  49.     Accessor: true iff roots are real
  50.     @return getDisc()>=0
  51.     */
  52.     public boolean hasSolutions()
  53.     {
  54.         if(getDisc()<0)
  55.         {
  56.             return false;
  57.         }
  58.         else
  59.         {
  60.             return true;
  61.         }
  62.     }
  63. }
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement