Member Avatar for Amoryethel

I'm creating a program that adds fractions together. That being said however, my program does not seem to be responding correctly. Everytime I run the client code, it outputs
18, when I want it to add 2/6 and 1/6.

// Constructor //

public class RationalNumber
{
	int num;
	int den;
	
	private int gcd(int x, int y)
	{
		while(y != 0)
		{
			int temp = x % y;
			x = y;
			y = temp;
		} 		
		return x;
	}
	
	public RationalNumber(int numerator, int denominator)
	{
		if(denominator == 0)
		{
			throw new IllegalArgumentException("You can't divide by zero.");
		}
		else
		{
			num = numerator;
			den = denominator;
		}
	}
	
	public RationalNumber()
	{
		this (0,1); 
	}
	
	public boolean equals(Object o)
	{
		if (o instanceof RationalNumber)
		{
			RationalNumber other = (RationalNumber) o;
			int r1 = this.gcd(num,den);
			int r2 = this.gcd(other.getNumerator(),other.getDenominator());
			return num/r1 == other.num/r2 && den/r1 == other.den/r2;	
		}
		else
		{
			return false;
		}
	}
	
	public int getDenominator()
	{
		return den;
	}
	
	public int getNumerator()
	{
		return num;
	}
	
	public void reduce()
	{
		int d;
		while (true)
		{
			d = this.gcd(this.num,this.den);
			if (d == 1)
			{
				return;
			}
			this.num /= d;
			this.den /= d;
		}
	}			

	public int add(RationalNumber r1)
	{
		int commonDen = den * r1.getDenominator();
		int num1 = num * r1.getDenominator();
		int num2 = r1.getNumerator() * den;
		int sum = num1 + num2;
		return sum;
	}
	
	public String toString()
	{
		if (den == 1)
		{
			return num + "";
		}
		else	
		{
			return num + "/" + den;
		}
	}
	
}
// Client Code //

public class RationalNumberTest
{
   public static void main (String[] args)
   {
	RationalNumber r1 = new RationalNumber(2,6);
	RationalNumber r2 = new RationalNumber(1,6);
		
	System.out.println(r1.add(r2));
   }
}

lines 82/83 you add together the numerators (after getting them over the LCM denominator), but you forgot to divide by the LCM denominator - ie you calc
(2*6) + (1*6)
when it should be
((2*6) + (1*6))/(6*6)

But in any case shouldn't add return a RationalNumber, rather than an int (which will be 0 in your test case because the answer is <1

Member Avatar for Amoryethel

Thank you for your response. Unfortunately, it still outputs the same answer.

public int add(RationalNumber r1)
	{
		int commonDen = den * r1.getDenominator();
		int num1 = num * r1.getDenominator();
		int num2 = r1.getNumerator() * den;
		int sum = num1 + num2;
		int total = sum / den * getDenominator();
		return total;
	}

That's not exactly what I said in my post. You can't ignore the brackets!...
18 / 6 * 6 = 18
18 / (6*6) = 1/2

B
Brackets first
O
Orders (ie Powers and Square Roots, etc.)
DM
Division and Multiplication (left-to-right)
AS
Addition and Subtraction (left-to-right)

Split your calculations using this order of operations

Member Avatar for Amoryethel
int commonDen = den * r1.getDenominator();
	int num1 = num * r1.getDenominator();
	int num2 = r1.getNumerator() * den;
	int sum = num1 + num2;
	int total =  sum / (den * den);
	return total;

Like so?
My program now outputs zero.

Come on! print some values out as you initialise them! Test your code!!
i.e.

System.out.println("value of sum: "+sum);//print after your initialisation of sum

Outputs zero just like I said in my first post. Read it again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.