Java Training Course/JT06: Difference between revisions

From tehowiki
Jump to navigation Jump to search
imported>Gfis
No edit summary
imported>Gfis
polish3
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Skeleton of Class ''Rational''==
==Preliminary Class ''Ratio''==
As announced in [[Java Training Course/JT05|JT05]], we now implement a useful type of objects with operations as a Java class. Below you will find a skeleton for the code. The task of this session is
As a preparation we implement a Java class which represents fractions and arithmetic operations thereon, but the operations (''methods'' in Java terminology) are all static. They are implemented in a rather straight-forward way, and they are not yet really object-oriented.
 
Below you will find a skeleton for the code. The task of this session is
* Fill in the bodies of the following methods if necessary.
* Fill in the bodies of the following methods if necessary.
* Thereby make as much use of other methods as possible.
* Thereby make as much use of other methods as possible.
* All Rational return values should be <em>reduce</em>d.
* Insert proper documentation comments.
* Insert proper documentation comments.
* Arrange the methods of the class in alphabetical order (since there are many).
* Arrange the methods of the class in alphabetical order (since there are many).
Line 18: Line 19:
     public Rational multiply(Rational rat2) { ... }
     public Rational multiply(Rational rat2) { ... }
     public Rational negate() { return new Rational(- numerator, denominator); }
     public Rational negate() { return new Rational(- numerator, denominator); }
     // public Rational reduce() is already implemented below
     public Rational reduce() is already implemented below
     // public Rational subtract(Rational rat2) is already implemented below
     public Rational subtract(Rational rat2) is already implemented below
===Starting Code===
===Implementation===
/* Fractions of integers
<pre>
  * @(#) $Id$
/* Fractions of integers
  * 2017-09-23, Georg Fischer
* @(#) $Id$
  */
* 2017-11-07: revised in Bekasi
// package name will later go here
* 2017-09-23, Georg Fischer
// imports will later go here
*/
// package name will later go here
/** Class <wm>Rational</em> represents an integer fraction
// imports will later go here
  *  together with the arithmetic operations on such Rationals.
 
  *  The denominator is always > 0. All arithmetic operations
/** Class <em>Ratio</em> represents an integer fraction
  *  return reduced fractions where GCD(numerator, denominator) = 1.
*  together with the arithmetic operations on such Ratios.
  *  The methods have name, parameters and return values analogous to methods
*  The denominator is always &gt; 0. All arithmetic operations
  *  of the Java object type BigInteger, see
*  return reduced fractions where GCD(numerator, denominator) = 1.
  *  https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
*  The methods have name, parameters and return values analogous to methods
  */
*  of the Java object type BigInteger, see
public class Rational {
*  https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
    //----------------
    // Internal Properties
    //----------------
    /** value above the bar, before the slash */
    private int numerator;
    /** value below the bar, after  the slash */
    private int denominator;
/*  Implement the following methods, and make use of basic operations.
    All Rational return values should be <em>reduce</em>d.
    Insert proper documentation comments.
    Always arrange all methods of the class in alphabetical order, since there are many.
  */
  */
    public Rational abs() { return this; }
public class Ratio {
    public Rational add(Rational rat2) { return this; }
    //----------------
    public int compareTo(Rational rat2) { return 0; } // return -1, 0, +1 iff this < = > rat2
    // Internal Properties
    public Rational divide(Rational rat2) { return this; }
    //----------------
    public boolean equals(Rational rat2) { return this.compareTo(rat2) == 0; }
    /** value above the bar, before the slash */
    public boolean isInteger() { return denominator == 1; }
    public int numerator;
    public int intValue() { return numerator / denominator; }
    /** value below the bar, after  the slash */
    public Rational max(Rational rat2) { return this; }
    public int denominator;
    public Rational min(Rational rat2) { return this; }
 
