Question

This assignment is designed to give you practice with Javadoc, creating unit tests and using Junit....

This assignment is designed to give you practice with Javadoc, creating unit tests and using Junit.

Be sure to follow all style and documentation requirements for THIS class. See the style and documentation requirements posted on Canvas.

You are to name your package assign1 and your file Palindrome.java.

Palindrome Class

Palindrome

  • testString : String

+ Palindrome (String)

+ isPalindrome (): boolean

The method isPalindrome is to determine if a string is a palindrome. A palindrome, for this assignment, is defined as a word or phrase consisting of alphanumeric characters that reads the same frontwards and backwards while ignoring cases, punctuation and white space. If there are nt alphanumeric characters, it is considered a palindrome. The method should return true if it is a palindrome and false otherwise.

For this assignment, you are limited to the following Java library classes.

  • Scanner
  • String
  • Character

Here are the restrictions on this method. Up to 20% penalty if not followed.

  1. You may NOT return from the inside of a loop.
  2. You may NOT break from the inside of a loop.
  3. You may use ONLY ONE loop (either while or do-while).
  4. You may NOT copy the String to another String.
  5. You may NOT process the String more than one time (only make one pass through it).
  6. You must STOP processing as early as possible (when you find that it is or is not a palindrome). In other words, using a for loop is not a good solution.

Testing

  1. Generate the initial set of junit test cases. All should have the initial result of “fail”. This will create the file “PalindromeTest.java”. There is an example of creating junit tests in the PowerPoint slides with this assignment. This process may vary with your version of Java.

  1. Feel free to work with the teaching assistant to help with this process. In addition, there are a lot of tutorials online to help you. Here are a couple of sources I found.
    • https://www.youtube.com/watch?v=LPPji9JESgQ
    • https://www.vogella.com/tutorials/JUnit/article.html

  1. Complete the code for your test cases. Create new methods as needed for different tests. Your tests should not have any if statements, loops or output. Use the asserts to check the results. Test your code.

  1. Make sure that all the code meets all coding and documentation standards. The junit test cases should include internal documentation while the OrderedIntList.java file should contain external documentation.

Submitting

Using the link on Canvas, submit your Palindrome.java file and your PalindromeTest.java file for grading. These will be downloaded, compiled and graded as well as checked for plagiarism and similarity to other students’ work.

Grading

Creation of junit tests – only if the code compiles                 25%

Coding style and documentation – only if code compiles     20%

Results from tests – only if code compiles                             55%

Note: Your test file must be submitted but your code will be tested using other tests.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Palindrome.java

public class Palindrome {

      private String testString;

      public Palindrome(String testString) {

            // assigning testString in lower case if it is not null

            if (testString != null)

                  this.testString = testString.toLowerCase();

      }

      public boolean isPalindrome() {

            if (testString == null) {

                  // test string is null (not empty). it should be invalid

                  return false;

            }

            // assuming string is palindrome

            boolean isPal = true;

            // creating variables to store indices of first and last characters

            int frontIndex = 0, rearIndex = testString.length() - 1;

            char c1, c2; // to store characters

            // loops as long as isPal is true and front index is smaller than

            // rearIndex

            while (isPal && frontIndex < rearIndex) {

                  // getting characters at both indices

                  c1 = testString.charAt(frontIndex);

                  c2 = testString.charAt(rearIndex);

                  // if c1 is not alphanumeric, just incrementing front index

                  if (!Character.isLetterOrDigit(c1)) {

                        frontIndex++;

                  }

                  // if c2 is not alphanumeric, just decrementing rear index

                  if (!Character.isLetterOrDigit(c2)) {

                        rearIndex--;

                  }

                  // if both are alphanumeric, comparing them

                  if (Character.isLetterOrDigit(c1) && Character.isLetterOrDigit(c2)) {

                        if (c1 != c2) {

                              // both are different, the loop wont run for next time

                              isPal = false;

                        } else {

                              // incrementing front index, decrementing rear index

                              frontIndex++;

                              rearIndex--;

                        }

                  }

            }

            // returns the value of isPal

            return isPal;

      }

}

// PalindromeTest.java

import static org.junit.Assert.*;

import org.junit.Test;

public class PalindromeTest {

      @Test

      public void testNormal() {

            // testing Palindrome using simple alphabetical words

            Palindrome p = new Palindrome("Hannah");

            assertTrue(p.isPalindrome());

            p = new Palindrome("Java");

            assertFalse(p.isPalindrome());

            p = new Palindrome("maLayalam");

            assertTrue(p.isPalindrome());

      }

      @Test

      public void testSpecialSymbols() {

            // testing Palindrome using Strings containing special symbols and

            // spaces

            Palindrome p = new Palindrome("ha n%n&a()h//;;");

            assertTrue(p.isPalindrome());

            p = new Palindrome("r...ace cAr");

            assertTrue(p.isPalindrome());

      }

      @Test

