Write a method hasOdd that accepts a set of integers as a parameter and returns true if the set contains at least one odd integer and false otherwise. If passed the empty set, your method should return false.
package collections;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class HasOdd {
// this method takes the parameter of set
public static boolean hasOdd(Set<Integer> set) {
// checks for each value in the set, if it perfectly divides, it returns false
// otherwise true
for(int value : set)
if(value%2==1)
return true;
return false;
}
public static void main(String[] args) {
Set<Integer> set = new HashSet<Integer>();
Collections.addAll(set,4,6,4,5,4,2);
System.out.println("does the given set has atleast one odd number: " +hasOdd(set));
}
}
Output:
does the given set has atleast one odd number: true