{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

final-fa12-solutions

# Implement this algorithm on the following page we

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

Implement this algorithm on the following page. We have provided a couple utility methods that may be of use. Your code should pass all of the tests above and it should never raise any exceptions. For your convenience, the documentation of the read() method of the Reader class is found in the Appendix — you should not need to use any other methods from the reader class. 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 == ’]’); } // 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 ﬁrst 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 conﬁguration: 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 conﬁguration 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;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page8 / 17

Implement this algorithm on the following page We have...

This preview shows document pages 8 - 12. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online