Explain JAVA code
I need some help to explain the meaning from line 5 to line 9. Thanks
String words = "Rain Rain go away";
String mutation1, mutation2, mutation3, mutation4;
mutation1 = words.toUpperCase();
System.out.println ("** " + mutation1 + " Nursery Rhyme **");
mutation1 = words.concat ("\nCome again another day");
mutation2 = "Johnny Johnny wants to play";
开发者_高级运维mutation3 = mutation2.replace (mutation2.charAt(5), 'i');
mutation4 = mutation3.substring (7, 27);
System.out.print ("\'" + mutation1 + "\n" + mutation4 + "\'\n");
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 thechar
value at the specified index.String toUpperCase()
Converts all of the characters in thisString
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 ofoldChar
in this string withnewChar
.String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specifiedbeginIndex
and extends to the character at indexendIndex - 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 +
Assign the string "Rain Rain go away" to the variable words.
Declare the variables mutation1, mutation2, mutation3, and mutation4 as String.
The value of words becomes uppercased "RAIN RAIN GO AWAY",
and assigned to the mutation1 variable.Prints the string "RAIN RAIN GO AWAY Nursery Rhyme" to the console.
"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."Johnny Johnny wants to play" is assigned to mutation2.
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.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.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'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.
精彩评论