Challenge: PigLatin

Develop an application for a simple game PigLatin using TDD with Java.

Task and requirements

You have to implement a Pig Latin converter in Java. Pig Latin is a language game where words are altered according to a simple set of rules. The challenge is to build a Pig Latin converter that transforms a given word into Pig Latin based on these rules. 

Your converter should be capable of handling single words and should follow the following rules:

  1. If a word starts with a vowel (a, e, i, o, or u), add "yay" to the end of the word.

  2. If a word starts with a consonant, move all the consonants at the beginning of the word to the end, then add "ay."

  3. Maintain the case (capitalization) of the original word.

Follow the TDD cycle by writing test cases first and then implementing the function. Write multiple test cases to cover different scenarios, including various combinations of numbers.

PigLatinConverter Class: Create a class named PigLatinConverter with the following methods:

  • String convertToPigLatin(String word): This method should take a word as input and return the word converted to Pig Latin based on the rules mentioned above.

  • Write a set of comprehensive test cases using JUnit to validate the Pig Latin converter's correctness. Your tests should cover various scenarios, including words starting with vowels, consonants, and edge cases.

Here are some sample input and output pairs to help you get started with writing your tests:

  • Input: "grape" => Output: "apegray"

  • Input: "Apple" => Output: "Applehay"

  • Input: "world" => Output: "orldway"

  • Input: "algorithm" => Output: "algorithmhay"

  • Input: "Pig" => Output: "Igpay"

  • Input: "Java" => Output: "Avajay"

  • Input: "8ball" => Output: "8ball"

Rules

Below are the constraints to consider when solving the challenge.

  • The input word consists of letters and might include numbers or special characters, which should not be transformed.

  • The PigLatin converter should preserve the case of the original word (e.g., "Pig" should become "Igpay," not "igpay").

  • Handle both uppercase and lowercase letters.

Now, you can follow the TDD process by starting with simple test cases and incrementally building your PigLatin converter to meet these requirements.

Try it yourself

Add or modify the code in the widget below to implement your solution.

Get hands-on with 1200+ tech skills courses.