Results 1 to 9 of 9
 12222009, 01:57 AM #1Member
 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"); } }}
Java Code:/************************************************************************* * Compilation: javac Rational.java * Execution: java Rational * * ADT for nonnegative Rational numbers. Barebones 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); } }
 12222009, 10:04 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,945
 Blog Entries
 7
 Rep Power
 22
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
 12222009, 02:15 PM #3Member
 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
 12222009, 02:22 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,945
 Blog Entries
 7
 Rep Power
 22
 12222009, 03:35 PM #5Member
 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)
 12222009, 03:55 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,945
 Blog Entries
 7
 Rep Power
 22
 12222009, 03:57 PM #7Member
 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)
 12222009, 04:42 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,945
 Blog Entries
 7
 Rep Power
 22
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
 12222009, 06:35 PM #9Member
 Join Date
 Nov 2009
 Posts
 19
 Rep Power
 0
Similar Threads

Quadratic Equation
By jpnym15 in forum New To JavaReplies: 4Last Post: 11122008, 04:29 AM 
Quadratic GUI
By ryn21 in forum New To JavaReplies: 1Last Post: 10302008, 06:58 AM 
Eclipse vs Rational Software Development
By tommy in forum Other IDEsReplies: 2Last Post: 05152008, 05:05 PM 
i have a problem whith cellualar automata
By besi in forum Java 2DReplies: 1Last Post: 03172008, 01:59 AM 
Help with quadratic equation in java
By paul in forum New To JavaReplies: 1Last Post: 07252007, 08:23 PM
Bookmarks