    public Rational multiply(Rational rat2) { return this; }
/*  Implement the following methods, and make use of basic operations.
    public Rational negate() { return this; }
    All Ratio return values should be reduced.
    // public Rational reduce() is already implemented below
    Insert proper documentation comments.
    // public Rational subtract(Rational rat2) is already implemented below
    Always arrange the public methods of the class in alphabetical order, since there are many.
   
*/
    //----------------
    // public static Ratio abs(Ratio rat1) { return new Ratio(); }
    // Constructors
    // public static Ratio add(Ratio rat1, Ratio rat2) { return new Ratio(); }
    //----------------
    // public static int compareTo(Ratio rat1, Ratio rat2) { return 0; } // return -1, 0, +1 iff this < = > rat2
    /** No-args constructor, creates 1/1
    // public static Ratio divide(Ratio rat1, Ratio rat2) { return new Ratio(); }
      */
    // public static boolean equals(Ratio rat1, Ratio rat2) { return rat1.compareTo(rat2) == 0; }
    public Rational() {
    // public static boolean isInteger(Ratio rat1) { return false; }
        numerator  = 1;
    // public static int intValue(Ratio rat1) { return 0; }
        denominator = 1;
    // public static Ratio max(Ratio rat1, Ratio rat2) { return rat1; }
    } // no-args constructor
    // public static Ratio min(Ratio rat1, Ratio rat2) { return rat2; }
    // public static Ratio multiply(Ratio rat1, Ratio rat2) is already implemented below
    /** Constructor with numerator, creates a/1
    // public static Ratio negate(Ratio rat1) { return new Ratio(); }
      *  @param a numerator
    // public static Ratio reduce(Ratio rat1) is already implemented below
      */
    // public static Ratio subtract(Ratio rat1, Ratio rat2) { return new Ratio(); }
    public Rational(int a) {
   
        numerator  = a;
    //----------------
        denominator = 1;
    // Constructors
    } // constructor(int)
    //----------------
    /** No-args constructor, creates 1/1
    /** Constructor with numerator and denominator, creates a/b
    */
      *  @param a numerator
    public Ratio() {
      *  @param b denominator
        numerator  = 1;
      */
        denominator = 1;
    public Rational(int a, int b) {
    } // no-args constructor
        numerator  = a;
 
        denominator = b;
    /** Constructor with numerator, creates a/1
    } // constructor(int, int)
    *  @param a numerator
    */
    /** Constructor from a String representation, creates a/b
    public Ratio(int a) {
      * @param str String of the form "a/b"
        numerator  = a;
      */
        denominator = 1;
    public Rational(String str) {
    } // constructor(int)
        int slashPos = str.indexOf("/");
 
        if (slashPos < 0) {
    /** Constructor with numerator and denominator, creates a/b
            str += "/1";
     *  @param a numerator
            slashPos = str.length() - 2;
    *  @param b denominator
        }
     */
        numerator   = Integer.parseInt(str.substring(0, slashPos));
    public Ratio(int a, int b) {
        denominator = Integer.parseInt(str.substring(slashPos + 1));
        numerator   = a;
    } // constructor(String)
        denominator = b;
    } // constructor(int, int)
    //----------------
 
     // Internal getters
    /** Constructor from a String representation, creates a/b
     //----------------
     * @param str String of the form "a/b"
    /** Gets the numerator
    */
      *  @return the numerator  of <em>this</em> Rational
    public Ratio(String str) {
      */
        int slashPos = str.indexOf("/");
    private int getNum() {
        if (slashPos < 0) {
        return numerator;
            str += "/1";
    } // getNum()
            slashPos = str.length() - 2;
        }
     /** Gets the denominator
        numerator    = Integer.parseInt(str.substring(0, slashPos));
      *  @return the denominator of <em>this</em> Rational
        denominator = Integer.parseInt(str.substring(slashPos + 1));
      */
    } // constructor(String)
    private int getDen() {
 
        return denominator;
    //----------------
    } // getDen()
    // Internal getters
    //----------------
    //----------------
    /** Gets the numerator
    // Public methods
     * @return the numerator  of this Ratio
    //----------------
    */
    /** Returns the greatest common divisor of 2 integers.
    private int getNum() {
      *  @param a 1st integer
        return numerator;
      *  @param b 2nd integer
    } // getNum()
      *  @return gcd(a,b), which is always positive
 
      */
    /** Gets the denominator
    public static int gcd(int a, int b) {
    *  @return the denominator of this Ratio
        int result = 0;
    */
        // not yet implemented
    private int getDen() {
        return result;
        return denominator;
    } // gcd(int,int)
    } // getDen()
 
