// Team simulation, illustrating inheritance, dynamic dispatch, // also usage of non-uniformly distributed random numbers. // Also uses sorts.java for mergesort import java.util.Random; interface Playable // should be made public and placed in different file { void win(); void lose(); void printrecord(); void play(team x); double winningpercentage(); } class team implements Playable, Comparable { protected int wins; // records number of wins protected int losses; protected String name; public String name() { return name; } protected static Random Rnd = new Random(); // random number generator // generates random number between 0 and 1 with expected mean = m: public static double brand(double m) { if (m<=0 || m>=1) throw new RuntimeException("bad mean"); double r = Rnd.nextDouble(); return Math.pow(r,1.0/m - 1); } // generates normally distributed random number with standard deviation // 10, offset by average public static double normRand(double ave, double dev) { double g = Rnd.nextGaussian(); return g*dev + ave; // multiply by deviation, adjust average } public team(String n) { name = n; wins = losses = 0; }// constructor // interface methods public void win() { wins++; } public void lose() { losses++; } public void printrecord() { System.out.println(wins+"-"+losses); } public double winningpercentage() { int played = wins+losses; // number of games played if (played<1) return 0; // no games played special case double p = wins*1.0/played; // round to 3 digits: p = ((int)(p*1000+0.5))/1000.0; return p; } public void play(team other) { if (Rnd.nextDouble()<0.5) { win(); other.lose(); System.out.println("I win, you suck"); } else { other.win(); this.lose(); System.out.println("You win, but you still suck"); } }//play (default) // Comparable interface public int compareTo(team other) { double wp1 = winningpercentage(), wp2=other.winningpercentage(); // return (int)(wp1*1000) - (int)(wp2*1000); if (wp1==wp2) return 0; else if (wp1score2)// I win, you suck { win(); other.lose(); System.out.printf("%s %d, %s %d.",name,score1,other.name,score2); } else // You win, but you still suck { other.win(); this.lose(); System.out.printf("%s %d, %s %d.",other.name,score2,name,score1); } System.out.println(); }//play }//bkteam /////// now for football teams, which can tie: interface CanTie extends Playable { void tie(); void play(fbteam otherteam); } class fbteam extends team implements CanTie // football team { protected int ties; protected double pct; // set to previous season's winning percentage public fbteam(String n, double pct) // takes base winning percentage { super(n); ties = 0; this.pct = pct; } public void tie() { ties++; } // this overrides super.play, which is no longer valid public void play(team other) { throw new RuntimeException("this team can only play teams that can tie"); } public void printrecord() // override { System.out.println(wins+"-"+losses+"-"+ties); } public double winningpercentage() // override { int played = wins+losses+ties; // number of games played if (played<1) return 0; // no games played special case double p = (wins + ties*.5)/played; // round to 3 digits: p = ((int)(p*1000+0.5))/1000.0; return p; } public void play(fbteam other) // new function { // set winning percentage, first few games based on previous seaon pct int played1 = wins+losses+ties; int played2 = other.wins + other.losses+other.ties; double wp1 = winningpercentage(); double wp2 = other.winningpercentage(); if (played1<5) wp1 = pct; if (played2<5) wp2 = other.pct; if (wp1>.95) wp1 = .96; // give other team a chance in any case if (wp2>.95) wp2 = .96; if (wp1<.05) wp1=.04; if (wp2<.05) wp2=.04; // typical football scores: int[] Scores={0,3,6,7,10,13,14,16,17,20,21,23,24,27,28,30,31,34,35,37,38,41,42,44,45,47,48,49,51,52,55,56,59}; // use winning percentage to determine average score of team scaled to // scores.length. Use biased random number generator double p1 = brand(wp1), p2 = brand(wp2); int score1 = Scores[(int)(p1 * Scores.length)]; int score2 = Scores[(int)(p2 * Scores.length)]; if (score1>score2) { win(); other.lose(); System.out.printf("%s %d, %s %d.",name,score1,other.name,score2); } else if (score1==score2) { tie(); other.tie(); System.out.printf("%s %d, %s %d.",name,score1,other.name,score2); } else { other.win(); this.lose(); System.out.printf("%s %d, %s %d.",other.name,score2,name,score1); } System.out.println(); }//play for fbteams }//fbteam public class tsim { public static void main(String[] av) { /* team team1 = new team("Knicks"); team team2 = new team("Nets"); team1.lose(); team2.win(); team2.lose(); team2.printrecord(); // should print "W-L: 1-1" team1.play(team2); // should print "knicks win" or "nets win" System.out.println(team1.winningpercentage()); // prints .000 to 1.00 */ String[] Teamnames = {"Knicks","Nets","Lakers","Celtics","Warriors","Spurs","Cavaliers","Raptors","Bulls","76ers"}; //team[] League = new team[Teamnames.length]; team[] League = new team[Teamnames.length]; for(int i =0;i sorter = new sorts(League); sorter.mergesort(); System.out.println("\nLeague Standings:"); for(int i=League.length-1;i>=0;i--) { System.out.print(League[i].name()+ ": "); League[i].printrecord(); } System.out.println(); System.exit(0); // force termination //////////////// for fbteam subclass String[] fbn = {"Giants","Jets","Cowboys","Patriots","Falcons","Steelers","Packers","Eagles","Chiefs","Bills","Seahawks","Broncos","Chargers","Raiders","Dolphins","Rams","49ers"}; double[] wps = {.688,.313,.813,.875,.688,.688,.625,.438,.750,.438,.656,.563,.313,.750,.625,.250,.125}; fbteam[] NFL = new fbteam[fbn.length]; for(int i=0;i=0;i--) { System.out.print(NFL[i].name()+ ": "); NFL[i].printrecord(); } System.out.println(); }//main }// public class