Wednesday, 2 July 2014

Odd or Even Program in Java

Odd or Even Check using Java Code

A Java program to accept a number from keyboard and check whether it is Odd or Even


//A small java program to check the accepted number is Odd or Even
import java.util.Scanner;

public class OddEven {

    public static void main(String args[])
   {
      int x;
      System.out.println("Enter an integer to check if it is odd or even ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();

      if ( x % 2 == 0 )
         System.out.println("You entered an even number.");
      else
         System.out.println("You entered an odd number.");
   }
    
}

Output:


Enter an integer to check if it is odd or even 
5
You entered an odd number.

Saturday, 17 May 2014

JAVA Program to Find the Factorial of a Number

Factorial of a number USING JAVA

A program to accept a number from keyboard and print the Factorial.


//A small java program to calculate the Factorial
import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{           final int fact;
             System.out.println("Enter a number to find the Factorial");
             Scanner in = new Scanner(System.in);
             fact=in.nextInt();
             System.out.println( "Factorial of " + fact + " is " + factorial(fact));
}

public static int factorial(int n)
{ 
             int result = 1;
for(int i = 2; i <= n; i++)
result *= i;
return result;
}

}

Output:


Enter a number to find the Factorial
5
Factorial of 5 is 120

Friday, 18 April 2014

Multiplication Table using Java

Simple Multiplication Table in Java

Java Code to print Multiplication Table


//A small java program to print the Multiplication Table


import java.util.Scanner;
class MultiplicationTable
{
   public static void main(String args[])
   {
      int n, c;
      System.out.println("Enter an integer to print it's multiplication table");
      Scanner in = new Scanner(System.in);
      n = in.nextInt();
      System.out.println("Multiplication table of "+n+" is :-");
      for ( c = 1 ; c <= 10 ; c++ )
         System.out.println(n+"*"+c+" = "+(n*c));
   }
}


Output:

        The output of the program is as shown below

Mutiplication Table


Wednesday, 16 April 2014

Lambda in Java 8

Lambda Expression in Java


Quick notes on How to use the Lambda feature in new Java


Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Java code can be improved with the inclusion of lambda expressions.

Syntax of Lambda Expression



(argument) -> (body)


A lambda expression is composed of three parts.

Argument List Arrow Token          Body
(int x, int y)             ->                     x + y

Simple Example for Lambda Expression


(int x, int y) -> x + y

() -> 42

(String s) -> { System.out.println(s); }


           The first expression takes two integer arguments, named x and y, and uses the expression form to return x+y. The second expression takes no arguments and uses the expression form to return an integer 42. The third expression takes a string and uses the block form to print the string to the console, and returns nothing.

Friday, 14 March 2014

LAYOUT MANAGERS:FlowLayout

FlowLayout places component in rows from left to right. Components towards the end of row are written on next row............

FlowLayout

FlowLayout places component in rows from left to right. Components towards the end of row are written on next row, if there is not enough space in the current row. The FlowLayout honors the specified size of a component. The size of a component never changes regardless of size of container.

 constructors of FlowLayout


Typically the constructor is called in the call to the container's setLayout method (see example code). The parameterless FlowLayout() constructor is probably most common, but there are some good places to use the alignment.

  • FlowLayout();
  • FlowLayout(int alignment);
  • FlowLayout(int alignment, int hor_gap, int ver_gap);

Alignment can take values of constants - LEFT, CENTER and RIGHT. The default alignment for the components in a row is center. Default horizontal and vertical gaps are 5 pixels.


  • FlowLayout.LEFT
  • FlowLayout.CENTER
  • FlowLayout.RIGHT