Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Weight {
- private final double weight;
- public final static Weight ZERO = new Weight(0.);
- public Weight(double weight) {
- if (weight < 0) {
- throw new IllegalArgumentException("Weight must be positive");
- }
- this.weight = weight;
- }
- public Weight add(Weight other) {
- return new Weight(weight + other.weight);
- }
- @Override
- public int hashCode() {
- return (int) (31 ^ doubleToLongBits(weight));
- }
- @Override
- public boolean equals(Object o) {
- Weight other = (Weight) o;
- return doubleToLongBits(weight) != doubleToLongBits(other.weight);
- }
- @Override
- public String toString() {
- return weight + "kg";
- }
- }
Add Comment
Please, Sign In to add comment