hi every one

suppose i have a Car class

public class Car{

[INDENT]int speed;
int id;
..
public void move(){
....
}[/INDENT]}

and a Race class

public class Race{
[INDENT]int array[][];//contains car ids in race or 0 for none [/INDENT] 
[INDENT]Car cars[];
....
public void startRace(){
...
}[/INDENT]
}

if a car wants to move in move() it should know whether there's another car infront of it or behind it.

how can i know in car class if there's another car infront of me or not?
how to implement that?

and thank you

Recommended Answers

All 8 Replies

You would have to store the order or location someplace. Perhaps you are doing so in the cars or array arrays. Index 0 is the front car, index 1 is the 2nd to front car, index 2 the 3rd to front car, etc.?

That's one way to do it. There are other ways. It all depends on how you are storing the information, and whether a car has a location, and whether you can assume that the array is ordered in some way.

thanks for ur reply.

but even if i define row and column numbers in Car class like this

public class Car
{
[INDENT]int id;
int speed;
int row;
int col;

public void move(){
}
[/INDENT]

...
}

how can i in move() method know if there are cars infront of this car when i have car number of row and column in the Array array in Race class where the race is going on multiline road and car can move forward or right or left if there is no cars in its way.

public class Race{
[INDENT]int array[number of lines ][number of positions in the line];//contains car ids in race or 0 for none

 Car cars[];
....
public void startRace(){
...
}[/INDENT]

}

Suppose you have three cars, with car numbers 1, 2, and 3. At a certain point in time, they are at the following (row, col) locations.

Car 1 (3, 4)
Car 2 (4, 4)
Car 3 (3, 3)

Let's say moving forward decreases the row by 1, moving right increases the column by 1, moving left decreases the column by 1.

So moving forward from the original position yields this:

Car 1 (2, 4) - legal (no cars in the way)
Car 2 (3, 4) - illegal (car 1 in the way)
Car 3 (2, 3) - legal (no cars in the way)

Moving left from the original position yields this:

Car 1 (3, 3) - illegal (car 3 in the way)
Car 2 (4, 3) - legal (no cars in the way)
Car 3 (3, 2) - legal (no cars in the way)

Moving right from the original position yields this:

Car 1 (3, 5) - legal (no cars in the way)
Car 2 (4, 5) - legal (no cars in the way)
Car 3 (3, 4) - illegal (car 1 in the way)

So at a time t, you know where everyone is. When a car is about to move, figure out where that car will be after the move and check the array to see if there is a car at that new location already. If there is, the car is blocked, so don't make a move. If there isn't, make the move.

do u suggest that:

public class Car {
	private int id;
	private int speed;
	private int lane;
	private int lpos;
	private Race race;

                public void move(){
                       .....
                       if (!race.isOccupied(lane-1,lpos))      
                       .....
                 }
}

and class Race

public class Race {
[INDENT]int array[][];
Car cars[];[/INDENT]
                .....
[INDENT]public boolean isOccupied(int row, int col){
   	if (race[row][col]!=0)
   		return true;
   	else
   		return false;
   }[/INDENT]

    .......
}

and thank you very much for your help

I don't mean to give you different advice than Vernon, but it seems odd that you'd want the Car class to track that information. The Race class should contain the method that tells you what Car(s) are in front of or behind a particular Car. The Car class probably shouldn't know anything about that.

I agree with you and that's what i'm trying to do
but how to do this in Car class?

in my previous reply i suggest to add Race object to Car class inorder to call Car's isOccupied(..) method that tells me what if the position infront of this car is occupied by another car or not.

if that's wrong, what is the right code to do that.

and really thank you two for your help :)

I don't mean to give you different advice than Vernon, but it seems odd that you'd want the Car class to track that information. The Race class should contain the method that tells you what Car(s) are in front of or behind a particular Car. The Car class probably shouldn't know anything about that.

I don't think we disagree here. From the OP's setup, I'm assuming that a car knows where it is, but has no idea where all the other cars are or that they exist, and doesn't care. I'm assuming that the car knows what race it's in. The car knows where it is and what direction it wants to move to, so it can figure out where it will be if the move is unblocked. The checking should be done in the Race class, which has an array of all the cars. Since the car knows what race it is in, it can call that Race object and have it do the checking. Sort of like a plane in a blizzard. It has no idea where all the other planes are and is essentially blind. It knows where it is located due to GPS or whatever, knows where it would like to go, and puts in a request to the Air Traffic Control tower, which knows the location of all the planes. The tower checks the plane[] array and figures out whether the request can be accomodated. Replace Plane with Car and AirTrafficControlTower with Race and you have the situation here.

do u suggest that:

public class Car {
    private int id;
    private int speed;
    private int lane;
    private int lpos;
    private [COLOR="Green"]Race race[/COLOR];

                public void move(){
                       .....
                       if (![COLOR="Green"]race[/COLOR].isOccupied(lane-1,lpos))      
                       .....
                 }
}

and class Race

public class Race {
    int array[][];
    Car cars[];
                .....
    public boolean isOccupied(int row, int col){
        if ([COLOR="Red"]race[/COLOR][row][col]!=0)
            return true;
        else
            return false;
    }

    .......
}

end quote.

I assume you have a Race object called race in the Race class in the ....... section of your code (see red in your quoted code). I doubt you want that. Having a Race object called race in the Car class makes sense (see green in your quoted code - sort of hard to see). It gives you a way to get from a Car object to the Race object.

Seems to me that there should be one Race object and many Car objects. The Car objects need to be passed the Race object so they have a way of calling methods from the Race class.

I have no idea what array represents.

:icon_smile:

thank you again, and sorry for making you confused about my Race class.
there was a typing error in it.
i wanted to write this:

public class Race {
int array[][];
Car cars[];
                
...
public boolean isOccupied(int row, int col){
if (array[row][col]!=0)
   return true;
else
   return false;
}
.......
}

so my array is ones zeros array where 1 means that there's a car in that line and position in the race.

i liked your idea about the plane and the blizzard, it really saves me here.

Thank you all for your help. and i'll continue working on VernonDozier theory that i can have race object in my Car class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.