Results 1 to 10 of 10
Thread: Object Arrays
- 12-05-2009, 12:32 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
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.
- 12-05-2009, 12:50 AM #2
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); }
-
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).
- 12-05-2009, 07:46 AM #4gcampton Guest
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)
there's a few other ways to do it, eg: using enumsJava Code:switch (flag) { case 1: return this.getClass().getName().compareTo(v2c.getClass().getName()); case 2: return this.getColor().compareTo(v2c.getColor()); etc etc.... }
you sort method simply calls the compareTo on each element.Last edited by gcampton; 12-05-2009 at 07:55 AM.
- 12-05-2009, 03:24 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
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.
-
Why not just do something like:
Java Code:if(cars[0].getColor().equals(cars[4].getColor()) { //... }
- 12-05-2009, 07:14 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
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,
-
No you don't.
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.My equals method.
public boolean equals(Object obj)
{
Car c = (Car) obj;
return colour.equals(c.colour);
}
Again, don't do this. Period.I use this to test the colour
if(car[0].equals(car[1]))
{
System.out.println("same colour");
}
Indeed it does.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.
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.
-
For instance say I have a Car class with 3 fields like so:
I'm going to make one equals method that compares all relevant fields. I probably should also override hashcode as well.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; } }
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(); } } } } }
- 12-05-2009, 11:27 PM #10
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Operator < cannot be applied to java.lang.Object, Object
By Albert in forum Advanced JavaReplies: 2Last Post: 11-26-2010, 02:12 AM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
Stuck with Java Object Orientated Programming - Arrays
By Phalanx in forum New To JavaReplies: 2Last Post: 12-10-2008, 04:40 PM -
Parsing a superclass object to subclass object dynamicly
By Andrefs in forum Advanced JavaReplies: 1Last Post: 07-22-2008, 04:27 PM -
Creating object of Type Object class
By venkatv in forum New To JavaReplies: 3Last Post: 07-17-2007, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks