# Thread: quadratic equation whith Rational class

1. Member
Join Date
Nov 2009
Posts
19
Rep Power
0

## quadratic equation whith Rational class

this is my quadratic equation code i need to make it give Rational numbers result:

Java 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...)
Java 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);

}

}```

2. 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

3. Member
Join Date
Nov 2009
Posts
19
Rep Power
0
ok ill try to fix it somhow.

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

4. Originally Posted by adamrain
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

5. Member
Join Date
Nov 2009
Posts
19
Rep Power
0
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)

6. Originally Posted by adamrain
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

7. Member
Join Date
Nov 2009
Posts
19
Rep Power
0
ok so what do you think i shold do?(if i knew i would not have posted it)

8. Originally Posted by adamrain
ok so what do you think i shold do?(if i knew i would not have posted it)
Don't try to solve those quadratic equations using rationals; stick to ordinary doubles and you'll be fine for the real numbers. Also read this link and pay special attention to the section that discusses numerical stability (near equation 14).

kind regards,

Jos

9. Member
Join Date
Nov 2009
Posts
19
Rep Power
0
so i shold send the first one?

i think they cant give me less then 65 ,mark becuz i did the second qustion perfect, they equal 50% each so mabe i can pass thanks aneway...

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•