jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Adding instances of a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Adding instances of a class" Watch "Adding instances of a class" New topic
Author

Adding instances of a class

Brent Williams
Greenhorn

Joined: Jun 11, 2006
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 class addRational
{
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++)
{
a.add(new Rational(x,y));
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;
}

public Rational add(Rational secondRational)
{
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;
}
}

}

cud some1 help please ?
[ June 11, 2006: Message edited by: Brent Williams ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16


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


[My Blog]
All roads lead to JavaRanch
Brent Williams
Greenhorn

Joined: Jun 11, 2006
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding instances of a class
 
Similar Threads
Help on understanding this code
Don't know where to start???
null pointer exception
sorting fractions problem
int can not be dereferenced error