LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2006, 05:15 PM   #1
mohama
Member
 
Registered: May 2005
Location: solar-system->earth->northern hemisphere
Distribution: ubunutu+knoppix+suse
Posts: 197

Rep: Reputation: 31
Question instanceof ..?


hi,

i have a question ?
how can i know with which constructor a specific object was created .. i mean which constructor within the class(in case there are more than one ) had initialise the object i have ??

-- i do know that the object i have is instaceof class X for example , but what i ask is within class X which constructor built it .. ?
-- i am using java ..!

muhammad-sameer,

Last edited by mohama; 01-11-2006 at 05:20 PM.
 
Old 01-11-2006, 05:30 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I don't know of a way to find that out unless you put the clue in the constructor itself, that is you have a property which each constructor would set to different values.

graeme.
 
Old 01-11-2006, 05:35 PM   #3
mohama
Member
 
Registered: May 2005
Location: solar-system->earth->northern hemisphere
Distribution: ubunutu+knoppix+suse
Posts: 197

Original Poster
Rep: Reputation: 31
replay..

well , this is exactly the problem i face , since i use doubles , and using '==' is problematic , and the EPSILON to use inorder to determine wither two doubles are equals : |d1-d2|<EPSILON
is the parameter i initialise at the constructor ...

thanks ,
m-s
 
Old 01-11-2006, 06:48 PM   #4
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
Can u post the constructor codes?
 
Old 01-11-2006, 07:04 PM   #5
mohama
Member
 
Registered: May 2005
Location: solar-system->earth->northern hemisphere
Distribution: ubunutu+knoppix+suse
Posts: 197

Original Poster
Rep: Reputation: 31
it's simple code ... but in order to implement the equals () method ( it's defined true iff the two objects were created by the same constructor with the same _precision)
Code:
public SineFunction ( )
{
     this( EPSILON ) ;
}
Code:
public SineFunction ( double precision )
{
     _precision = precision ;
}
the point is that i can't check wether ... |EPSILON - EPSILON|<EPSILON ..???!!!

hope that it's clearer now , thanks alot ,
m-s
 
Old 01-11-2006, 11:55 PM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
the point is that i can't check wether ... |EPSILON - EPSILON|<EPSILON ..???!!!

hope that it's clearer now , thanks alot ,
m-s
graeme has a good point... the approach i also would take would be to put a flagg in the class.. i do not know of any other way to find out which constructor was called after the control has returned from the constructor.. though i am no java expert, but i still dont see this being possible.. here is a rough idea..
Code:
private boolean precisionSet;
public SineFunction ( ){
     precisionSet=false;
     this( EPSILON ) ;
}
public SineFunction ( double precision ){
     precisionSet=true;
     _precision = precision ;
}
then when you overload the operators you can put in code to test for the precision and do any conversions or whatnot on the fly..

hth

<edit>fixed broken quote!</edit>

Last edited by xhi; 01-12-2006 at 12:07 PM.
 
Old 01-12-2006, 07:09 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
the point is that i can't check whether ... |EPSILON - EPSILON|<EPSILON ..???!!!
I think you want.
Code:
|d1-d2|<EPSILON
where d1 and d2 are objects of double.

If EPSILON is always a fixed value then I would make it static so that you have the same value for every instance of double. If not then you will need to make a decision on which EPSILON to use, I would guess that the double to the left of the operation would make sense, hence

Code:
double d1(56.5, 0.5);
double d2(56.0, 0.25);
...
d1.equal(d2); // would use EPSILON .5 and equate to true
d2.equal(d1); // would use EPSILON .25 and equate to false
I hope that helps a little.

graeme.
 
Old 01-12-2006, 10:57 AM   #8
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
Can u exaplain ur code graeme...
I wonder what the two argument constructor does...
 
Old 01-12-2006, 11:25 AM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Well the idea was that the first argument was the actual value whilst the second argument was the epsilon value. This only necessary if the epsilon value may change and it could be defaulted to a fixed value. Also the actual values I used are not very good ones! You would want the epsilon to be much smaller in relation to the magnitude of the actual number.

graeme
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration