Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-08-2008, 08:50 PM
Member
 
Join Date: Mar 2008
Posts: 2
amexudo is on a distinguished road
exercise of recursive method
hello,
i was trying to do an exercise but i cannot resolve it.
the exercise asks us to write a recursive method
public static int myCompare (String s1, String s2)
that receives 2 strings and compare them.
it returns 1 if string s1 comes after s2 in the dictionary (lexicographly)
the method returns 2 if string s1 comes after s2 in the dictionary
and it returns 0 if strings s1 and s2 are exactly the same.
example:
input:
s1 = "mother" and s2 = "class"
output: 1 ('m' comes after 'c')

the method must be a recursive method and mustn't use loops.

in the solution it's only allowed to use the following existing methods (from String class):
public char charAt(int i)
public String substring(int i)
public int length()

thanks for your help!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-08-2008, 10:02 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
To get you started think about comparing the charAt(0) of each string. If they are the same you remove the first letter from each string and return a call to the method using the substrings as arguments.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-09-2008, 06:55 PM
Member
 
Join Date: Mar 2008
Posts: 2
amexudo is on a distinguished road
code
public static int myCompare(String s1, String s2){

if (s1.charAt(0)==s2.charAt(0)){
if (s1.length() == 1 && s2.length()!= 1)
return 2;
else
if (s1.length() != 1 && s2.length() ==1)
return 1;
else
if (s1.length()==1 && s2.length() == 1)
return 0;
else
return myCompare(s1.substring(1),s2.substring(1));
}
else {
if (s1.charAt(0)<s2.charAt(0))
return 2;
else
return 1;
}
}



this is what i've done. i think it's right, didn't find any problem till now.
thanks again for your help!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive Method ==> find minimum value from array NatNat New To Java 1 02-16-2008 10:10 PM
Recursive Method ==> find how many times a value is repeated in an array NatNat New To Java 2 02-16-2008 09:52 PM
Recursive Method bluegreen7hi New To Java 5 11-29-2007 05:45 AM
I/O exercise Feldom New To Java 1 10-28-2007 05:48 PM
help with exercise e_as're New To Java 3 09-25-2007 11:14 AM


All times are GMT +3. The time now is 08:24 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org