[Show all top banners]

cowgirl1996
Replies to this thread:

More by cowgirl1996
What people are reading
Subscribers
:: Subscribe
Back to: Computer/IT Refresh page to view new replies
 Java Help
[VIEWED 11834 TIMES]
SAVE! for ease of future access.
Posted on 12-16-13 3:13 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

This is the assignment:

Write a Java application titled MyStringTest 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 to complete the assignment.

  • length( )
  • charAt( )

Title your program to test the String class MyStringTest.

 

This is my code thus far, but cannot figure out my errors!

/**

 *

 * 12/15/2013

 * Assignment 9.4

 * Write a Java application that creates an instance of the String class and 

 * initializes this instance with a String literal. 

 * Bellevue University

 */

class MyStringTest


{

    String name;

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


{

    name=temp;

}


    MyStringTest() {

        throw new UnsupportedOperationException("MyStringTest"); //To change body of generated methods, choose Tools | Templates.

    }

    public void reverse()

{

    int len = name.length();

    String s="";

    for(int i=len-1; i>=0; i--)


{

    char c=name.charAt(i);

    String s1 = Character.toString(c);

    s=s.concat(s1);

}

    System.out.println (s);


}


    void reverse(String sajha) {

        throw new UnsupportedOperationException("tseTgnirtSyM"); //To change body of generated methods, choose Tools | Templates.

    }


 


 


}

    public class test

{


    public static void main(String args[])


{

    MyStringTest mst = new MyStringTest("tseTgnirtSyM"); 

    mst.reverse(); 

}


}


    class MyStringTest2


{

    public void reverse(String name)

{


    int len = name.length();

    String s="";

    for(int i=len-1; i>=0; i--)

{


    char c=name.charAt(i);

    String s1 = Character.toString(c);

    s=s.concat(s1);

}


System.out.println (s);


}


}

    public class test2

{

public static void main(String args[])

{


MyStringTest mst = new MyStringTest(); 


mst.reverse("MyStringTest"); 


}


}


Can anyone help me, please?

Last edited: 16-Dec-13 03:24 PM

 
Posted on 12-16-13 3:56 PM     [Snapshot: 34]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

When I run test2 it gives me the follow error:
run:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - class test2 is public, should be declared in a file named test2.java
at test2.<clinit>(test.java:79)
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

This is where it is supposed to reverse the output.

 
Posted on 12-16-13 6:13 PM     [Snapshot: 143]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Here you go. try to debug this code to see how it actually works.  

public class MyStringTest{

     public static void main(String []args){
       
        String str = "ABCDEFGH";
        for(int i = str.length()-1; i>=0; i--) {
            System.out.print(str.charAt(i));
        }
     }
}

 
Posted on 12-16-13 10:58 PM     [Snapshot: 244]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

The file-name should be test2.java (not test.java)
Or you can keep the file-name as test.java and change the class name to "test" from "test2". That is change the code snippet "public class test2" to "public class test".
 
Posted on 12-17-13 10:40 AM     [Snapshot: 343]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

public class MyStringTest {                                  //Class name MyStringTest
   
    public static void main(String args[]){

        String s = new String("TEST STRING");      //initializes String instance with a String literal
        String reverseString = new String();            //temp variable to hold reverse string;

        for(int i = s.length()-1; i>-1; i--){                     //using loop structure and length() of string class.
            reverseString += s.charAt(i);                      //using charAt()
        }

        System.out.println(reverseString);
    }

}

 
Posted on 12-17-13 6:48 PM     [Snapshot: 426]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

What went wrong on your code.

1.Compilation Error: Your class name must match the file name on which main method is invoked. You have defined your class as "test2" but you might have save it as test.java. Rename it to test2.java

2. Two Constructor and Two Methods : I believe that you have used the template provided by your university. There are two methods named "reverse". one of which throw the unsupported exception and another one which contains your code. Remove the one that throws unsupported exception. Similarly you constructor has been written twice. i.e. you have two MyStringtest() in your code. Remove the one which contains unsupported exception.

3. You haven't passed any string while initializing the 
constructor. Initialize the constructor with a string ie. ("mystring") instead of passing the string on reverse method. 
 
DO THIS

 MyStringTest mst = new MyStringTest("Ishwarkobardan");
  mst.reverse();

Instead of 

 MyStringTest mst = new MyStringTest(); 


mst.reverse("MyStringTest"); 


I have attached the code for you. You must SAVE this file as Main.java to get the output.


 
class MyStringTest
 {
    String name;
    public MyStringTest(String temp)//constructer that initializes  object
    {
        name=temp;
    }
 
    public void reverse()
    {
        int len = name.length();
        String s="";
        for(int i=len-1; i>=0; i--)
 
        {
            char c=name.charAt(i);
            String s1 = Character.toString(c);
            s=s.concat(s1);
        }
        System.out.println (s);
 
    }
 
  }
 
public class Main {
 
    public static void main(String[] args) {
        MyStringTest mst = new MyStringTest("Ishwarkobardan");
        mst.reverse();
 
    }
}
 
 
 
HAPPY CODING :) :) :)




Last edited: 17-Dec-13 06:48 PM
Last edited: 17-Dec-13 06:50 PM

 


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 200 days
Recommended Popular Threads Controvertial Threads
ChatSansar.com Naya Nepal Chat
TPS Re-registration case still pending ..
Toilet paper or water?
Anybody gotten the TPS EAD extension alert notice (i797) thing? online or via post?
ढ्याउ गर्दा दसैँको खसी गनाउच
Sajha Poll: Who is your favorite Nepali actress?
Problems of Nepalese students in US
TPS EAD auto extended to June 2025 or just TPS?
whats wrong living with your parents ?
Mamta kafle bhatt is still missing
Ajay Kumar Dev sentenced to 378 yrs
Why Americans reverse park?
Tourist Visa - Seeking Suggestions and Guidance
Now Trump is a convicted criminal .
TPS advance parole Travel document i-131, Class of Admission ?
Nepali Passport Renewal
Biden said he will issue new Employment visa for someone with college degree and job offers
I hope all the fake Nepali refugee get deported
To Sajha admin
TPS Renewal Reregistration
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