     /** Returns the least common multiple of 2 integers.
    //----------------
      * @param a 1st integer
    // Public methods
      *  @param b 2nd integer
    //----------------
      *  @return lcm(a,b)
 
      */
    /** Returns the sum of one Ratio and a second.
    public static int lcm(int a, int b) {
    @param rat1 1st Ratio
        int result = a * b;
    @param rat2 2nd Ratio
        if (result < 0) { // absolute, make positive
    @return (rat1 + rat2)
            result = - result;
    */
        } // abs
    public static Ratio add(Ratio rat1, Ratio rat2) {
        return result / gcd(a, b);
        int denom = Ratio.lcm(rat1.denominator, rat2.denominator);
    } // lcm(int,int)
        int fact1 = denom / rat1.denominator;
        int fact2  = denom / rat2.denominator;
    /** Reduces and normalizes the fraction, that means:
        int numSum = rat1.numerator * fact1
      &lt;ul&gt;
                    + rat2.numerator * fact2;
      &lt;li&gt;divides the numerator and the denominator by
        return Ratio.reduce(new Ratio(numSum, denom));
      their greatest common divisor, if that is &gt; 1,&lt;/li&gt;
    } // add(Ratio)
      * &lt;li&gt;makes the denominator always positive,&lt;/li&gt;
 
      * &lt;li&gt;normalizes the denominator to 1 if the numerator is 0.&lt;/li&gt;
    /** Returns the quotient of one Ratio and a second.
      * &lt;/ul&gt;
    *  @param rat1 1st Ratio
      */
    *  @param rat2 2nd Ratio
    public Rational reduce() {
    *  @return (rat1 / rat2)
        int common = gcd(numerator, denominator);
    */
        if (common > 1) {
    public static Ratio divide(Ratio rat1, Ratio rat2) {
            numerator  /= common;
        return Ratio.reduce(new Ratio
            denominator /= common;
                ( rat1.numerator  * rat2.denominator
        }
                , rat1.denominator * rat2.numerator    )
        if (numerator == 0) {
                );
            denominator = 1;
    } // divide(Ratio)
        } else if (denominator < 0) {
 
            numerator  = - numerator;
    /** Returns the greatest common divisor of 2 integers.
            denominator = - denominator;
    *  @param a 1st integer
        }
    *  @param b 2nd integer
        return this;
    *  @return gcd(a,b), which is always positive
    } // reduce()
    */
 
    public static int gcd(int a, int b) {
    /** Returns the difference between <em>this</em> Rational and a second.
        int result = Math.abs(a);
      *  @param rat2 2nd Rational
        if (result != 1) {
      *  @return (this - rat2)
            int p = result;
      */
            int q = Math.abs(b);
    public Rational subtract(Rational rat2) {
            while (q != 0) {
        return this.add(rat2.negate());
                int temp = q;
    } // subtract(Rational)
                q = p % q;
                p = temp;
    /** Returns <em>this</em> Rational as a String.
            }
      *  @return a String of the form "a/b", or only "a" if b is 1.
            result = p;
      */
        } // if > 1
    public String toString() {
        return Math.abs(result);
        String result = String.valueOf(numerator);
    } // gcd(a, b)
        if (denominator != 1) {
   
            result += "/" + String.valueOf(denominator);
    /** Returns the least common multiple of 2 integers.
        }
    *  @param a 1st integer
        return result;
    * @param b 2nd integer
    } // toString()
     *  @return lcm(a,b)
    */
    //================
    public static int lcm(int a, int b) {
    /** Test program, shows a series of fixed operations
        int result = a * b;
      *  @param args String array of commandline arguments
        if (result < 0) { // absolute, make positive
      */
            result = - result;
     public static void main(String[] args) {
        } // abs
        int iarg = 1;
        return result / gcd(a, b);
        String  str0 = "0/1";
    } // lcm(int,int)
        Rational rat0 = new Rational(str0);
 
        while (iarg < args.length) {
    /** Returns the product of one Ratio and a second.
            String  str1 = args[iarg];
    *  @param rat1 1st Ratio
            Rational rat1 = new Rational(str1);
    *  @param rat2 2nd Ratio
            System.out.println(str0 + " + " + str1 + " = " + rat0.add    (rat1).toString());
    *  @return (rat1 * rat2)
            System.out.println(str0 + " - " + str1 + " = " + rat0.subtract(rat1).toString());
    */
            System.out.println(str0 + " * " + str1 + " = " + rat0.multiply(rat1).toString());
    public static Ratio multiply(Ratio rat1, Ratio rat2) {
            System.out.println(str0 + " / " + str1 + " = " + rat0.divide (rat1).toString());
        return Ratio.reduce(new Ratio
            str0 = str1;
                ( rat1.numerator  * rat2.numerator
            rat0 = rat1;
                , rat1.denominator * rat2.denominator)
            iarg ++;
                );
        } // while iarg
    } // multiply(Ratio)
    } // main
 
    /** Returns the negative of a Ratio.
    *  @param rat1 Ratio to be negated
    *  @return (- rat1)
    */
    public static Ratio negate(Ratio rat1) {
        return Ratio.reduce(new Ratio( - rat1.numerator, rat1.denominator));
    } // negate(Ratio)
 
    /** Reduces and normalizes the fraction, that means:
    *  (1) divides the numerator and the denominator by
    *  their greatest common divisor, if that is &gt; 1,
    *  (2) makes the denominator always positive,
    *  (3) normalizes the denominator to 1 if the numerator is 0.
    *  @param rat1 the Ratio to be reduced
    *  @return the reduced Ratio
    */
    public static Ratio reduce(Ratio rat1) {
        Ratio result = new Ratio(rat1.numerator, rat1.denominator);
        int common = gcd(rat1.numerator, rat1.denominator);
        if (common > 1) {
            result.numerator  /= common;
            result.denominator /= common;
        }
        if (result.numerator == 0) {
            result.denominator = 1;
        } else if (result.denominator < 0) {
            result.numerator  = - result.numerator;
            result.denominator = - result.denominator;
        }
        return result;
    } // reduce(Ratio)
   
   
  } // Rational
    /** Returns the difference of one Ratio and a second.
    *  @param rat1 1st Ratio
    *  @param rat2 2nd Ratio
    *  @return (rat1 - rat2)
    */
    public static Ratio subtract(Ratio rat1, Ratio rat2) {
        return Ratio.add(rat1, Ratio.negate(rat2));
    } // subtract(Ratio)
 
    /** Returns this Ratio as a String.
    * @return a String of the form "a/b", or only "a" if b is 1.
    */
    public String toString() {
        String result = String.valueOf(numerator);
        if (denominator != 1) {
            result += "/" + String.valueOf(denominator);
        }
        return result;
    } // toString()
 
    //================
    /** Test program, shows a series of fixed operations
    *  @param args String array of commandline arguments
    */
    public static void main(String[] args) {
        int iarg = 0;
        String  str0 = "0/1";
        Ratio rat0 = new Ratio(str0);
        while (iarg < args.length) {
            String  str1 = args[iarg];
            Ratio rat1 = new Ratio(str1);
            System.out.println(str0 + " + " + str1 + " = " + Ratio.add    (rat0, rat1).toString());
            System.out.println(str0 + " - " + str1 + " = " + Ratio.subtract(rat0, rat1).toString());
            System.out.println(str0 + " * " + str1 + " = " + Ratio.multiply(rat0, rat1).toString());
            System.out.println(str0 + " / " + str1 + " = " + Ratio.divide  (rat0, rat1).toString());
            System.out.println();
            str0 = str1;
            rat0 = rat1;
            iarg ++;
        } // while iarg
    } // main
} // Ratio
</pre>
[[Java Training Course/JT05|&lt; Previous: JT05]] Control Structures: The Greatest Common Divisor<br />
[[Java Training Course/JT05|&lt; Previous: JT05]] Control Structures: The Greatest Common Divisor<br />
[[Java Training Course/JT07|&gt; Next: JT07]] Diskussion of Class ''Rational''
[[Java Training Course/JT07|&gt; Next: JT07]] Class ''Rational''

Latest revision as of 15:25, 7 November 2017

Preliminary Class Ratio

As a preparation we implement a Java class which represents fractions and arithmetic operations thereon, but the operations (methods in Java terminology) are all static. They are implemented in a rather straight-forward way, and they are not yet really object-oriented.

Below you will find a skeleton for the code. The task of this session is

  • Fill in the bodies of the following methods if necessary.
  • Thereby make as much use of other methods as possible.
  • Insert proper documentation comments.
  • Arrange the methods of the class in alphabetical order (since there are many).

Desired Methods

    public Rational abs() { return new Rational((numerator < 0 ? - numerator : numerator), denominator) }
    public Rational add(Rational rat2) { ... }
    public int compareTo(Rational rat2) { /* return -1, 0, +1 iff this < = > rat2 */ ... }
    public Rational divide(Rational rat2) { ... }
    public boolean equals(Rational rat2) { return this.compareTo(rat2) == 0; }
    public boolean isInteger() { return denominator == 1; }
    public int intValue() { return numerator / denominator; }
    public Rational max(Rational rat2) { return this.compareTo(rat2) > 0 ? this : rat2 }
    public Rational min(Rational rat2) { return this.compareTo(rat2) < 0 ? this : rat2 }
    public Rational multiply(Rational rat2) { ... }
    public Rational negate() { return new Rational(- numerator, denominator); }
    public Rational reduce() is already implemented below
    public Rational subtract(Rational rat2) is already implemented below

Implementation

/* Fractions of integers
 * @(#) $Id$
 * 2017-11-07: revised in Bekasi
 * 2017-09-23, Georg Fischer
 */
// package name will later go here
// imports will later go here

/** Class <em>Ratio</em> represents an integer fraction
 *  together with the arithmetic operations on such Ratios.
 *  The denominator is always > 0. All arithmetic operations
 *  return reduced fractions where GCD(numerator, denominator) = 1.
 *  The methods have name, parameters and return values analogous to methods
 *  of the Java object type BigInteger, see
 *  https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
 */
public class Ratio {
    //----------------
    // Internal Properties
    //----------------
    /** value above the bar, before the slash */
    public int numerator;
    /** value below the bar, after  the slash */
    public int denominator;

/*  Implement the following methods, and make use of basic operations.
    All Ratio return values should be reduced.
    Insert proper documentation comments.
    Always arrange the public methods of the class in alphabetical order, since there are many.
*/
    // public static Ratio abs(Ratio rat1) { return new Ratio(); }
    // public static Ratio add(Ratio rat1, Ratio rat2) { return new Ratio(); }
    // public static int compareTo(Ratio rat1, Ratio rat2) { return 0; } // return -1, 0, +1 iff this < = > rat2
    // public static Ratio divide(Ratio rat1, Ratio rat2) { return new Ratio(); }
    // public static boolean equals(Ratio rat1, Ratio rat2) { return rat1.compareTo(rat2) == 0; }
    // public static boolean isInteger(Ratio rat1) { return false; }
    // public static int intValue(Ratio rat1) { return 0; }
    // public static Ratio max(Ratio rat1, Ratio rat2) { return rat1; }
    // public static Ratio min(Ratio rat1, Ratio rat2) { return rat2; }
    // public static Ratio multiply(Ratio rat1, Ratio rat2) is already implemented below
    // public static Ratio negate(Ratio rat1) { return new Ratio(); }
    // public static Ratio reduce(Ratio rat1) is already implemented below
    // public static Ratio subtract(Ratio rat1, Ratio rat2) { return new Ratio(); }
    
    //----------------
    // Constructors
    //----------------
    /** No-args constructor, creates 1/1
     */
    public Ratio() {
        numerator   = 1;
        denominator = 1;
    } // no-args constructor

    /** Constructor with numerator, creates a/1
     *  @param a numerator
     */
    public Ratio(int a) {
        numerator   = a;
        denominator = 1;
    } // constructor(int)

    /** Constructor with numerator and denominator, creates a/b
     *  @param a numerator
     *  @param b denominator
     */
    public Ratio(int a, int b) {
        numerator   = a;
        denominator = b;
    } // constructor(int, int)

    /** Constructor from a String representation, creates a/b
     *  @param str String of the form "a/b"
     */
    public Ratio(String str) {
        int slashPos = str.indexOf("/");
        if (slashPos < 0) {
            str += "/1";
            slashPos = str.length() - 2;
        }
        numerator    = Integer.parseInt(str.substring(0, slashPos));
        denominator = Integer.parseInt(str.substring(slashPos + 1));
    } // constructor(String)

    //----------------
    // Internal getters
    //----------------
    /** Gets the numerator
     *  @return the numerator   of this Ratio
     */
    private int getNum() {
        return numerator;
    } // getNum()

    /** Gets the denominator
     *  @return the denominator of this Ratio
     */
    private int getDen() {
        return denominator;
    } // getDen()

    //----------------
    // Public methods
    //----------------

    /** Returns the sum of one Ratio and a second.
     *  @param rat1 1st Ratio
     *  @param rat2 2nd Ratio
     *  @return (rat1 + rat2)
     */
    public static Ratio add(Ratio rat1, Ratio rat2) {
         int denom  = Ratio.lcm(rat1.denominator, rat2.denominator);
         int fact1  = denom / rat1.denominator;
         int fact2  = denom / rat2.denominator;
         int numSum = rat1.numerator * fact1
                    + rat2.numerator * fact2;
         return Ratio.reduce(new Ratio(numSum, denom));
    } // add(Ratio)

    /** Returns the quotient of one Ratio and a second.
     *  @param rat1 1st Ratio
     *  @param rat2 2nd Ratio
     *  @return (rat1 / rat2)
     */
    public static Ratio divide(Ratio rat1, Ratio rat2) {
        return Ratio.reduce(new Ratio
                ( rat1.numerator   * rat2.denominator
                , rat1.denominator * rat2.numerator    )
                );
    } // divide(Ratio)

    /** Returns the greatest common divisor of 2 integers.
     *  @param a 1st integer
     *  @param b 2nd integer
     *  @return gcd(a,b), which is always positive
     */
    public static int gcd(int a, int b) {
        int result = Math.abs(a);
        if (result != 1) {
            int p = result;
            int q = Math.abs(b);
            while (q != 0) {
                int temp = q;
                q = p % q;
                p = temp;
            }
            result = p;
        } // if > 1
        return Math.abs(result);
    } // gcd(a, b)
    
    /** Returns the least common multiple of 2 integers.
     *  @param a 1st integer
     *  @param b 2nd integer
     *  @return lcm(a,b)
     */
    public static int lcm(int a, int b) {
        int result = a * b;
        if (result < 0) { // absolute, make positive
            result = - result;
        } // abs
        return result / gcd(a, b);
    } // lcm(int,int)

    /** Returns the product of one Ratio and a second.
     *  @param rat1 1st Ratio
     *  @param rat2 2nd Ratio
     *  @return (rat1 * rat2)
     */
    public static Ratio multiply(Ratio rat1, Ratio rat2) {
        return Ratio.reduce(new Ratio
                ( rat1.numerator   * rat2.numerator
                , rat1.denominator * rat2.denominator)
                );
    } // multiply(Ratio)

    /** Returns the negative of a Ratio.
     *  @param rat1  Ratio to be negated
     *  @return (- rat1)
     */
    public static Ratio negate(Ratio rat1) {
        return Ratio.reduce(new Ratio( - rat1.numerator, rat1.denominator));
    } // negate(Ratio)

    /** Reduces and normalizes the fraction, that means: 
     *  (1) divides the numerator and the denominator by
     *  their greatest common divisor, if that is > 1,
     *  (2) makes the denominator always positive, 
     *  (3) normalizes the denominator to 1 if the numerator is 0.
     *  @param rat1 the Ratio to be reduced
     *  @return the reduced Ratio
     */
    public static Ratio reduce(Ratio rat1) {
        Ratio result = new Ratio(rat1.numerator, rat1.denominator);
        int common = gcd(rat1.numerator, rat1.denominator);
        if (common > 1) {
            result.numerator   /= common;
            result.denominator /= common;
        }
        if (result.numerator == 0) {
            result.denominator = 1;
        } else if (result.denominator < 0) {
            result.numerator   = - result.numerator;
            result.denominator = - result.denominator;
        }
        return result;
    } // reduce(Ratio)
 
    /** Returns the difference of one Ratio and a second.
     *  @param rat1 1st Ratio
     *  @param rat2 2nd Ratio
     *  @return (rat1 - rat2)
     */
    public static Ratio subtract(Ratio rat1, Ratio rat2) {
        return Ratio.add(rat1, Ratio.negate(rat2));
    } // subtract(Ratio)

    /** Returns this Ratio as a String.
     *  @return a String of the form "a/b", or only "a" if b is 1.
     */
    public String toString() {
        String result = String.valueOf(numerator);
        if (denominator != 1) {
            result += "/" + String.valueOf(denominator);
        }
        return result;
    } // toString()

    //================
    /** Test program, shows a series of fixed operations
     *  @param args String array of commandline arguments
     */
    public static void main(String[] args) {
        int iarg = 0;
        String   str0 = "0/1";
        Ratio rat0 = new Ratio(str0);
        while (iarg < args.length) {
            String   str1 = args[iarg];
            Ratio rat1 = new Ratio(str1);
            System.out.println(str0 + " + " + str1 + " = " + Ratio.add     (rat0, rat1).toString());
            System.out.println(str0 + " - " + str1 + " = " + Ratio.subtract(rat0, rat1).toString());
            System.out.println(str0 + " * " + str1 + " = " + Ratio.multiply(rat0, rat1).toString());
            System.out.println(str0 + " / " + str1 + " = " + Ratio.divide  (rat0, rat1).toString());
            System.out.println();
            str0 = str1;
            rat0 = rat1;
            iarg ++;
        } // while iarg
    } // main
} // Ratio

< Previous: JT05 Control Structures: The Greatest Common Divisor
> Next: JT07 Class Rational