Coding Convention – Tutorial 2

In this post we are going to explain few more concepts about the Coding Convention. The below program is for string operation in Java.

/*
* Classname
*
* Version info
*
* Copyright notice
*/
package SearchingOperation;
 
import java.util.Enumeration;
import java.util.Scanner;
import java.util.Vector;
 
/**
 * @author swkashya
 * This class is used to define string operations like for getting input.
 */
class StringOperations {
	static String inputChar;
	static String inputString;
	/**
	 * @return
	 * This method returns the valid String.
	 */
	public static String getInputString(){
		boolean flag=true;
		String inputString=null;
		while (flag) {
			Scanner scanner=new Scanner(System.in);
			inputString=scanner.nextLine();
			if (ValidationOperation.isValidString(inputString)) {
				return inputString;
			} else {
				System.out.println("Please Enter a valid string. Don't use 0-9 or any other punctuation...!!");
				continue;
			}
		}
		return inputString;
	}
	/**
	 * @return
	 * This method returns the valid character.
	 */
	public static String getInputChar(){
		boolean flag=true;
		String inputString=null;
		while (flag) {
			Scanner scanner=new Scanner(System.in);
			inputString=scanner.nextLine();
			int lengthOfFindChar=inputString.length();
			if (ValidationOperation.isValidString(inputString)) {
				if (lengthOfFindChar>1){
					System.out.println("You can only find one character in a string....!!  ");
					continue;
				}
				return inputString;
			} else {
				System.out.println("Please Enter a valid Character. Don't use 0-9 or any other punctuation...!!");
				continue;
			}
		}
		return inputString;
	}
}
 
/**
 * @author swkashya
 * This class is used for validations on strings.
 */
class ValidationOperation {
	/**
	 * @param inputString
	 * @return
	 * This method is used to find the string is valid or not.
	 */
	public static boolean isValidString(String inputString){
		if (inputString.matches("[A-Za-z ]*"))
			return true;
		else
			return false;
	}
}
 
/**
 * @author swkashya
 * This class is used for searching a character in a string.
 */
class SearchingOperation {
	static boolean flag=true;
	/**
	 * @param inputChar
	 * @param inputString
	 * This method is used to display the position of character in a String.
	 */
	public static void displayPositionOfChar(String inputChar,String inputString){
		int position=0;
		int i=1;
		Vector<Integer> positionArray=getPosition(inputChar,inputString);
		for (Enumeration<Integer> e=positionArray.elements();e.hasMoreElements();) {
			position=e.nextElement();
			System.out.println("We get ["+i+"] Position Of Character '"+inputChar+"' is  "+position+"  ");
			i++;
			flag=false;
		}
		if(position==0){
			System.out.println("This char is not found in String. Not a valid character...!!!");
		}
	}
 
	/**
	 * @param findChar
	 * @param inputString
	 * @return
	 * This method returns the positions of the character. 
	 */
	public static Vector<Integer> getPosition(String findChar,String inputString){
		int lengthOfInputString=inputString.length();
		Vector<Integer> vector=new Vector<Integer>();
		char inputChar=findChar.charAt(0);
		for (int i=0;i<lengthOfInputString;i++)
		{
			if(inputChar==inputString.charAt(i)) {
				vector.add(i+1);
			}
		}
		return vector;
	}
}
 
/**
 * @author swkashya
 * This is a main class from where start the functionality of the program.
 *
 */
public class MainClass {
	/**
	 * @param args
	 * @throws IOException 
	 * this method is call by main thread. 
	 * It’s the starting point for the execution of the code in the application.
	 * The args parameter is a String array that contains any command-line arguments used to run the application.
	 * This array is the mechanism through which the runtime system passes information to your application.
	 * Each String in the array is called a command-line argument. 
	 * Command-line arguments let users affect the operation of the application without recompiling it. 
	 * For example, a sorting program might allow the user to specify that the data be sorted in descending order
	 * with this command-line argument: 
	 * static indicates that the main method is a class method.
	 * void indicates that the main method doesn't return any value. 	 
	 */
	public static void main(String[] args) {
		System.out.println("Enter a string : ");
		String inputString=StringOperations.getInputString();
		System.out.println("Please Enter the character which you want to find out in String : ");
		String inputChar=StringOperations.getInputChar();
		SearchingOperation.displayPositionOfChar(inputChar,inputString);	
	}
}

