3 Replies - 390 Views - Last Post: 14 November 2012 - 01:46 PM Rate Topic: -----

#1 clyde1993  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • 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  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 clyde1993  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • 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 :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1