Results 1 to 18 of 18
  1. #1
    xdrazkalnytex is offline Member
    Join Date
    Mar 2012
    Posts
    34
    Rep Power
    0

    Default Homework Assignment, Need some help

    I fill in the parts, i got the subtraction and multiplication to work , but the addition and quotient doesn't work. For the quotient I don't know how to do reciprocals. I got the program to work, but 2 of the printout is wrong... Would someone help me?
    Java Code:
    // Rational.java
    
    public class Rational
    {
        private int n; // numerator
        private int d; // denominator
    
        // Return the greatest common divisor (gcd) of a and b.
        private static int gcd(int a, int b)
        {
            // TBD
          if( a < 0) a = -a;
          if( b < 0) b = -b;
          if( 0 ==b)
            return a;
          else
            return gcd(b, a & b);
        }
    
        // Construct a rational number given its numerator and denominator.
        public Rational(int n, int d)
        {
            // TBD
          this.n = n;
          this.d = d;           
        }
    
        // Return the sum of this number and b.
        public Rational plus(Rational b)
        {
            // TBD
          int numerator = (this.n * b.d ) + (this.d * b.n);
          int denominator = (this.d * b.d);
          return new Rational(numerator, denominator);
        }
    
        // Return the difference of this number and b.
        public Rational minus(Rational b)
        {
            // TBD
          int numerator = (this.n * b.d ) - (this.d * b.n);
          int denominator = this.d * b.d;
          return new Rational(numerator, denominator);
        }
    
        // Return the product of this number and b.
        public Rational times(Rational b)
        {
            // TBD
          int numerator = (this.n * b.n );
          int denominator = (this.d * b.d);
          return new Rational(numerator,denominator);
        }
    
        // Return the quotient of this number and b.
        public Rational over(Rational b)
        {
            // TBD   
          int numerator = (this.n * b.d );
          int denominator = (this.d * b.n);
          return new Rational(numerator,denominator);
        }
    
        // Return true if this number equals b, false otherwise.
        public boolean equals(Rational b)
        {
            // TBD
          boolean equals = this.n != this.d;
          return (equals);
        }
    
        // Return a string representation of this number.
        public String toString()
        {
            // TBD
            String result;
    
            if (n == 0)
                result = "0";
            else
                if (d == 1)
                    result = n + "";
                else
                    result = n + "/" + d;
        
            return result;
        }
    
        // Test driver.
        public static void main(String[] args)
        {
            Rational a = new Rational(1, 3);
            Rational b = new Rational(2, 3);
            System.out.println("Should print 1/1: " + a.plus(b));
            System.out.println("Should print true: " + b.minus(a).equals(a));
            System.out.println("Should print 2/9: " + a.times(b));
            System.out.println("Should print 1/2: " + a.over(b));
        }
    }
    Last edited by Norm; 04-10-2012 at 08:29 PM. Reason: added code tags

  2. #2
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Homework Assignment, Need some help

    First, use [code] tags. They'll make your code far more readable.

    Second, when you say " 2 of the printout is wrong" - Which 2? Can you show us what is wrong? What are they supposed to say?

  3. #3
    xdrazkalnytex is offline Member
    Join Date
    Mar 2012
    Posts
    34
    Rep Power
    0

    Default Re: Homework Assignment, Need some help

    > run Rational
    Should print 1/1: 9/9
    Should print true: true
    Should print 2/9: 2/9
    Should print 1/2: 3/6
    >

    first printout which should be 1/1 (suppose to be addition)
    last printout which should be 1/2 (suppose to be division)

  4. #4
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Homework Assignment, Need some help

    First, use [code] tags. They'll make your code far more readable.
    Also,
    Can you say what is wrong with the answers you get?
    Last edited by Diargg; 04-10-2012 at 08:15 PM.

  5. #5
    xdrazkalnytex is offline Member
    Join Date
    Mar 2012
    Posts
    34
    Rep Power
    0

    Default Re: Homework Assignment, Need some help

    it shows the wrong print out ,

    and what is [code] tags?

  6. #6
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Homework Assignment, Need some help

    But what is wrong with it, beyond not being right? Should the numbers be different? Should they mean different things? (ie 3/4 means .75 )

    Edit your initial post to put [code ] and [/code] around your Java code

  7. #7
    xdrazkalnytex is offline Member
    Join Date
    Mar 2012
    Posts
    34
    Rep Power
    0

    Default Re: Homework Assignment, Need some help

    it should print out what what the print out said,
    like 1/1 should be 1/1 and so on.

  8. #8
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Homework Assignment, Need some help

    So what is the difference between 9/9 and 1/1? Are they the same?

  9. #9
    xdrazkalnytex is offline Member
    Join Date
    Mar 2012
    Posts
    34
    Rep Power
    0

    Default Re: Homework Assignment, Need some help

    it supposed to print out 1/1 , but i guess 9/9 will do

  10. #10
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Homework Assignment, Need some help

    One last try I guess.

    Read this. Are you familiar with how fractions work? All of the code you need is present in the example you provided in the first post. The code isn't correct, and we can discuss that, but first you need to understand what you need to do to fix the problem.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Homework Assignment, Need some help

    Why have you implemented that gcd( ... ) method in your class? You're not using it ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Homework Assignment, Need some help

    I have a general hint for you: Never use the same name for local and class attributes like you do... ;)

    First reads as it does because it is correct. 9/9 == 1/1 - you just need to cut it down.
    Last reads as it does because it is correct. 3/6 == 1/2 - you just need to cut it down.

    Add a method that trims it down - you will maybe need gcd() for that ... ;)

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Homework Assignment, Need some help

    Given the toString() method I don't think 1/1 is ever printed out; I also don't think that code was written by the OP ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Homework Assignment, Need some help

    @JosAH: I agree to both and I think it is part of the homework to find a solution for the correct output before calling toString()... so he should give it a try. ;)

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Homework Assignment, Need some help

    Quote Originally Posted by Sierra View Post
    @JosAH: I agree to both and I think it is part of the homework to find a solution for the correct output before calling toString()... so he should give it a try. ;)
    Ah, ok, so all we can do (and have to do) is sit back and enjoy the show ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Homework Assignment, Need some help

    Quote Originally Posted by JosAH View Post
    Ah, ok, so all we can do (and have to do) is sit back and enjoy the show ;-)

    kind regards,

    Jos
    /bows

    I'm personally a fan of the (a & b) integer being passed about.

  17. #17
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Homework Assignment, Need some help

    I wonder if that is/should be Euclid's algorithm...?

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Homework Assignment, Need some help

    Quote Originally Posted by Sierra View Post
    I wonder if that is/should be Euclid's algorithm...?
    Nah, just a feeble attempt to it; it is utterly incorrect but the OP will find out ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 7
    Last Post: 02-29-2012, 01:15 PM
  2. Need help with homework
    By bkim33 in forum New To Java
    Replies: 9
    Last Post: 02-11-2011, 04:50 AM
  3. Homework help please
    By chick in forum New To Java
    Replies: 22
    Last Post: 03-19-2010, 07:39 AM
  4. Calculator Program HELP NEEDED FAST! Homework assignment
    By SteroidalPsycho in forum New To Java
    Replies: 3
    Last Post: 03-05-2009, 04:02 AM
  5. Homework help...
    By robrobrob in forum New To Java
    Replies: 4
    Last Post: 10-17-2008, 04:24 AM

Posting Permissions

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