Complete the body of the isPalindrome static method using only the SequenceKerne
ID: 667195 • Letter: C
Question
Complete the body of the isPalindrome static method using only the SequenceKernel methods (add, remove, and length).
Run the program and test your implementation of isPalindrome.
Once your first implementation works, provide an alternative implementation that is recursive if your first implementation was not recursive, or make your second implementation not recursive if the first one was recursive. You can just comment out the code of your first implementation (select the code to comment out and press CTRL-/, i.e., the Control key and the '/' key at the same time).
Run the program and test your second implementation of isPalindrome.
Explanation / Answer
import components.sequence.Sequence; import components.sequence.Sequence1L; import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; public final class SequencePalindrome { /** * Private constructor so this utility class cannot be instantiated. */ private SequencePalindrome() { } /** * Construct and return a sequence from a given array. * * @param args * the array of integer * @return the sequence of integer * @ensures createFromArgs = [the sequence of integers in args] */ private static Sequence createFromArgs(int[] args) { assert args != null : "Violation of: args is not null"; Sequence s = new Sequence1L(); for (int x : args) { s.add(s.length(), x); } return s; } /** * Checks if a given {@code Sequence} is a palindrome. * * @param s * the {@code Sequence} to check * @return true if the given {@code Sequence} is a palindrome, false * otherwise * @ensures isPalindrome = (s = rev(s)) */ private static boolean isPalindrome(Sequence s) { assert s != null : "Violation of: s is not null"; if (s.length()Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.