Results 1 to 10 of 10

Thread: Object Arrays

  1. #1
    rsvr is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Object Arrays

    Hi All,

    I have an issue.

    I have an array that holds car objects (make, year, colour....etc)

    I have overridden equals to test if a car colour is equal to another car colour.
    Now I want to test if they are the same model. How can I use equals for this when I have already used it for the colour. Both variables colour and model are Strings.

    ie

    Car1, Ford, Blue, 2001
    Car2, BMW, Red, 2002
    Car3, Ford, Red, 2004.

    public boolean equals(Object obj)
    {
    Car a = (Car) obj;
    return colour.equals(a.colour);
    }

    Thanks in advance.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The equals method in your Car class should test any car instance given to see if it is equal to this car. To do this you would check all Car fields for equality. If you want to test for the same color make a method to do just that, eg,
    Java Code:
    public boolean isSameColor(Car car) {
        return this.colour.equals(car.colour);
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you are talking about the equals method for the Car class, then this should not be used to test just color or model but should only be set up so that it tests that one car is the true equivalent of another car (often this is true when all properties of one object are the same as another).

    Your use of equals to test equality of color needs to be done another way. If you have a CarColor class or enum, you certainly can give this an equals method (in the case of an enum, an equals method isn't necessary actually).

  4. #4
    gcampton Guest

    Default

    Do you happen to have a Vehicle class and truck class? sounds like the assignment I did a couple months ago for broken down car yard. if this IS the case typically if you doing this for a sort you'll want implement comparable in your vehicle class and then have a compareTo(Vehicle vehicleToCompare) in there as well, I simply set a public static int flag=0; as my class field and then when I called sort would set the vehicle.flag to whatever flag I wanted to sort by eg: Class/Registration, Make/Model... Color etc.

    simply need to implement a switch statement in compareTo(Vehicle v2c)
    Java Code:
    switch (flag)
    {
        case 1:
            return this.getClass().getName().compareTo(v2c.getClass().getName());
        case 2:
            return this.getColor().compareTo(v2c.getColor());
       etc etc....
    }
    there's a few other ways to do it, eg: using enums

    you sort method simply calls the compareTo on each element.
    Last edited by gcampton; 12-05-2009 at 08:55 AM.

  5. #5
    rsvr is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks for the ideas guys.

    I have a number of things I should probably explain.
    Each object of Car is stored in an Array.......[Car1][Car2]......

    I don't really want to see if a car is an exact match to another. I just want to test different elements of the existence to see if they match.

    What I am after but is not possible is something like

    public boolean equals(Object obj)
    {
    Car a = (Car) obj;
    return colour.equals(a.colour);
    }

    public boolean equals(Object obj)
    {
    Car a = (Car) obj;
    return model.equals(a.model);
    }

    I want to be able to call equals like this.

    Cars[] car = new Car[10];
    car[2] = new Car("mazda","red",2004,34043);

    if(cars[0].equals(cars[4]) // this will only test colour
    {
    ...
    }

    I would also like to use equals to test for model regardless of the colour.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why not just do something like:

    Java Code:
    if(cars[0].getColor().equals(cars[4].getColor()) {
      //...
    }

  7. #7
    rsvr is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    This is where I am a bit confused. I have pretty much what you have suggested.

    My equals method.
    public boolean equals(Object obj)
    {
    Car c = (Car) obj;
    return colour.equals(c.colour);
    }

    I use this to test the colour
    if(car[0].equals(car[1]))
    {
    System.out.println("same colour");
    }


    I use this way to test the model. It works but I don't see why.
    Why should this work when with the way the equals is overridden?
    It's return is for colour, not the model.

    if(car[0].getModel().equals(car[2].getModel())
    {
    System.out.println("same model");
    }

    Maybe my understanding of equals need a lot of work.

    Cheers,

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rsvr View Post
    This is where I am a bit confused. I have pretty much what you have suggested.
    No you don't.


    My equals method.
    public boolean equals(Object obj)
    {
    Car c = (Car) obj;
    return colour.equals(c.colour);
    }
    Again, do not use equals to test equality of just one field. I'm not sure how we can say this in any stronger way so you'll agree with us.

    I use this to test the colour
    if(car[0].equals(car[1]))
    {
    System.out.println("same colour");
    }
    Again, don't do this. Period.

    I use this way to test the model. It works but I don't see why.
    Why should this work when with the way the equals is overridden?
    It's return is for colour, not the model.

    if(car[0].getModel().equals(car[2].getModel())
    {
    System.out.println("same model");
    }

    Maybe my understanding of equals need a lot of work.
    Indeed it does.

    We are all suggesting that you use equals only to test for functional equality of one object to another. That's it.

    I think that we need to know what problem you are trying to solve in a general sense before we can tell you how to do what you want in a better fashion.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For instance say I have a Car class with 3 fields like so:
    Java Code:
    public class Car {
      enum CarColor {
        RED, BLUE, GREEN, WHITE, BLACK
      }
      enum CarMake {
        FORD, HONDA, TOYOTA, GM, MAZDA, VW
      }
      private CarColor carColor;
      private CarMake carMake;
      private int year;
      
      public Car(CarColor carColor, CarMake carMake, int year) {
        this.carColor = carColor;
        this.carMake = carMake;
        this.year = year;
      }
    
      public CarColor getCarColor() {
        return carColor;
      }
    
      public CarMake getCarMake() {
        return carMake;
      }
    
      public int getYear() {
        return year;
      }
      
      @Override
      public boolean equals(Object o) {
        if (o == null) {
          return false;
        } else if (!(o instanceof Car)) {
          return false;
        } else {
          Car c2 = (Car) o;
          // == works as I'm using enums
          return carColor == c2.getCarColor()
              && carMake == c2.getCarMake()
              && year == c2.getYear();
        }
      }
      
      @Override
      public String toString() {
        return "Make: " + carMake + ", Color: " + carColor + ", Year: " + year; 
      }
    }
    I'm going to make one equals method that compares all relevant fields. I probably should also override hashcode as well.

    Then I can simply test like so:
    Java Code:
    public class CarTest {
      public static void main(String[] args) {
        Car[] cars = {
          new Car(Car.CarColor.BLUE, Car.CarMake.FORD, 2003),
          new Car(Car.CarColor.RED, Car.CarMake.HONDA, 2006),
          new Car(Car.CarColor.GREEN, Car.CarMake.MAZDA, 2007),
          new Car(Car.CarColor.WHITE, Car.CarMake.TOYOTA, 2004),
          new Car(Car.CarColor.BLUE, Car.CarMake.VW, 2000),
          new Car(Car.CarColor.BLACK, Car.CarMake.GM, 1999),
          new Car(Car.CarColor.BLUE, Car.CarMake.HONDA, 2008), 
          new Car(Car.CarColor.WHITE, Car.CarMake.TOYOTA, 2004)
        };
        
        System.out.println("Show cars with the same color:");
        for (int i = 0; i < cars.length - 1; i++) {
          for (int j = i + 1; j < cars.length; j++) {
            
            // == will also work here since my code uses enums.  i.e.,
            // if (cars[i].getCarColor() == cars[j].getCarColor()) { ...
            
            if (cars[i].getCarColor().equals(cars[j].getCarColor())) {
              System.out.println(cars[i]);
              System.out.println(cars[j]);
              System.out.println();
            }
          }
        }
        
        System.out.println();
        System.out.println("Show Matching Cars");
        for (int i = 0; i < cars.length - 1; i++) {
          for (int j = i + 1; j < cars.length; j++) {
            if (cars[i].equals(cars[j])) {
              System.out.println(cars[i]);
              System.out.println(cars[j]);
              System.out.println();
            }
          }
        }
      }
    }

  10. #10
    rsvr is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    All I can say is thanks for you large effort. I will sit back and digest what you have given me.
    Again, Thanks for you feedback.

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  2. Replies: 1
    Last Post: 03-04-2009, 07:14 PM
  3. Replies: 2
    Last Post: 12-10-2008, 05:40 PM
  4. Replies: 1
    Last Post: 07-22-2008, 05:27 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •