Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Making sure I answered this question completely.

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Making sure I answered this question completely.

    Here's the question first of all: Change the PigLatinTranslator from chapter 4 so that its translatemethod is static. Change the PigLatin class so it invokes the method correctly.

    Original PigLatinTranslator

    import java.util.Scanner;
    public class PigLatinTranslator
    {
       //-----------------------------------------------------------------
       //  Translates a sentence of words into Pig Latin.
       //-----------------------------------------------------------------
       public String translate (String sentence)
       {
          String result = "";
          sentence = sentence.toLowerCase();
          Scanner scan = new Scanner (sentence);
          while (scan.hasNext())
          {
             result += translateWord (scan.next());
             result += " ";
          }
          return result;
       }
       //-----------------------------------------------------------------
       //  Translates one word into Pig Latin. If the word begins with a
       //  vowel, the suffix "yay" is appended to the word. Otherwise,
       //  the first letter or two are moved to the end of the word,
       //  and "ay" is appended.
       //-----------------------------------------------------------------
       private String translateWord (String word)
       {
          String result = "";
          if (beginsWithVowel(word))
             result = word + "yay";
          else
             if (beginsWithBlend(word))
                result = word.substring(2) + word.substring(0,2) + "ay";
             else
                result = word.substring(1) + word.charAt(0) + "ay";
          return result;
       }
    //-----------------------------------------------------------------
       // Determines if the specified word begins with a vowel.
       //-----------------------------------------------------------------
       private boolean beginsWithVowel (String word)
       {
          String vowels = "aeiou";
          char letter = word.charAt(0);
          return (vowels.indexOf(letter) != -1);
       }
       //-----------------------------------------------------------------
       //  Determines if the specified word begins with a particular
       //  two-character consonant blend.
       //-----------------------------------------------------------------
       private boolean beginsWithBlend (String word)
       {
          return ( word.startsWith ("bl") || word.startsWith ("sc") ||
                   word.startsWith ("br") || word.startsWith ("sh") ||
                   word.startsWith ("ch") || word.startsWith ("sk") ||
                   word.startsWith ("cl") || word.startsWith ("sl") ||
                   word.startsWith ("cr") || word.startsWith ("sn") ||
                   word.startsWith ("dr") || word.startsWith ("sm") ||
                   word.startsWith ("dw") || word.startsWith ("sp") ||
                   word.startsWith ("fl") || word.startsWith ("sq") ||
                   word.startsWith ("fr") || word.startsWith ("st") ||
                   word.startsWith ("gl") || word.startsWith ("sw") ||
                   word.startsWith ("gr") || word.startsWith ("th") ||
                   word.startsWith ("kl") || word.startsWith ("tr") ||
                   word.startsWith ("ph") || word.startsWith ("tw") ||
                   word.startsWith ("pl") || word.startsWith ("wh") ||
                   word.startsWith ("pr") || word.startsWith ("wr") );
       }
    }

    Original PigLatin

    import java.util.Scanner;
    public class PigLatin
    {
       //-----------------------------------------------------------------
       //  Reads sentences and translates them into Pig Latin.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          String sentence, result, another = "y";
          Scanner scan = new Scanner (System.in);
          PigLatinTranslator translator = new PigLatinTranslator();
          while (another.equalsIgnoreCase("y"))
          {
             System.out.println ();
             System.out.println ("Enter a sentence (no punctuation):");
             sentence = scan.nextLine();
             System.out.println ();
             result = translator.translate (sentence);
             System.out.println ("That sentence in Pig Latin is:");
             System.out.println (result);
             System.out.println ();
             System.out.print ("Translate another sentence (y/n)? ");
             another = scan.nextLine();
          }
       }
    }

    Editited PigLatinTranslator:

    import java.util.Scanner;
    public class PigLatinTranslator
    {
       //-----------------------------------------------------------------
       //  Translates a sentence of words into Pig Latin.
       //-----------------------------------------------------------------
       public static String translate (String sentence)
       {
          String result = "";
          sentence = sentence.toLowerCase();
          Scanner scan = new Scanner (sentence);
          while (scan.hasNext())
          {
             result += translateWord (scan.next());
             result += " ";
          }
          return result;
       }
       //-----------------------------------------------------------------
       //  Translates one word into Pig Latin. If the word begins with a
       //  vowel, the suffix "yay" is appended to the word. Otherwise,
       //  the first letter or two are moved to the end of the word,
       //  and "ay" is appended.
       //-----------------------------------------------------------------
       private static String translateWord (String word)
       {
          String result = "";
          if (beginsWithVowel(word))
             result = word + "yay";
          else
             if (beginsWithBlend(word))
                result = word.substring(2) + word.substring(0,2) + "ay";
             else
                result = word.substring(1) + word.charAt(0) + "ay";
          return result;
       }
    //-----------------------------------------------------------------
       // Determines if the specified word begins with a vowel.
       //-----------------------------------------------------------------
       private static boolean beginsWithVowel (String word)
       {
          String vowels = "aeiou";
          char letter = word.charAt(0);
          return (vowels.indexOf(letter) != -1);
       }
       //-----------------------------------------------------------------
       //  Determines if the specified word begins with a particular
       //  two-character consonant blend.
       //-----------------------------------------------------------------
       private static boolean beginsWithBlend (String word)
       {
          return ( word.startsWith ("bl") || word.startsWith ("sc") ||
                   word.startsWith ("br") || word.startsWith ("sh") ||
                   word.startsWith ("ch") || word.startsWith ("sk") ||
                   word.startsWith ("cl") || word.startsWith ("sl") ||
                   word.startsWith ("cr") || word.startsWith ("sn") ||
                   word.startsWith ("dr") || word.startsWith ("sm") ||
                   word.startsWith ("dw") || word.startsWith ("sp") ||
                   word.startsWith ("fl") || word.startsWith ("sq") ||
                   word.startsWith ("fr") || word.startsWith ("st") ||
                   word.startsWith ("gl") || word.startsWith ("sw") ||
                   word.startsWith ("gr") || word.startsWith ("th") ||
                   word.startsWith ("kl") || word.startsWith ("tr") ||
                   word.startsWith ("ph") || word.startsWith ("tw") ||
                   word.startsWith ("pl") || word.startsWith ("wh") ||
                   word.startsWith ("pr") || word.startsWith ("wr") );
       }
    }

    Is this all I would need to do or is there more to it? Thanks in advance!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Making sure I answered this question completely.

    I'm assuming you made the changes to your PigLatin class to let you run it?

    Did you try running it to see if it works?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making sure I answered this question completely.

    I didn't edit PigLatin but it runs perfectly.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Making sure I answered this question completely.

    Oh dur, of course it'll run...

    The second half of the question does ask you to modify your PigLatin class to call the static method directly. It's always a good idea to call static methods using the class reference rather than an object reference, though technically it's not required (which I forgot this last point).

    This basically means that instead of creating a PigLatinTranslator object and calling the method, you use the class qualifier.

    // calling a static method
    SomeClass.someMethod(); // no object is used to call static methods, just the class reference

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making sure I answered this question completely.

    Right, I replaced the new statement in PigLatin with PigLatinTranslator.translate(); but it gives me an error saying ican't apply () to translate.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Making sure I answered this question completely.

    Could you post your updated PigLatin code?

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making sure I answered this question completely.

    yeah sure, I figured out i need to put in the parameter sentence, but NOW it says i need to initialize sentence.

    import java.util.Scanner;
    public class PigLatin
    {
       //-----------------------------------------------------------------
       //  Reads sentences and translates them into Pig Latin.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          String sentence, result, another = "y";
          Scanner scan = new Scanner (System.in);
          PigLatinTranslator.translate(sentence);
          while (another.equalsIgnoreCase("y"))
          {
             System.out.println ();
             System.out.println ("Enter a sentence (no punctuation):");
             sentence = scan.nextLine();
             System.out.println ();
             result = PigLatinTranslator.translate (sentence);
             System.out.println ("That sentence in Pig Latin is:");
             System.out.println (result);
             System.out.println ();
             System.out.print ("Translate another sentence (y/n)? ");
             another = scan.nextLine();
          }
       }
    }

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Making sure I answered this question completely.

       public static void main (String[] args)
       {
          String sentence, result, another = "y";
          Scanner scan = new Scanner (System.in);
          PigLatinTranslator.translate(sentence);
          while (another.equalsIgnoreCase("y"))

    Notice the second to last line, I think you left this in there from an earlier experiment.

  9. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making sure I answered this question completely.

    Yep, all good now.

Similar Threads

  1. completely stuck on a simple code
    By disp in forum Loops & Control Statements
    Replies: 6
    Last Post: June 30th, 2011, 12:02 PM
  2. Creating a Gun in Java, I'm completely lost.
    By Pryde in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2011, 08:13 AM
  3. Completely lost, creating a bar graph
    By lairel in forum Object Oriented Programming
    Replies: 1
    Last Post: February 28th, 2011, 07:18 AM
  4. School java project, completely stuck
    By John1818 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 18th, 2010, 04:10 AM
  5. Completely New to Java
    By slimshadie in forum Java Theory & Questions
    Replies: 7
    Last Post: September 6th, 2009, 01:55 PM