You are here: Home / Topics / Ball Game example In Java

Ball Game example In Java

Filed under: Java on 2024-02-16 10:05:17

In this example, i have shown you an example which have mainly three classes:

  1. BallGame.java
  2. BallGameImpl.java
  3. App.java

What does this game does?

In this game, you will fill the no. of players and no. of balls. Then this game will distribute these balls among the players. After distribution it will tell how many balls each player have and how many balls are left. 

 

BallGame.java 

package ballgame;

public class BallGame {
            private Integer ballCount;
            private Integer playerCount;

           public BallGame() {

           }

           public Integer getBallCount() {
                    return ballCount;
            }

          public void setBallCount(Integer ballCount) {
           this.ballCount = ballCount;
            }

            public Integer getPlayerCount() {
            return playerCount;
           }

          public void setPlayerCount(Integer playerCount) {
           this.playerCount = playerCount;
           }

}

 

BallGameImpl.java

package ballgame;

public class BallGameImpl {
private BallGame ballGame;

public BallGameImpl(BallGame ballGame) {
 this.ballGame = ballGame;
}

public Integer calculateRemainingBallCount() {
 Integer remainingBallCount = this.ballGame.getBallCount() % this.ballGame.getPlayerCount();
 return remainingBallCount;
}

public Integer calculateEqualityDistributedBall() {
 Integer distributedBalls = this.ballGame.getBallCount() / this.ballGame.getPlayerCount();
 return distributedBalls;
}

}

App.java to start this game.

package ballgame;

public class App {

public static void main(String[] args) {
 BallGame ballGame = new BallGame();
 ballGame.setBallCount(34);
 ;
 ballGame.setPlayerCount(5);

 BallGameImpl ballGameImpl = new BallGameImpl(ballGame);
 System.out.println(ballGameImpl.calculateRemainingBallCount());
 System.out.println(ballGameImpl.calculateEqualityDistributedBall());
}
}
 

 

I hope you enjoyed this example. If having any doubts ask me in comments.

About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.