Question: Hello need help with this Java lab! Starting programming level 1. Download the class files, Main, Translator, PigLatin, from the course website 2. The program
Hello need help with this Java lab!
Starting programming level
1. Download the class files, Main, Translator, PigLatin, from the course website
2. The program uses the Translator interface to translate a sentence in English to another language The Translator interface requires implementation of the method translateWord which translates a single word to the new language The PigLatin class is an example that translates to Pig Latin
3. Write a Loud and Pirate class that implement the Translator interface Loud should translate the text to all caps while Pirate should translate using word substitutions shown on the next slide
Your program should output the following text:
ellohay irsay hatway isway ouryay amenay ? hereway isway hetay lassroomcay ? ahoy matey what be yer name Arr! whar be th' classroom Arr! HELLO SIR WHAT IS YOUR NAME ? WHERE IS THE CLASSROOM ?
/*****************************************
* CSCE A201 Lab
* Test of the Translator interface
*
*******************************************/
public class Main
{
public static void main(String[] args)
{
String sentence;
sentence = "hello sir what is your name ? where is the classroom ?";
PigLatin pig = new PigLatin(); // Create a Pig Latin object
translate(sentence, pig);
// Uncomment the following four lines
// after you complete the Pirate and Loud classes to
// test them out
/*
Pirate pirate = new Pirate();
translate(sentence, pirate);
Loud yelling = new Loud();
translate(sentence, yelling);
*/
}
public static void translate(String s, Translator translator)
{
// Get each word in the sentence
String[] words = s.split(" "); // This puts each word from s into an array, delimited by a space
for (String word : words) // This is a for-each loop that iterates through each word in the array
{
System.out.print(translator.translateWord(word)); // Translate each word and print it
System.out.print(" ");
}
System.out.println();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
public class Pirate implements Translator
{
public String translateWord(String word)
{
// Ignore period or question mark
if (word.equals(".") || word.equals("?"))
return "Arr!";
switch (word)
{
case "hello":
return "ahoy";
case "sir":
return "matey";
case "where":
return "whar";
case "is":
return "be";
case "the":
return "th'";
case "my":
return "me";
case "your":
return "yer";
default:
return word;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
* Translator.java
*
* This is the Translator interface. Any class that implements it
* must have a translateWord method with the signature shown below.
* The method should take a string that represents a word in English and
* return the word translated to the target "language". In this lab, the
* languages are Pig Latin, Pirate-speak, and LOUD speak.
*/
public interface Translator
{
public String translateWord(String word);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
