Wednesday, August 12, 2009

Puzzle 47 - IsEqual Unequal?

Language – Java | Type – Concept | Last date 16-Aug-2009 12:00 p.m. IST | Points 3

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.

6 comments:

  1. Easy one

    public 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
    }

    ReplyDelete
  2. public class Mystery {

    public 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
    }
    }

    ReplyDelete
  3. DataType1 and 2: Number

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

    ReplyDelete
  4. public class Mystery {
    public 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
    }

    }

    ReplyDelete
  5. 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.

    package 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
    }
    }

    ReplyDelete
  6. Not sure what the hints or criteria are but my solution is this:

    public 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
    }
    }

    ReplyDelete

Solution for this question?