1

Can anyone help me. i want to compare if two cars are faster by a method isFaster(Car otherCar). Can someone help me understand how the Car object is compared with the otherCar object which is an argument on the isFaster method. How can I go about creating the body of the method.

public class Car {
  private String make ="";
  private int year = 0;
  private int maxSpeed = 0;

  public Car(String make,int year,int maxSpeed){
    this.make = make;
    this.maxSpeed = maxSpeed;
    this.year = year;
  }

  public void setSpeed(int maxSpeed){
    this.maxSpeed = maxSpeed;
  }

  public void setMake(String make){
    this.make = make;
  }

  public void setYear(int year){
    this.year = year;
  }

  public int getMaxSpeed(){
    return maxSpeed;
  }

  public int getYear(){
    return year;
  }

  public String getMake(){
    return make;
  }

  public String toString(double param){
    String temp = String.valueOf(param);
    return temp;
  }

  public String toString(int param){
    String temp = String.valueOf(param);
    return temp;
  }

  public String toString(char param){
    String temp = String.valueOf(param);
    return temp;
  }
}
Morten Kristensen
  • 7,412
  • 4
  • 32
  • 52
Lux
  • 23
  • 5

4 Answers4

3

In the Car object, there must be some attributes with it, say

class Car{

    int gear;
    double speed;
}

When you need to compare, you need to decide, on the deciding attribute to compare.

If its speed then

// isFaster will only return true if the calling car object is faster 
// than the otherCar

// for your class this should work

isFaster(Car otherCar){       
    return this.getMaxSpeed() - otherCar.getMaxSpeed()  ;
}

// If you use double for speed, and you need precision
// you can set some tolerance value for comparisons.

isFaster(Car otherCar){       
    return (this.getMaxSpeed() - otherCar.getMaxSpeed() ) < TOLERANCE ;
}

Hope this helps.

JNL
  • 4,683
  • 18
  • 29
  • 5
    May I suggest: `return this.speed > otherCar.speed;` – pamphlet Sep 11 '13 at 16:46
  • @pamphlet it is not good to compare `double`s like that: http://stackoverflow.com/q/434657/1065197 http://stackoverflow.com/q/11390853/1065197 – Luiggi Mendoza Sep 11 '13 at 16:48
  • @LuiggiMendoza I agree, but I was just advising against the if-true-return-true-else-return-false anti-pattern. – pamphlet Sep 11 '13 at 16:50
  • @pamphlet Edited it for taking tolerance into consideration. Also I had that if else true false format to help OP understand it better. – JNL Sep 11 '13 at 16:51
  • Assuming that the speed is directly input in the constructor rather than calculated at some point a double comparison aught to be safe even on the "fine" boundary, surely? – Richard Tingle Sep 11 '13 at 16:51
3

Assuming the Car class has a method such as double getMaxSpeed (), you could implement the isFaster (Car otherCar) as follows:

boolean isFaster (Car otherCar) {
    return this.getMaxSpeed() > otherCar.getMaxSpeed ();
}
Laf
  • 7,965
  • 4
  • 37
  • 52
0

you have to override .equals() method based on speed then only you can compare your objects meaningfully. otherwise by default .equals() uses == to compare objects in this case objects can be equal only if they are same

ankit
  • 4,919
  • 7
  • 38
  • 63
0

Create a method like this

public boolean isFaster(Car anotherCar){
   return this.maxSpeed > anotherCar.maxSpeed;
}

In a client code example:

public static void main(String args []){
  Car car1 = new Car();
  car1.setSpeed(200);// should be setMaxSpeed();
  Car car2 = new Car();
  car2.setSpeed(250);

  System.out.println(car1.isFaster(car2));// prints false

}
nachokk
  • 14,363
  • 4
  • 24
  • 53