Results 1 to 7 of 7
  1. #1
    kathyla18 is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default 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 09:30 PM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    in order to implement this, you must try. (and in order for us to help you, we need a specific problem)

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  4. #4
    fonso gfx is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    @kathyla18

    I advise you take read more about the "StringBuffer" class and method " reverse()" which is very easy to implement .

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    @fonso gfx

    only use the methods charAt, concat, and length from the class String.

  6. #6
    fonso gfx is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    @mtyoung
    I know, but the problem is that it will be more difficult to write the program.

  7. #7
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    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 07:46 AM.

Similar Threads

  1. Help on Stack Implementation
    By danver_2009 in forum New To Java
    Replies: 1
    Last Post: 02-16-2009, 09:12 AM
  2. Need little Help In Calculater Implementation..
    By realahmed8 in forum New To Java
    Replies: 6
    Last Post: 12-18-2008, 02:39 AM
  3. VietPad 2.0 (.NET implementation)
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-05-2008, 05:32 PM
  4. Graph DPS and BFS implementation
    By hey in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 10:19 PM
  5. Help with recursive implementation
    By toby in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •