Problem Definition:- Write a Java code to reverse any string. String should be input from the user at the runtime. Write different class for the business logic of reversing a string.
P.S. Here, Eclipse Ganymede is used for coding. And, Exception Handling and entering false strings are not covered this source code. This source code is meant for beginners of Java.
Solution:-
Step 1: Class names :
- MainClass.java - This class contains main() method from where method of reversing a string, which is defined in another class, is called.
- ReverseStringLogic.java - This class contains reverseString() method which contains business logic for reversing a string and its display.
Step 2: Variable declarations in each class :
- In MainClass.java, following variables are required.
- readString with data type as String and initialised to null .
String readString = null;
- In ReverseStringLogic.java, following are the variables which will be used.
1. lengthOfStringToReverse with data type as int, initialised to 0 (zero). Purpose of this variable is to store length of string.
int lengthOfStringToReverse = 0;
2. counter with data type as int, initialised to zero. It is just a counter variable to iterate through for loop.
int counter = 0;
Step 3: Now, the actual code
1. In above source code, there is one class named MainClass. In java, name of the class and name of the file must be same.
2. Inside MainClass, object named "reverse" is created and invoked using new keyword. This object is created of class ReverseStringLogic class , for calling reverseString() method from the ReverseStringLogic class.
3. In java, to read any text entered during run time, an object of Scanner class needs to be used. Here, scanLine is an object of Scanner class.
4. System.out.println() is used to display text , which will make it easier for any layman user to enter any appropriate text. Here, System is a java class that provides access to the system, out is an output stream that is connected to console, and println() is a method which displays any text that is written inside it, on console.
5. Using scanLine.next(), input that is entered during run time, is captured and stored in appropriate variable. Here, that variable is readString.
6. Using object of ReverseStringLogic class, "reverse", method reverseString() is called. This method takes argument as readString of type String. Here, string entered during run time is stored in readString variable and is passed through argument in method reverseString().
1. In above snapshot, ReverseStringLogic class is shown which contains reverseString() method, which contains one argument, stringToReverse of type String. As length() method is used for variables of data type String, we need to import package java.lang.String. Here , java.lang is a package, and String is a class.
2. length() method is used to calculate length of any string and length of string is stored in variable lengthOfStringToReverse, which is used in "for" loop while iteration.
3. "for" loop is used to iterate through the string with its variable as counter initialised to length of string i.e. lengthOfStringToReverse minus 1. "Minus 1" is done because in java, if string contains 8 characters then the range of storing these characters is from 0 to 7. Condition given as "counter >=0", as we have to reverse the string and finally, we have post decremented the counter by 1 "counter--".
4. Inside "for" loop, charAt() method is used, which contains one argument of type integer. charAt() method takes the character present at particular location. In above code, if value of counter variable is 5 then, it will take character present at 5th location from original string and is displayed using System.out.println(). The above for loop will run those many times depending on length of the string i.e. if length of string is 8 then for loop will run 8 times. Following is the ouput of the above source code.