3

a friend of mine came up with an idea for a racing game, and i'm trying to create that in java. now, i have made 3 classes for cars, 1 for player cars, 1 for computer(ai)cars and a main one that holds some variables like location (x,y on the screen) and name to name a few. the first 2 inherit from the last one. i hoped this would allow me to create one array with both the players and the computer players in it. this however doesnt work, and now my question:

is there any way it is possible to have an array with different kinds of objects in them, and if this is possible, how can i do it or are there any tutorials on it?

i have looked at interfaces, but i dont think that would do the trick, but please correct me if im wrong.

this was the idea that i had:

MainCar[] carsArray = new MainCar[totalPlayers];
for(int i = 0; i < totalHumanPlayers; i++)
{
  carsArray[i] = new PlayerCar();
}
for(int i = 0; i < totalComputerPlayers; i++)
{
  carsArray[i] = new ComputerCar();
}

the idea with it is that i can loop through all the players(human and computer) to draw them at their locations and to decide whos turn it is next turn

many thanks, and please forgive my english, i don't know if it's correct or not, not my first language :)

3
  • 2
    Your code should work as long as PlayerCar and ComputerCar both extend MainCar. What exact problems do you have? Commented Dec 24, 2014 at 13:19
  • it gave an error for incorrect types, i dont recal the excact error, but the awnsers should fix it i think, but thanks :) Commented Dec 24, 2014 at 13:29
  • why was my comment removed ?? Commented Dec 24, 2014 at 14:22

4 Answers 4

2

Ok, with the second loop you are overwriting the values previously set, because you start at the same offset i=0.

I'd suggest something like:

for(int i = totalHumanPlayers; i < (totalHumanPlayers + totalComputerPlayers); i++)

You can hold both types of car in your array, just as you did (declaring the array to be of type MainCar), remember that MainCar can be a ComputerCar or a PlayerCar, you will need to cast to make use one of them (if you need to access to specific members of PlayerCar or ComputerCar) like this:

PlayerCar player = (PlayerCar)carsArray[x];

But in your case, if you only need coords at MainCar you just can access them directly (if they are members of MainCar of course) like:

carsArray[i].location.x

or

carsArrays[i].x

Just remember to initialize correctly the array. (Don't overlap your data)

EDIT:

And to determine if a car is of one type or another use the instanceof operator, here's an example:

if (carVar instanceof PlayerCar) { PlayerCar player = (PlayerCar)carVar; }

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your fast reply, and yes, i just saw the second loop as well, that wasn't the problem, but the cast, can i somehow know whether it was a PlayerCar or a ComputerCar?
yes, use instanceof operator, like: if (carVar instanceof PlayerCar)
2
public class PlayerCar : MainCar {}
public class ComputerCar : MainCar {}



MainCar[] carsArray = new MainCar[totalPlayers];
for (int i = 0; i < totalCarsCount; i++) {
   if (carsArray[i] instanceof PlayerCar) {
       System.out.println("Player car");
   } else {
       System.out.println("Computer Car");
   }
}

Comments

1

It might not be a good idea to have the same array for player car and computer car, unless they both inherit same class. For example its alright if:

interface MainCar{
    public void horn();
}

class PlayerCar implements Car{
    public void run(){ System.out.println("Broom Broom");}
}

class ComputerCar implements Car{
    public void run(){ System.out.println("tak tak");}
}

MainCar[] cars = new MainCar[totalPlayers];
cars[0] = new PlayerCar();
cars[1] = new ComputerCar();

This happens due to co-variance nature of Arrays in Java.

Had PlayerCar and ComputerCar not inherited MainCar, then the above would not had been possible (definitely not recommended). But due to co-variance you could:

Object[] cars = new Object[totalPlayers];    
cars[0] = new PlayerCar();
cars[1] = new ComputerCar();

The reason it is not preferred is, if you access cars[0] above. You do not know what type will it be as:

Object a = cars[0];

a can be a String, Animal, List or anything for that matter. And ig you have to use it as a car, you need to type cast it as which is very very very unsafe:

if(a instanceof PlayerCar){
 //then do something
}else if(a instanceof ComputerCar){
 //then do something
}

PS: Change your username from javaNoobsForever, you cant be that bad :). Well we all are noobs :)

1 Comment

haha, it seemed apropriate, since that would be the main source for my questions :P, and thanks for your quick awnser
-1

Make a car interface that all of the cars implement, and then make the array of the interface type.

Note. You will only be able to call methods from the interface, unless you cast them back into their appropriate classes

1 Comment

thanks for the quick awnser, i can figure it out now i think :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.