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: Help with Ration Program on JAVA

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Ration Program on JAVA

    I have to create a program that when used with the test program below should produce the results below. So far I have this please help me. Thank you.

    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 Program:
    public class RationalTest
    {

    private static void report(String fmt, Object... args)
    { System.out.printf(fmt, args); }

    public static void main(String[] args)
    {
    Rational r0 = new Rational(0);
    report("r0: %s%n", r0);

    Rational r5 = new Rational(5);
    report("r5: %s%n", r5);

    Rational r05 = new Rational(0, 5);
    report("r05: %s%n", r05);

    Rational rhalf = new Rational(1, 2);
    report("rhalf: %s%n", rhalf);
    Rational rhalf5 = new Rational(5, 10);
    report("rhalf5: %s%n", rhalf5);
    report("rhalf == rhalf5: %s%n", rhalf.equals(rhalf5));
    Rational rtwo5 = new Rational(10, 5);
    report("rtwo5: %s%n", rtwo5);

    Rational rnegn = new Rational(-3, 4);
    report("rnegn: %s%n", rnegn);
    Rational rnegd = new Rational(3, -4);
    report("rnegd: %s%n", rnegd);
    Rational rneg2 = new Rational(-3, -4);
    report("rneg2: %s%n", rneg2);

    report("rden0: ");
    try {
    Rational rden0 = new Rational(3, 0);
    report("%s%n", rden0);
    }
    catch (IllegalArgumentException iae)
    { System.err.println(iae.getMessage()); }

    Rational recip1 = rnegn.reciprocal();
    report("recip1: %s%n", recip1);
    Rational recip2 = rhalf5.reciprocal();
    report("recip2: %s%n", recip2);
    Rational recip3 = rtwo5.reciprocal();
    report("recip3: %s%n", recip3);

    Rational rtwothirds = new Rational(2, 3);
    int cmp = rneg2.compareTo(rtwothirds);
    String reln = (cmp < 0 ? "<" : (cmp > 0 ? ">" : "="));
    report("%s %s %s%n", rneg2, reln, rtwothirds);
    cmp = rtwothirds.compareTo(rneg2);
    reln = (cmp < 0 ? "<" : (cmp > 0 ? ">" : "="));
    report("%s %s %s%n", rtwothirds, reln, rneg2);
    Rational rfourth = new Rational(1, 4);
    Rational rsixth = new Rational(1, 6);

    Rational sum1 = rtwothirds.plus(rfourth);
    report("sum1: %s%n", sum1);
    Rational sum2 = rtwothirds.plus(rsixth);
    report("sum2: %s%n", sum2);
    Rational sum3 = rnegn.plus(rnegd);
    report("sum3: %s%n", sum3);

    Rational diff1 = rtwothirds.minus(rfourth);
    report("diff1: %s%n", diff1);
    Rational diff2 = rtwothirds.minus(rsixth);
    report("diff2: %s%n", diff2);
    Rational diff3 = rnegn.minus(rnegd);
    report("diff3: %s%n", diff3);

    Rational prod1 = rtwothirds.times(rfourth);
    report("prod1: %s%n", prod1);
    Rational prod2 = rtwothirds.times(rsixth);
    report("prod2: %s%n", prod2);
    Rational prod3 = rnegn.times(rhalf);
    report("prod3: %s%n", prod3);

    Rational quot1 = rtwothirds.divide(rfourth);
    report("quot1: %s%n", quot1);
    Rational quot2 = rtwothirds.divide(rsixth);
    report("quot2: %s%n", quot2);
    Rational quot3 = rnegn.divide(rhalf);
    report("quot3: %s%n", quot3);
    report("quot4: ");
    try {
    Rational quot4 = rnegn.divide(r0);
    report("%s%n", quot4);
    }
    catch (Exception e) { System.err.println(e.getMessage()); }
    }
    }




    Result
    r0: 0
    r5: 5
    r05: 0
    rhalf: 1/2
    rhalf5: 1/2
    rhalf == rhalf5: true
    rtwo5: 2
    rnegn: -3/4
    rnegd: -3/4
    rneg2: 3/4
    rden0: Cannot construct a Rational with a zero (0) denominator
    recip1: -4/3
    recip2: 2
    recip3: 1/2
    3/4 > 2/3
    2/3 < 3/4
    sum1: 11/12
    sum2: 5/6
    sum3: -3/2
    diff1: 5/12
    diff2: 1/2
    diff3: 0
    prod1: 1/6
    prod2: 1/9
    prod3: -3/8
    quot1: 8/3
    quot2: 4
    quot3: -3/2
    quot4: Cannot construct a Rational with a zero (0) denominator


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,223
    Thanks
    52
    Thanked 2,295 Times in 2,266 Posts

    Default Re: Help with Ration Program on JAVA

    Can you post the program's current output for comparison? Add some comments to it that point out where the output is wrong.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 06:16 AM
  2. convert java program to java ME program
    By abhishek1212 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2013, 02:22 PM
  3. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  4. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM
  5. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Swing Tutorials
    Replies: 6
    Last Post: March 6th, 2012, 11:25 AM

Tags for this Thread