PassBall

Now we are ready to move on to larger programs with multiple classes and objects. In thie program, you will simulate people throwing balls back and forth between themselves. You will write a Ball class and a Player class, and the a slightly more sophistocated Main class than you have written before.

  1. Start up a new project, called PassBall, with a main class called Main.java.
  2. In the blank file Main.java, write the following code:
  3. public class Main {
    
    	public static void main(String[] args) {
    
    	}
    
    }
    
  4. Create another new blank text file called Ball.java, and add it to the project.
  5. In Ball.java, type in the following code:
  6. public class Ball {
    
            public Ball() {}
    
    }
    
  7. Create another new blank text file called Player.java, and add it to the project.
  8. In Player.java, type in the following code:
  9. public class Player {
    
            private final String name;
            private Ball ball;
    
            public Player(String name) {
                    this.name = name;
                    ball = null;
            }
    
            public boolean hasBall() {
                    return ball != null;
            }
    
            public void passBallTo(Player otherPlayer) {
                    otherPlayer.acceptBall(giveBall());
            }
    
            public void acceptBall(Ball ball) {
                    this.ball = ball;
            }
    
            public Ball giveBall() {
                    Ball tempBallReference = ball;
                    ball = null;
                    return tempBallReference;
            }
    
            public String toString() {
                    return name + ": possession = " + hasBall();
            }
    
    }
    
  10. Now go back to Main.java, and add the following code:
  11. public class Main {
    
    	private static Player p1;
    	private static Player p2;
    	private static Player p3;
    	private static Player p4;
    	private static Player p5;
    
    	public static void main(String[] args) {
    		initializePlayers();
    		play();
    	}
    
    	public static void initializePlayers() {
    		p1 = new Player("Ellen");
    		p2 = new Player("Erin");
    		p3 = new Player("Chris");
    		p4 = new Player("Aradan");
    		p5 = new Player("Heidi");
    	}
    
    	public static void play() {
    		printPlayers();
    
    		giveAnotherBallTo(p1);
    		p1.passBallTo(p2);
    		p2.passBallTo(p4);
    		printPlayers();
    
    		giveAnotherBallTo(p3);
    		p3.passBallTo(p5);
    		p4.passBallTo(p3);
    		p1.passBallTo(p2);
    		printPlayers();
    		
    		p2.passBallTo(p3);
    		p5.passBallTo(p2);
    		p3.passBallTo(p1);
    		p4.passBallTo(p3);
    		p2.passBallTo(p4);
    		printPlayers();
    
    		giveAnotherBallTo(p1);
    		giveAnotherBallTo(p2);
    		giveAnotherBallTo(p3);
    		giveAnotherBallTo(p5);
    		p3.passBallTo(p1);
    		p1.passBallTo(p2);
    		p4.passBallTo(p3);
    		p5.passBallTo(p5);
    		printPlayers();
    
    	}
    
    	public static void giveAnotherBallTo(Player p) {
    		p.acceptBall(new Ball());
    	}
    
    	public static void printPlayers() {
    		System.out.println(p1);
    		System.out.println(p2);
    		System.out.println(p3);
    		System.out.println(p4);
    		System.out.println(p5);
    		System.out.println();
    	}
    
    }
    
  12. Compile and run your program. You should get the following output:
  13. Ellen: possession = false
    Erin: possession = false
    Chris: possession = false
    Aradan: possession = false
    Heidi: possession = false
    
    Ellen: possession = false
    Erin: possession = false
    Chris: possession = false
    Aradan: possession = true
    Heidi: possession = false
    
    Ellen: possession = false
    Erin: possession = false
    Chris: possession = true
    Aradan: possession = false
    Heidi: possession = true
    
    Ellen: possession = false
    Erin: possession = false
    Chris: possession = false
    Aradan: possession = true
    Heidi: possession = false
    
    Ellen: possession = false
    Erin: possession = true
    Chris: possession = true
    Aradan: possession = false
    Heidi: possession = true
    
  14. Press enter, as the console window says, and the black console window will disappear.