












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
CIS 120 — Programming Languages and Techniques. Final Exam ... A sample ASM (from lecture 28) ... Create at least four new test cases with examples of.
Typology: Summaries
1 / 20
This page cannot be seen from the preview
Don't miss anything!













class A { private int x; public A (int x0) { x = x0; } public int getX () { return x; } } class B extends A { private int x1; private int y; public B (int x0, int y0) { super (x0 + 1); x1 = x0; y = y0; } public int getX() { return x1; } public int getY() { return y; } }
(a) (12 points) For each of the following variable declarations and initializations, list the static type of the variable and the dynamic class of the object referred to by that variable. If the initialization is invalid, write “INVALID” in both blanks. Assume that these statements occur in order, and that INVALID initializations do not prevent subsequent lines from referring to the variable.
A a1 = new A(3); Static: A Dynamic: A B b1 = a1; INVALID: B INVALID: A B b2 = new B(3,3); Static: B Dynamic: B A a2 = b2; Static: A Dynamic: B A a3 = new Object(); INVALID: A INVALID: Object Object o1 = new B(2,3); Static: Object Dynamic: B
Grading scheme: 1 point per blank. If they misread and put the correct static type for the two INVALIDs, do not count off.
(c) (5 points) Briefly define the terms “static type” and “dynamic class” in Java. How are they related?
Answer: The dynamic class of an object is the class that the object was created from. The static type of a variable or expression is the type declared by the programmer or calculated by the type checker. The static type of a variable is a supertype of the dynamic classes of every object that can be stored in that variable. Grading scheme: 2 points each for definition of static type and dynamic class. 1 point for saying that static type is a supertype of the dynamic class.
(d) (5 points) Briefly describe how the ASM model determines what code to put on the workspace at a method call. Answer: The Java ASM uses dynamic dispatch for method calls. Dynamic dispatch means that the code (e.g. method body) that is executed depends on the dynamic class of the object. The dynamic class is stored in the heap as part of the object. Methods are stored in the class table, if the method is not in the object’s class, then the ASM looks in the superclasses. Grading scheme: 2 points for stating that the code executed depends on the dynamic class of the object. 1 point for saying dynamic class is stored in the heap as part of the object. 1 point for saying that methods are retrieved from the class table. 1 point for describing method lookup.
(a) (5 points) Recall the OCaml function map:
let rec map (f : ’a -> ’b) (l : ’a list) : ’b list = begin match l with | [] -> [] | (x :: xs) -> (f x) :: map f xs end
What is the value of the following OCaml expression?
map (fun (x : int) -> (x,x+1)) [1;3;5]
Answer : [(1,2);(3,4);(5,6)] Grading scheme: 5 points total.
(b) (5 points) Now consider this OCaml function:
let rec foo (g : ’a -> ’a) (x : ’a) (n : int) : ’a = if n <= 0 then x else foo g (g x) (n-1)
What is the value of the following OCaml expression?
foo (fun (x : string list) -> "a" :: x) ["b"] 2
Answer : ["a"; "a"; "b"] grading scheme: 5 points total.
(b) let rec in_t (t : ’a tree) : ’a list = begin match t with | Empty -> [] | Node (t1,a,t2) -> (in_t t1) @ [a] @ (in_t t2) end in in_t example_tree
Circle the result of this program.
[1; 2; 3; 4; 5; 6; 7]
[4; 2; 1; 3; 6; 5; 7]
[7; 6; 5; 4; 3; 2; 1]
[1; 3; 2; 5; 7; 6; 4]
(c) let rec post_t (t : ’a tree) : ’a list = begin match t with | Empty -> [] | Node (t1,a,t2) -> (post_t t1) @ (post_t t2) @ [a] end in post_t example_tree
Circle the result of this program.
[1; 2; 3; 4; 5; 6; 7]
[4; 2; 1; 3; 6; 5; 7]
[7; 6; 5; 4; 3; 2; 1]
[1; 3; 2; 5; 7; 6; 4]
(d) let rec no_t (t : ’a tree) : ’a list = begin match t with | Empty -> [] | Node (t1,a,t2) -> (no_t t1) @ (no_t t2) end in no_t example_tree
Write the result of this program. Answer: []
(a) (0 points) Step 1 is understanding the problem. You don’t have to write anything for this part—your answers below will demonstrate whether or not you succeeded with Step 1.
(b) (4 points) Step 2 is formalizing the interface. The first part of formalizing the interface is to understand the interfaces of the classes that this method should interact with. The argument to isAlmostCorrect should be an instance of the class Word, which represents sequences of letters. This class has the following interface:
public class Word { // Construct a word from the given string. If the argument string // contains nonletter characters (such as spaces or punctuation), // this constructor throws an InvalidArgumentException public Word (String w) { ... }
// Return the number of letters in the word public int length() { ... }
// Return a new word that exchanges the positions of the characters // at indices i and i+1. // For example, new Word("cta").swapAt(1) returns the word "cat" // // throws IllegalArgumentException when i is out of range // (i.e. not 0 <= i < length()-1 ) public Word swapAt(int i) { ... }
// Compare two words ignoring their case public boolean equals(Object o) { ... }
// Determine the ordering between words, ignoring their case public int compareTo(Object o) { ... } }
The method isAlmostCorrect should be a member of the class Dictionary. This class contains a set of correct words, a constructor, and isAlmostCorrect. The declarations for these class members are at the top of the next page.
(d) (12 points) Step 4 is implementing the program. Implement the isAlmostCorrect method to complete the design. For reference, documentation for the Java Collections class TreeSet appears in the Appendix. Answer:
boolean isAlmostCorrect(Word word) { if (word == null) { throw new InvalidArgumentException(); } if (dict.contains(word)) { return true; } for (int i=0; i < word.length()-1; i++) { Word n = word.swapAt (i); if (dict.contains(n)) { return true; } } return false; }
Grading scheme: 1 point per part unless otherwise stated
Ignore any attempts to to handle capitalization.
Linked data structures The next few questions concern an implementation of mutable queues in Java. The classes Queue, QueueNode, and EmptyQueueException are shown in the Appendix. This implementation is a slight variant of the code that we discussed in class and lecture, so read through the classes now before continuing on to the problems below.
(a) Queue
prints a prints b does nothing
throws NullPointerException throws EmptyQueueException
(b) Queue
prints a prints b does nothing
throws NullPointerException throws EmptyQueueException
(c) Queue
prints a prints b does nothing
throws NullPointerException throws EmptyQueueException
(a) (8 points) Briefly describe four test cases for the insert method. You don’t have to give the complete code for these test cases, just describe what they should test in words. Answer: potential tests include
Grading scheme, 1 point each
(b) (14 points) Now implement the insert method.
public void insert(int n, E v) {
if (head == null || n <= 0) { head = new QueueNode
for(int i = 1; i < n && cursor.next != null; i++) { cursor = cursor.next; }
cursor.next = new QueueNode
if (tail == cursor) { tail = cursor.next; } }
Grading scheme: 1 point for each item
Starred items may be replaced with enq in some solutions.
class java.util.TreeSet
boolean add(E e) Adds the specified element to this set if it is not already present. Returns true if the set did not already contain the specified element.
boolean contains(Object o) Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null? e==null : o.equals(e)).
boolean isEmpty() Returns true if this set contains no elements.
int size() Returns the number of elements in this set.
boolean remove(Object o) Removes the specified element from this set if it is present. Returns true if the set contained the specified element.
The logic of these classes is the same as the code presented in class. The differences are (a) there is no division between the Queue interface and the QueueImpl implementation (b) deq throws an exception when called on an empty queue (c) fields in QueueNode are public, there are no getter or setter methods.
/** Nodes in the linked list */ class QueueNode
public QueueNode(E v0, QueueNode
/** Thrown when deq invoked on an empty queue. */ class EmptyQueueException extends RuntimeException { }
class Queue
/** A reference to the first node in the queue */ private QueueNode
/** Create an empty queue */ public Queue() { head = null; tail = null; }
/** isEmpty() is true when the list has no elements */ public boolean isEmpty() { return head == null; }
public void enq(E x) { QueueNode
Most of the method implementations are not shown, and are replaced with....
public interface Shape { public void draw(Graphics2D gc); }
public class PointShape implements Shape { ... } public class LineShape implements Shape { ... }
public class Paint { /** Area of the screen used for drawing */ private Canvas canvas;
/** Current drawing color */ private Color color = Color.BLACK;
/** Strokes for drawing shapes with thin/thick lines */ public final static Stroke thinStroke = new BasicStroke(1); public final static Stroke thickStroke = new BasicStroke(3);
/** Current drawing thickness */ private Stroke stroke = thinStroke;
/** Available modes for user input */ public enum Mode { PointMode, LineStartMode, LineEndMode, }
/** Current drawing mode */ private Mode mode = null;
/** Location of the mouse when it was last pressed. */ private Point modePoint;
/** The list of shapes that will be drawn on the canvas. */ private LinkedList
/** an optional shape for preview mode */ private Shape preview;
private class Canvas extends JPanel { public Canvas(){ ... { public void paintComponent(Graphics gc) { ... } public Dimension getPreferredSize() { ... } }
/** Code to execute when the button is pressed while the mouse
/** Create the toolbar containing radio buttons for the input modes as well as the thickness checkbox, Undo and Quit buttons */ private JPanel createModeToolbar(final JFrame frame) { ... }
/** Create the color toolbar */ private JPanel createColorToolbar() { ... }
/** Creates the main application window, assembles the user interface and displays it. */ public Paint () { final JFrame frame = new JFrame(); frame.setLayout(new BorderLayout());
canvas = new Canvas(); Mouse mouseListener = new Mouse(); canvas.addMouseMotionListener(mouseListener); // dragged events canvas.addMouseListener(mouseListener); // press/release events frame.add(canvas, BorderLayout.CENTER);
JPanel pane = new JPanel(); pane.setLayout(new GridLayout(2,1)); frame.add(pane, BorderLayout.PAGE_END); pane.add(createModeToolbar(frame)); pane.add(createColorToolbar());
frame.pack(); frame.setVisible(true); }
/** Entry point for the Paint application. */ public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable() { public void run() { new Paint(); } }); }
}