Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. public class Fraction implements Comparable<Fraction>
  2. {
  3. private int num = 0, denom = 0;
  4.  
  5. public Fraction(int num, int denom)
  6. {
  7. this.num = num;
  8. this.denom = denom;
  9. }
  10.  
  11. public int compareTo(Fraction other)
  12. {
  13. double x = ((other.denom * num) - (other.num * denom));
  14. return (int)Math.signum(x);
  15.  
  16. }
  17.  
  18. public boolean equals(Object other)
  19. {
  20. if(other instanceof Fraction && compareTo((Fraction)other) == 0 )
  21. {
  22. return true;
  23. }
  24. else
  25. {
  26. return false;
  27. }
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement