zeePa
Replies to this thread:

More by zeePa
What people are reading
Subscribers
Subscribers
[Total Subscribers 1]

paroo
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Java help Needed!
[VIEWED 3887 TIMES]
SAVE! for ease of future access.
Posted on 11-12-08 8:09 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

 
Write a Java application that creates an instance of the String class and initializes this instance with a String literal. Use a for loop structure to print the string in reverse order. Implement the following two String member methods.


length( )
charAt( )

 Write a program to test class MyStringTest.

Here is my solution. But how else will you test MyStringaTest???? Somebody wants three different ways to do it??? Guys help please???!! Anyone?

  Solution:

public class StringTest {
public static void main(String args[]) {   
      String name= "sajha";
      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);      
      
  }
}


 
Posted on 11-12-08 8:37 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

What I would like to see is a class that either has a method taking a String as a parameter or has a constructor that takes a String as the parameter.  All manipulation of the String occurs within a method of that class.  A separate class has a main method that declares and instantiates an object of the type of your first class.  Use this object reference and pass it a String (of course, if the other class' constructor requires the String, pass it when you create the object) which is then manipulated as per the assignment instructions.
Alternatively, code a single class that has a method that performs all the String manipulation; in that same class, create a main() method that declares and instantiates an object, then uses the String manipulation method...

 

COMMON GUYS?

WHERE R YA TECHGUY?


 
Posted on 11-12-08 8:51 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

######################## public class MyString { public static void main(String[] args) { String str = "some string"; String revStr = reverseString(str); System.out.println("The reverse of (" + str + ") is: " + revStr); System.out.println("Is the reverseString method working correctly? " + reverseStringTest()); } private static String reverseString(String str) { StringBuffer sb = new StringBuffer(); for(int i = str.length()-1; i>= 0; i--) { sb.append(str.charAt(i)); } return sb.toString(); } private static boolean reverseStringTest() { String str = "test"; String result = "tset"; String revStr = reverseString(str); return revStr.equals(result); } }
Last edited: 12-Nov-08 08:51 PM

 
Posted on 11-12-08 8:53 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Sorry, how do you post code in here?
 
Posted on 11-12-08 9:07 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

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

 
Posted on 11-12-08 9:18 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

oops i didnt see ur second post.

So you want to pass string as a paramter right.

That means your class will now should accept parameter.

Two ways you can do this.

Either use constructer, so that whenever you create an object out of your MyStringTest class, the string member is initialized.

Remember the constructer name is always same as the class Name, so in your case the constructer name will be MyStringTest

Now this is how your code will look like

class MyStringTest

{

String name;

public MyStringTest(String temp)//constructer that initializes your object

{

name=temp;

}

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("sajha"); //creates an instance of the class you created above

mst.reverse(); //calls the method inside you class

}

}

 

The second method is by making your method "reverse" to take a String as an argument.

So you dont need a constructer right. And you wont even need String member variable, since you will be passing that from main method. Then your code will be something like this

class MyStringTest

{

public void reverse(String name)

{

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("sajha"); //calls the method inside you class

}

}

 

Last edited: 12-Nov-08 09:22 PM

 
Posted on 11-12-08 9:28 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

dang!!!!!!

techguy, short of words now to thank ya!!!!

man, i appreciate your will to help!

there are only few good people in this world like ya.

I am honored to say you are my java guru!

zeepa


 
Posted on 11-12-08 9:32 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

no no ya're  fine. thank ya so much howard roark!!!
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 365 days
Recommended Popular Threads Controvertial Threads
शीर्षक जे पनि हुन सक्छ।
NRN card pros and cons?
TPS Re-registration case still pending ..
What are your first memories of when Nepal Television Began?
Anybody gotten the TPS EAD extension alert notice (i797) thing? online or via post?
TPS Re-registration
Democrats are so sure Trump will win
Basnet or Basnyat ??
TPS EAD auto extended to June 2025 or just TPS?
nrn citizenship
Toilet paper or water?
Sajha has turned into MAGATs nest
Nas and The Bokas: Coming to a Night Club near you
ढ्याउ गर्दा दसैँको खसी गनाउच
Mamta kafle bhatt is still missing
ChatSansar.com Naya Nepal Chat
whats wrong living with your parents ?
डीभी परेन भने खुसि हुनु होस् ! अमेरिकामाधेरै का श्रीमती अर्कैसँग पोइला गएका छन् !
3 most corrupt politicians in the world
अमेरिकामा बस्ने प्राय जस्तो नेपालीहरु सबै मध्यम बर्गीय अथवा माथि (higher than middle class)
Nas and The Bokas: Coming to a Night Club near you
TPS Update : Jajarkot earthquake
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters