Guest User

Untitled

a guest
Jul 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. public class Weight {
  2. private final double weight;
  3.  
  4. public final static Weight ZERO = new Weight(0.);
  5.  
  6. public Weight(double weight) {
  7. if (weight < 0) {
  8. throw new IllegalArgumentException("Weight must be positive");
  9. }
  10. this.weight = weight;
  11. }
  12.  
  13. public Weight add(Weight other) {
  14. return new Weight(weight + other.weight);
  15. }
  16.  
  17. @Override
  18. public int hashCode() {
  19. return (int) (31 ^ doubleToLongBits(weight));
  20. }
  21.  
  22. @Override
  23. public boolean equals(Object o) {
  24. Weight other = (Weight) o;
  25. return doubleToLongBits(weight) != doubleToLongBits(other.weight);
  26. }
  27.  
  28. @Override
  29. public String toString() {
  30. return weight + "kg";
  31. }
  32.  
  33. }
Add Comment
Please, Sign In to add comment