Lab 3 Solution


Category: Tag:


Section Readings: 1.3, 1.4

Problem 1. (Matching Parentheses) Implement the static method match() in Parentheses that takes a string s as argument and uses a stack to determine whether its parenthe-ses are properly balanced, and returns true if they are and false otherwise. You may assume that s only consists of parentheses (curly, square, and round).

  • java Parentheses [()]{}{[()()]()} <ctrl-d>


  • java Parentheses [(])

<ctrl-d> false

Problem 2. (Kth String from the End) Write a Queue client KthString that takes a command-line argument k and prints the kth string from the end found on standard input, assuming that standard input has k or more strings.

  • java KthString 42 < numbers.txt 236

Problem 3. (Min Max ) Implement the static methods min() and max() in MinMax that take a reference first to the rst node in a linked list of integer-valued items as argument and returns the minimum and the maximum values respectively.

  • java MinMax < numbers.txt -289 700

Problem 4. (Equal Pairs) Implement the static method equalPairs() in EqualPairs that takes an array numbers of integer values and returns the number of equal pairs, calculated using a linearithmic algorithm. Hint: Use Arrays.sort() and the fact that the number of ways to select 2 items out of n is n2 = n(n 1)=2.

  • java EqualPairs

1212234333 <ctrl-d>


  • java EqualPairs

1212234333077777 <ctrl-d>


Spring 2015 1 of 2

CS210 Lab 3 Swami Iyer

Files to Submit:





Spring 2015 2 of 2

error: Content is protected !!