Q6. What is wring with this code?
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class WhatIsWrong { public static void main(String args[]) { float sum = 0; while (sum != 1.0) { sum += 0.1; } System.out.print("The sum is: "+sum); } } |
A6. Infinite loop at due to sum != 1.0. Using floating point variables like float or double in loops to compare for equality can lead to infinite loops. Comparing for “>” or “<" can cause precision issues. A better approach is
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.math.BigDecimal; public static void main(String[] args) { BigDecimal sum = BigDecimal.ZERO; while (sum.compareTo(BigDecimal.ONE) != 0) { sum = sum.add(BigDecimal.valueOf(0.1)); } System.out.print("The sum is: "+sum); //The sum is: 1.0 } |
Q7. What is wring with this code?
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.math.BigDecimal; public class WhatIsWrong { public static void main(String args[]) { System.out.println(1.05 - 0.42); //1 System.out.println(new BigDecimal("1.05").subtract(new BigDecimal("0.42"))); //2 System.out.println(new BigDecimal(1.05).subtract(new BigDecimal(0.42))); //3 System.out.println(BigDecimal.valueOf(1.05).subtract(BigDecimal.valueOf(0.42))); //4 System.out.println(105 - 42); //5 } } |
A7. In the above code, //2, //4, and //5 are correct and //1 and //3 are incorrect usage leading to rounding issues. //3 is wrong due to incorrect usage of BigDecimal. So, either use BigDecimal properly as shown in //2 and //4, or use the smallest monetary value like cents with data type long as shown in //5. The cents approach performs better, and handy in applications that have heavy monetary calculations. Never use a floating point variable like float or double for monetary claculations. As they cause rounding issues as shown in //1.
Q8. What is wring with this code?
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; public class WhatIsWrong { public static void main(String args[]) { Set<Person> people = new CopyOnWriteArraySet<>(); people.add(new Person("John", 35)); people.add(new Person("John", 32)); people.add(new Person("Simon", 30)); people.add(new Person("Shawn", 30)); System.out.println(people); Person search = new Person("John", 35); if (people.contains(search)) { System.out.println("found: " + search); } else { System.out.println("not found: " + search); } } //inner class static class Person { private String name; private Integer age; public Person(String name, Integer age) { this.name = name; this.age = age; } // getters and setters @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } } } |
A8. The output will be
|
1 2 |
[Person [name=John, age=35], Person [name=John, age=32], Person [name=Simon, age=30], Person [name=Shawn, age=30]] not found: Person [name=John, age=35] |
Q. Why is the person not found even though that person is there in the collection?
A. Every Java object implicitly extends the Object class, which has the default implementation of equals(…) and hashCode( ) methods. The equals(…) method is implicitly invoked by the Set class’s contains(…) method. In this case the default implementation in the Object class performs a shallow comparison of the references, and not the actual values like name and age. The equals and hashCode methods in Java are meant for overridden for POJOs. So, this can be fixed as shown below by overriding the equals and hashCode methods in Person.
After adding “hashCode()” and “equals(Object obj)” methods
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// inner class static class Person { private String name; private Integer age; public Person(String name, Integer age) { this.name = name; this.age = age; } // getters and setters @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((age == null) ? 0 : age.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age == null) { if (other.age != null) return false; } else if (!age.equals(other.age)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } |
Output:
|
1 2 3 |
[Person [name=John, age=35], Person [name=John, age=32], Person [name=Simon, age=30], Person [name=Shawn, age=30]] found: Person [name=John, age=35] |