Results 1 to 4 of 4
 05182012, 09:49 PM #1Senior Member
 Join Date
 Apr 2012
 Posts
 115
 Rep Power
 0
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.
 05182012, 10:08 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,784
 Blog Entries
 7
 Rep Power
 21
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,
Joscenosillicaphobia: the fear for an empty beer glass
 05182012, 10:46 PM #3Senior Member
 Join Date
 Apr 2012
 Posts
 115
 Rep Power
 0
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; }
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()); }
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
 05192012, 09:06 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,784
 Blog Entries
 7
 Rep Power
 21
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,
Joscenosillicaphobia: the fear for an empty beer glass
Similar Threads

Objects passing information to constructors.
By Kirstielol in forum New To JavaReplies: 6Last Post: 03092012, 12:44 AM 
Constructors Objects and Classes
By Tykk in forum New To JavaReplies: 4Last Post: 10112009, 12:31 AM 
Passing objects into constructors
By aaronfsimons in forum New To JavaReplies: 8Last Post: 04142009, 01:08 PM 
Applications in different difficulty
By bubbless in forum New To JavaReplies: 2Last Post: 03112009, 01:31 AM 
difficulty
By Daniela_v in forum New To JavaReplies: 2Last Post: 03042009, 06:36 PM
Bookmarks