# Strange output with Fraction program using multiple classes

Page 1 of 1

## 3 Replies - 517 Views - Last Post: 14 November 2012 - 01:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=300181&amp;s=1c27f132025759b5bf0cd8b3963755e3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 clyde1993

Reputation: 3
• Posts: 38
• Joined: 15-February 12

# Strange output with Fraction program using multiple classes

Posted 14 November 2012 - 08:53 AM

The output of the following program is Rational@1186fab * Rational@14b7453 = Rational@c21495
and I don't understand why.

```public class Rational{
public int numerator, denominator;
public Rational( int num, int den ) // This constructor takes two numbers and makes a fraction out of them
{
if (den!=0)
{
numerator = num;
denominator = den;
}
else
System.exit(0);
}

public Rational times( Rational that ) // This Method multiplies two fractions together
{
return new Rational( this.numerator * that.numerator, this.denominator * that.denominator );
}

public Rational dividedBy( Rational that)
{
return new Rational( this.numerator * that.denominator , this.denominator / that.numerator );

}
public Rational plus( Rational that)
{
return new Rational( (this.numerator * that.denominator) + (this.denominator * that.numerator), this.denominator*that.denominator);

}
public Rational minus( Rational that)
{
return new Rational( (this.numerator * that.denominator) - (this.denominator * that.numerator), this.denominator*that.denominator);

}
public void reduce() {
int num = Math.abs(numerator);
int den = Math.abs(denominator);

while (den != 0) {
int c = den;
den = num % den;
num = c;
}

int gcd = num;

numerator /= gcd;
denominator /= gcd;
}

}

```

```public class RationalTester{
public static void main(String[] args)
{
Rational x = new Rational(6, 4); //This fraction is 6/4
Rational y = new Rational(5, 2); // This fraction is 5/2
Rational z = x.times(y); //This method multiplies the two fractions x and y and assigns the product to z
z.reduce();  // This method reduces the fraction z
System.out.println( x.toString() + " * " + y.toString() + " = " + z.toString() );
System.exit(0);
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Strange output with Fraction program using multiple classes

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Strange output with Fraction program using multiple classes

Posted 14 November 2012 - 09:16 AM

Because your Rational class does not have a

public String toString() method
so println() prints the object address in memory

### #3 clyde1993

Reputation: 3
• Posts: 38
• Joined: 15-February 12

## Re: Strange output with Fraction program using multiple classes

Posted 14 November 2012 - 01:10 PM

how do I create a toString method, do I parse?

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Strange output with Fraction program using multiple classes

Posted 14 November 2012 - 01:46 PM

for a Rational class I would use
```public String toString() {
return " " + numerator + "/" + denominator + " ";
}

```

I guess