views:

172

answers:

4

I need some help to explain the meaning from line 5 to line 9. Thanks

  1. String words = "Rain Rain go away";

  2. String mutation1, mutation2, mutation3, mutation4;

  3. mutation1 = words.toUpperCase();

  4. System.out.println ("** " + mutation1 + " Nursery Rhyme **");

  5. mutation1 = words.concat ("\nCome again another day");

  6. mutation2 = "Johnny Johnny wants to play";

  7. mutation3 = mutation2.replace (mutation2.charAt(5), 'i');

  8. mutation4 = mutation3.substring (7, 27);

  9. System.out.print ("\'" + mutation1 + "\n" + mutation4 + "\'\n");

  10. System.out.println ("Title length: " + words.length());

+7  A: 

The String class API documentation page is a good place to start looking for answers. Also, if you're looking to have a future as a programmer, you should put more effort into research before asking arround for free answers.

marcos
This explains 1-10. You shouldn't need anything other than this one page.
Stefan Kendall
I've to study basic programing and I will not become a programmer in the future so it's quite hard for me
MIW
+1  A: 

STEP 5

mutation1 = words.concat ("\nCome again another day");

This performs a concatnation of a string.

Examples:

 "cares".concat("s") returns "caress"
 "to".concat("get").concat("her") returns "together"

So in your case, your string will be concatenated to words string and as a result you will get mutation1 as

Rain Rain go away

Come again another day

STEP 6

mutation2 = "Johnny Johnny wants to play";

This is just an assignment of a string to a variable.

STEP 7

mutation3 = mutation2.replace (mutation2.charAt(5), 'i');

Here charAt method finds a character at a given position within a string. So in this case it gets the char at index 5 in mutation 2 first. It's "y". Note indices are started from 0. and replaces all the occurences of mutation2 by "y", and assign the resultant to mutation3.

STEP 8

mutation4 = mutation3.substring (7, 27);

Substring is used to filterout a part of a string. So in this case the substring started from index 7 will be taken. And the sub string ends from index 27. The string you get will we assigned to the variable "mutation4"

STEP 9

System.out.print ("\'" + mutation1 + "\n" + mutation4 + "\'\n");

Now the easy part. Just print them to console... mutation1 and mutation4 with some new lines.

Chathuranga Chandrasekara
Thanks a lot for your help but in line 6 what will be appeared ? Only "Johnny Johnny wants to play" or it comes together with "Rain Rain go away Come again another day"
MIW
Nothing... You just create a new variable called "mutation2" and put the string "Johnny Johnny wants to play". that is the first occurence of mutation2 and it is a direct assignment; so its straight and simple. If you use System.out.println(mutation2); then you'll see "Johnny Johnny wants to play"
Chathuranga Chandrasekara
+2  A: 

java.lang.String


You may also want to look up:

polygenelubricants
+2  A: 
  1. Assign the string "Rain Rain go away" to the variable words.

  2. Declare the variables mutation1, mutation2, mutation3, and mutation4 as String.

  3. The value of words becomes uppercased "RAIN RAIN GO AWAY",
    and assigned to the mutation1 variable.

  4. Prints the string "RAIN RAIN GO AWAY Nursery Rhyme" to the console.

  5. "Rain Rain go away\nCome again another day" is assigned to mutation1. \n is a new line.
    When you print mutation1 to the console,
    "Come again another day" is displayed in a new line.

  6. "Johnny Johnny wants to play" is assigned to mutation2.

  7. mutation2.charAt(5) gets the 6th character in mutation2,
    because charAt index starts at 0; this character is 'y'. mutation2.replace (mutation2.charAt(5), 'i') then becomes mutation2.replace ('y', 'i'), which says that all characters 'y' is to be replaced with 'i'.
    So, the string "Johnni Johnni wants to plai" is assigned to mutation3.

  8. Get the 8th to 28th character in mutation3 (Check the API why this is so).
    So, the string "Johnni wants to plai" is assigned to mutation4.

  9. Prints the string to the console (single quote and new lines are included):
    'Rain Rain go away
    Come again another day
    Johnni wants to plai'

  10. Gets the number of characters in words, which is 17.

Of course, the best way to figure out if my answers are correct is to read the API, and write and run the sample code yourself. :)

geff_chang
Thanks for your help :)
MIW