Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Editor2 implements Ed
- {
- private String str;
- private int slice;
- public Editor2( String _str, int index )
- {
- str = _str;
- slice = index;
- }
- public String toString()
- {
- return str.substring( 0, slice ) + "||" + str.substring( slice );
- }
- public String getBefore() { return str.substring( 0, slice ); }
- public String getAfter() { return str.substring( slice ); }
- // working example that needs error case to be finished
- public Ed rightArrow()
- {
- if (slice == str.length())
- return this;
- return new Editor2( str, slice + 1 );
- }
- public Ed leftArrow()
- {
- if (slice == 0)
- return this;
- return new Editor2( str, slice - 1 );
- }
- public Ed delete()
- {
- if (slice == str.length())
- return this;
- return new Editor2(str.substring(0, slice) + str.substring(slice + 1), slice);
- }
- public Ed backspace()
- {
- if (slice == 0)
- return this;
- return new Editor2( str.substring(0,slice-1) + str.substring(slice), slice - 1 );
- }
- public Ed insertString(String c) // Originally insert(char c), which is fine if you teach the char type
- {
- return new Editor2( str.substring(0,slice) + c + str.substring(slice), slice );
- }
- public Ed homeKey()
- {
- return new Editor2( str, 0 );
- }
- public Ed endKey()
- {
- return new Editor2( str, str.length() );
- }
- public static void main( String [] args )
- {
- Editor2 eddie = new Editor2( "bigdog", 3 );
- System.out.println( eddie + " right arrow = " + eddie.rightArrow() );
- System.out.println( eddie + " left arrow = " + eddie.leftArrow() );
- }
- }
Add Comment
Please, Sign In to add comment