Skip to main content
deleted 48 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Guessing game - am I using the concept of Objectsobjects in Java properly?

I'amI'm new to Java and find it hard to grasp the concept of OBJECTSobjects. I wrote a simple guessing game to practice the concept of OO programming but I am not sure if I am doing it correctly.

The objective of the game is to guess a number from 0-9 with 3 players and the first player who guesses it correctly will win the game.

My code is as follows:

OBJECT PLAYER:Player

OBJECT GUESSGAME:GuessGame

andAnd I have a gameLauncher where I have the main() method which creates the game.

Guessing game - am I using the concept of Objects in Java properly?

I'am new to Java and find it hard to grasp the concept of OBJECTS. I wrote a simple guessing game to practice the concept of OO programming but I am not sure if I am doing it correctly.

The objective of the game is to guess a number from 0-9 with 3 players and the first player who guesses it correctly will win the game.

My code is as follows:

OBJECT PLAYER:

OBJECT GUESSGAME:

and I have a gameLauncher where I have the main() method which creates the game.

Guessing game - am I using the concept of objects in Java properly?

I'm new to Java and find it hard to grasp the concept of objects. I wrote a simple guessing game to practice the concept of OO programming but I am not sure if I am doing it correctly.

The objective of the game is to guess a number from 0-9 with 3 players and the first player who guesses it correctly will win the game.

Player

GuessGame

And I have a gameLauncher where I have the main() method which creates the game.

added 50 characters in body; edited tags; edited title
Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157

Am Guessing game - am I using the concept of Objects in Java properly?

Good day!

IamI'am new to Java and find it hard to grasp the concept of OBJECTS. I wrote a simple GUESSING GAMEguessing game to practice the concept of OO programming but I am not sure if I am doing it correctly.

The objective of the game is to guess a number from 0-9 with 3 players and the first player who guesses it correctly will win the game. My

My code is as follows:

public class Player {

    private String playerName;
    private int number;

    public int getNumber() {
        return number;
    }
    
    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public void guessNumber(){
        number = Integer.parseInt(JOptionPane.showInputDialog(playerName +"'s 
 Turn\nGuess   
         + "'s Turn\nGuess the Number"));
        JOptionPane.showMessageDialog(null, playerName+ "'s GUESS is " + number);
    }
}
public class GuessGame {

    private int numberToGuess;
    private Player p1;
    private Player p2;
    private Player p3;
    
    public void startGame(){
        numberToGuess = (int) (Math.random()*10);
        p1 = new Player();
        p2 = new Player();
        p3 = new Player();

        p1.setPlayerName(JOptionPane.showInputDialog("Enter Player 1 Name: "));
        p2.setPlayerName(JOptionPane.showInputDialog("Enter Player 2 Name: "));
        p3.setPlayerName(JOptionPane.showInputDialog("Enter Player 3 Name: "));
        
        int flagWinner = 0;
        while(0==flagWinner){
            p1.guessNumber();
            if (p1.getNumber()==numberToGuess){
                flagWinner = 1;
                break;
            }
            JOptionPane.showMessageDialog(null, p1.getPlayerName()
                + "'s Guess is Wrong!");
            p2.guessNumber();
            if (p2.getNumber()==numberToGuess){
                flagWinner = 2;
                break;
            }
            JOptionPane.showMessageDialog(null, p2.getPlayerName()
                + "'s Guess is Wrong!");
            p3.guessNumber();
            if (p3.getNumber()==numberToGuess){
                flagWinner = 3;
                break;
            }
            JOptionPane.showMessageDialog(null, p3.getPlayerName()
                + "'s Guess is Wrong!");
        }
        if (1 == flagWinner){
            JOptionPane.showMessageDialog(null,p1.getPlayerName()+ " Wins!");
        } else if (2 == flagWinner){
            JOptionPane.showMessageDialog(null,p2.getPlayerName()+ " Wins!");
        } else JOptionPane.showMessageDialog(null,p3.getPlayerName()+ " Wins!");
    }
}

and I have a gameLaunchergameLauncher where I have the main()main() method which creates the game.

Am iI doing it correctly especially in the playerPlayer class where I created a method guessNumber() instead of setter setNumber(int number);?

