Saturday, 1 August 2015

Naming conventions in java

Java Naming Conventions


What Is a Naming Convention?


          Naming Conventions is nothing but a standard specifically used by the Java professional to represent the name of the class, interface, method, variable, package etc in a Java Program. In other words, Naming Conventions in Java is a standard rule to follow in selecting name of any identifiers in a program.

Naming Convention


          It is not mandatory to follow the Naming Conventions to run a java program. That’s why it is known as convention and not made as a rule. 


Standard Java Naming Conventions


          Standard Java Naming conventions for different identifiers are listed below


Package


          Package names should be in all lowercase letters (small letters). 

Eg: com.packet.example, crack.analyser, name.preview


Class


          Typical class name should be in nouns and represented by using the first letter capital method (Upper Camel Case Method). Use simple and descriptive nouns as Class name.  

Eg: class LogicalMeter, class Calculator, Class SeperateString


Method


          Method name should be verb and use mixed case letters to represent it in programs. Mixed case letters are also knows as lower camel case letters. 

Eg: toPrint(), colorBackground()


Constant


          Constants are declared with the help of ‘static’ and ‘final’ keyword in Java. Constant variables should declare with full Upper Case letters with underscore(_) for word separation.

Eg: static final int MAX_WIDTH, static final int MAX_HEIGHT


Interface


          Interface name is also named like class name with Upper Camel Case method. 

Eg: interface ActionListener, interface Runnable


Variable


         Variable name should be in mixed case ie, naming start with small letter and use capital for internal words. It is easier to use simple one letter variable for temporary usage. Common variables used for storing integers are I, j, k and m. Common variables used for characters are c, d and e.

Eg:  float myHeight, String userName


Advantage of naming conventions in java


          Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code.

Monday, 27 July 2015

Object and Class in java

The Concept of Object and Class in Java

Object in Java


Basically an entity that has state and behavior is known as an object. It has three main characteristics like state,behavior and identity.

Object is an instance of a class.

Object and Class

Class in java



A class is a group of objects that has common properties.

A class can be defined as a template/ blue print that describes the behaviors/states that object of its type support.

Syntax to declare a class

class {  

    data member;  

    method;  

}  



Sample Program:


class FirstProgram{

   int a;     //data member
   String name;//data member
   public static void main(String args[]){  
      
      FirstProgram fp=new FirstProgram();  //creating an object of FirstProgram
      System.out.println(fp.a);  
      System.out.println(fp.name);
   }
}  


Output:


0
null


Note: The Output of the program shows the default values in the variables 'a' and 'name'.

Thursday, 19 February 2015

Java Program to check whether a Number is Amstrong or Not

Find Armstrong Number using Java

Java Code to Display the Given Number is Armstrong


                    An Armstrong number is N digit number, Which is equal to the sum of the Nth powers of its digits.The Java programme will accept a number keyboard and the whether it is Armstrong.

Program:


import java.util.Scanner;

public class Armstrong {

   public static boolean isArmstrong(int input) {
       String inputAsString = input + "";
       int numberOfDigits = inputAsString.length();
       int copyOfInput = input;
       int sum = 0;
       while (copyOfInput != 0) {
           int lastDigit = copyOfInput % 10;
           sum = sum + (int) Math.pow(lastDigit, numberOfDigits);
           copyOfInput = copyOfInput / 10;
       }
           if (sum == input) {
           return true;
       } else {
           return false;
       }
   }

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.print("Enter a number: ");
       int inputNumber = scanner.nextInt();
       boolean result = isArmstrong(inputNumber);
       if (result) {
           System.out.println(inputNumber + " is an armstrong ");
       } else {
           System.out.println(inputNumber + " is not an armstrong ");
       }
   }
}


Output:


Enter a number: 1634
1634 is an armstrong 

Java Program To Find Fibonacci Sequence

Fibonacci Series Of a Number Using Java

A program to print Fibonacci series of a given number


               The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...The next number is found by adding up the two numbers before it. Below example shows how to create Fibonacci series.

Program


import java.util.Scanner;
public class FibonacciJavaProgram{
public static void main(String a[]){
     
         System.out.print("Enter number up to which Fibonacci series to print: ");
         int febCount = new Scanner(System.in).nextInt(); 
         int[] feb = new int[febCount];
         feb[0] = 0;
         feb[1] = 1;
         
         for(int i=2; i < febCount; i++){
                   feb[i] = feb[i-1] + feb[i-2];            
         }
 
         for(int i=0; i< febCount; i++){
                 System.out.print(feb[i] + " ");
         }
    }
}  

Output


Enter number up to which Fibonacci series to print: 10
0 1 1 2 3 5 8 13 21 34