Lab: Sequence Palindromes
Objective
In this first lab you will review writing a static method by
implementing the isPalindrome method that determines whether a given
Sequence<Integer> is a palindrome.
Setup
Follow these steps to set up a project for this lab.
- Create a new Eclipse project by copying
ProjectTemplate. Name the new projectSequencePalindrome. - Open the
srcfolder of this project and then open(default package). As a starting point you can use any of the Java files. Rename itSequencePalindromeand delete the other files from the project. - Follow the link to
SequencePalindrome.java, select all the code on that page (click and hold the left mouse button at the start of the program and drag the mouse to the end of the program) and copy it to the clipboard (right-click the mouse on the selection and choose Copy from the contextual pop-up menu), then come back to this page and continue with these instructions. - Finally in Eclipse, open the
SequencePalindrome.javafile; select all the code in the editor, right-click on it and select Paste from the contextual pop-up menu to replace the existing code with the code you copied in the previous step. Save your file.
Method
- Take a look at the provided code skeleton and make sure you understand it.
- Complete the body of the
isPalindromestatic method. In an effort to channel your solutions in the directions we intend, we insist you obey the following restriction. While you may use methodentry, do not use any other method that is introduced in the enhanced interfaceSequence. Among the methods still permitted for your use are all those inherited by or introduced inSequenceKernel, includingadd,remove, andlength. - 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.
Additional Activities
- Copy
SequencePalindrome.javainto a new file in the same project and name itStackPalindrome.java. Update the code so that it uses the typeStack<Integer>instead ofSequence<Integer>and implement two versions (one recursive and one not) ofisPalindrome. - Copy
SequencePalindrome.javainto a new file in the same project and name itQueuePalindrome.java. Update the code so that it uses the typeQueue<Integer>instead ofSequence<Integer>and implement two versions (one recursive and one not) ofisPalindrome.