Multiplication of Fractions
by Isai Damier

/***************************************************************************
 *  Author: Isai Damier
 * Title: Multiplication of Fractions
 * Project: geekviewpoint
 * Package: algorithms
 *
 * Statement:
 *   Multiplication the two given fractions
 *
 * Sample Input: {-5,6}, {6,8})
 * Sample Output: {-5,8}
 *
 * Technical Details:
 *   This is the same algorithm you learned in grade school:
 *   1) multiply the numerators
 *   2) multiply the denominators
 *   4) reduce the resulting fraction using the GCD (greatest common
 *     divisor) of its numerator and denominator.
 *
 **************************************************************************/ 
 public int[] fractionMultiplication(int[] a, int[] b) {
    int numerator = a[0] * b[0];
    int denominator = a[1] * b[1];
    int num = 0 > numerator? -numerator:numerator;
    int den = 0 > denominator? -denominator:denominator;
    int gcd = GCD(num, den);
    gcd = 0 > gcd ? -gcd : gcd;
    return new int[]{numerator / gcd, denominator / gcd};
  }
import org.junit.Test;
import static org.junit.Assert.*;

public class NumbersTest {

  /**
   * Test of fractionMultiplication method, of class Numbers.
   */
  @Test
  public void testFractionMultiplication() {
    System.out.println("fractionMultiplication");
    Numbers numbers = new Numbers();
    int[] expResult = {16, 15};
    int[] result = numbers.fractionMultiplication(
            new int[]{8, 5}, new int[]{10, 15});
    assertArrayEquals(expResult, result);
    
    expResult = new int[]{-5, 8};
    result = numbers.fractionMultiplication(
            new int[]{-5, 6}, new int[]{6, 8});
    assertArrayEquals(expResult, result);
  }
}