Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: Java  |  size: 0.91 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class CompareNumber {
  2.         private float tolerance = 25f;
  3.        
  4.         public class CompareNumber {
  5.         private float tolerance = 25f;
  6.        
  7.         public boolean isInRange(float firstNum,  float secNumber) {
  8.                         BigDecimal max = new BigDecimal(firstNum + this.tolerance);
  9.                         BigDecimal min = new BigDecimal(firstNum - this.tolerance);
  10.                         if(min.compareTo(new BigDecimal(0)) < 0) {
  11.                                 min = new BigDecimal(360).add(min);
  12.                         }
  13.                 BigDecimal number = new BigDecimal(secNumber);
  14.                         return max.compareTo(number) >= 0
  15.                        && min.compareTo(number) <= 0;            
  16.         }
  17.        
  18.         public static void main(String[] args) {
  19.                 CompareNumber cn = new CompareNumber();
  20.                 System.out.println(cn.isInRange(0, 359)); // Should return true
  21.                 System.out.println(cn.isInRange(0, 21)); // Should return false
  22.                 System.out.println(cn.isInRange(0, 339); // Should retrun false
  23.                 System.out.println(cn.isInRange(0, 340); // Should retrun true
  24.                
  25.         }
  26. }