{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

final-fa12-solutions

Implement this algorithm on the following page we

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

View Full Document Right Arrow Icon
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
Background image of page 8

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

View Full Document Right Arrow Icon
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
Background image of page 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
Background image of page 10

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

View Full Document Right Arrow Icon
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;
Background image of page 11
Image of page 12
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 Right Arrow Icon bookmark
Ask a homework question - tutors are online