title: Iterating through an array,retrieving element values and adding them
body: <p>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:</p>
  
  <p><strong>UPDATE:</strong> <em>I still do not know how to access getCoinValue if both Coin and TrafficLight are in the array of the type Object.</em></p>
  
  <p><strong>FIX:</strong> <em>I fixed this by explicitly casting the Coin object like this:</em></p>
  
  <pre><code>Coin coin= (Coin)map[position];
  
                  currentCoinValue += coin.getCoinValue();
  </code></pre>
  
  <p>The rest of the code...</p>
  
  <pre><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&lt; map.length; position++) {
  
          if (position==19) {
              RaceEnd = true;
              System.out.println(player.getPlayer1() + " finished the race!");
          //  s.interrupt();
  
              break;
          } else {
  
              if (map[position] != null &amp;&amp; map[position] instanceof Coin) {
  
               Coin coin= (Coin)map[position];
  
              currentCoinValue += coin.getCoinValue();
  </code></pre>
  
  <blockquote>
  <pre><code>               &gt; THE CASTING IS NOT AN OPTION,STILL SHOWS UNDEFINED
  </code></pre>
  </blockquote>
  
  <pre><code>                System.out.println(player.getPlayer1() + " has collected " + mapa[pozicija] );
                  System.out.println("Jack currently has: " + currentCoinNumber);
              /*  } else {
                      if (map[position] != null &amp;&amp; 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!");
                      }
                  }
  
              }
  
          }
  }
  </code></pre>
  
  <blockquote>
    <p>Question is,how to acces getCoinValue and retrieve an element value and <strong>add</strong> 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.
    <strong>i.e.  Desired output to be like this</strong></p>
    
    <p>Jack has collected coins: 75 Jack currently has: 75</p>
    
    <p>Jack has collected coins: 18 Jack currently has: 93</p>
    
    <p>Jack has collected coins: 38 Jack currently has: 131</p>
    
    <p>Jack has collected coins: 95 Jack currently has: 226</p>
    
    <p>Jack has collected coins: 25 Jack currently has: 251</p>
    
    <p>Jack finished the race!</p>
  </blockquote>
  
  <p>I tried to do this and it is not working,it does not allow me to getCoinValue,it seems invisible.</p>
  
  <pre><code>int currentCoinNumber += map[position].getCoinValue();
  </code></pre>
  
  <blockquote>
    <p>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(<strong>class
    cast did not work</strong>)</p>
  </blockquote>
  
  <p>To mention one more time I have defined classes,Coin and Player.
  Player also has constructor and getters and setters.</p>
  
  <pre><code>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;
  }}
  </code></pre>