As I defined an Object array called "map" and reserved space for 20 elements, in the class "Coin" I have created constructor which will assign random coinValue to each coin and put it in the array map. Also I have a class player with counstructor and getters/setters set to Player 1 and Player 2. Class TrafficLight is a Thread but this is not of much importance for my question,thus I left it in the code to avoid confusion. Then I created four Coin objects and assigned them to some specified indexes in my "map" array. Now I will need to create Traffic light and Coin objects and put them in the Object[]map. Now,the code is shown below:
UPDATE: I still do not know how to access getCoinValue if both Coin and TrafficLight are in the array of the type Object.
FIX: I fixed this by explicitly casting the Coin object like this:
Coin coin= (Coin)map[position];
currentCoinValue += coin.getCoinValue();
The rest of the code...
public static boolean RaceEnd = false;
public static void main(String[] args) {
Object[]map = new Object[20];
Player player = new Player("Jack", "Max");
map[8]= new Coin(100);
map[1]= new Coin(100);
map[17]= new Coin(100);
map[19]= new Coin(100);
map[4]= new Coin(100);
map[13]= new Coin(100);
TrafficLight s = new TrafficLight(0,1);
map[7] = new TrafficLight(0,1);
map[18] = new TrafficLight(2,1);
int currentCoinNumber = 0;
for (int position= 0; position< map.length; position++) {
if (position==19) {
RaceEnd = true;
System.out.println(player.getPlayer1() + " finished the race!");
// s.interrupt();
break;
} else {
if (map[position] != null && map[position] instanceof Coin) {
Coin coin= (Coin)map[position];
currentCoinValue += coin.getCoinValue();
> THE CASTING IS NOT AN OPTION,STILL SHOWS UNDEFINED
System.out.println(player.getPlayer1() + " has collected " + mapa[pozicija] );
System.out.println("Jack currently has: " + currentCoinNumber);
/* } else {
if (map[position] != null && map[position] instanceof TrafficLight) {
//s.run();
System.out.println(player.getPlayer1() + " is at the " + map[position]);
} else {
if (map[position] == null ) {
s.interrupt();
wait(500);
position++;
}*/
}if (!RaceEnd){
System.out.println(player.getPlayer1() + " did not finish the race!");
}
}
}
}
}
Question is,how to acces getCoinValue and retrieve an element value and add it as we iterate through the map so that as we iterate towards the end of the map(Race)each time we get a current number of coins collected. i.e. Desired output to be like this
Jack has collected coins: 75 Jack currently has: 75
Jack has collected coins: 18 Jack currently has: 93
Jack has collected coins: 38 Jack currently has: 131
Jack has collected coins: 95 Jack currently has: 226
Jack has collected coins: 25 Jack currently has: 251
Jack finished the race!
I tried to do this and it is not working,it does not allow me to getCoinValue,it seems invisible.
int currentCoinNumber += map[position].getCoinValue();
So I need to add both objects(Traffic light and Coin to my map,thus map needs to be an Object array,but if the map is an Object array(and not of type Coin)I do not know how to access the getCoinValue(class cast did not work)
To mention one more time I have defined classes,Coin and Player. Player also has constructor and getters and setters.
Random r = new Random();
public int coinValue ;
public Coin(int val) {
val = r.nextInt(100);
this.coinValue = val;
}
@Override
public String toString() {
return "coins: " + coinValue;
}
public int getCoinValue(){
return coinValue;
}
public void setCoinValue(int coinValue){
this.coinValue = coinValue;
}}