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.

  1. Create a new Eclipse project by copying ProjectTemplate. Name the new project SequencePalindrome.
  2. Open the src folder of this project and then open (default package). As a starting point you can use any of the Java files. Rename it SequencePalindrome and delete the other files from the project.
  3. 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.
  4. Finally in Eclipse, open the SequencePalindrome.java file; 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

  1. Take a look at the provided code skeleton and make sure you understand it.
  2. Complete the body of the isPalindrome static method. In an effort to channel your solutions in the directions we intend, we insist you obey the following restriction. While you may use method entry, do not use any other method that is introduced in the enhanced interface Sequence. Among the methods still permitted for your use are all those inherited by or introduced in SequenceKernel, including add, remove, and length.
  3. Run the program and test your implementation of isPalindrome.
  4. 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).
  5. Run the program and test your second implementation of isPalindrome.

Additional Activities

  1. Copy SequencePalindrome.java into a new file in the same project and name it StackPalindrome.java. Update the code so that it uses the type Stack<Integer> instead of Sequence<Integer> and implement two versions (one recursive and one not) of isPalindrome.
  2. Copy SequencePalindrome.java into a new file in the same project and name it QueuePalindrome.java. Update the code so that it uses the type Queue<Integer> instead of Sequence<Integer> and implement two versions (one recursive and one not) of isPalindrome.