Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lesson13;
- import java.util.Random;
- public class SoccerTournament {
- Random rand = new Random();
- private int games = 0;
- private String name;
- private int wins = 0;
- ArithmeticException notgames = new ArithmeticException("You need at least 3 games to win.");
- SoccerTournament(String name){
- this.name = name;
- }
- public void playGame(SoccerTournament a) {
- while (a.wins <= 3 || this.wins <= 3) {
- if (rand.nextBoolean()) {
- a.wins ++;
- a.games ++;
- } else {
- this.wins ++;
- this.games ++;
- }
- }
- }
- public void getWinner(SoccerTournament a) {
- if (a.wins > this.wins) {
- if (a.games > 1) System.out.println(a.name + " is the winner.");
- else throw notgames;
- } else {
- if (this.games > 1) System.out.println(this.name + " is the winner.");
- else throw notgames;
- }
- }
- public static void main(String args[]) {
- SoccerTournament TorontoFC = new SoccerTournament("Toronto FC");
- SoccerTournament MontrealFC = new SoccerTournament("Montreal FC");
- MontrealFC.playGame(TorontoFC);
- TorontoFC.getWinner(MontrealFC);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement