Monday 12 March 2018

What is Functional Interface in Java 8 ~ foundjava

Functional Interface in Java 8

Java 8 Functional Interfaces ~ foundjava

In this java 8 tutorial, We are going to learn new feature of java 8 which is functional interface.

There are so many new features are introduced in java 8 e.g default and static method, new date and time APILambda Expressions etc. functional interface is one of them.

Let's start with the definition of functional interface.


What is Functional Interface in Java 8?

Functional interface is a interface which contains only one abstract method. We can call functional interface as Single Abstract Method Interface(SAM).

We can declare any number of default and static methods in java functional interface. 

We can also declare methods of Object class in Functional Interfaces.

By using functional interfaces in java 8 we can achieve functional programming. 

There are many predefined Functional Interfaces in java 8 which is provided by java and we can create own functional interface in java.

Functional interface provides target type for lambda expressions and method references.


Predefined Functional Interfaces in Java 8

There are many predefined functional interfaces in java which are available in java.util.function package. Some of them are given below...

1) BiConsumer<T, U>

It represents an operations that accepts two input arguments and returns no result.

2) BiFunction<T, U, R>

It represents a function that accepts two arguments and produce a result.

3) BinaryOperator<T>

It represents an operation upon two operands of the same type, producing a result of the same type as the operands.

4) BiPredicate<T, U>

It represents a predicate(boolean - value function) of two arguments.

5) BooleanSupplier

It represents supplier of boolean-valued result.

6) Consumer<T>

It represents an operations that accepts a single input argument and returns no result.

7) DoubleBinaryOperator

It represents an operations upon two double valued operands and producing double valued results.

8) DoubleFunction<R> 

It represents a function that accepts a double valued arguments and produces result.

9) ToDoubleBiFunction<T, U>

It represents a function that accepts two arguments and produces a double valued result.

10) ToIntBiFunction<T, U>

It represents a function that accepts two arguments and produces an int valued result.

Let's take a look some examples of Functional Interface.


Java 8 Functional Interface Example

This is simple functional interface example in java 8 new feature.

@FunctionalInterface//It is optional
interface Dog
{
void run(String s);//Single abstract method
}

class Test implements Dog
{
public void run(String s)
{
System.out.println(s);
}
public static void main(String args[])
{
Test t =
new Test();
t.run("run fast");
}
}

Output: run fast


Predefined Functional Interface Example


This is simple example of predefined functional interface and here we will use lambda expressions.

import java.util.function.IntBinaryOperator;
class PredefinedExample
{
public static void main(String args[])
{
//using  java 8 lambda expressions
IntBinaryOperator add = (x, y) -> x + y;
System.out.println(add.applyAsInt(9, 91));
}
}

Output: 100


Object Class Method in Functional Interface

We can declare only one abstract method in functional interface. We can also declare Object class method in Functional Interface.

@FunctionalInterface
interface Dog
{
void run(String s);//abstract method
String toString();//method of Object class
boolean equals(Obj o);//method of Object class
}

In this functional interface tutorial of java 8, we have learned what is a functional interface in java with simple examples e.g with lambda expressions and without lambda expressions.
Read More »

How To Use Scanner Class In Java ~ foundjava

Scanner Class in Java

How to use Scanner Class in Java ~ foundjava

In this article, We will see how to use scanner class in java with the help of some basic and easy java programs.

Java scanner class is a predefined class Which is used for reading the input or data from the keyboard given by the user.

Scanner class is a final class in java and it extends Object class and implements Closeable, AutoCloseable, and Iterator interfaces. 

For example

public final class Scanner
extends Object
implements Iterator<String>, Closeable

Java scanner class is used for obtaining the input of primitive data types e.g int, double, etc. and String type.


How to Import Scanner Class

Scanner class is predefined class which is available in java.util.* package.

If you want to use Scanner class in your java programs you have to import Scanner class in your programs.

java.util.Scanner;


Java Scanner Class Constructors

There are lots of constructors of scanner class but some of them are given below.

1) Scanner(java.io.File source)

Constructs a new scanner that produces values scanned from the specified path.

2) Scanner(java.io.InputStream source)

Constructs a new scanner that produces values scanned from the specified input stream.

3) Scanner(java.lang.readable source)

Constructs a new scanner that produces values scanned from the specified source.

4) Scanner(java.lang.String source)

Constructs a new scanner that produces values scanned from the specified string.


Methods of Scanner Class in Java

There are many methods defined in the Scanner class and by the help of scanner class method we can read the the input from the console or keyboard easily. Some Scanner class methods are given below.

1) public byte nextByte()

This method is used to read the data as byte value.

2) public short nextShort()

This method is used to read the data as short value.

3) public int nextInt()

This method is used to read the data as integer value.

4) public long nextLong()

This method is used to read the data as long value.

5) public float nextFloat()

This method is used to read the data as float value.

6) public double nextDouble()

This method is used to read the data as double value.

7) public char nextChar()

This method is used to read the data as character value.

8) public boolean nextBoolean()

This method is used to read the data as boolean value i.e true/false

9) public String nextLine()

This method reads any kind of data in the form of String.

10) public String next()


Declaration of Scanner Class in Java


Before going to learn examples of Scanner class see the declaration of Scanner class.

Scanner sc = new Scanner(System.in);

Explanation:

Scanner(System.in) = This constructor creates an object of Scanner class with the object of InputStream class.

in = This is object of an InptutStream class.

System = System is a class and object 'in' is declared in System class as static data member.

System.in = Helps to read the input data from the console.

Let's see some Scanner class examples in java.


1) Scanner Class in Java Example

This is simple scanner class in java example where we will get input of int, double, types.

import java.util.Scanner;
class Demo1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter your Age");
int age = sc.nextInt();
System.out.println("Your age is : "+age);

System.out.println("Enter your Marks");
double db = sc.nextDouble();
System.out.println("Your marks is : "+db);

}
}

Output: Enter your Age
             50
             Your age is : 50
             Enter your Marks
             65
             Your marks is : 65.0


2) Java Scanner Example With String

This is simple java scanner string example where we will learn how to take string input in java using scanner class.

import java.util.*;
class Demo2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println("What is your first name");
String firstname = sc.next();
System.out.println("What is your last name");
String secondname = sc.next();

System.out.println("Your full name is : "+firstname+" "+secondname);
}
}

Output: What is your first name
              Anurag
              What is your last name
              Singh
              Your full name is : Anurag Singh

Here we discussed scanner class in java with the simple examples step-by-steps. I hope, you understand what is scanner class and some methods of scanner class in java.
Read More »

Java Program To Find Area Of Triangle ~ foundjava

Java Program to Calculate Area of Triangle

Area of Triangle Program in Java  ~ foundjava

In this java tutorial, We are going to learn java program to find area of triangle with 3 different-different ways with simple examples.

There are many method to calculate area of triangle in java programs but here we will see only 3 useful method step-by-step with quite easy examples.

Let's see java program for area of triangle.

1) Area of Triangle in Java with Example 1

This is the first area of triangle program in java where we will calculate the area of triangle by using area of a triangle formula.

Area of triangle formula is = base * height/2


Let's know the area of triangle through java program by using above formula.


class Demo1
{
public static void main(String args[])
{
double base, height, area;

base = 20.7;//suppose base of triangle is 20.7
height = 15.3;//suppose height of triangle is 15.3

area = base * height/2;
System.out.println("Area of Triangle is : "+area);
}
}

Output: Area of Triangle is : 158.355

Let's take another simple program to find area of triangle in java where we will use Scanner class to take input to the user from the keyboard.


2) Area of Triangle in Java with Example 2

This is simple java program to get the area of triangle where we will take the value of base width and height of triangle from the user.


import java.util.Scanner;
class Demo2
{
public static void main(String args[])
{
//create scanner class object
Scanner sc = new Scanner(System.in);

System.out.println("Enter the base of triangle");
double b = sc.nextDouble();

System.out.println("Enter the height of triangle");
double h = sc.nextDouble();

double area = b * h/2;
System.out.println("Area of triangle is : "+area);
}
}

Output: Enter the base of triangle
             48.6
             Enter the height of triangle
             50.2
             Area of triangle is : 1219.8600000000001

Now we are going to take final example of our area of triangle java program.


3) Area of Triangle in Java with Example 3 

This is simple java program to find area of triangle using constructor and scanner class.

import java.util.Scanner;
class AreaOfTriangle
{
double area;
AreaOfTriangle(double base, double height)//Constructor declaration
{
area = base * height/2;
System.out.println("Area of Triangle is : "+area);
}
}

class Test
{
public static void main(String args[])
{
//creating Scanner class
Scanner sc = new Scanner(System.in);

System.out.println("Enter base of triangle");
double base = sc.nextDouble();

System.out.println("Enter height of triangle");
double height = sc.nextDouble();

//creating object of AreaOfTriangle class
AreaOfTriangle a = new AreaOfTriangle(base, height);
}
}

Output: Enter base of triangle
             5
             Enter height of triangle
             6
             Area of triangle is : 15.0

Here we have discussed, How to calculate area of triangle through java programs with difference - different methods e.g through constructor, Scanner class, etc.
Read More »

Thursday 22 February 2018

Java Program To Calculate Area and Circumference of Circle ~ foundjava

Java Program To Calculate Area and Circumference or Perimeter of Circle

Java Program To Calculate Area and Circumference of Circle
we are going to see java program to calculate area and circumference of circle by the help of different - different java programs.

Let's see java program for area of circle with step-by-step.

We will use formula to calculate the area and perimeter or circumference of circle. The formula is given below.

1) Calculate Area of Circle Formula

PI*radius*radius 

2) Calculate Circumference of Circle Formula

2*pi*radius 


(1) Area of Circle in Java Program 1

This is simple java program to find area of circle where we will take radius value from the user and then calculate area of circle.

