Comparable interface homework

591 Views Asked by At

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;
}

}

2

There are 2 best solutions below

2
On

The Comparable interface contains a single method: compareTo(). This method returns an int that 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 int will tell the caller in which order the two objects belong.

If the compareTo method returns a negative, the the object that called the method comes before the object that was used as the argument. If compareTo return a positive, the objected that called the method comes after the object. If compareTo returns 0, then the objects are considered equal and order does not matter.

Your homework is asking you to implement Comparable and override the compareTo method in order to sort objects of the Bike class by their price. Given bikeA with price of two dollars, and bikeB with price of three dollars, and bikeC with price of three dollars:

bikeA.compareTo(bikeB) //should return negative
bikeB.compareTo(bikeA) //should return positive
bikeC.compareTo(bikeB) //should return 0
0
On

Well, first, the price should be a double, and the rating should be an int. It looks like you got that backwards.

But for implementing the compareTo, let's look at what was asked:

Create a class Bike that implements the Comparable interface.

That's pretty easy: just declare the class as public class Bike implements Comparable.

You should perform comparisons based on their prices.

So? If you are having trouble comprehending the compareTo method, here's something to help:

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

That's the official description of what this method should do.

Compare them using a tolerance value of 0.0001.

Here you might need some help. double values are imprecise, and must be compared with tolerance for non-exactitude. For example, the expression 0.1 == (0.2 - 0.1) actually evaluates to false because of this. So instead, you can try something like this:

(0.1 - (0.2 - 0.1)) < 0.0001

Here we just take the difference and see if it is less than a certain amount. That 0.0001 is the tolerance.