
public class MixedNumber {

	int wholePart;
	int num;
	int deno;
	
	public MixedNumber()
	{
		wholePart=0;
		num=0;
		deno=0;
	}
	
	public MixedNumber(int wholePart, int num, int deno)
	{
		this.wholePart= wholePart;
		this.num = num;
		this.deno = deno;
	}
	
	public int getWholePart()
	{
		return wholePart;
	}
	
	public int getNumPart()
	{
		return num;
	}
	
	public int getDenoPart()
	{
		return deno;
	}
	
	public void addMixedNumber(MixedNumber m2)
	{
		this.wholePart = this.wholePart + m2.wholePart;
		
		//add fraction part
		int newNum = (num * m2.deno + m2.num * deno);
		int newDen = (deno * m2.deno);
		num = newNum;
		deno = newDen;
		
		//reduce the fraction
		int gcd = gcd(num, deno);
		num = num/gcd;
		deno = deno/gcd;
		
		this.wholePart= (int)(num/deno) + this.wholePart;
		this.num = (int)(num%deno);
		
		
	}
	
	public String toString(){
		if (num !=0)
			return (wholePart + " " + num + "/" + deno);
		else
			return (wholePart+"");
	}
	
	private int gcd(int n1, int n2)
    	{
        int min;
        if (n1 < n2)
            min = n1;
        else
            min = n2;
        for (int i = min; i > 1; i--) {
            if (n1 % i == 0 && n2 % i == 0)
                return i;
        }
		return 1;
	}
}