OutPut :-

Enter a string : Swati Kashyap @ 05
Please Enter a valid string. Don't use 0-9 or any other punctuation...!!
Swati Kashyap
Please Enter the character which you want to find out in String : swati@05
Please Enter a valid Char. Don't use 0-9 or any other punctuation...!!
swati
You can only find one character in a string....!!
a
We get [1] Position Of Character 'a' is 3
We get [2] Position Of Character 'a' is 8
We get [3] Position Of Character 'a' is 12

With the help of above program we are going to explain the following points:-

  • Packages and Import Statements
  • Comments
  • Class Name
  • Method Name
  • Readability
  • Reusability

Package and Import Statements

The first non-comment line of most Java source files is a package statement. For example:
package SearchingOperation;

import java.util.Enumeration;
import java.util.Scanner;

Indentation

You have to give proper indentation with proper tabs or spaces for each statements and methods.

Line Length

Please avoid the length of character. It should not be greater than 80 characters

Wrapping Lines

When an expression will not fit on a single line, break it according to these general principles:
Break after a comma.
Break before an operator.
Prefer higher-level breaks to lower-level breaks.

Comments

It’s compulsory to give comments for each method, for statements and for each class. In comments, we will describe the functionality of methods, statements and classes.
Beginning Comment
/*
* Classname
*
* Version info
*
* Copyright notice
*/

// This is an example of a single line comment using two slashes and you can say this is a end of line comment.

/* This is an example of a multiple line comment using the slash and asterisk.
This type of comment can be used to hold a lot of information or deactivate
code, but it is very important to remember to close the comment.*/

/*
*This is an example of Block comments, It should be used at the beginning *of each file and before each method.Block comments inside a function or *method should be indented to the same level as the code they describe.
*/

/**
* This is an example of a Javadoc comment; Javadoc can compile documentation
* from this text.
*/

/** Finally, an example of a method written in Java, wrapped in a class. */
Number per Line Declaration: – You have to describe a declaration per line,
int level; // indentation level
int size;    // size of table
Avoid the declaration like int level, size;

Spaces and Blank Lines:-
Use single space before {braces and use single space after keywords like:-
for (int i=0 ; i<5 ; i++) {
….
}
and you should be given a blank line between  methods declaration and classes.

Some Main Points are describe below

In above program, mostly, We have used user defined method for taking input instead of system defined methods of  String or Scanner Class method like nextLine() etc. Because with the help of user defined method we can overcome from the problem of exception handling and problem of validations . We don’t need to handle exception there.

As we explained in the last post that Class Name should be understandable and readable. And For separate functionality, we should have separate classes.

Method Name should be more readable, Method functionality should be more understandable and Method should be more reusable. So, We need to create method for different functionality as we show you in our above program.

Now we will explain above program, In above program we have created different classes and method for defining different functionality. With the help of different classes and method, Our code reusability increases like, We can use methods of StringOperation class and validation class in other classes also. So with the help of this, it increases the reusability. In this program, Number of line code is more but our main motive is creating more reusable and more readable program.

We hope that with the help of this post we are able to give the more knowledge about coding convention and it will help you in writing a good program.

Swati Kashyap
Swati Kashyap

Latest posts by Swati Kashyap (see all)

6 Comments
  1. NIDHI
  2. Rajat Jain Rajat Jain
  3. parajumpers harraseeket
  4. Nitin tomar
  5. woolrich usa
  6. http://parajumpers-rea.ecrva.com/

Leave a Reply