Name: CS 101G Spring 2004 Student ID: Monday, May 10 Final Exam Note 1: Exam is closed book and notes. Note 2: Read questions carefully. Note 3: Ask instructor if you have any questions regarding the exam. Note 4: To enable grading for partial credit, show your work and provide as much information as possible. 1.) Definitions and Concepts (15 points): For each definition/description in the left column, mark the number of the term from the right column that best defines it. Note: Terms from the right column may be used 0, 1, or more times. a. a class (or set of classes) for data abstraction where the amount of data may vary dynamically in size b. the ancestor for a particular class c. Java code for a web browser d. the portion of the execution stack corresponding to a single dynamic instance of a method e. the condition upon which recursion usually terminates f. waterfall method g. good programming style h. ability of a class to execute methods as if they were defined in that class itself i. Java code that does not require a main() function j. a first-in, first-out data structure k. ability of a method call to perform different functions based on its class type l. iterative programming style in which a method calls itself m. spiral method n. a class (or set of classes) for data abstraction that defines an interface and its associated behavior o. storage location for method s return address, input arguments, and local variables 1. abstract data type 2. HTML 3. software life cycle 4. applet 5. inheritance 6. base case 7. recursion 8. polymorphism 9. association 10. activation record 11. queue 12. superclass 13. self-documenting code 14. data structure 15. abstraction
2.) Procedural Abstraction (25 points): (a) (7 points) public String bar (int a, int n) { int z = (int) ((double) (Math.pow (2,32) 1.0) / (double) a); for (i = 0; ((i < n) && (i <= z)); i++) result = result + + (a * Math.pow (2,n)); return result; Given the above procedure, what will be the output for the following statement? System.out.println (bar (5, 12)); What is the purpose of the variable z? (b) (8 points) public class ListItem { int number; ListItem next; public ListItem (double number, ListItem next) { this.number = number; this.next = next; Given the above class definition for ListItem, and the ListItem head corresponding to the head a linked list, give the Java code for a procedure that inserts a new ListItem, x, into the list before the tail item of the list. The procedure should return the head of the resulting list. public ListItem InsertBeforeTail (ListItem head, ListItem x) { // add your code here...
2.) cont (b) (10 points) public class ListItem { int number; ListItem next; public ListItem (double number, ListItem next) { this.number = number; this.next = next; public ListItem foo (int n) { if (this == null) return null; else if (this.number == n) return this; else return next.foo(); There is an error in the above procedure. - What is this error? - How can you fix this error? (please show the working code) - What is the intended purpose of this procedure?
3.) Data Abstraction (20 points): public class ListItem { double number; ListItem next; public ListItem (double number, ListItem next) { this.number = number; this.next = next; public class Equation { double b; public Equation (double b) { this.b = b; In this problem you are to define an Equation ADT. Given above are two classes, the standard ListItem class we ve been using in this course, and a basic equation class, which defines the function: f ( x, y) = b Given these two classes, construct an Equation ADT that supports: a 1-D polynomial (for any power of x) e.g. a 2-D polynomial e.g. a logarithmic function e.g. 4 2 f ( x) a x + a x + a x + b = 4 2 1 2 2 f ( x, y) = a x + a xy + a xy + b 20 f ( x) = a ln x + b 11 Define three classes for these types of equations, building off of the existing classes given above. Use inheritance whenever possible. For these classes, you only need to give the instance variables, constructors, and the addterm() method for the 1-D and 2-D polynomials. You may assume that a method append() exists (in the 1-D and 2-D polynomial classes) for appending ListItems to a linked list. For the 1-D and 2-D polynomial, it is recommended that you store each term s powers (of x and/or y) as well as each term s coefficient (e.g. for a 2 x 2, store a2, 2 for the power of x, and 0 for the power of y). Also draw the class diagram of the resulting class hierarchy. 12
3.) cont
4.) Data Structures (20 points): (a) (9 points) Given the Relation ADT, and the Set ADT, we can form a Set of Relations. We can use this Set of Relations to have a set of employees, with the Relation for each employee associating the employee s name with his/her salary. For example, (Jeremy, 50000) (Ron, 65000) (Terry, 38000) (Fred, 90000) (Sabrina, 15000) (Theresa, 42500) Given this example, what would the binary tree containing these relations look like, when: 1) Organizing the nodes of the tree according to the Relation's domain. 2) Organizing the nodes of the tree according to the Relation's range. Show each of these trees. Assume the tree is initially empty, and the Relations are inserted in the order given above. After a few years, Ron leaves the company for a better job. Show the two tree after the Relation for Ron is deleted.
4.) cont (b) (5 points) As discussed in class, the Set ADT can be implemented a variety of ways. - Which implementation (among those discussed in class) is best for the insert function? - Which is best for the intersection function? - Why doesn't the implementation that is best for the insert function also provide the best implementation for intersection function? (c) (6 points) You are creating a Java project that requires an array of X bins, where each bin is an element of class Bin (the Bin ADT), with the name Bin {# associated with it (e.g. Bin 1, Bin 2, Bin 3,, Bin X ). Write the Java procedure createbins(int X), which creates this array of X bins. Assume the constructor for class Bin receives one input parameter, String name. Use iteration wherever possible.
5.) Object-Oriented Design (20 points): Consider the following Java code and then answer the questions. public class Game { String name; double avgreturn; public Game (String name, double avgreturn) { this.name = name; this.avgreturn = avgreturn; public GameResult play (double bet) { GameResult currentbet; double rand = Math.random (); if (rand < (avgreturn / 2)) currentbet = new GameResult (true, "Won", bet); else currentbet = new GameResult (false, "Lost", -bet); return currentbet; public String getname () { return name; public String tostring () { return getname (); public class GameResult { boolean won; double winnings; String result; public GameResult (boolean won, String result, double winnings) { this.won = won; this.result = result; this.winnings = winnings; public boolean getwon () { return won; public double getwinnings () { return winnings; public String getresult () { return result; public String tostring () { if (won) return "" + result + "! Won $" + winnings; else return "" + result + ". Lost -$" + (-winnings);
public class Slots extends Game { double payoff; public Slots (double avgreturn, double payoff) { super ("the slot machines", avgreturn); this.payoff = payoff; public GameResult play (double bet) { GameResult currentbet; double rand = Math.random (); if (rand < (avgreturn / payoff)) currentbet = new GameResult (true, "Won", bet * payoff); else currentbet = new GameResult (false, "Lost", -bet); return currentbet; public class Blackjack extends Game { public Blackjack () { super ("Blackjack", 1); // 'avgreturn' not used by Blackjack public GameResult play (double bet) { GameResult currentbet; double rand = Math.random (); if (rand <.1) currentbet = new GameResult (true, "Blackjack", 1.5 * bet); else if (rand <.4) currentbet = new GameResult (true, "Won", bet); else currentbet = new GameResult (false, "Lost", -bet); return currentbet; public class Person { String name; double winnings; double bet; double lastbet; double winincrease; public Person (String name, double winincrease) { this.name = name; this.winincrease = winincrease; winnings = 0.0; bet = 1.0; lastbet = 0.0; public GameResult playgame (Game game) { return playgame (game, this.bet);
public GameResult playgame (Game game, double bet) { this.bet = bet; lastbet = bet; GameResult result = game.play (bet); winnings += result.getwinnings(); if (result.getwon()) this.bet += bet * winincrease; return result; public String getname () { return name; public double getlastbet () { return lastbet; public double getwinnings () { return winnings; (a) (3 points) What methods are inherited in each of the classes of this class hierarchy? (b) (3 points) What methods use polymorphism in each of the classes of this class hierarchy?
5.) cont (c) (14 points) Given the above class hierarchy, the following code computes 10 random numbers and updates each person s winnings accordingly. Given that the 10 random values in a particular execution of the code are as follows, what is the output of the code? 0.659, 0.652, 0.850, 0.757, 0.562, 0.287, 0.805, 0.231, 0.501, 0.594 public class Casino { public static void personplaygame (Person person, Game game) { GameResult result = person.playgame (game); System.out.println ("Sally bet $" + person.getlastbet() + " at " + game + ". " + result); public static void personplaygame (Person person, Game game, double bet) { GameResult result = person.playgame (game, bet); System.out.println ("Sally bet $" + person.getlastbet() + " at " + game + ". " + result); public static void printpersonwinnings (Person person) { System.out.println (""); System.out.print (person.getname() + "'s winnings total "); if (person.getwinnings() >= 0) System.out.println ("$" + person.getwinnings()); else System.out.println ("-$" + (-sally.getwinnings())); public static void main(string[] args) { GameResult result; Person sally = new Person (.5); Person josh = new Person (1.0); Person peter = new Person (0.0); Game doublemoney = new Game ("Double Your Money", (Math.random() *.08) +.9); Game fiveslots = new Slots ((Math.random() *.1) +.85, 5); Game tenslots = new Slots ((Math.random() *.06) +.9, 10); Game blackjack = new Blackjack (); personplaygame (sally, doublemoney, 10); personplaygame (peter, tenslots, 2); personplaygame (sally, doublemoney); personplaygame (sally, fiveslots, 10); personplaygame (josh, blackjack, 25); personplaygame (peter, fiveslots); personplaygame (josh, doublemoney); printpersonwinnings (sally); printpersonwinnings (josh); printpersonwinnings (peter);
5.) cont