diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 4375608..ac23c7e 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -1073,6 +1073,49 @@ public int compareTo(Fraction other) { // compare this fraction with another fra + + +

+ Rearrange the blocks to create a Bicycle class that implements Comparable<Bicycle>. The class should feature a private double weight field and a compareTo method that compares bicycles based on their weight. +

+
+ + + + public class Bicycle implements Comparable<Bicycle> { + private double weight; + + + public class Bicycle extends Comparable<Bicycle> { + public double weight; + + + + + public Bicycle(double weight) { + this.weight = weight; + } + + + + + public int compareTo(Bicycle other) { + return Double.compare(this.weight, other.weight); + } + + + public boolean compareTo(Bicycle other) { + return this.weight - other.weight; + } + + + + + } + + +
+