Java programming 20 points in this problem you will

Info iconThis preview shows pages 7–9. Sign up to view the full content.

View Full Document Right Arrow Icon
3. Java Programming (20 points) In this problem, you will write a Java program that uses the Stack<A> abstraction from Problem 2. Consider the problem of matching well-nested brackets when reading characters from some data source. For example, you might want to determine whether each open parenthesis character ’(’ is later followed by a matching ’)’ , and similarly for ’{’ and ’}’ and ’[’ and ’]’ . Eclipse uses such an algorithm to check that a Java program doesn’t have syntax errors. One subtlety is that such brackets should not only match, but they should also be well-nested . The string "( [ ) ]" is not well-nested because the [ is closed by ) and not ] , as it should be. On the other hand, the string "()[()]" is well-nested. We have written the following test cases that illustrate many more examples of the desired behavior of this method, called BracketMatcher.matched . For the purposes of this problem, the matcher simply ignores non-bracket characters. Be sure you understand these tests before continuing. import static org.junit.Assert. * ; import; import; import org.junit.Test; public class BracketMatcherTest { private void testString( boolean matched, String s) { Reader r = new StringReader(s); assertEquals(matched, BracketMatcher.matched(r)); } @Test public void testMatchesEmpty() { testString( true , ""); } @Test public void testNoMatchOpen() { testString( false , "("); } @Test public void testNoMatchClose() { testString( false , ")"); } @Test public void testMatchOpenClose() { testString( true , "()"); } @Test public void testNoMatchOpenOpenClose() { testString( false , "(()"); } @Test public void testMatchOpenOpenCloseClose() { testString( true , "(())"); } 7
Background image of page 7

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

View Full Document Right Arrow Icon
@Test public void testMatchOpenOpenCloseCloseJunk() { testString( true , "(x(e)xxabxx)"); } @Test public void testMatchOpen1Open2Close2Close1() { testString( true , "([])"); } @Test public void testNoMatchOpen1Open2Close1Close2() { testString( false , "([)]"); } @Test public void testMatchProblemStatement() { testString( true , "()[()]"); } @Test public void testNoMatchOpen1Open2Close1Close2Junk() { testString( false , "(ax[asda)b]"); } @Test public void testMatchOpen3Open2Open1Close1Close2Close3() { testString( true , "{[()]}"); } } How do we implement such a matching algorithm? We read through the sequence of characters (provided by a Reader object as in the Spellchecking project). Each time we encounter a left bracket (like ’[’ ), we push it onto a stack. Each time we encounter a right-bracket (like ’]’ ), we pop the top of the stack and check to make sure that it matches. If we ever hit a mismatch, or if the stack is empty when it shouldn’t be, then the sequence isn’t well-nested. After reading all of the sequence, if the stack isn’t empty, the sequence isn’t well matched. Implement this algorithm on the following page. We have provided a couple utility methods that
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page7 / 17

Java Programming 20 points In this problem you will write a...

This preview shows document pages 7 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online