Am iI violating any object-oriented concepts in my code?

How can I improve my code?

Thank you.

Am I using the concept of Objects in Java properly?

Good day!

Iam new to Java and find it hard to grasp the concept of OBJECTS. I wrote a simple GUESSING GAME to practice the concept of OO programming but I am not sure if I am doing it correctly.

The objective of the game is to guess a number from 0-9 with 3 players and the first player who guesses it correctly will win the game. My code is as follows:

public class Player {

    private String playerName;
    private int number;

    public int getNumber() {
        return number;
    }
    
    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public void guessNumber(){
        number = Integer.parseInt(JOptionPane.showInputDialog(playerName +"'s Turn\nGuess   
         the Number"));
        JOptionPane.showMessageDialog(null, playerName+ "'s GUESS is " + number);
    }
}
public class GuessGame {

    private int numberToGuess;
    private Player p1;
    private Player p2;
    private Player p3;
    
    public void startGame(){
        numberToGuess = (int) (Math.random()*10);
        p1 = new Player();
        p2 = new Player();
        p3 = new Player();

        p1.setPlayerName(JOptionPane.showInputDialog("Enter Player 1 Name: "));
        p2.setPlayerName(JOptionPane.showInputDialog("Enter Player 2 Name: "));
        p3.setPlayerName(JOptionPane.showInputDialog("Enter Player 3 Name: "));
        
        int flagWinner = 0;
        while(0==flagWinner){
            p1.guessNumber();
            if (p1.getNumber()==numberToGuess){
                flagWinner = 1;
                break;
            }
            JOptionPane.showMessageDialog(null, p1.getPlayerName() + "'s Guess is Wrong!");
            p2.guessNumber();
            if (p2.getNumber()==numberToGuess){
                flagWinner = 2;
                break;
            }
            JOptionPane.showMessageDialog(null,p2.getPlayerName() + "'s Guess is Wrong!");
            p3.guessNumber();
            if (p3.getNumber()==numberToGuess){
                flagWinner = 3;
                break;
            }
            JOptionPane.showMessageDialog(null,p3.getPlayerName() + "'s Guess is Wrong!");
        }
        if (1 == flagWinner){
            JOptionPane.showMessageDialog(null,p1.getPlayerName()+ " Wins!");
        } else if (2 == flagWinner){
            JOptionPane.showMessageDialog(null,p2.getPlayerName()+ " Wins!");
        } else JOptionPane.showMessageDialog(null,p3.getPlayerName()+ " Wins!");
    }
}

and I have a gameLauncher where I have the main() method which creates the game.

Am i doing it correctly especially in the player class where I created a method guessNumber() instead of setter setNumber(int number);

Am i violating any object-oriented concepts in my code?

How can I improve my code?

Thank you.

Guessing game - am I using the concept of Objects in Java properly?

I'am new to Java and find it hard to grasp the concept of OBJECTS. I wrote a simple guessing game to practice the concept of OO programming but I am not sure if I am doing it correctly.

The objective of the game is to guess a number from 0-9 with 3 players and the first player who guesses it correctly will win the game.

My code is as follows:

public class Player {

    private String playerName;
    private int number;

    public int getNumber() {
        return number;
    }
    
    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public void guessNumber(){
        number = Integer.parseInt(JOptionPane.showInputDialog(playerName  
            + "'s Turn\nGuess the Number"));
        JOptionPane.showMessageDialog(null, playerName+ "'s GUESS is " + number);
    }
}
public class GuessGame {

    private int numberToGuess;
    private Player p1;
    private Player p2;
    private Player p3;
    
    public void startGame(){
        numberToGuess = (int) (Math.random()*10);
        p1 = new Player();
        p2 = new Player();
        p3 = new Player();

        p1.setPlayerName(JOptionPane.showInputDialog("Enter Player 1 Name: "));
        p2.setPlayerName(JOptionPane.showInputDialog("Enter Player 2 Name: "));
        p3.setPlayerName(JOptionPane.showInputDialog("Enter Player 3 Name: "));
        
        int flagWinner = 0;
        while(0==flagWinner){
            p1.guessNumber();
            if (p1.getNumber()==numberToGuess){
                flagWinner = 1;
                break;
            }
            JOptionPane.showMessageDialog(null, p1.getPlayerName()
                + "'s Guess is Wrong!");
            p2.guessNumber();
            if (p2.getNumber()==numberToGuess){
                flagWinner = 2;
                break;
            }
            JOptionPane.showMessageDialog(null, p2.getPlayerName()
                + "'s Guess is Wrong!");
            p3.guessNumber();
            if (p3.getNumber()==numberToGuess){
                flagWinner = 3;
                break;
            }
            JOptionPane.showMessageDialog(null, p3.getPlayerName()
                + "'s Guess is Wrong!");
        }
        if (1 == flagWinner){
            JOptionPane.showMessageDialog(null,p1.getPlayerName()+ " Wins!");
        } else if (2 == flagWinner){
            JOptionPane.showMessageDialog(null,p2.getPlayerName()+ " Wins!");
        } else JOptionPane.showMessageDialog(null,p3.getPlayerName()+ " Wins!");
    }
}

and I have a gameLauncher where I have the main() method which creates the game.

Am I doing it correctly especially in the Player class where I created a method guessNumber() instead of setter setNumber(int number)?

Am I violating any object-oriented concepts in my code?

How can I improve my code?

Tweeted twitter.com/#!/StackCodeReview/status/38974265382551552
Source Link
user1827
user1827

Am I using the concept of Objects in Java properly?

Good day!

Iam new to Java and find it hard to grasp the concept of OBJECTS. I wrote a simple GUESSING GAME to practice the concept of OO programming but I am not sure if I am doing it correctly.

The objective of the game is to guess a number from 0-9 with 3 players and the first player who guesses it correctly will win the game. My code is as follows:

OBJECT PLAYER:

public class Player {

    private String playerName;
    private int number;

    public int getNumber() {
        return number;
    }
    
    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public void guessNumber(){
        number = Integer.parseInt(JOptionPane.showInputDialog(playerName +"'s Turn\nGuess   
        the Number"));
        JOptionPane.showMessageDialog(null, playerName+ "'s GUESS is " + number);
    }
}

OBJECT GUESSGAME:

public class GuessGame {

    private int numberToGuess;
    private Player p1;
    private Player p2;
    private Player p3;
    
    public void startGame(){
        numberToGuess = (int) (Math.random()*10);
        p1 = new Player();
        p2 = new Player();
        p3 = new Player();

        p1.setPlayerName(JOptionPane.showInputDialog("Enter Player 1 Name: "));
        p2.setPlayerName(JOptionPane.showInputDialog("Enter Player 2 Name: "));
        p3.setPlayerName(JOptionPane.showInputDialog("Enter Player 3 Name: "));
        
        int flagWinner = 0;
        while(0==flagWinner){
            p1.guessNumber();
            if (p1.getNumber()==numberToGuess){
                flagWinner = 1;
                break;
            }
            JOptionPane.showMessageDialog(null, p1.getPlayerName() + "'s Guess is Wrong!");
            p2.guessNumber();
            if (p2.getNumber()==numberToGuess){
                flagWinner = 2;
                break;
            }
            JOptionPane.showMessageDialog(null,p2.getPlayerName() + "'s Guess is Wrong!");
            p3.guessNumber();
            if (p3.getNumber()==numberToGuess){
                flagWinner = 3;
                break;
            }
            JOptionPane.showMessageDialog(null,p3.getPlayerName() + "'s Guess is Wrong!");
        }
        if (1 == flagWinner){
            JOptionPane.showMessageDialog(null,p1.getPlayerName()+ " Wins!");
        } else if (2 == flagWinner){
            JOptionPane.showMessageDialog(null,p2.getPlayerName()+ " Wins!");
        } else JOptionPane.showMessageDialog(null,p3.getPlayerName()+ " Wins!");
    }
}

and I have a gameLauncher where I have the main() method which creates the game.

Am i doing it correctly especially in the player class where I created a method guessNumber() instead of setter setNumber(int number);

Am i violating any object-oriented concepts in my code?

How can I improve my code?

Thank you.