Java Training Course/JT06

From tehowiki
Revision as of 10:49, 24 September 2017 by imported>Gfis (new)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Skeleton of Class Rational

/* Rational: exact fractions of integers
 * @(#) $Id$
 * 2017-09-23, Georg Fischer
 */
// package name will later go here
// imports will later go here

/** Class <wm>Rational represents an integer fraction
 *  together with the arithmetic operations on such Rationals.
 *  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 Rational {
    /** value above the bar, before the slash */
    private int numerator;
    /** value below the bar, after  the slash */
    private int denominator;
    
    /** No-args constructor, creates 1/1
     */
    public Rational() {
        numerator   = 1;
        denominator = 1;
    } // no-args constructor
    
    /** Constructor with numerator, creates a/1
     *  @param a numerator
     */
    public Rational(int a) {
        numerator   = a;
        denominator = 1;
    } // constructor(int)
    
    /** Constructor with numerator and denominator, creates a/b
     *  @param a numerator
     *  @param b denominator
     */
    public Rational(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 Rational(String str) {
        int slashPos = str.indexOf("/");
        if (slashPos < 0) {
            str += "/1";
            slashPos = str.length() - 2;
        }
        numerator   = Integer.parseInt(str.substr(0, slashPos));
        denominator = Integer.parseInt(str.substr(slashPos + 1));
    } // constructor(String)
    
    /** Gets the numerator
     *  @return the numerator   of this Rational
     */
    private int getNum() {
    	return numerator;
    } // getNum()
        
    /** Gets the denominator
     *  @return the denominator of this Rational
     */
    private int getDen() {
    	return denominator;
    } // getDen()
        
    /** Returns the Rational 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()
    
    /** 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 int gcd(int a, int b) {
        int result = 0;
        return result;
    } // gcd(int,int)
    
    /** Returns the least common multiple of 2 integers.
     *  @param a 1st integer
     *  @param b 2nd integer
     *  @return lcm(a,b)
     */
    public int lcm(int a, int b) {
        int result = a * b;
        if (result < 0) { // make absolute
        	result = - result;
        } // abs
        return result / gcd(a, b);
    } // lcm(int,int)
     
    /** Returns the difference between this Rational and a second.
     *  @param rat2 2nd Rational
     *  @return (this - rat2)
     */
    public Rational subtract(Rational rat2) {
    	return this.add(rat2.negate()).reduce();
    } // subtract(Rational)
    
    /** Returns the product of this Rational and a second.
     *  @param rat2 2nd Rational
     *  @return (this * rat2)
     */
    public Rational multiply(Rational rat2) {
    	return (this.getNum() * rat2.getNum() / (this.getDen() * rat2.getDen())).reduce();
    } // multiply(Rational)

    /*  Implement the following methods in the same fashion:
    ... reduce
    ... add
    ... subtract
    ... multiply (c.f. above)
    ... divide
    ... equals
    ... compareTo
    ... abs
    ... max
    ... min
    ... negate
    ... intValue
    */
    public static void main(String[] args) {
        System.out.println(args[0]);
    } // main
} // Rational