Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: "Reduce" Method and Adding Rational Numbers

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default "Reduce" Method and Adding Rational Numbers

    This is the part of the homework where I am stuck:
    Write a modifier named reduce that reduces a rational number to its lowest terms by finding
    the GCD of the numerator and denominator and then dividing top and bottom by the GCD.
    Write a method called add that takes two Rational numbers as arguments and returns a new
    Rational object. The return object, not surprisingly, should contain the sum of the two
    rational numbers and it should be reduced (using your reduce method, of course).


    Here is where I am stuck: This is my current code.
    I cannot quite figure out how to set up my reduce method. And I am also having some issues with adding two rational numbers, can anyone add some insight on this?

    public class MainClass
    {
        //Declarationg
        int num, den;
     
     
        public MainClass () {
       //Initalizing Variables
        this.num = 0;
        this.den = 0;
    }
        public void negate (Rational r) {
            this.num = -this.num;
        }
        public void invert (Rational r) {
            Math.pow (this.num / this.den, -1);
        }
     
     
        public void printRational (Rational r){
            System.out.println(this.num + " / " + this.den);
        }
        public double toDouble () {
            return (double) num/den;
        }
        public static int gcd (int n, int d) {
            n = 2;
            d = 3;
            if (n < 0) {
                n = -n;
            }
            if (d < 0) {
                d = -d;
            }
            return n * (d / gcd (n, d));
        }
        public double reduce (Rational r) {
     
        }
        public double add (Rational r1, Rational r2) {
     
            return Rational (add (num + den));
        }
     
     
     
        public void main(String[] args) {
            MainClass r1 = new MainClass ();
            r1.num = 4;
            r1.den = 12; 
            System.out.println(r1.num + "/" + r1.den);
            System.out.println("Reduced Amount");
            System.out.println(reduce (num + den));
     
        }
    }


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: "Reduce" Method and Adding Rational Numbers

    you haven't implemented the reduce method yet, once you have implemented the reduce method then you can use it in your add function like this:
    r1=reduce(r1);
    r2=reduce(r2);
    return Rational(r1+r2);

Similar Threads

  1. [SOLVED] Adding the "final" keyword makes difference, I'm confused with the sequence flow.
    By SmokyBrain in forum Object Oriented Programming
    Replies: 2
    Last Post: April 30th, 2012, 01:50 AM
  2. "Separate Numbers"-Problem:Assigning Variables to User Inputs.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 10th, 2012, 06:08 PM
  3. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM