Question

String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line wh...

String variables/Selection & loop

Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline(), converts the string into Ubbi dubbi and displays the translated sentence. Ubbi dubbi works by adding ub before each vowel sound in a syllable. For example, hello becomes hubellubo. The word speak has the vowel sound ea, so in Ubbi dubbi it becomes spubeak.

Modified Ubbi dubbi rules for this assignment

1. The vowels are a, e, i, o and u.

2. In the case that a word has 2 vowels following one another we only place ub in front of the 1st vowel. So zoom would be zuboom and not zuboubom. Similarly, steak would be stubeak and not stubeubak.

3. For words that are 1 character or 2 character long we add ub in front of each character whether they are vowels or consonants. (Example: I am becomes ubi ubaubm)

4. If e is the last character of the word, we don’t add ub in front of it. For example, the word one is translated as ubone and not ubonube.

5. Any other characters (punctuations, digits) are treated as non-vowels.

Assumptions

For this assignment you can assume that

1. There is exactly 1 space between words.

2. The entered string does not start nor end with a space.

3. All words are in lower case letters.

4. The sentence has at least one non-blank character

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

import java.util.Scanner;

public class UbbiDubbi {
  
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine().toLowerCase().trim();
  
String [] words = sentence.split(" ");
String newWord = "", newSen = "";
  
for(String word : words)
{
// if word length is 1 or 2
if(word.length() == 1 || word.length() == 2)
{
if(word.length() == 1)
{
newWord = "ub" + word;
}
else if(word.length() == 2)
{
newWord = "ub" + word.charAt(0) + "ub" + word.charAt(1);
}
}
// if first and last character of the word is a vowel
else if((word.charAt(0) == 'a' || word.charAt(0) == 'e' || word.charAt(0) == 'i'
|| word.charAt(0) == 'o' || word.charAt(0) == 'u')
&&
(word.charAt(word.length() - 1) == 'a' || word.charAt(word.length() - 1) == 'e' ||
word.charAt(word.length() - 1) == 'i' || word.charAt(word.length() - 1) == 'o' ||
word.charAt(word.length() - 1) == 'u')
)
{
newWord = "ub" + word;
}
// if only the last character of the word is a vowel
else if(word.charAt(word.length() - 1) == 'a' || word.charAt(word.length() - 1) == 'e' ||
word.charAt(word.length() - 1) == 'i' || word.charAt(word.length() - 1) == 'o' ||
word.charAt(word.length() - 1) == 'u')
{
for(int i = 0; i < word.length(); i++)
{
if(word.charAt(i) == 'a' || word.charAt(i) == 'e' || word.charAt(i) == 'i' || word.charAt(i) == 'o' ||
word.charAt(i) == 'u')
{
newWord = word.substring(0, i) + "ub" + word.substring(i);
break;
}
}
}
else
{
for(int i = 0; i < word.length(); i++)
{
// if there are 2 consecutive vowels in the word
if((word.charAt(i) == 'a' || word.charAt(i) == 'e' || word.charAt(i) == 'i' || word.charAt(i) == 'o' ||
word.charAt(i) == 'u')
&&
(word.charAt(i + 1) == 'a' || word.charAt(i + 1) == 'e' || word.charAt(i + 1) == 'i' ||
word.charAt(i + 1) == 'o' || word.charAt(i + 1) == 'u'))
{
newWord = word.substring(0, i) + "ub" + word.substring(i);
break;
}
else
{
newWord = word.replaceAll("(?i)([aeiou])", "ub$1");
}
}
}
newSen += newWord + " ";
}
System.out.println("New sentence: " + newSen.trim() + "\n");
}
}

********************************************************************* SCREENSHOT ******************************************************

Output 1:

run Enter a sentence: I am speaking New sentence: ubi ubaubm spubeaking BUILD SUCCESSFUL (total time: 6 seconds)

Output 2:

run: Enter a sentence: Please zoom on it! New sentence: plubease zuboom uboubn ubit! BUILD SUCCESSFUL (total time: 5 seconds)

Output 3:

run Enter a sentence: I just love steak New sentence: ubi jubust lubove stubeak BUILD SUCCESSFUL (total time: 11 seconds)

Output 4:

run: Enter a sentence: Onee rose New sentence: ubone rubose BUILD SUCCESSFUL (total time: 5 seconds)

Add a comment
Know the answer?
Add Answer to:
String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line wh...
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
  • In Java please Only use methods in the purpose. Thank you The purpose of this assignment is to help you learn Java iden...

    In Java please Only use methods in the purpose. Thank you The purpose of this assignment is to help you learn Java identifiers, assignments, input/output nested if and if/else statements, switch statements and non-nested loops. Purpose Question 2-String variables/Selection & loops. (8.5 points) Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline), converts the string into Ubbi...

  • ***LOOPS NOT ALLOWED*** String variables and use of String methods: Write a complete Java program which...

    ***LOOPS NOT ALLOWED*** String variables and use of String methods: Write a complete Java program which prompts the user for a string with 3 words each separated by a single space and reads the line into one String variable using nextline(). Extract each word and add them to a new String variable in reverse order with a space between the words. Have the 1st letter of the new string in upper case and all other strings in lower case letter....

  • Use C++ language, keep it simple Exercise #2: Strings' operations Write a C++ program that prompts...

    Use C++ language, keep it simple Exercise #2: Strings' operations Write a C++ program that prompts the user to enter a sentence, the program then does the following: 1. Prints the total characters in the sentence. 2. Prints the word count of the sentence, assuming only one space between each tow words. 3. Prints total vowel (a, e, i, o, u) in the sentence. 4. Prints the vowel frequency as shown in the sample below. 5. Prints the sentence in...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • Program Description: A Java program is to be created to produce Morse code. The Morse code...

    Program Description: A Java program is to be created to produce Morse code. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the...

    JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the input message so that it’s easier to work with. Write a method called normalizeText which does the following: Removes all the spaces from your text Remove any punctuation (. , : ; ’ ” ! ? ( ) ) Turn all lower-case letters into upper-case letters Return the result. The call normalizeText(“This is some \“really\” great. (Text)!?”) should return “THISISSOMEREALLYGREATTEXT” Part 2 - Obfuscation...

  • In this assignment you’ll implement a data structure called a trie, which is used to answer...

    In this assignment you’ll implement a data structure called a trie, which is used to answer queries regarding the characteristics of a text file (e.g., frequency of a given word). This write-up introduces the concept of a trie, specifies the API you’re expected to implement, and outlines submission instructions as well as the grading rubric. Please carefully read the entire write-up before you begin coding your submission. Tries A trie is an example of a tree data structure that compactly...

  • Python program This assignment requires you to write a single large program. I have broken it...

    Python program This assignment requires you to write a single large program. I have broken it into two parts below as a suggestion for how to approach writing the code. Please turn in one program file. Sentiment Analysis is a Big Data problem which seeks to determine the general attitude of a writer given some text they have written. For instance, we would like to have a program that could look at the text "The film was a breath of...

  • Write a French/English dictionary lookup program. Read a list of pairs of English and French words...

    Write a French/English dictionary lookup program. Read a list of pairs of English and French words from a file specified by the user. English/French words should be exact matches (don't try to find partial matches). Use the supplied EnglishFrenchDictionary.java class as your main class. Fill in the missing code in the DictionaryTable.java class to read the input file and perform the searches. Add code to the DictionaryTable read() method to: read pairs of lines (English word is on the first...

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