13 Replies Latest reply: Oct 17, 2006 7:00 AM by 807607 RSS

    Help with mutation. (String Class)

    807607
      hey guys, im a computer science student and I'm having hard time dealing with this homework they gave us. it says:

      Write a Java application which instantiates a string literal containing the sentence �This is my first CMPS200 assignment� and does the following:

      �Prints the mutated sentence which replaces all empty space in the given sentence by a backslash.
      �Prints the mutated sentence �THIS is My first cmps200 assignment�.
      �Prints the mutated sentence �assignment CMPS200 first my is This�
      NOTE: Use only methods from the String class using the minimal number of string reference variables needed.


      ------------------------------------------------------------------------------

      all I could get off is barely the first (and even that, i couldnt get a backslash done.. i dont know) , and the second point.. im having hard time dealing with the 3rd one especially they asked us to use only string class methods...

      here i my code

      public class JavaMutation
      {
      public static void main (String[] args)
      {
      String phrase = new String ("This is my first CMPS200 assignment");

      String mutation1, upcase, mutation2, mutation3, mutation4, mutation5, mutation6, mutation7;

      mutation1 = phrase.replace(' ', '/');
      System.out.println ("Mutation #1: " +mutation1);

      upcase = phrase.toUpperCase();
      mutation2 = upcase.substring(0,4);
      mutation4 = upcase.substring(8,9);
      mutation3 = phrase.substring(5,7);
      mutation5 = phrase.substring(9,35);
      System.out.println (mutation2 +" "+ mutation3 + " " mutation4 mutation5);
      }
      }

      I'd be glad if anyone could help
        • 1. Re: Help with mutation. (String Class)
          800322
          I'd like to point out that none of these methods mutates a String. If your teacher claims it does, he's clueless.

          As for 3: look at String.split() on whitespace and print the resulting array backwards.
          • 2. Re: Help with mutation. (String Class)
            796440
            What exactly is the problem?

            When you post code, please use[code] and [/code] tags as described in Formatting tips on the message entry page. It makes it much easier to read.
            • 3. Re: Help with mutation. (String Class)
              JosAH
              On top of what previous replies have already indicated, read the API
              documentation closely for the String class and you're in business.

              kind regards,

              Jos
              • 4. Re: Help with mutation. (String Class)
                807607
                public class JavaMutation
                {
                
                    public static void main (String[] args)
                  {
                      String phrase = new String ("This is my first CMPS200 assignment");
                      
                      String mutation1, upcase, mutation2, mutation3, mutation4, mutation5, mutation6, mutation7;
                      
                      mutation1 = phrase.replace(' ', '/');
                      System.out.println ("Mutation #1: " +mutation1);
                      
                      upcase = phrase.toUpperCase();
                      mutation2 = upcase.substring(0,4);
                      mutation4 = upcase.substring(8,9);
                      mutation3 = phrase.substring(5,7);
                      mutation5 = phrase.substring(9,35);
                      System.out.println (mutation2 +" "+ mutation3 + " " +mutation4 + mutation5);
                   }
                }   
                there ya go
                • 5. Re: Help with mutation. (String Class)
                  807607
                        upcase = phrase.toUpperCase();
                        mutation2 = upcase.substring(0,4);
                        mutation4 = upcase.substring(8,9);
                        mutation3 = phrase.substring(5,7);
                        mutation5 = phrase.substring(9,35);
                        System.out.println (mutation2 +" "+ mutation3 + " " +mutation4 + mutation5);
                  This is more complicated than it needs to be. You don't need to split the second part of the sentence up, just print it out after the capitalized part.
                  System.out.println(phrase.substring(5, 35);
                  • 6. Re: Help with mutation. (String Class)
                    807607
                    Better yet!

                    Split the sentence up using .split(' ') and store the array. You could use the same array for all three mutations. For 1, print each word followed by a /. For 2, capitalize the first word, then print the rest separated by ' '. For 3, iterate over the array backwards.
                    • 7. Re: Help with mutation. (String Class)
                      807607
                      To show you how to do a backslash:
                             public static void main (String[] args)
                             {
                                 String phrase = new String ("This is my first CMPS200 assignment");
                             
                                 String one = phrase.replaceAll("\\s", "\\\\");
                                
                                 System.out.println(one);
                             }
                      • 8. Re: Help with mutation. (String Class)
                        807607
                        That's not quite right. He needs to convert the first word to all caps, InitCap the 3rd word, and toLower the 5th word. Here is how to do that, quick and dirty, but it works:
                                   String [] array = phrase.split("\\s");
                        
                                   for(int i = 0; i < array.length; i++)
                                   {
                                        switch(i)
                                        {
                                             case 0:
                                                  System.out.print(array.toUpperCase() + " ");
                                            break ;
                                       case 2:
                                            System.out.print(array[i].replace('m', 'M') + " ");
                                            break;
                                       case 4:
                                            System.out.print(array[i].toLowerCase() + " ");
                                            break;
                                       default:
                                            System.out.print(array[i] + " ");
                                       
                                  }
                             }
                             
                             System.out.println();
                        • 9. Re: Help with mutation. (String Class)
                          807607
                          That's not quite right. He needs to convert the first
                          word to all caps, InitCap the 3rd word, and toLower
                          the 5th word. Here is how to do that, quick and
                          dirty, but it works:
                          Oh, thanks, I didn't even notice the other capitalization changes.
                          • 10. Re: Help with mutation. (String Class)
                            jschellSomeoneStoleMyAlias
                            If it really is the very first assignment then introducing arrays might be a bit premature.
                            • 11. Re: Help with mutation. (String Class)
                              807607
                              If it really is the very first assignment then
                              introducing arrays might be a bit premature.
                              very true, this is infact our first assignement and i dont know what does most of the solutions posted here means. Not that they are not right, it is just something we haven't tackeled yet in our course..

                              The only string methods we took so far are

                              String (String str)
                              Constructor: creates a new string object with the same characters as str.

                              char charAt (int index)
                              Returns the character at the specified index.

                              int compareTo (String str)
                              Returns an integer indicating if this string is lexically before (a negative return
                              value), equal to (a zero return value), or lexically after (a positive return value),
                              the string str.


                              String concat (String str)
                              Returns a new string consisting of this string concatenated with str.

                              boolean equals (String str)
                              Returns true if this string contains the same characters as str (including
                              case) and false otherwise.


                              boolean equalsIgnoreCase (String str)
                              Returns true if this string contains the same characters as str (without
                              regard to case) and false otherwise.
                              int length ()
                              Returns the number of characters in this string.


                              String replace (char oldChar, char newChar)
                              Returns a new string that is identical with this string except that every
                              occurrence of oldChar is replaced by newChar.


                              String substring (int offset, int endIndex)
                              Returns a new string that is a subset of this string starting at index offset
                              and extending through endIndex-1.


                              String toLowerCase ()
                              Returns a new string identical to this string except all uppercase letters are
                              converted to their lowercase equivalent.


                              we havent taken the if or any other thing mentioned in your replies.. so it leaves me to no clue as how to answer efficiently on the questions asked.

                              Thank you for your time anyway :)
                              • 12. Re: Help with mutation. (String Class)
                                807607
                                very true, this is infact our first assignement and i
                                dont know what does most of the solutions posted here
                                means. Not that they are not right, it is just
                                something we haven't tackeled yet in our course..
                                Ok then, sorry for the confusion. You can always come back to this when you get into arrays. :) The way you are doing it in reply 4 is good then. You can do the same thing for the backwards mutation by printing the substrings in a different order.
                                Thank you for your time anyway :)
                                You're welcome.
                                • 13. Re: Help with mutation. (String Class)
                                  807607
                                  OK then, sorry for the answer I gave.

                                  To give you a hint about getting the backslash, since the backslash (\) is used in java as the escape character, you need to escape it to print a literal backslash, so in the phrase.replace function, do this
                                  phrase.replace(' ','\\');
                                  Good Luck!