Reputation: 115
I have two arrays. In one, I have stored objects. In the second, there are just values. I need to compare an attribute of the stored objects with the element of the second array. In the code I have posted below, I need to compare the 5th value of the objects with the elements of the second array. And come up with a method if the value is greater. Any suggestions?
ParkedCar a1 = new ParkedCar ("Toyota", "Camry", "Blue", 672, 85);
ParkedCar a2 = new ParkedCar ("Mitsubishi", "Pajero", "White", 988, 40);
ParkedCar a3 = new ParkedCar ("Ferrari", "GTC4", "Yellow", 1, 220);
ParkedCar a4 = new ParkedCar ("Perodua", "Myvi", "White", 3714, 260);
ParkedCar a5 = new ParkedCar ("Toyota", "Prius", "Black", 1472, 367);
ParkedCar parkingLot[] = {a1, a2, a3, a4, a5};
ParkingMeter aa = new ParkingMeter ();
aa.setMinutesPurchased(120);
ParkingMeter bb = new ParkingMeter ();
bb.setMinutesPurchased(60);
ParkingMeter cc = new ParkingMeter ();
cc.setMinutesPurchased(120);
ParkingMeter dd = new ParkingMeter ();
dd.setMinutesPurchased(150);
ParkingMeter ee = new ParkingMeter ();
ee.setMinutesPurchased(60);
ParkingMeter meters[] = {aa, bb, cc, dd, ee};
Upvotes: 0
Views: 171
Reputation: 691
Actually to acceess the elements' parameter in the second array,you have to
use getters to get the value of them and compare with the first array's fifth elements value.
In your ParkingMeter
put a getter method like this:
public double getMinutesPurchased(){
return this.MinutesPurchased;
}
Upvotes: 0
Reputation: 168
So you want to //Do Something
if the ParkedCar's 5th value is higher than the minutes purchased.
You can write a function to do this. Including checks for if both arrays are same size. Your function is simply
for(int i = 0, i < parkingLot.getLength(), i++) {
if(parkingLot[i].getFifthValue() > meters[i].getMinutesPurchased()))
//DoSomething
}
However, ParkedMeter is quite a simple class. Why not add that as an attribute to ParkedCar. In that case, this meter value check is simply a oneliner boolean function in your ParkedCar Class return fifthValue > minutesPurchased;
Upvotes: 0
Reputation: 97
Your question is a bit vague, so I am not sure if this is exactly what you are going for; but here's my take on it.
ParkedCar is just an object right? So each of those values you pass into it when creating {a1, a2... a5} are just attributes of a ParkedCar-just like ParkingMeter. With those attributes of each object, you can make "getters" for comparing the two.
E.G.
In your ParkedCar class:
public class ParkedCar{
private double attributeFive;
ParkedCar(){}
ParkedCar(String Make, String Model, String Color, double valueFour, double valueFive){
//logic for setting values
attributeFive = valueFive;
}
public double getValueFive(){
return attributeFive;
}
}
Now there are things missing in this code such as setters, but this should give you a picture. You can do the same thing in your ParkingMeter class.
All you need to do now is to compare the two values (say in the same method you creating the objects).
if(a1.getValueFive < aa.getMinutesPurchesed){
//do something
}
It seems like you had most of the logic already there, but was just missing a simple comparison. You might try and watch some YouTube tutorials to get the basic principles down for Java-I know that's what helped me start.
Upvotes: 0
Reputation: 1038
If I understand, you are assuming that parkingLot
and meters
have the same length, and that meters[i].minutesPurchased
contains a value that refers to parkingLot[i]
.
I assume you have some getter method in ParkedCar
and ParkingMeter
classes that get what you need.
If so, the only viable solution is some kind of for-loop:
for (int i = 0; i < parkingLot.length; ++i)
if (parkingLot[i].fifthField > meters[i].minutesPurchased)
callSomeMethod();
Personally, I think it's a bad idea to keep the link between the two classes in two different arrays. Probably you could add some method ParkedCar.getParkingMeter()
to have the correct answer without the need of a for-loop.
Upvotes: 2