//------------------------------------------------------------ // Question 1 // // Part A public int pieceCount (String url) { int pieces = 0; int index = url.indexOf("="); while (index >= 0) { index = url.indexOf("=", index + 1); pieces++; } // OR: // int pieces = 1; // guaranteed to be at least one // int index = url.indexOf("&"); // while (index >= 0) // { // pieces++; // index = url.indexOf("&", index + 1); // } // OR: // int pieces = 0; // Scanner input = new Scanner(url); // input.useDelimiter("&"); // while (input.hasNext()) // { // input.next(); // pieces++; // } return pieces; } // Part B public String getNthPiece (String url, int n) { for (int k = 1; k < n; k++) { url = url.substring(url.indexOf("&") + 1); } int index = url.indexOf("&"); if (index < 0) return url; else return url.substring(0, index); // OR: // String result = ""; // Scanner input = new Scanner(url); // input.useDelimiter("&"); // for (int k = 0; k < n; k++) // { // result = input.next(); // } // return result; } // Part C public void splitURL (String url, List ids, List values) { int numPieces = pieceCount(url); for (int k = 1; k <= numPieces; k++) { String pair = getNthPiece(url, k); int index = pair.indexOf("="); ids.add(pair.substring(0, index)); values.add(pair.substring(index + 1)); } } //------------------------------------------------------------ // Question 2 // // Part A // A method or instance variable can declared with the following // access restrictions: // - public, no restrictions, available to any class // - protected, more restricted, only available within its // class and its subclasses // - private, most restricted, only available within its class // // Within the Shape class, the method area and the costructor are // the only public aspects of the class. The methods getWidth() // and getHeight() provide protected access to the private values // that represent the width and height of the shape. Because the // actual instance variables are private, the shape's dimensions // may not be changed once it has been created, making it immutable. // Part B // There are several errors that appeared in the code, here are // the two I intended for you to notice: // - syntax error: myHeight is a private instance variable of the // superclass and so is not valid for the subclass to use it in // its area method // - logic error: myWidth is declared in the sublcass, shadowing // the superclass definition, but since it is never set in the // constructor its value when area is called is 0 // Part C public class Rectangle extends Shape { public Rectangle (double width, double height) { super(width, height); } // returns area of rectangle public double area () { return getWidth() * getHeight(); } } // Part D public double averageArea (ArrayList shapes) { if (shapes.size() > 0) { double total = 0; for (int k = 0; k < shapes.size(); k++) { Shape current = (Shape)shapes.get(k); total += current.area(); } return total / shapes.size(); } else { return 0; } } //------------------------------------------------------------ // Question 3 // // Part A public ArrayList readTeam (Scanner input) { ArrayList team = new ArrayList(); while (input.hasNext()) { // read name String name = input.nextLine(); // read minutes ArrayList minutes = new ArrayList(); Scanner line = new Scanner(input.next()); while (line.hasNext()) { minutes.add(line.nextInt()); } // create Player team.add(new Player(name, minutes)); } return team; } // Part B public int totalNumberOfMinutes (Player player) { int total = 0; for (int k = 0; k < player.getNumberGamesPlayed(); k++) { total += player.getMinutesForGame(k); } return total; } // Part C public Player mostMinutes (ArrayList team) { // assume at least one player Player result = team.get(0); int mostMinutes = totalNumberOfMinutes(result); // compare to rest of team for (int k = 1; k < team.size(); k++) { Player current = team.get(k); int minutes = totalNumberOfMinutes(current); if (minutes > mostMinutes) { mostMinutes = minutes; result = current; } } return result.getName(); }