import java.util.*; /* * YOU MUST FILL IN THE CODE FOR THE FOLLOWING TWO METHODS: * - runTournament * - hasNext in the BottomUpIterator * - next in the BottomUpIterator * * This class uses an array to manage a full tree. */ public class TournamentBracketManager { // ALL OF OUR TREE DATA GOES IN THIS ARRAY. REMEMBER, WE // ARE NOT STORING SEQUENTIAL DATA. private Team[] bracket = null; // THIS REPRESENTS THE NUMBER OF NODES IN THE LOWEST LEVEL // IT'S IMPORTANT FOR VARIOUS CALCULATIONS private int numTeams = 0; /* * Default constructor, it does nothing */ public TournamentBracketManager() {} /* * ACCESSOR METHOD */ public int getNumTeams() { return numTeams; } /* * DO NOT CHANGE THIS METHOD * * This method is given the 8 teams participating in the * tournament and it places them in the correct locations * inside the array. */ public void initTeams(Team[] teams) { numTeams = teams.length; bracket = new Team[(numTeams*2)-1]; int bracketFirstTeamIndex = bracket.length - numTeams; // ADD THE TEAMS for (int i = 0; i < teams.length; i++) { bracket[bracketFirstTeamIndex + i] = teams[i]; } // THE REST OF THE NODES ARE NOT YET DETERMINED // SINCE WE HAVEN'T YET PLAYED THE GAMES for (int j = 0; j < bracketFirstTeamIndex; j++) { bracket[j] = null; } } /* * YOU MUST DEFINE THIE METHOD * * This method should walk the tree and use the playGame to * determine winners in all games. It should then fill in the * bracket with this data. * * NOTE: You'll need helper methods for this one. */ public void runTournament() { // ADD YOUR CODE HERE } /* * DO NOT CHANGE THIS METHOD (UNLESS YOU REALLY WANT TO) * * This method takes 2 constructed team objects and returns the * winner. It uses the records of the two teams to make a weighted * random prediction and returns the winner. */ public Team playGame(Team team1, Team team2) { double team1RandomNum = team1.calculateWinningPercentage() * Math.random(); double team2RandomNum = team2.calculateWinningPercentage() * Math.random(); if (team1RandomNum > team2RandomNum) { team1.incWins(); team2.incLosses(); return team1; } else { team1.incLosses(); team2.incWins(); return team2; } } /* * DO NOT CHANGE THIS METHOD * * This method returns your Iterator */ public Iterator bottomUpBracketIterator() { return new BottomUpBracketIterator(); } /* * YOU MUST DEFINE THIS ITERATOR. YOU MAY ADD INSTANCE VARIABLES * AND HELPER METHODS. IT SHOULD PRODUCE ELEMENTS LEFT TO RIGHT, * BOTTOM LEVEL FIRST. */ class BottomUpBracketIterator implements Iterator { public boolean hasNext() { } public Object next() { } public void remove(){} } }