Interface Text
-
public interface Text
An editable sequence of characters with a current "cursor" position. The character sequence can be any length. The cursor can be at any character in the sequence, OR at the end. When the cursor is at the end, there is no character to get() or delete(), but insert() will add a new character at the end of the sequence. There should be a no-argument constructor which creates an initially-empty sequence of text, with the cursor at the end. All operations should be O(1) worst-case or O(1) amortized time.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description boolean
canMoveLeft()
Returns whether there is another character to the left of the cursor.boolean
canMoveRight()
Returns whether the cursor is NOT at the end.void
delete()
Deletes the character at the current cursor position.char
get()
Returns the character at the current cursor position.void
insert(char c)
Inserts a new character before the current cursor position.void
moveLeft()
Moves the cursor one character to the left.void
moveRight()
Moves the cursor one character to the right.void
print()
Displays the current sequence of characters one one line, with the cursor underneath.
-
-
-
Method Detail
-
get
char get() throws java.util.NoSuchElementException
Returns the character at the current cursor position.- Throws:
java.util.NoSuchElementException
-
insert
void insert(char c)
Inserts a new character before the current cursor position.
-
delete
void delete() throws java.util.NoSuchElementException
Deletes the character at the current cursor position. The cursor should move to the right of what was just deleted.- Throws:
java.util.NoSuchElementException
- if the cursor is at the end.
-
canMoveLeft
boolean canMoveLeft()
Returns whether there is another character to the left of the cursor.
-
moveLeft
void moveLeft() throws java.util.NoSuchElementException
Moves the cursor one character to the left.- Throws:
java.util.NoSuchElementException
- if the cursor is already at the beginning.
-
canMoveRight
boolean canMoveRight()
Returns whether the cursor is NOT at the end.
-
moveRight
void moveRight() throws java.util.NoSuchElementException
Moves the cursor one character to the right.- Throws:
java.util.NoSuchElementException
- if the cursor is already at the end.
-
print
void print()
Displays the current sequence of characters one one line, with the cursor underneath. Two lines should always be printed to System.out. For example, if the current characters are a, b, c, d, and the cursor is at the end, we should see: abcd ^ With the same characters, but the cursor under the 'b', we would see: abcd ^
-
-