Link to home
Start Free TrialLog in
Avatar of danceswithwolves
danceswithwolves

asked on

A function that can take either a double or int and return a double or int

I want to combine the two functions:

int compare(int x)
{
 if(x>0)
 {
   return 5;
 }
}

double compare(double x)
{
 if(x>0)
 {
   return 5;
 }
}

Is there anyway to do this in java?
Avatar of imladris
imladris
Flag of Canada image

There is no apparent way to accomplish that, no. If you explain why you want to do that, we may be able to make some other suggestion that will solve your problem though.
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> double x = Double.doubleValue();

should be n.doubleValue()

> int x = Integer.intValue();

should also be n.intValue();
Since int is assignable to a double and double castable to int you can do

double compare(double x)
{
 if(x>0)
 {
   return 5;
 }
}

int i = 2;
i = (int)compare(i);

double d = 2.2;
d = compare(d);
Avatar of pranjan252
pranjan252

You can assign int to double but not double to int. Compiler will give you error that some info will be lost if you assign this way.

int x = 5;
double z = 5.6;
int a;
a = z;  // compiler will give you problem
double y;
y = x; // no problem

double compare(double x)
{
 if(x>0)
 {
   return 5;
 }
}

is perfectly ok.
CEHJ :-:--> Since int is assignable to a double and double castable to int you can do

int is assiagnable to double but double is not assignable to int. If you assign double to int type compiler will give you error:
"possible loss of precision" if you want you can test it.

Avatar of danceswithwolves

ASKER

what I wanted to do is write a sort, that sorted an array of doubles or an array of ints, but I didn't want to write the code twice.
>>but double is not assignable to int.

Yes, which is why i said

>>and double castable to int

and not 'double assignable to int'
LinkedList ll = new LinkedList();
ll.add(new Double(1.1));
ll.add(new Double(0.1));
Collections.sort(ll);


LinkedList ll = new LinkedList();
ll.add(new Integer(1));
ll.add(new Integer(0));
Collections.sort(ll);
And you can do

double[] doubles = { .... };
java.util.Arrays.sort(doubles);
int[] ints = { .... };
java.util.Arrays.sort(ints)
String myNumberD = Double.toString(4.2)+"D";
String myNumberI = Integer.toString(1);
int result = compare(myNumberD);
int result = compare(myNumberI);
.
.
.

compare(String num)
{
if(num.substring(num.length()).equals("D"))
{
if(Integer.parseInt(num.substring(0,num.length()-1))>0{
  return 5;
}
}
else{
if(Integer.parseInt(num.substring(0,num.length())>0){
return 5;
}
}
}
oops ...

compare(String num)
{
if(num.substring(num.length()).equals("D"))
{
if(Double.parseDouble(num.substring(0,num.length()-1))>0{
 return 5;
}
}
else{
if(Integer.parseInt(num)>0){
return 5;
}
}
}
sorry .. again1

if(num.substring(num.length()).equals("D"))

should be

if(num.substring(num.length()-1).equals("D"))