hi i am a beginner in java(juz started abt a month) :roll: and i dun quite really the following question which was given by my lecturer.....can anyone please help me... 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
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.
Now add a public static voidmain member that takes a Stringarray 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 ]
it does print Tabz... thanks...now if it is reversed, it should print zbaT ..rite??..how do i go about doing that??? is there any way that i can print the string backwards if it is what i have to do.. (btw..have u come across such a question like mine?)
yea..i do get the 'cannot resolve symbol' err.. so let me get this straight..there is no way that a string can be reversed in this manner....rite how come? 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 i wish i could kill my lect!!)
No, that's not necessary! We have to change the Stringname 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 returnnew 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)
btw the question asks me for the definition of the StringReverse class.... this means tht i must state the way the code works..like how it uses Stringbuffer to manipulate the String...rite...or is it a bullsh*t question given to me???
It could be a "mad scientist" research question. The lecturer perhaps wants you to find out that the String class has no reverse() method. That's because String objects can't be changed inside once they have been created. 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 when it gets clearer. 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 ]
is anyone can help me ... i was trying to use 'sun 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,
Yuan, the program will only work given a String argument to work on. I did not want to make it more complex than needed. 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 ]