It's been a long break since we had a puzzle here, so without much ado here is the next one.
package com.twister;
public class Mystery {
public static boolean isEqual(<DataType1> f1, <DataType2>f2){
return f1.equals(f2);
}
public static void main(String[] args) {
<DataType3> f1= <value1>
<DataType4> f2 = <value2>
System.out.println("f1 is equals to f2 : "+ (f1==f2)); //prints true
System.out.println("f1 is equals to f2 : "+ isEqual(f1,f2)); //prints false
}
}
Everything that you need to solve this is present in the code snippet above. All you need to do is replace everything in brackets namely - DataType1 - DataType4 & Value-1 - Value-2 with appropriate constructs, so that the prints happen as per the comments. No other changes to be done to this code!
You might want to watch out for hints and more restrictions @ Twitter.
Got a solution. Do leave it here.
Treasure Hunt Results to be updated next week.
Easy one
ReplyDeletepublic static boolean isEqual(Float f1, Double f2){
return f1.equals(f2);
}
public static void main(String[] args) {
float f1 = 1f/3;
double f2 = 1f/3;
System.out.println("f1 is equals to f2 : " + (f1 == f2)); //prints true
System.out.println("f1 is equals to f2 : " + isEqual(f1, f2)); //prints false
}
public class Mystery {
ReplyDeletepublic static boolean isEqual(Object f1, Object f2){
return f1.equals(f2);
}
public static void main(String[] args) {
Object f1= new Object(){
@Override
public boolean equals(Object obj) {
return false;
}
};
Object f2 = f1;
System.out.println("f1 is equals to f2 : "+ (f1==f2)); //prints true
System.out.println("f1 is equals to f2 : "+ isEqual(f1,f2)); //prints false
}
}
DataType1 and 2: Number
ReplyDeleteDataType3: int
DataType4: double
value1 and value2: 1
So:
public static boolean isEqual(Number f1, Number f2){
return f1.equals(f2);
}
public static void main(String[] args) {
int f1= 1;
double f2 = f1;
System.out.println("f1 is equals to f2 : "+ (f1==f2)); //prints true
System.out.println("f1 is equals to f2 : "+ isEqual(f1,f2)); //prints false
}
Prints true, false.
Problem here is autoboxing and Integers are never equal to non-Integers, even if their value is the '=='-same.
public class Mystery {
ReplyDeletepublic static boolean isEqual(Double f1, Double f2){
return f1.equals(f2);
}
public static void main(String[] args) {
double f1 = -0.0;
double f2 = 0.0;
System.out.println("f1 is equals to f2 : " + (f1 == f2)); // prints true
System.out.println("f1 is equals to f2 : " + isEqual(f1, f2)); // prints false
}
}
Since u haven't said that I can't add the code , I just added a overridden isEqual method which always return false, Dont give me points If this is not the solution within the rules.
ReplyDeletepackage com.twister;
public class Mystery {
public static boolean isEqual(Mystery f1, Mystery f2){
return f1.equals(f2);
}
public static boolean isEqual(Object f1, Object f2){
return false;
}
public static void main(String[] args) {
Object f1= new Object();
Object f2 = new Object();
System.out.println("f1 is equals to f2 : "+ (f1==f2)); //prints true
System.out.println("f1 is equals to f2 : "+ isEqual(f1,f2)); //prints false
}
}
Not sure what the hints or criteria are but my solution is this:
ReplyDeletepublic class Mystery {
public static boolean isEqual(Float f1, int f2){
return f1.equals(f2);
}
public static void main(String[] args) {
Float f1= 1f;
int f2 = 1;
System.out.println("f1 is equals to f2 : "+ (f1==f2)); //prints true
System.out.println("f1 is equals to f2 : "+ isEqual(f1,f2)); //prints false
}
}