Skip to content

Commit

Permalink
Implement CharSequence interface by ReversedSequence class. Add Rever…
Browse files Browse the repository at this point in the history
…sedSequence tests

Complete interfaces homework additional task.
  • Loading branch information
JackKaif committed Feb 5, 2024
1 parent 061d1d0 commit 35f01a2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
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
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());
}
}

0 comments on commit 35f01a2

Please sign in to comment.