Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help me with this coding assignment in Java: CANNOT USE ARRAYS or anything other while loops, for loops, if statements, etc. For this program, you

Help me with this coding assignment in Java:

CANNOT USE ARRAYS or anything other while loops, for loops, if statements, etc.

For this program, you are working with Pig Latin. The program is supposed to convert words or phrases entered by the user to Pig Latin.

You have been brought in to assess this code and to ensure a working program that successfully passes all unit testing is delivered to the client and/or determine that the code is correct and the program does indeed work (hint: It doesn't).

Program Specifications:

First, let's make sure you understand the rules of the Pig Latin language:

Words that begin with consonant sounds - All letters before the initial vowel are placed at the end of the word followed by the word "ay" which is added as a suffix at the end of the word. Here are a few examples:

hello -> ellohay

happy-> appyhay

bagel -> agelbay

Words that begin with consonant clusters (multiple consonants) - The whole cluster is added to the end of the word followed by the suffix "ay" like this:

child -> ildchay

snoopy -> oopysnay

Words that begin with vowels (a, e, i, o, u, y) - Words that start with vowels are handled in a similar fashion as above except that first character vowel is moved to the end of the word along with adding the suffix "way". Here are some examples:

open-> penoway

is -> siway

Words that have no vowels - Words that have no listed vowels (a, e, i, o, u, y) should be left alone.

Sample Run 1:

Enter the word or phrase to be converted. Type quit to end the program: Java <-- user enters word or phrase here Java in pigLatin is ... ==> AVAJAY Enter the word or phrase to be converted. Type quit to end the program: 

Sample Run 2:

Enter the word or phrase to be converted. Type quit to end the program: <-- user just hits enter key without any data Input phrase required. Enter the word or phrase to be converted. Type quit to end the program: 

Sample Run 3:

Enter the word or phrase to be converted. Type quit to end the program: I love Java I love Java in pigLatin is ... ==> IWAY OVELAY AVAJAY Enter the word or phrase to be converted. Type quit to end the program: 

Important: Keep in mind that you should not rewrite the entire program from scratch, nor just go about deleting entire methods and rewriting them from scratch (you will lose points if you do). Remember, your job is to modify the existing program to match specs using the same approach. You can add additional variables or small code segments as needed.

main() - The main method makes use of other methods as described below to get the input, perform the conversion and display the Pig Latin. Program execution should continue until the user types in the phrase "quit".

String getPhrase(Scanner keyboard) - Called from the main() method and accepts as a parameter, a scanner object. This method should prompt the user for the word/phrase to convert and then returns the string input by the user. Data Validation: Ensure that the length of the phrase entered is non-empty (i.e., no whitespace only input and no empty string input). If the phrase is not valid, you should display a validation error and re-prompt (see sample run above).

String convertToPigLatin(Scanner wordScanner) - Called from the main method and accepts a secondary scanner object that is pointing to that String (see tips below). This method gradually builds the Pig Latin string by going through the phrase word-by-word and making use of a helper method called convertWord(). Returns an uppercase string representing the fully converted Pig Latin for the phrase.

String convertWord(String word) - Called from within convertToPigLatin() and accepts a single word as a string parameter. This method converts the single word to Pig Latin and returns it to the convertToPigLatin() method. Makes use of a helper method, isVowel() to determine if a given character in the word is a vowel.

boolean isVowel(char ch) - Called from convertWord(), this boolean helper method accepts a single character and returns a boolean true if the character is a vowel (a, e, i, o, u, y) and false otherwise.

void displayPigLatin(String phrase, String pigLatin) - Called from main(), this method accepts the original string and the pig latin conversion and display the output.

Constants - Be sure to use an appropriate constant to represent the common keyword replacement for VOWELS as being equal to the string "AEIOUY". This will allow us to easily adjust our definition of vowels in the future to not include Y if needed.

Method length and line length - Make sure no method has more than 25 lines of code (comment lines and blank line do not count). Also make sure no line exceeds 100 characters in length, including lines with comments.

Tips:

Start Small - Rather than attempting to write a program that handles the entire phrase, just worry about being able to convert a single word to Pig Latin. Do not attempt to do multiple words until you know your program works for single words.

Build Method-by-Method and Test Often - Don't try to write a full program on the first sitting. Instead, write the getPhrase() method that reads in a valid string and returns it. Test it by calling it from your main method to ensure it works properly and matches the sample run prompt. Once you have that working, write the isVowel() method. Again, test it before moving onward. Then add the method to convertWord() and ensure that works properly. Once all this is working, then you can deal with multiple words (see tip below).

Handling Multiple Word Phrases - One way to handle this is realizing that you can actually define a second Scanner object that is pointing at the String phrase that was read into your program. This allows you to use the next() method to easily read word-by-word in the String phrase. This code would go in your convertToPigLatin() method and would look something like this near the top:

 // ****This would go somewhere in your main method **** //set a secondary scanner object to parse the string Scanner wordScanner = new Scanner(phrase); // call convertToPigLatin to do the dirty work String pigLatin = convertToPigLatin (wordScanner); 

 // ****This would go somewhere in your convertToPigLatin method **** //go through each word in the phrase and convert each word to Pig Latin for (int wordCount = 0; wordScanner.hasNext(); wordCount++) { //put some smart coding here

Provided Code:

import java.util.Scanner;

public class PigLatinTeacher { //This works in IntelliJ but not in Zybooks???!!!

private static final String VOWELS = "AEIOU"; //characters that are considered vowels

//********************************************** //main() - manager method for pig latin translator //********************************************** public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String phrase = ""; //holds user input phrase

//continue to get phrases to convert to pig latin until user quits

while (!phrase.equals("quit")) { phrase = getInput();

//make sure user didn't enter an empty string if (phrase.length() == 0) { System.out.println("Input phrase required"); } else { //convert the phrase String pigLatin = convertToPiglatin(phrase); displayPigLatin(pigLatin);

} } } public static void displayPigLatin(String phrase) { System.out.println(phrase + " in pigLatin is ... "); System.out.println("==> " + phrase.toUpperCase()); System.out.println(); }

public static String convertToPiglatin(String phrase) { String pigLatin = ""; //holds a converted word String convertedPhrase = ""; //holds a converted phrase Scanner wordScanner = new Scanner(phrase);

//get the next word and convert it to pigLatin String word = wordScanner.next(); pigLatin = convertWord(word, false);

//attach converted word to converted phrase convertedPhrase = convertedPhrase + " " + pigLatin;

return convertedPhrase; }

public static String convertWord(String word, boolean done) { String pigLatin = ""; //holds the converted word done = false; //sentinel for stopping loop int i = 0; //tracks position in string

//go through word character by character looking for first vowel while (!done) { String x = word.substring(i, 1); if (isVowel(x) == 1) { //found a vowel! pigLatin = word.substring(i) + " " + word.substring(0, i) + "AY"; done = true; } i++; //move to next character in word } return pigLatin; }

public static String getInput() { Scanner console = new Scanner(System.in);

//prompt for input phrase System.out.print("Enter the word or phrase to be converted. "); System.out.println("Type quit to end the program:");

String line = console.next(); //read input return line; }

//isVowel() - accepts the letter to be checked //and sees if that letter is a vowel (a,e,i,o,u,y) or not public static int isVowel(String x) {

//loop through all possible vowels and see if there's a match for (int i = 0; i <= VOWELS.length(); i++) if (VOWELS.contains(x)) { //yes, this is a vowel return 1; } return -1; //went through all vowels and determined this is not a vowel } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

1. Understand how verbal and nonverbal communication differ.

Answered: 1 week ago