// Sample solutions to lab 1b public class lab1bsolutions { //1 public static boolean inarray(String s, String[] SA) { for(String x:SA) if (x.equals(s)) return true; return false; } //2a public static int sum(int[] A) { int ax = 0; // accumulator for(int x:A) ax+=x; return ax; } //2b public static String smallest(String[] SA) { if (SA==null || SA.length==0) return null; int si = 0; // assume smallest string is at index 0 for(int i=1;i { public final String name; // name of team, can't be changed protected int wins = 0; protected int losses=0; public team(String n) { name=n; } // constructor public void win() { wins++; } public void lose() { losses++; } public void printrecord() { System.out.println("W-L: "+wins+"-"+losses); } public double winningpercentage() { int games = wins+losses; // games played if (games<1) return 0; return (wins*1.0)/games; // *1.0 converts int to double. } public void play(team other) // other instead of x is fine { double r = Math.random(); // random num between 0 and 1 if (r<0.5) // win { win(); other.lose(); System.out.println(name+" win, "+other.name+" lose"); } else //lose { lose(); other.win(); System.out.println(other.name+" win, "+name+" lose"); } }//play // comparable for sorting: public int compareTo(team other) { int wp1 = (int)(winningpercentage()*1000+0.5); // round off percentages int wp2 = (int)(other.winningpercentage()*1000+0.5); return wp1 - wp2; } //////// public static void seasonsim() // for testing, called from main { String[] fbn = {"Giants","Jets","Rams","Patriots","Falcons","Steelers","Packers","Eagles","Chiefs","Bills","Seahawks","Cowboys","Chargers","Raiders","Dolphins","Saints","49ers"}; team[] TEAM = new team[fbn.length]; for(int i =0;i0) bi = k; System.out.println(TEAM[bi].name+" has the best winning percentage at "+TEAM[bi].winningpercentage()); // sorting skipped, but can use quicksort code. }//seasonsim }//team class ///////// Sample solution to time class: class time implements Comparable