0

Analyze the below code and invoke the method using main method. Please help me with this problem.

public int compareTo(String anotherString) { int len1 = value.length; int le2 = anotherString.value.length; int lim = Math.min(len1, len2); char v1[] = value; char v2[] = anotherString.value; int k = 0; whlie (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; }

23rd Jul 2020, 5:16 AM
Prashant Pal
Prashant Pal - avatar
8 Respostas
+ 3
Prashant Pal This code seems incomplete and wrong because there is no any string with value and what is anotherString.value.length. We get length of string but what is here anotherString.value?
23rd Jul 2020, 5:48 AM
A͢J
A͢J - avatar
+ 1
Not getting you all.
23rd Jul 2020, 11:59 AM
Prashant Pal
Prashant Pal - avatar
+ 1
No.. i need to invoke this method using main method and generate same output as built-in method compareTo() gives.
28th Jul 2020, 9:43 AM
Prashant Pal
Prashant Pal - avatar
+ 1
whlie was just typing mistake by me.😅
28th Jul 2020, 9:44 AM
Prashant Pal
Prashant Pal - avatar
0
The provided code is exactly the source code of the method compareTo implemented inside the String class: see for example http://hg.openjdk.java.net/jdk7u/jdk7u6/jdk/file/8c2c5d63a17e/src/share/classes/java/lang/String.java at line 1137 A simple example which invokes this method is here: https://code.sololearn.com/cHK5GbJV9jQ1
23rd Jul 2020, 6:43 AM
Bilbo Baggins
Bilbo Baggins - avatar
0
Prashant Pal Share your complete Code.
25th Jul 2020, 5:11 AM
A͢J
A͢J - avatar
0
I think he has no code at all, just a quiz in some test to explain the use of compareTo method in String class. By the way there is a typo during the copy of the method: 'whlie' instead of 'while'
25th Jul 2020, 6:07 AM
Bilbo Baggins
Bilbo Baggins - avatar