/**
 * Class for processing rational numbers (factions)
 * @author Henry M. Walker
 */

package examples;

public class Rational {

	/**
	 * a rational number is a numerator (num) divided by a 
	 * denominator (denom)
	 * Assertion:  throughout, denom > 0
	 */
	private int num;
	private int denom;
	/**
	 * Constructors
	 */
	
	/**
	 * 0-parameter:  the default value is 0/1 == 0
	 */

	public Rational() {
		num = 0;
		denom = 1;
	}

	/**
	 * 1-parameter:  Given the numerator, the denominator is 1
	 * @param num
	 */
	public Rational(int num) {
		this.num = num;
		denom = 1;
	}
	
	/**
	 * 2-parameter:  Both numerator and denominator are given
	 * @param num, the fraction numerator
	 * @param denom, the fraction denominator
	 */
	public Rational(int num, int denom) {
		this.num = num;
		this.denom = denom;
	}
	
	/**
     * 	form string representation
     */	
	public String toString () {
		return num + "/" + denom;
	}
	
	/**
	 * arithmetic operations 
	 */
	
	/**
	 * addition of rational numbers, using a common denominator
	 * @param:  rat is the number to be added to the current Rational
	 * @return:  the sum of this Rational with rat
	 */
	 Rational add (Rational rat) {
	 
	 //add by utilizing a common denominator
	 int numerator  =  num * rat.denom + rat.num * denom;
	 int denominator = denom * rat.denom;
	 return new Rational (numerator, denominator);
	 }		
	 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Rational rat1 = new Rational (1, 3);
		Rational rat2 = new Rational (1, 4);
		
		Rational sum = rat1.add (rat2);
		
		System.out.print("the sum of " + rat1 + " and " + rat2);
		System.out.println (" is " + sum);	
	}
}
