/* File: League.java - March 2011 */ package l08; /** * Illustrate the use of the Comparable interface for Team by simulating * a league * * @author Michael Albert * */ public class League { public static Team[] teams = { new Team("Blues"), new Team("Gunners"), new Team("Riversiders"), new Team("Latics"), new Team("Canaries"), new Team("Tangerines"), new Team("Pool"), new Team("Red Devils") }; public static void main(String[] args) { Team[] teamsCopy = new Team[teams.length]; System.arraycopy(teams, 0, teamsCopy, 0, teams.length); for(int round = 1; round < teams.length; round++) { playRound(round); System.out.println(); java.util.Arrays.sort(teamsCopy); for(int k = teamsCopy.length-1; k >= 0; k--) { System.out.println(teamsCopy[k]); } System.out.println(); System.out.println("---------------------------------"); } System.out.println(); } public static void playRound(int round) { System.out.println("Round " + round); int[] matches = new int[teams.length]; int i = 1; for(int k = round; k < teams.length; k++) { matches[k] = i; i++; } for(int k = 1; k < round; k++) { matches[k] = i; i++; } int a = 0; int b = teams.length-1; while (a < b) { play(teams[matches[a]], teams[matches[b]]); a++; b--; } } public static void play(Team a, Team b) { int aGoals = (int) (Math.random()*3.5); int bGoals = (int) (Math.random()*3.5); a.addResult(aGoals, bGoals); b.addResult(bGoals, aGoals); System.out.println(a.getName() + " " + aGoals + ", " + b.getName() + " " + bGoals); } }