Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. interface MyFunc<T> { boolean func(T v1, T v2); }
  2.  
  3. class HighTemp {
  4. private int hTemp;
  5.  
  6. HighTemp(int ht) { hTemp = ht; }
  7.  
  8. boolean lessThanTemp(HighTemp ht2) {
  9. return hTemp < ht2.hTemp;
  10. }
  11. }
  12.  
  13. class InstanceMethWithObjectRefDemo {
  14. static <T> int counter(T[] vals, MyFunc<T> f, T v) {
  15. int count = 0;
  16.  
  17. for(int i=0; i < vals.length; i++)
  18.  
  19. //*** THIS LINE
  20. if(f.func(vals[i],v)) count++;
  21.  
  22. return count;
  23. }
  24.  
  25. public static void main(String args[])
  26. {
  27. int count;
  28.  
  29. HighTemp[] weekDayHighs2 = { new HighTemp(32), new HighTemp(12),
  30. new HighTemp(24), new HighTemp(19),
  31. new HighTemp(18), new HighTemp(12),
  32. new HighTemp(-1), new HighTemp(13) };
  33.  
  34.  
  35. count = counter(weekDayHighs2, HighTemp::lessThanTemp,
  36. new HighTemp(19));
  37. System.out.println(count + " days had a high of less than 19");
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement