Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8482089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:44:06+00:00 2026-06-10T19:44:06+00:00

this is the player class package day6; public class Player { int playerId; public

  • 0

this is the player class

package day6;

public class Player {
int playerId;

public Player(int playerId) {
    super();
    this.playerId = playerId;
}

public int getPlayerId() {
    return playerId;
}

}

this is the position class

package day6;

public class Position {


    private int positionNumber = 0;

    public Position(int positionNumber) throws Exception {
        super();
        this.positionNumber = positionNumber;

    }

    public int getPositionNumber() {
        return positionNumber;
    }

    }

this is the game class

 package day6;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Random;

    public class Game {


        private int numberOfPlayers;


        HashMap<Player, Position> hashMapForPositionOfPlayer = new HashMap<Player, Position>(numberOfPlayers);

        @SuppressWarnings("unchecked")
        public Game(int numberOfPlayers) throws Exception {
            super();

            this.numberOfPlayers = numberOfPlayers;
            for (int i = 0; i <numberOfPlayers; i++) {
                hashMapForPositionOfPlayer.put(new Player(i), new Position(0));
            }
        }

        public void play() throws Exception {

            Position currentPosition;




                for (int i = 0; i < numberOfPlayers; i++) {

                    Player player = new Player(i);
                    currentPosition = hashMapForPositionOfPlayer.get(player);
                    int value = currentPosition.getPositionNumber();

                    }
                }
            }

I tried to run the program Game class and game.play() but it is showing null value to the 0th value of the hashmap.

it means currentPosition=null for hashmap of key value 0.

Help me..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:44:07+00:00Added an answer on June 10, 2026 at 7:44 pm

    Since you’re using Player instances as keys of the HashMap, it should implement hashCode() and equals() consistently, so that two Player instances are considered to represent the same player when their playerId‘s are equal.

    Without implementing equals() properly, the following snippet would return false:

    Player p1 = new Player(1);
    Player p2 = new Player(1);
    return p1.equals(p2);
    

    Also, given the nature of the Map implementation you’ve chosen, HashMap, the check for existing keys in the Map will be associated to their hashCode, so it is necessary to also implement hashCode to reflect that two Player instances are equal if their playerId‘s are.

    The following question might be useful to you: Understanding the workings of equals and hashCode in a HashMap.

    Sample code:

    package day6;
    
    public class Player {
        int playerId;
    
    public Player(int playerId) {
        super();
        this.playerId = playerId;
    }
    
    public int getPlayerId() {
        return playerId;
    }
    
    public int hashCode(){
        return playerId.hashCode();
    }
    
    public boolean equals(Object o){
        if (o instanceOf Player) { 
            return playerId.equals( ((Player)o).getPlayerId());
        }
        return false;
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the below class public class Player { private int id; public int getId()
I have a package with a public abstract class Player { /*...*/ } and
I have this class package net.omnosis.mazegame.components; import net.omnosis.mazegame.SlicedBitmap; import android.graphics.Bitmap; public class PlayerLayer extends
This is my problem: I have a player class and SwipeDetector Class in c#,
I am not sure how to approach this problem: 'Player' class maintains a list
var playerClass = $(this).parent().attr('class') Question about only selecting a specific class from the dom.
So I want to design a team/player relationship like this: every player belongs to
I have this game where balloons are coming from bottom and player has to
This is for a game in a flash AS3 only project. the player controls
I am developing a music player app. In this I ll retrieving all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.