Problem Statement
Printing a sequence of characters on a piece of paper could be faster if we
were willing to write every other line from right to left. That way, we could
avoid the "horizontal retrace" at the end of each line, simply moving down one
spot and continuing to print horizontally in the opposite direction.
Create a class Serpentine that contains a method column that is given a String s and the width of the paper. It is also given the 0-based index of a column. It returns the contents of the indicated column that results from printing s using the above plan, printing the first row from left to right and alternating the direction of successive rows.
It should return the column as a String in top to bottom order.
Definition
- Class:
- Serpentine
- Method:
- column
- Parameters:
- String, int, int
- Returns:
- String
- Method signature:
- String column(String s, int width, int index)
- (be sure your method is public)
Constraints
- s has length between 1 and 50 inclusive
- s contains only uppercase letters 'A'-'Z'
- width is between 1 and 50 inclusive
- index is between 0 and width-1 inclusive
Examples
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8
0
Returns: "APQ"
ABCDEFGH PONMLKJI QRSTUVWX ZY index 0 refers to the first column. Note that the returned String must not contain a trailing space."ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8
6
Returns: "GJWZ"
"ABCDEFG"
10
8
Returns: ""