Final
Final
You have 110 minutes to complete this exam. You may receive a deduction if you keep working after the instructor calls for papers. This exam is open-book/notes. You may not use any computing devices including calculators. Code will be graded on proper behavior/output and not on style, unless otherwise indicated. Do not abbreviate code, such as "ditto" marks or dot-dot-dot ... marks. The only abbreviations that are allowed for this exam are: S.o.p for System.out.print, and S.o.pln for System.out.println.
You do not need to write import statements in your code. If you enter the room, you must turn in an exam before leaving the room. You must show your Student ID to a TA or instructor for your exam to be accepted. Good luck!
Problem Description Earned 1 Inheritance/Polymorphism 2 Inheritance/Comparable Programming 3 Linked List Programming 4 Searching and Sorting 5 Binary Search Trees 6 Binary Tree Programming 7 Binary Tree Programming X Extra Credit TOTAL Total Points
Max
15 15 15 15 15 15 10 +1 100
1 of 12
In the table below, indicate in the right-hand column the output produced by the statement in the left-hand column. If the statement produces more than one line of output, indicate the line breaks with slashes as in "a / b / c" to indicate three lines of output with "a" followed by "b" followed by "c". If the statement causes an error, fill in the right-hand column with the phrase "error" to indicate this.
Statement
var1.one(); var1.two(); var1.three(); var2.one(); var2.two(); var2.three(); var3.two(); var3.three(); var4.one(); ((Blue) var1).one(); ((Yellow) var1).two(); ((Red) var2).three(); ((Yellow) var2).two(); ((Green) var4).three(); ((Yellow) var4).two();
Output
________________________ ________________________ ________________________ ________________________ ________________________ ________________________ ________________________ ________________________ ________________________ ________________________ ________________________ ________________________ ________________________ ________________________ ________________________
2 of 12
Description private data of each ticket constructs a ticket with the given price, purchased the given number of days early, with no promotion code returns how many days early the ticket was bought returns the ticket's price returns the ticket's promotion code ("" if none) sets the ticket's promotion code to the given value (throws an exception if null is passed) returns a string representation of the ticket A
You are to define a new class called StudentTicket that extends Ticket through inheritance. StudentTicket should behave like a Ticket except for the following differences:
Student tickets are always bought by the campus ticket sales agency, two weeks (14 days) ahead of the event. Students always get a 1/2 price discount off the initial price of any ticket to any event. Honor students get an additional $5 off the price after the 1/2 discount, down to a minimum cost of $0 (free). Student tickets have special promotion codes. Any promotion code that is set on a student ticket should be modified to have the suffix " (student)" attached. For example, if the client sets the promotion code to "KEXP call-in winner", a student ticket should actually store "KEXP call-in winner (student)". Constructor/Method Description constructs a student ticket with the given base (non-discounted) price, possibly for an honor student returns true if ticket is for an honor student
You should provide the same methods as the superclass, as well as the following new behavior.
public StudentTicket(double price, boolean honors)
Note that some of the existing behaviors from Ticket should behave differently on StudentTicket objects, as described previously. You must also make StudentTicket objects comparable to each other using the Comparable interface. StudentTickets are compared by price, breaking ties by promotion code. In other words, a StudentTicket object with a lower price is considered to be "less than" one with a higher price. If two tickets have the same price, the one whose promotion code comes first in alphabetical order is considered "less." (You should compare the strings as-is and not alter their casing, spacing, etc.) If the two objects have the same price and promotion code, they are considered to be "equal." The majority of your grade comes from implementing the correct behavior. Part of your grade also comes from appropriately utilizing the behavior you have inherited from the superclass and not re-implementing behavior that already works properly in the superclass. (Write your answer on the next page.)
3 of 12
4 of 12
The call list.trimEnds(3); would change the list to store the following elements:
[40, 50, 60, 70, 80]
If we followed this by a second call of list.trimEnds(1); , the list would store the following elements:
[50, 60, 70]
If the list is not large enough to remove k elements from each side, throw an IllegalArgumentException. If the list contains exactly 2k elements, it should become empty as a result of the call. If k is 0 or negative, the list should remain unchanged. For full credit, obey the following restrictions in your solution: The method should run in no worse than O(N) time, where N is the length of the list. o (You can make more than one pass over the list, but you may not make k or N passes over it.) Do not call any methods of the linked list class to solve this problem. o (Note that the list does not have a size field, and you are not supposed to call its size method.) Do not use auxiliary data structures such as arrays, ArrayList, Queue, String, etc. Do not modify the data field of any nodes; you must solve the problem by changing the links between nodes. You may not create new ListNode objects, though you may create as many ListNode variables as you like.
Assume that you are using the LinkedIntList and ListNode class as defined in lecture and section:
public class LinkedIntList { private ListNode front; methods } public class ListNode { public int data; public ListNode next; // data stored in this node // a link to the next node in the list
public ListNode() { ... } public ListNode(int data) { ... } public ListNode(int data, ListNode next) { ... } }
5 of 12
6 of 12
Write the indexes of the elements that would be examined by the binary search (the mid values in our algorithm's code) and write the value that would be returned from the search. Assume that we are using the binary search algorithm shown in lecture and section. Indexes examined: Value Returned: ___________________________________________________________ __________________________
(b) Write the elements of the array below after each of the first 3 passes of the outermost loop of a selection sort.
int[] numbers = {22, 88, 44, 33, 77, 66, 11, 55}; selectionSort(numbers);
(c) Trace the complete execution of the merge sort algorithm when called on the array below, similarly to the example trace of merge sort shown in the lecture slides. Show the sub-arrays that are created by the algorithm and show the merging of sub-arrays into larger sorted arrays.
int[] numbers = {22, 88, 44, 33, 77, 66, 11, 55}; mergeSort(numbers);
7 of 12
(b) Write the elements of your above tree in the order they would be visited by each kind of traversal: Pre-order: In-order: Post-order: ____________________________________________________________________ ____________________________________________________________________ ____________________________________________________________________
8 of 12
Before Call
/ / +----+ | 80 | +----+ / \ / \ +----+ +----+ | 16 | | 21 | +----+ +----+ / / +----+ | 45 | +----+ +----+ | 67 | +----+ \ / / +----+ | 52 | +----+ \ \ +----+ | 99 | +----+
After Call
+----+ | 67 | +----+ \
If a tree has no nodes or is a leaf, it should not be affected by a call to your method. You may define private helper methods to solve this problem, but otherwise you may not call any other methods of the tree class nor create any data structures such as arrays, lists, etc. You should not construct any new node objects or change the data of any nodes. (The contents of the IntTree and IntTreeNode classes are shown on the next page for reference.)
9 of 12
The table below shows what the state of the tree would be if various calls were made: Call
tree.hasPath(67, tree.hasPath(80, tree.hasPath(67, tree.hasPath(16, tree.hasPath(52, tree.hasPath(99, tree.hasPath(80, tree.hasPath(67, tree.hasPath(-1, tree.hasPath(42, 99) 45) 67) 16) 99) 67) 99) 100) 45) 64)
Result
true true true true true false false false false false
Reason path exists 67 52 99 path exists 80 21 45 node exists with data of 67 node exists with data of 16 path exists 52 99 nodes do exist, but in wrong order nodes do exist, but there is no path from 80 to 99 end of 100 doesn't exist in the tree start of -1 doesn't exist in the tree start/end of -1 and 45 both don't exist in the tree
An empty tree does not contain any paths, so if the tree is empty, your method should return false. You should not assume that your tree is a binary search tree (BST); its elements could be stored in any order. You may define private helper methods to solve this problem, but otherwise you may not call any other methods of the class nor create any data structures (arrays, lists, etc.). You should not construct any new node objects or modify the tree in any way in your code. Recall the IntTree and IntTreeNode classes as shown in lecture and section:
public class IntTreeNode { public int data; public IntTreeNode left; public IntTreeNode right; constructors } // data stored in this node // reference to left subtree // reference to right subtree
10 of 12
11 of 12
X. Extra Credit
The TAs are the real "heroes" of this class. But what if your TA really were a superhero? Draw a picture of your TA as a superhero, and write or draw their hero name and super power. Make sure to put the TA's name somewhere in the picture so we know who it is. (This is not an art class, so any drawing that appears to reflect more than a moment's effort will get the point.)
12 of 12