# Adding instances of a class

Brent Williams
Greenhorn
Posts: 2
hi im trying to add rational numbers from 1/2 to 98/99 using the class Rational but all it does is display the first rational, and i dont know how i cud do it without creating like 90 objects of this class
here's my code

{
public static void main(String [] args)
{
// Declaring variables to be used
Rational a = new Rational(1,2);
int x = 1, y = 2;

// Adding a new rational to the rational a
for(x=1; x<=99; x++)
{
y++;
}

// Printing the results
System.out.println(a.doubleValue());
}
}

public class Rational extends Number implements Comparable
{
private long numerator = 0;
private long denominator = 1;

public Rational()
{
this(0,1);
}

public Rational(long numerator, long denominator)
{
long gcd = gcd(numerator, denominator);
this.numerator = ((denominator > 0) ? 1 : -1) * numerator / gcd;
this.denominator = Math.abs(denominator) / gcd;
}

private static long gcd(long n, long d)
{
long n1 = Math.abs(n);
long n2 = Math.abs(d);
int gcd = 1;

for (int k = 1; k <= n1 && k <= n2; k++)
{
if (n1 % k == 0 && n2 % k ==0)
{
gcd = k;
}
}

return gcd;
}

public long getNumerator()
{
return numerator;
}

public long getDenominator()
{
return denominator;
}

{
long n = numerator * secondRational.getDenominator() + denominator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n,d);
}

public Rational subtract(Rational secondRational)
{
long n = numerator * secondRational.getDenominator() - denominator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n,d);
}

public Rational multiply(Rational secondRational)
{
long n = numerator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}

public Rational divide(Rational secondRational)
{
long n = numerator * secondRational.getDenominator();
long d = denominator * secondRational.getNumerator();
return new Rational(n,d);
}

public String toString()
{
if (denominator == 1)
{
return numerator + " ";
}
else
{
return numerator + "/" + denominator;
}
}

public boolean equals(Object parm1)
{
if ((this.subtract((Rational)(parm1))).getNumerator() == 0)
{
return true;
}
else
{
return false;
}
}

public int hashCode()
{
return new Double(this.doubleValue()).hashCode();
}

public int intValue()
{
return (int)doubleValue();
}

public float floatValue()
{
return (float)doubleValue();
}

public double doubleValue()
{
return numerator * 1.0 / denominator;
}

public long longValue()
{
return (long)doubleValue();
}

public int compareTo(Object o)
{
if((this.subtract((Rational)o)).getNumerator() > 0)
{
return 1;
}
else if((this.subtract((Rational)o)).getNumerator() < 0)
{
return -1;
}
else
{
return 0;
}
}

}

[ June 11, 2006: Message edited by: Brent Williams ]

Sheriff
Posts: 14691
16

The add method returns a new Rational. You should use it, may be like:

Brent Williams
Greenhorn
Posts: 2
doesnt work just freezes jbuilder,
well it works but it is really slow like ive waited ten minutes and it still aint finished, the output is correct tho it just seems the more it goes thru the longer it takes
[ June 12, 2006: Message edited by: Brent Williams ]