final-fa12-solutions

# You may need to use the intvalue method that is

This preview shows pages 8–14. Sign up to view the full content.

You may need to use the intValue() method that is provided by the Integer class. It returns the int underlying a given Integer object. For example, ( new Integer(3)).intValue() evaluates to 3 . 8

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
import java.io.IOException; import java.io.Reader; public class BracketMatcher { // returns true if c is a legal left bracket private static boolean isLeftBracket( int c) { return c == ’(’ || c == ’{’ || c == ’[’; } // returns true if c is a legal right bracket private static boolean isRightBracket( int c) { return c == ’)’ || c == ’}’ || c == ’]’; } // returns true if l and r are a matched bracket pair private static boolean isBracketPair( int l, int r) { return (l == ’(’ && r == ’)’) || (l == ’{’ && r == ’}’) || (l == ’[’ && r == ’]’); } // Returns true if the sequence of brackets read from r form well - nested matching pairs . // Ignores non - bracket characters . public static boolean matched(Reader r) { if (r == null ) return false ; // OPTIONAL Stack<Integer> s = new LinkedStack<Integer>(); try { int c = r.read(); while (c != -1) { if (isLeftBracket(c)) s.push(c); if (isRightBracket(c)) { if (s.isEmpty()) { return false ; } else { int d = s.pop().intValue(); if (!isBracketPair(d, c)) return false ; } } c = r.read(); } } catch (IOException e) { return false ; } if (s.isEmpty()) return true ; return false ; } } 9
Rubric: Instantiating Stack (1 pt. typing, 1 pt. constructor) Reading chars: 1 pt. read before first check, 1 pt. update around loop While loop: 2 pts. correct condition Try/Catch: exists 1 pt., correct exception 1 pt. Dealing with left, right bracket conditions: 1 pt. Push: 2 pt. Empty case: 2 pt. pop: 2 pt., use of intValue() 1 pt. correct call to isBracketPair 2 pt. empty at the end – correct return: 2 pt. 10

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4. Abstract Stack Machine (10 points) Consider the following Java class (which is similar to the many linked datastructures we’ve seen in class): public class DNode { String name; DNode next; DNode prev; DNode(String name, DNode n, DNode p) { this .name = name; this .next = n; this .prev = p; } void foo() { ... } } Suppose that, for some inscrutable reason, you wanted to write a program to construct the follow- ing stack and heap configuration: DNode name “A” next null prev Stack Heap x DNode name “B” next prev Write a sequence of Java commands for the body of method foo that would create the ASM configuration shown above (ignore the workspace that would be saved on the stack by a call to foo() ). Note that there is only one variable on the stack. Rubric; 4 points for constructors: (1 points for both use of new DNode constructor, 1 point for correct arguments) 3 points for “wiring”: 1 point per pointer 3 points for stack shape: 1 point for type declaration, 2 points for only using variable x Two example solutions (there are many others): DNode x = new DNode("B", null , null ); x.next = x; x = new DNode("A", null , x); x.prev.prev = x; 11
or DNode x = new DNode("A", null , null ); x = new DNode("B", null , x); x.next = x; x.prev.prev = x; x = x.prev; 12

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
5. Java’s Type System (8 points) Consider the following class definitions: class S { } class T extends S { } class U extends S { } class V extends U { } For each of the questions below, circle all the correct answers—there may be zero, one, or more.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern