Awesome!..so finally u r learning java..nice to see u doing that...and looks like its nicely done..and it looks complete too, just a lil flipflop.
This is your question right.
Write a program to test class MyStringTest.
1.So create a class MyStringTest. I'll use your code but just change the structure.
class MyStringTest
{
}
2. creates an instance of the String class and initializes this instance with a String literal
String name= "sajha";
put this inside your class MyStringTest, so that "name" will be your member variable of class MyStringTest
public class MyStringTest
{
String name= "sajha";
}
3. Use a for loop structure to print the string in reverse order
Create a method in that class "MyStringTest" , that will access the member variable "name" of that class "MyTestString" and display it in reverse. I'm using your logic here too, just changing the methods name to"reverse".
public void reverse()
{
int len = name.length();
String s="";
for(int i=len-1; i>=0; i--)
{
//Get the character at that index
char c=name.charAt(i);
//convert that char to string
String s1 = Character.toString(c);
//concatenage each returned string till the loop is completed
s=s.concat(s1);
}
System.out.println (s);
}
5. Putting it all together
class MyStringTest
{
String name= "sajha";
public void reverse()
{
int len = name.length();
String s="";
for(int i=len-1; i>=0; i--)
{
//Get the character at that index
char c=name.charAt(i);
//convert that char to string
String s1 = Character.toString(c);
//concatenage each returned string till the loop is completed
s=s.concat(s1);
}
System.out.println (s);
}
}
6. So you have class MyTestString, which creates an instance of the String class and initializes this instance with a String literal. Then you have used a for loop structure to print the string in reverse order, you have also implemented the following two String member methods.
length( )
charAt( )
7. This is how all the java classes are written, there is hardly a main method inside it.
For example ArrayList is a class. You cant just run ArrayList can you. Similarly you cant run you MyTestString class directly. You need to call this MyTestString from some other class.
8. So create a test class that has main method inside, remember when u run a program using java, it will always search for main method. So try to make the name of your file same as the class name that has main method inside, and since you will be accessing this class from command prompt make it public.
This is how it will look like
public class test
{
public static void main(String args[])
{
MyStringTest mst = new MyStringTest(); //creates an instance of the class you created above
mst.reverse(); //calls the method inside you class
}
}
9. Save your program as "test.java" as i told you its better to have the class having main method as public so that it could be accesssed from command prompt.
So when you do
c:\java test
, it will call this call this public class's main method.
So your final program will look something like this
class MyStringTest
{
String
name= "sajha";
public void reverse()
{
int len = name.length();
String s=
"";
for(int i=len-1; i>=0; i--)
{
//Get the character at that index
char c=name.charAt(i);
//convert that char to string
String s1 = Character.toString(c);
//concatenage each returned string till the loop is completed
s=s.concat(s1);
}
System.
out.println (s);
}
}
public class test
{
public static void main(String args[])
{
MyStringTest mst =
new MyStringTest(); //creates an instance of the class you created above
mst.reverse();
//calls the method inside you class
}
}
Lemme know if u got some doubt
Last edited: 12-Nov-08 09:21 PM