Don't really understand how to implement this in the asked way. here is the question
Create a class Bike that implements the Comparable interface. You should have the following attributes in the Bike class, color, price, manufacturer, model and rating. You should perform comparisons based on their prices. Compare them using a tolerance value of 0.0001. Write a main driver to test the program by create bikes where the information is gathered at the console.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Program3 {
public static void main(String[] args) {
Bike b1 = new Bike(300,"red","schwin","mountain crusher",8.6);
Bike b2 = new Bike(500,"black","cassio","road dominator",12.5);
Bike b3 = new Bike(200,"blue","rolex","blue diamond",4.3);
Bike b4 = new Bike(1524500,"silver","military","spy bike",143.2);
List<Bike> bike = new ArrayList<Bike>();
bike.add(b1);
bike.add(b2);
bike.add(b3);
bike.add(b4);
System.out.println(bike);
Collections.sort(bike);
System.out.println(bike);
public class Bike {
private int price;
private String color,brand,model;
private double rating;
public Bike(int price, String color, String brand, String model, double rating) {
this.price=price;
this.color=color;
this.brand=brand;
this.model=model;
this.rating=rating;
}
}
The
Comparableinterface contains a single method:compareTo(). This method returns anintthat describes how one object compares to another.It is typically used for sorting a collection of objects that this method will be called on, as the value of the returned
intwill tell the caller in which order the two objects belong.If the
compareTomethod returns a negative, the the object that called the method comes before the object that was used as the argument. IfcompareToreturn a positive, the objected that called the method comes after the object. IfcompareToreturns 0, then the objects are considered equal and order does not matter.Your homework is asking you to implement
Comparableand override thecompareTomethod in order to sort objects of theBikeclass by their price. GivenbikeAwith price of two dollars, andbikeBwith price of three dollars, andbikeCwith price of three dollars: