-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement CharSequence interface by ReversedSequence class. Add Rever…
…sedSequence tests Complete interfaces homework additional task.
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
java-oop-ru/interfaces/src/main/java/exercise/ReversedSequence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,31 @@ | ||
package exercise; | ||
|
||
// BEGIN | ||
public class ReversedSequence implements CharSequence{ | ||
private String sequence; | ||
public ReversedSequence(String sequence) { | ||
var reversedSequence = new StringBuilder(sequence); | ||
this.sequence = reversedSequence.reverse().toString(); | ||
} | ||
|
||
@Override | ||
public int length() { | ||
return this.sequence.length(); | ||
} | ||
|
||
@Override | ||
public char charAt(int index) { | ||
return this.sequence.toCharArray()[index]; | ||
} | ||
|
||
@Override | ||
public CharSequence subSequence(int start, int end) { | ||
return this.sequence.substring(start, end); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.sequence; | ||
} | ||
} | ||
// END |
17 changes: 17 additions & 0 deletions
17
java-oop-ru/interfaces/src/test/java/exercise/ReversedSequenceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package exercise; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ReversedSequenceTest { | ||
CharSequence sequence = new ReversedSequence("abcdef"); | ||
|
||
@Test | ||
public void testReversedSequence() { | ||
assertEquals("fedcba", sequence.toString()); | ||
assertEquals('e', sequence.charAt(1)); | ||
assertEquals(6, sequence.length()); | ||
assertEquals("edc", sequence.subSequence(1, 4).toString()); | ||
} | ||
} |