English 中文(简体)
Help with writing a void reverse() method and use recursion to reverse a complete sentence(Java)
原标题:
  • 时间:2009-12-08 19:58:23
  •  标签:
  • java
  • reverse

I already wrote something that removes the first character of a string and puts it after the remaining substring and then prints it out, the instructions were to reverse the sentence using recursion by removing the first letter of the sentence and concatenate it to the reversed remaining substring, ie. "Hello" yields "olleH". But i dont know about the recursion part, any help would be appreciated, thanks. This is my code:

public class Sentence {

   private String sentence;

   public Sentence(String astring) {
      sentence = astring;
   }

   public void reverse(){

   String firstChar = sentence.substring(0,1);

   String remainingSen = sentence.substring(1,sentence.length());

   System.out.println(remainingSen+firstChar);  
  }
}
最佳回答

Do you understand the concept of recursion? If so, figure out the base case (the condition that would stop the recursion) and what you want to do in each recursive step. Hint: you ll need a recursive method that takes the string to be reversed and returns the reversed string. Not going to straight up answer a HW question, but that should get you started.

EDIT: (One more hint) don t try to make the void reverse() method recursive. Have it call a different, private recursive method that actually does the reversing.

问题回答

Seeing that this is a homework assignment, I ll give some hints to get started:

  • a recursive method calls itself to do part of the work
  • a reverse() method taking a String argument and returning the reversed version of the string could call itself.
  • if you remove the first character and add it to the end of the reversed left over, your work is done.

If you work out the hints above, you should have solved your problem :-)

Generally, if you want to write a recursive function, you will be calling the function within itself. For example:

void fn() {
    fn() 
}

This example will obviously be an infinite loop.

In your case, you want to call your reverse function repeatedly until you reach a defined state (where Hello is transformed to olleH).

public class Sentence {

  private String sentence;

  // ... etc ...

  public void reverse() {
      // Base Case: When do you want this to end? This statement is designed
      // to end the recursion when a desired state is reached

      // some sort of string manipulation (which you have already worked on)

      // call reverse() to continue the  looping  until 
      // a desired _case_ is reached
  }
}

I assume this is a homework question and it s due soon, so I m not going to provide an exact answer...

Update 1 : I modified the reverse example to match the constraints that were expressed.

public void reverse()
{
    if(text.length() > 0)
    {
        String first = text.substring(0,1);
        String remaining = text.substring(1);

        Sentence shorter = new Sentence(remaining);
        shorter.reverse();

        text = shorter.text + first;
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签