开发者

Explain JAVA code

开发者 https://www.devze.com 2022-12-23 06:54 出处:网络
I need some help to explain the meaning from line 5 to line 9. Thanks String words = \"Rain Rain go away\";

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. 开发者_高级运维
  8. mutation3 = mutation2.replace (mutation2.charAt(5), 'i');

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

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

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


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.


java.lang.String

  • int length()
    Returns the length of this string.
  • char charAt(int index)
    Returns the char value at the specified index.
  • String toUpperCase()
    Converts all of the characters in this String to upper case using the rules of the default locale.
  • String concat(String str)
    Concatenates the specified string to the end of this string.
  • String replace(char oldChar, char newChar)
    Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
  • String substring(int beginIndex, int endIndex)
    Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.

You may also want to look up:

  • JLS 3.10.6 Escape Sequences for Character and String Literals
  • JLS 15.18.1 String Concatenation Operator +


  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. :)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号