public class Main {
public static void main(String[] args) {
}
}
|
public class Ball {
public Ball() {}
}
|
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();
}
}
|
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();
}
}
|
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 |