tag:blogger.com,1999:blog-9193160645817040317.post9216684981621285087..comments2015-12-22T12:31:53.234+05:30Comments on Twisters - The New Age Java Quiz: Puzzle 47 - IsEqual Unequal?Saifuddin Merchanthttp://www.blogger.com/profile/09008041357659535766noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-9193160645817040317.post-8450496362308396992009-08-15T01:06:45.847+05:302009-08-15T01:06:45.847+05:30Not sure what the hints or criteria are but my sol...Not sure what the hints or criteria are but my solution is this:<br /><br />public class Mystery {<br />public static boolean isEqual(Float f1, int f2){<br /> return f1.equals(f2);<br /> }<br /> <br /> public static void main(String[] args) {<br /> Float f1= 1f;<br /> int f2 = 1;<br /> System.out.println("f1 is equals to f2 : "+ (f1==f2)); //prints true<br /> System.out.println("f1 is equals to f2 : "+ isEqual(f1,f2)); //prints false<br /> }<br />}Arshia Khanhttps://www.blogger.com/profile/04023130805491879540noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-17684652467022085232009-08-14T19:19:47.199+05:302009-08-14T19:19:47.199+05:30Since u haven't said that I can't add the ...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.<br /><br />package com.twister;<br />public class Mystery {<br /><br /> public static boolean isEqual(Mystery f1, Mystery f2){<br /> return f1.equals(f2);<br /> }<br /> public static boolean isEqual(Object f1, Object f2){<br /> return false;<br /> }<br /> <br /> public static void main(String[] args) {<br /> Object f1= new Object();<br /> Object f2 = new Object();<br /> System.out.println("f1 is equals to f2 : "+ (f1==f2)); //prints true<br /> System.out.println("f1 is equals to f2 : "+ isEqual(f1,f2)); //prints false<br /> }<br />}vishwanathhttps://www.blogger.com/profile/03753309726590344870noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-69866923312381952402009-08-14T14:03:39.453+05:302009-08-14T14:03:39.453+05:30public class Mystery {
public static boolean i...public class Mystery {<br /> public static boolean isEqual(Double f1, Double f2){<br /> return f1.equals(f2);<br /> }<br /><br /> public static void main(String[] args) {<br /> double f1 = -0.0;<br /> double f2 = 0.0;<br /> System.out.println("f1 is equals to f2 : " + (f1 == f2)); // prints true<br /> System.out.println("f1 is equals to f2 : " + isEqual(f1, f2)); // prints false<br /> }<br /><br />}Unknownhttps://www.blogger.com/profile/17981613452226918992noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-54956325449174822052009-08-13T21:44:17.380+05:302009-08-13T21:44:17.380+05:30DataType1 and 2: Number
DataType3: int
DataType4:...DataType1 and 2: Number<br /><br />DataType3: int<br />DataType4: double<br /><br />value1 and value2: 1<br /><br />So:<br /><br /> public static boolean isEqual(Number f1, Number f2){<br /> return f1.equals(f2);<br /> }<br /><br /> public static void main(String[] args) {<br /> int f1= 1;<br /> double f2 = f1;<br /> System.out.println("f1 is equals to f2 : "+ (f1==f2)); //prints true<br /> System.out.println("f1 is equals to f2 : "+ isEqual(f1,f2)); //prints false<br /> }<br /><br /><br />Prints true, false.<br /><br />Problem here is autoboxing and Integers are never equal to non-Integers, even if their value is the '=='-same.Sebastianhttp://www.yworks.comnoreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-69763979185499917802009-08-13T18:08:41.105+05:302009-08-13T18:08:41.105+05:30public class Mystery {
public static boolean ...public class Mystery {<br /><br /> public static boolean isEqual(Object f1, Object f2){<br /> return f1.equals(f2);<br /> }<br /> <br /> public static void main(String[] args) {<br /> Object f1= new Object(){<br /><br /> @Override<br /> public boolean equals(Object obj) {<br /> return false;<br /> }<br /> <br /> };<br /> Object f2 = f1;<br /> System.out.println("f1 is equals to f2 : "+ (f1==f2)); //prints true<br /> System.out.println("f1 is equals to f2 : "+ isEqual(f1,f2)); //prints false<br /> }<br /> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-23133778314242153832009-08-13T08:31:25.104+05:302009-08-13T08:31:25.104+05:30Easy one
public static boolean isEqual(Float f1, ...Easy one<br /><br />public static boolean isEqual(Float f1, Double f2){<br />return f1.equals(f2);<br />}<br /><br />public static void main(String[] args) {<br />float f1 = 1f/3;<br />double f2 = 1f/3;<br />System.out.println("f1 is equals to f2 : " + (f1 == f2)); //prints true<br />System.out.println("f1 is equals to f2 : " + isEqual(f1, f2)); //prints false<br />}TheMalkolmhttps://www.blogger.com/profile/11358745568436186926noreply@blogger.com