      public void testDigits() {

            // testing Palindrome using Strings containing digits

            Palindrome p = new Palindrome("hannah990");

            assertFalse(p.isPalindrome());

            p = new Palindrome("racecar88188racecar");

            assertTrue(p.isPalindrome());

      }

      @Test

      public void testEmpty() {

            // testing Palindrome using Strings containing empty string

            Palindrome p = new Palindrome("");

            assertTrue(p.isPalindrome());

      }

      @Test

      public void testSentences() {

            // testing Palindrome using Strings containing long sentences

            Palindrome p = new Palindrome("Madam I'm Adam");

            assertTrue(p.isPalindrome());

            p = new Palindrome("Sit on a potato pan, Otis");

            assertTrue(p.isPalindrome());

            p = new Palindrome("Able was I, ere I saw Elba");

            assertTrue(p.isPalindrome());

            p = new Palindrome("Nope! This is not a palindrome");

            assertFalse(p.isPalindrome());

      }

}

/*OUTPUT of JUnit test*/


Runs: 5/5 Errors: 0 Failures: 0 palindrome.PalindromeTest [Runner: JUnit 4] (0.0 testNormal (0.000 s) testSpecialSymbols (0.0

Add a comment
Know the answer?
Add Answer to:
This assignment is designed to give you practice with Javadoc, creating unit tests and using Junit....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this assignment, you will create a program that tests a string to see if it...

    For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function: bool isPalindrome(string str, int lower, int upper) that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is...

  • For this activity you will create unit tests using JUnit and some of the available features...

    For this activity you will create unit tests using JUnit and some of the available features of JUnit adhering to the Arrange, Act, Assert (AAA) unit testing methodology. Specifications: Select two different parts of your course project to unit test. You will create two separate test class files in JUnit in the appropriate area. Each JUnit test class must include the following: At least two @Test methods. Appropriate assertions. One or more JUnit test class must include the following: @Before...

  • For this activity you will create unit tests using JUnit and some of the available features...

    For this activity you will create unit tests using JUnit and some of the available features of JUnit adhering to the Arrange, Act, Assert (AAA) unit testing methodology. Specifications: Select two different parts of your course project to unit test. You will create two separate test class files in JUnit in the appropriate area. Each JUnit test class must include the following: At least two @Test methods. Appropriate assertions. One or more JUnit test class must include the following: @Before...

  • Attached to this assignment as a separate document is the C++ code for a program that...

    Attached to this assignment as a separate document is the C++ code for a program that determines if a given string is a palindrome or not. A palindrome is a word that is spelled the same backward or forward. "bob" is an example of a palindrome. The program is sometimes a talking point during lecture, and may not fully adhere to the many bobisms I've been telling you about. In fact, it may not entirely work but that wasn't its...

  • his assignment will help the student by: Create shapes using Java code Using and creating colors...

    his assignment will help the student by: Create shapes using Java code Using and creating colors with Java Coding JFrames and using the Graphics g method Using Loops (to draw) Your program will generate a drawing using java. You should draw an object that makes sense, not just spare shapes and colors. You must use at least 3 different shapes You must use at least 2 different fonts You must use at least 2 predefined java colors and one custom-made...

  • PLEASE USE MATLAB This is a basic problem that illustrates the concept of strings. A palidrome...

    PLEASE USE MATLAB This is a basic problem that illustrates the concept of strings. A palidrome is a string of characters that is read the same forwards as it is backwards. For example, racecar' is a palindrome; reversing the order of the characters leaves the string unchanged. Write a otherwise. function called isPalindrome that accepts one input and returns one output. The input str is a string of characters, and the output is 1 if str is a palindrome, For...

  • Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement...

    Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement two recursive methods and write JUnit tests for each one. You may write all three methods in the same class file. You are required to write Javadoc-style documentation for all of your methods, including the test methods. Procedure 1) Write method!! a recursive method to compare two Strings using alphabetical order as the natural order (case insensitive, DO NOT use the String class built-in...

  • The code: def isPalindrome(text): """ >>> isPalindrome("alula") True...

    The code: def isPalindrome(text): """ >>> isPalindrome("alula") True >>> isPalindrome("love") False >>> isPalindrome("Madam") True >>> isPalindrome(12.5) False >>> isPalindrome(12.21) False >>> isPalindrome("Cigar? Toss it in a can.! It is so tragic.") True >>> isPalindrome("travel.. a town in Alaska") False """ # --- YOU CODE STARTS HERE if type(text) is not str: return False l = len(text) - 1 i = 0 while i < l: if text[i] in string.punctuation or text[i] == ' ': i += 1 elif text[l] in...

  • This lab will give you a practice with both queue and stack ADTs. In this work,...

    This lab will give you a practice with both queue and stack ADTs. In this work, you are to determine if an input string is a palindrome. A string of characters is a palindrome if and only if it reads the same forward and backward. Examples: eye, abba, civic, radar, and so on. Sample Output: Please enter a string of characters: abba The given string is a palindrome. Want to examine another string? (y/n): y Please enter a string of...

  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT