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?
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> double x = Double.doubleValue();
should be n.doubleValue()
> int x = Integer.intValue();
should also be n.intValue();
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);
double compare(double x)
{
if(x>0)
{
return 5;
}
}
int i = 2;
i = (int)compare(i);
double d = 2.2;
d = compare(d);
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.
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.
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.
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'
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);
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(doub les);
int[] ints = { .... };
java.util.Arrays.sort(ints )
double[] doubles = { .... };
java.util.Arrays.sort(doub
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.lengt h()).equal s("D"))
{
if(Integer.parseInt(num.su bstring(0, num.length ()-1))>0{
return 5;
}
}
else{
if(Integer.parseInt(num.su bstring(0, num.length ())>0){
return 5;
}
}
}
String myNumberI = Integer.toString(1);
int result = compare(myNumberD);
int result = compare(myNumberI);
.
.
.
compare(String num)
{
if(num.substring(num.lengt
{
if(Integer.parseInt(num.su
return 5;
}
}
else{
if(Integer.parseInt(num.su
return 5;
}
}
}
oops ...
compare(String num)
{
if(num.substring(num.lengt h()).equal s("D"))
{
if(Double.parseDouble(num. substring( 0,num.leng th()-1))>0 {
return 5;
}
}
else{
if(Integer.parseInt(num)>0 ){
return 5;
}
}
}
compare(String num)
{
if(num.substring(num.lengt
{
if(Double.parseDouble(num.
return 5;
}
}
else{
if(Integer.parseInt(num)>0
return 5;
}
}
}
sorry .. again1
if(num.substring(num.lengt h()).equal s("D"))
should be
if(num.substring(num.lengt h()-1).equ als("D"))
if(num.substring(num.lengt
should be
if(num.substring(num.lengt