• 12-22-2009, 01:57 AM
this is my quadratic equation code i need to make it give Rational numbers result:

Code:

```        public static void main(String[]args){         // Create a Scanner     Scanner input = new Scanner (System.in);         // Prompt the user to enter three double intergers.     System.out.print("Enter a:");     double a = input.nextDouble();     System.out.println("Enter b:");     double b = input.nextDouble ();     System.out.println("Enter c:");     double c = input.nextDouble();     double discriminat = Math.pow(b,2) - 4*a*c;     double x1 = (-b + Math.sqrt(discriminat))/(2*a);     double x2 = (-b - Math.sqrt(discriminat))/(2*a);     double i=Math.sqrt(-1);     double x3 = (-b + (Math.sqrt(Math.abs(discriminat))))/(2*a);     double x4 = (-b + (Math.sqrt(Math.abs(discriminat))))/(2*a);       if (discriminat > 0 ){     System.out.println("there are two solutions:" +x1+"and"+x2);     }     else if (discriminat == 0){     System.out.println("The solutions is:"+x1);     }     else if (discriminat < 0){     System.out.println("The solutions are"+x3 + "i" + " and "+ x4 + "i");     }   }}```
using a class that looks and dose the same as this (they didnt bring me the code so i found a clone...)
Code:

```/*************************************************************************  *  Compilation:  javac Rational.java  *  Execution:    java Rational  *  *  ADT for nonnegative Rational numbers. Bare-bones implementation.  *  Cancel common factors, but does not stave off overflow. Does not  *  support negative fractions.  *  *  Invariant: all Rational objects are in reduced form (except  *  possibly while modifying).  *  *  Remarks  *  --------  *    - See  *      for a version that supports negative fractions and arbitrary  *      precision numerators and denominators.  *  *  % java Rational  *  5/6  *  1  *  28/51  *  17/899  *  0  *  *************************************************************************/ public class Rational {     private int num;  // the numerator     private int den;  // the denominator     // create and initialize a new Rational object     public Rational(int numerator, int denominator) {         if (denominator == 0) {             throw new RuntimeException("Denominator is zero");         }         int g = gcd(numerator, denominator);         num = numerator  / g;         den = denominator / g;     }     // return string representation of (this)     public String toString() {         if (den == 1) { return num + "";        }         else          { return num + "/" + den; }     }     // return (this * b)     public Rational times(Rational b) {         return new Rational(this.num * b.num, this.den * b.den);     }     // return (this + b)     public Rational plus(Rational b) {         int numerator  = (this.num * b.den) + (this.den * b.num);         int denominator = this.den * b.den;         return new Rational(numerator, denominator);     }     // return (1 / this)     public Rational reciprocal() { return new Rational(den, num);  }     // return (this / b)     public Rational divides(Rational b) {         return this.times(b.reciprocal());     }   /*************************************************************************     *  Helper functions     *************************************************************************/     // return gcd(m, n)     private static int gcd(int m, int n) {         if (0 == n) return m;         else return gcd(n, m % n);     }   /*************************************************************************     *  Test client     *************************************************************************/     public static void main(String[] args) {         Rational x, y, z;         // 1/2 + 1/3 = 5/6         x = new Rational(1, 2);         y = new Rational(1, 3);         z = x.plus(y);         System.out.println(z);         // 8/9 + 1/9 = 1         x = new Rational(8, 9);         y = new Rational(1, 9);         z = x.plus(y);         System.out.println(z);         //  4/17 * 7/3 = 28/51         x = new Rational(4, 17);         y = new Rational(7,  3);         z = x.times(y);         System.out.println(z);         // 203/16957 * 9299/5887 = 17/899         x = new Rational(203, 16957);         y = new Rational(9299, 5887);         z = x.times(y);         System.out.println(z);         // 0/6 = 0         x = new Rational(0, 6);         System.out.println(x);     } }```
• 12-22-2009, 10:04 AM
JosAH
I hope you realize that the square root of a (non negative) rational number can only be a rational number if the numerator and denumerator of the original rational number are perfect squares; otherwise the result is not a rational number.

kind regards,

Jos
• 12-22-2009, 02:15 PM
ok ill try to fix it somhow.

ill need to re write all of it whith rasional class somhow
• 12-22-2009, 02:22 PM
JosAH
Quote:

ok ill try to fix it somhow.

ill need to re write all of it whith rasional class somhow

It cannot be fixed for reasons I gave above; only if b*b-4*a*c is a special rational number of the form d*d/n*n it can be done, otherwise it can't be done with rational numbers.

kind regards,

Jos
• 12-22-2009, 03:35 PM
but that what i was asked to do to tell if thers one solution or two or none (they told us to use the rational class for some reason)
• 12-22-2009, 03:55 PM
JosAH
Quote:

but that what i was asked to do to tell if thers one solution or two or none (they told us to use the rational class for some reason)

That's not what I'm saying, e.g. there are two roots for the equation x^2+3x+1 but none of them are rational numbers, i.e. none of them can be written as n/d where both n and d are integers.

kind regards,

Jos
• 12-22-2009, 03:57 PM
ok so what do you think i shold do?(if i knew i would not have posted it)
• 12-22-2009, 04:42 PM
JosAH
Quote: