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.
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
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 :)
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
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
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