File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i dun understand....

 
Tabrez Muchhala
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you do this part?
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.
 
Tabrez Muchhala
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is what i think....


but i dun really understand wat the question wants....esp the Define part....
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good. this part is what we need.

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.
 
Tabrez Muchhala
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hope i right in this part....
thanks .....
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!

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 ]
 
Tabrez Muchhala
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think i wrong and i dun know why or how..pls correct me....

[ March 16, 2003: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Tabrez Muchhala
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get three errors...
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I found the bug. We have got to have:
public String reverse()
{
return name;
}
See it? Fix it and try it again.
Getting nearer!
 
Tabrez Muchhala
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah!!! the code compiled w/o any errors.....but wheni ran the program, i got the following exception..
is it b'cause there is nothing to reverse yet(thts wat i think)Pls correct me if i am wrong.....
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, try java StringReverse Tabz
 
Tabrez Muchhala
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?)
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is where it gets interesting. It would be great in the reverse method if we could just do:
return name.reverse();
but we can't.
Try it.
 
Tabrez Muchhala
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!)
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's not necessary!
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)
 
Tabrez Muchhala
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i followed yr steps ....i got three errors...
i confused....here's the code
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Tabz! You changed a bit too much!
Just the reverse method needs to be changed. You did that OK. All the other stuff stays the same.
Try it out now.
 
Tabrez Muchhala
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U R GREAT!!

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???
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Tabrez Muchhala
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Barry for enlightening me with your knowledeg!!....u r GREAT!!!
 
Yuan Tseng
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic