Results 1 to 4 of 4
  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Difficulty with constructors and objects, is this a bad solution?

    ok so my task is to create a class that deals with fractional (rational) numbers, first I have to reduce the fractions in the constructor

    I created a method called reduce which I used in the constructor, it took two parameters and then assigned the reduced versions to my instance variables. I tested it and it worked fine. however I thought I could improve the plugability of my code by changing the method to not set the instance variables but instead return a new object with the reduced values, I could then change it to static and have a nice portable method.

    The problem now is that I cant use it in the constructor because in the reduce method I create the same object, which then calls the constructor and so on and so forth... stack overflow. I was thinking, if I wanted to stop the stackoverflow and use method reduce in my constructor I could create another "special" constructor only to be called in the reduce method. this constructor wont call reduce and therefore no stack overflow would occur...

    now, I havent heard of anyone using special constructors in this way... is this bad programming practice? can I do this or would you say, if I want the reduce method as it is, I shouldnt have it in my constructor.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Difficulty with constructors and objects, is this a bad solution?

    For a Fraction class I'd do this in the constructor:

    1) do prelininary checks (e.g. if the denominator == 0 throw an IllegalArgumentException)
    2) calculate the greatest common divisor of the numerator and denominator
    3) divide both the numerator and denominator by that greatest common divisor and store the results.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Difficulty with constructors and objects, is this a bad solution?

    I suck at maths so much, this is the reduce method I came up with all by myself it seems to work so I'm proud of it :)

    Java Code:
    	public static RationalNumbers_8_16 reduce(double numerator, double denominator)
    	{
    		int count = 0;
    		double numeratorReserve = numerator;
    		double denominatorReserve = denominator;
    		double numeratorReduced = 0;
    		double denominatorReduced = 0;
    
    		if ( numerator >= denominator )
    		{
    			count = (int)Math.ceil(numerator);
    		}
    		else
    		{
    			count = (int)Math.ceil(denominator);
    		}
    
    		for( int i = 2 ; i <= count ; i++ )
    		{
    			if ( numerator % i == 0 && denominator % i == 0) 
    			{
    				numeratorReduced = numerator / i;
    				denominatorReduced = denominator / i;
    			}
    		}
    
    		if( numeratorReduced == 0 && denominatorReduced == 0 )
    		{			
    			numeratorReduced = numeratorReserve;
    			denominatorReduced = denominatorReserve;
    		}
    
    		RationalNumbers_8_16 reduced = new RationalNumbers_8_16(numeratorReduced, denominatorReduced);
    
    		return reduced;
    	}
    Could you lend me your guidence for the add method? I did this

    Java Code:
    public void add(double numerator, double denominator)
    	{
    		double originalNumerator = numerator;
    		double originalDenominator = denominator;
    
    		while( true )
    		{
    			if( denominator < this.denominator )
    			{
    				for( int i = 1 ; i <= this.denominator ; i++ )
    				{
    					numerator = numerator + originalNumerator;
    					denominator = denominator + originalDenominator;
    
    					if( denominator == this.denominator )
    					{
    						this.numerator = numerator;
    						this.denominator = denominator;
    						return;
    					}
    				}
    			}
    
    			if( denominator >= this.denominator )
    			{
    				for( int i = 1 ; i <= denominator ; i++ )
    				{
    					numerator = numerator + originalNumerator;
    					denominator = denominator + originalDenominator;
    
    					if( denominator == this.denominator )
    					{
    						this.numerator = numerator;
    						this.denominator = denominator;
    						return;
    					}
    				}
    			}
    		}
    
    //jump to main
    
    public static void main(String[] args)
    	{
    		RationalNumbers_8_16 rational = new RationalNumbers_8_16(1, 3);
    		//rational = reduce(rational.numerator, rational.denominator);
    		rational.add( 1, 6);
    		System.out.println(rational.toString());
    	}
    but it doesnt work... in fact nothing prints out (does this mean its hanging? also what does hanging mean) I'm terrible at maths... I tried following it through and I release it does end up calculating vast numbers if it misses the other number, and because that end result is used as an argument for the count variable the next time the number of calculations increases exponentionally!

    remember I'm bad at maths (and still not very good at programming) can you give me some advice on how to go about this, you can see I'm trying

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Difficulty with constructors and objects, is this a bad solution?

    First get rid of all the doubles; fractions have an integer numerator and denominator. Next, have a look at Euclid's algorithm on this page. It's easiest to make your Fraction objects immutable (i.e. they can't change their value once it's set in their constructor).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Objects passing information to constructors.
    By Kirstielol in forum New To Java
    Replies: 6
    Last Post: 03-08-2012, 11:44 PM
  2. Constructors Objects and Classes
    By Tykk in forum New To Java
    Replies: 4
    Last Post: 10-10-2009, 11:31 PM
  3. Passing objects into constructors
    By aaronfsimons in forum New To Java
    Replies: 8
    Last Post: 04-14-2009, 12:08 PM
  4. Applications in different difficulty
    By bubbless in forum New To Java
    Replies: 2
    Last Post: 03-11-2009, 12:31 AM
  5. difficulty
    By Daniela_v in forum New To Java
    Replies: 2
    Last Post: 03-04-2009, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •