e)A class is named StringReverse is declared with a constructor that takes in an argument S of type string for initializing the private member variable name of type String. It shows the reverse string
Give the definition of the StringReverse class
Print the string in reverse

A class is named StringReverse is declared with a constructor that takes in an argument S of type String for initializing the private member variable name of type String.
Try coding it and post it here.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Let's look at the next part of the requirement.
"It shows the reverse string". That's confusing.
Can you add to StringReverse a public method called reverse that takes no arguments and returns a String? For now have the method return the name member variable not reversed.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Now add a public static void main member that takes a String array called args.
The main method creates a variable name of type StringReverse using the first argument passed into main. The main method then prints the value returned by calling the method reverse() on the name variable.
[ March 16, 2003: Message edited by: Barry Gaunt ]
[ March 16, 2003: Message edited by: Barry Gaunt ]
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
How does that compile and run? We are not finished yet.
[ March 16, 2003: Message edited by: Barry Gaunt ]
[ March 16, 2003: Message edited by: Barry Gaunt ]
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
public String reverse()
{
return name;
}
See it? Fix it and try it again.
Getting nearer!
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
return name.reverse();
but we can't.
Try it.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
so let me get this straight..there is no way that a string can be reversed in this manner....rite

do u know what is meant by "give the definition of the ReverseString class" i mean how in the world do i define it when the question itself tells me what to do??(which also cannot be done


We have to change the String name into "something else", reverse it, and then change it back into a String again. The "something else" is a StringBuffer.
Here goes:
1. Make a StringBuffer from name:
new StringBuffer( name )
2. Reverse it "inside" the StringBuffer:
new StringBuffer( name ).reverse()
3. Change it back to a String again:
new StringBuffer( name ).reverse().toString()
So instead of
return name.reverse()
do
return new StringBuffer( name ).reverse().toString(); // put this into the reverse method
Try that. But I'm finished for today, I'll take a look at your post tomorrow morning (European time)
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Just the reverse method needs to be changed. You did that OK. All the other stuff stays the same.
Try it out now.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
StringBuffer objects can be changed inside. The "Tabz" inside a StringBuffer can be reversed.
That's what we did in your program. But remember we had to change it back to a String again. The new String had "zbaT" inside it. You are stuck with that new String object because you cannot change it.
You can do many things with Strings and StringBuffers. Check out the Application programming Interface Documentation. Or take a look at the free Sun Tutorial. Above all, don't run too fast. This stuff ain't easy, but it's

If you don't understand some of this, go ask that "mad scientist" lecturer of yours, I'm sure he's not so daft as you think.
Cheers - Barry
[ March 17, 2003: Message edited by: Barry Gaunt ]
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
one studio 4' to run this program but i got this error messages: java.lang.ArrayIndexOutOfBoundsException
at StringReverse.main(Reverse.java:13)
Exception in thread "main"
please tell me how do i correct that?
and on java StringReverse YRRABUKNAHT
how do i set up a test data if i run this on 'sun one stdio 4'?
thanks,
I do not have SunOne ( Forte for Java ?) but there must be some buttons to press or menus to select so that you can specify an argument list to the program.
A hint: if you are just starting out with java do not use an IDE. Use a good simple editor and use the DOS command window ( or Cygwin ). If you must use an IDE I recommend the free learning tool BlueJ you can get from http://www.bluej.org/
[ March 19, 2003: Message edited by: Barry Gaunt ]
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
