Results 1 to 7 of 7
Thread: Method implementation
- 04-01-2009, 11:44 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 16
- Rep Power
- 0
Method implementation
hi i wanted to know how can i implement the following methods
public String reverse(String string) - a method that will recursively take the String received, reverse its characters, and return the reversed String. You may only use the methods charAt, concat, and length from the class String. You may create as many helper methods as you need.
for this method this is what i have
import java.util.Scanner;
public class P5{
public static void main (String[] args){
Scanner scanner = new Scanner (System.in);
System.out.println("Enter text to reverse: ");
String string = scanner.nextLine();
System.out.print(reverse(string));
}
public static String reverse(String string){
if (string.length() == 1){
return string;
}
else{
return reverse(string.substring(1)) + string.charAt(0);
}
}
}
i did it with the substring method but im not supposed to use i dont how to change it to use only the other methods
public String reverse(String string, int start, int end) - a method that will recursively take the String received, reverse the characters within substring specified, and return the new String containing the reversed subset (the non-reversed and reversed portions together). You may only use the methods charAt, concat, and length from the class String. You may create as many helper methods as you need.Last edited by kathyla18; 04-02-2009 at 08:30 PM.
- 04-02-2009, 12:14 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
in order to implement this, you must try. (and in order for us to help you, we need a specific problem)
- 04-02-2009, 05:46 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
kathyla18, how long you are working on Java. Do you know about method/constructor declarations in Java?
Defining Method Java
- 04-02-2009, 06:20 AM #4
Member
- Join Date
- Mar 2009
- Posts
- 7
- Rep Power
- 0
@kathyla18
I advise you take read more about the "StringBuffer" class and method " reverse()" which is very easy to implement .
- 04-02-2009, 06:24 AM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
@fonso gfx
only use the methods charAt, concat, and length from the class String.
- 04-02-2009, 06:32 AM #6
Member
- Join Date
- Mar 2009
- Posts
- 7
- Rep Power
- 0
@mtyoung
I know, but the problem is that it will be more difficult to write the program.
- 04-02-2009, 06:44 AM #7
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
in general, do not following instruction, deduct some of marks or zero mark
use reverse, mean do nothing, java do it for you...
when studying, it is important to implement something by ourselves, just like we may implement linked List in learning instead of direct using ArrayList Class
recursive function just like for/while loop
first try to use for loop to implement method reverse(String string)Last edited by mtyoung; 04-02-2009 at 06:46 AM.
Similar Threads
-
Help on Stack Implementation
By danver_2009 in forum New To JavaReplies: 1Last Post: 02-16-2009, 08:12 AM -
Need little Help In Calculater Implementation..
By realahmed8 in forum New To JavaReplies: 6Last Post: 12-18-2008, 01:39 AM -
VietPad 2.0 (.NET implementation)
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-05-2008, 04:32 PM -
Graph DPS and BFS implementation
By hey in forum New To JavaReplies: 1Last Post: 01-09-2008, 09:19 PM -
Help with recursive implementation
By toby in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks