Results 1 to 9 of 9
  1. #1
    adamrain is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    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. #3
    adamrain is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default

    ok ill try to fix it somhow.

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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by adamrain View Post
    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. #5
    adamrain is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default

    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. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by adamrain View Post
    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. #7
    adamrain is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default

    ok so what do you think i shold do?(if i knew i would not have posted it)

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by adamrain View Post
    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. #9
    adamrain is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default

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

Similar Threads

  1. Quadratic Equation
    By jpnym15 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 03:29 AM
  2. Quadratic GUI
    By ryn21 in forum New To Java
    Replies: 1
    Last Post: 10-30-2008, 05:58 AM
  3. Eclipse vs Rational Software Development
    By tommy in forum Other IDEs
    Replies: 2
    Last Post: 05-15-2008, 05:05 PM
  4. Replies: 1
    Last Post: 03-17-2008, 12:59 AM
  5. Help with quadratic equation in java
    By paul in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:23 PM

Posting Permissions

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