1

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;
}}

2 Answers 2

1

The type Object does not have a method getCoinValue, so if you have an array of Objects you cannot invoke that method.

I won't address the questionable decision to use an array of Objects, but if you want to do this, it's technically perfectly valid. In order to access getCoinValue you need to cast the Object to a Coin.

Say a Coin is at array index 9. In order to cast and call getCoinValue you can do it this way:

((Coin)map[9]).getCoinValue()

Extra parentheses are required for the cast to evaluate correctly if the cast and method call are done on one line.

Alternately, this does the same thing, but might be more readable:

Coin coin = (Coin)map[9];
Sign up to request clarification or add additional context in comments.

2 Comments

I used Object as an array type because of a need for putting two different objects in it(Traffic Light and Coin). Imagine my array map as a random array of interchangeable coins and traffic lights,where player,whilst iterating through the map,can either collect a coin or stop at the traffic light(implemented as a thread). And Thank you for the help, When I tried my cast for the first time,like this: currentCoinNumber += (Coin)map[position].getCoinValue(); it did not work,so it really confused me.
Yes, I see. There are still options to avoid using Object. In this case, the command pattern would avoid using Objects.
0

simply add up the current coins with the collected. could look like this:

int currentCoinNumber = 0;should be before the for-loop.

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) {
                    currentCoinNumber+=map[position].getCoinValue();
                    System.out.println(player.getIgrac1() + " has collected " + mapa[pozicija]);
                    System.out.println("Jack currently has: " + currentCoinNumber);

                } else {
                    if (mapa[pozicija] != null && mapa[pozicija] instanceof TrafficLight) {
                        // s.run();
                        System.out.println(player.getPlayer1() + " is at the " + mapa[pozicija]);

                    } else {
                        if (map[position] == null) {
                            s.interrupt();
                            wait(500);
                            position++;
                        }
                    }
                    if (!RaceEnd) {

                        System.out.println(player.getPlayer1() + " did not finish the race!");
                    }
                }

            }
        }

this is the code you posted a bit modified. this only works for 1 player at a time

8 Comments

I tried it like this but it does not allow me to getCoinValue at all,I really don't know why,that is why I decided to post it here in the first place.
what is the error if you want to call getCoinValue?
It is not an error,it is just undefined for the type Object.
If I create an object Coin like this: Coin n = new Coin(100); Then I can access the n.getCoinValue; but as I assigned my coins to the array(named map in this case) as you can see from the example,I can not access them through a map. Basically when an iterator comes to the map index which is not null and instance of Coin,it should get the coin value but as the Coins are only passed to an array with a constructor.I dont know how to access them
you have your map initialized like this: Object[]map = new Object[20]; instead of object, use Con like this:Coin[] map=new Coin[20]; this should solve the problem
|

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.