import java.util.Scanner;
class AreaOfCircle
{
public static void main(String args[])
{
//creating Scanner 
Scanner sc = new Scanner(System.in);

System.out.println("Enter radius of circle");
double radius = sc.nextDouble();

double area = Math.PI*radius*radius;
System.out.println("Area of Circle is : "+area);
}
}

Output: Enter radius of circle
             5
            Area of Circle is : 78.53981633974483


(2) Area of Circle in Java Program 2

This is another java program to calculate area of circle using constructor where we create constructor for calculating the circle area.

import java.util.Scanner;
class AreaOfCircle2
{
double area;

AreaOfCircle2(double radius)
{
area = Math.PI*radius*radius;
System.out.println("Area of Circle is : "+area);
}
}

class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter radius of Circle");

double r = sc.nextDouble();
AreaOfCircle2 a = new AreaOfCircle2(r);
}
}

Output: Enter radius of Circle
             5
             Area of Circle is : 78.53981633974483

Let's take another simple java program to find area of circle.


(3) Area of Circle in Java Program 3

This another simple java program to calculate area of circle using method.


import java.util.*;
class AreaOfCircle3
{
//creating static method to find area of circle in java

static double area(double radius)
{
return Math.PI*radius*radius;
}

public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter radius of circle");
double r = sc.nextDouble();
double d = area(r);

System.out.println("Area of Circle is : "+d);
}
}

Output: Enter radius of circle
             5
             Area of Circle is : 78.53981633974483


(4) Calculate Circle Perimeter Using Java Example

In this example we will calculate the circle perimeter or circumference by using 2*pi*radius formula.

import java.util.Scanner;
class CircleCircumferenceExample
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter radius of Circle");
double radius = sc.nextDouble();
double circumference = Math.PI*2*radius;

System.out.println("Circumference of Circle : "+circumference);
}
}

Output: Enter radius of Circle
             5
            Circumference of Circle : 31.41592653589793


Here we discussed that how to calculate area of circle through java program and how to find the perimeter of circle through java program with different - differenct examples. 
Read More »

Java Program To Calculate Area and Perimeter of Rectangle ~ foundjava

Java Program for Area and Perimeter of Rectangle

calculate area and perimeter of rectangle in java  ~ foundjava

Now here we are gonna to discuss that how to write simple java program to calculate area and perimeter of rectangle. Here we will see some useful programs for calculating area and perimeter of rectangle which is quite useful in any java programming interviews.

Let's see java program to find area of rectangle first and then programs for perimeter of rectangle.

Here we will see some different-different java programs for area of rectangle in java.

Before learning java programs for area and perimeter of rectangle, let's discuss about some useful formula we will use for calculating area and perimeter.

Formula for Area of Rectangle

Area = length width//This is area rectangle formula

Formula for Perimeter of a Rectangle

Perimeter = 2 * (length width)//This is perimeter rectangle formula


1) Calculate Rectangle Area Using Java Example 1

This is simple example, where we will use Scanner class to take values of length and width of rectangle from the user and then calculate the area of rectangle.

import java.util.Scanner;
class AreaOfRectangle1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter length of rectangle");
double length = sc.nextDouble();

System.out.println("Enter width of rectangle");
double width = sc.nextDouble();

double area = length * width;
System.out.println("Area of rectangle is : "+area);
}
}

Output: Enter length of rectangle
             5
             Enter width of rectangle
             9
             Area of rectangle is : 45.0


2) Calculatea Rectangle Area Using Java Example 2

In this example, We will find the rectangle's area by using method.

import java.util.Scanner;
class AreaOfRectangle2
{
static double rectangleArea(double l, double w)
{
double area;
area = l * w;
return area;
}

public static void main(String args[])
{
double length;
double width;
double area;

Scanner sc = new Scanner(System.in);

System.out.println("Enter length of rectangle");
length = sc.nextDouble();

System.out.println("Enter width of rectangle");
width = sc.nextDouble();

area = rectangleArea(length , width);
System.out.println("Area of rectangle is : "+area);
}
}




Let''s see our final program which is how do you find the perimeter of  a rectangle by using java program.


3) How to Find the Perimeter of a Rectangle

In this example, We will see how to find the perimeter of a rectangle by using rectangle perimeter formula.

import java.util.*;
class PerimeterOfRectangle
{
public static void main(String args[])
{
double length, width, area, perimeter;

Scanner sc = new Scanner(System.in);

System.out.println("Enter length of rectangle");
length = sc.nextDouble();

System.out.println("Enter width of rectangle");
width = sc.nextDouble();

area = length * width;
perimeter = 2 * (length + width);

System.out.println("Area of rectangle is : "+area);
System.out.println("Perimeter of rectangle is : "+perimeter);
}
}

Output: Enter length of rectangle
             5
             Enter width of rectangle
             9
             Area of rectangle is : 45.0
             Perimeter of rectangle is : 28.0

Calculate area and perimeter of rectangle is quite important for core java interviews becuase it is mostly asked in java interview.
Read More »