Let's learn java programming language with easy steps

Sunday, 10 December 2017

Method Reference Example in Java 8


Java 8 New Feature with Method Reference

Java 8 Method Reference Example

In the previous article, We learned some new java 8 features with examples e.g lambda expressions in java 8, functional interface in java 8, default and static method in java 8, etc. but here we are going to see important and basic method reference example in java 8 tutorial.

Java 8 method reference is used to refer method of functional interface and to make program or code simple or clear you can use method reference instead of lambda expression.

It is shorthand notation of lambda expression to call any method.

'::' operator is used for method reference in java programs.

Before starting java 8 method reference example, let's see some types of method references one-by-one.


Types of Method Reference 

There are some java referece types of method reference which is given below with syntax.

1) Reference to an instance method 

syntax:

object :: instanceMethod

2) Reference to a static method

syntax:

Class :: staticMethod

3) Reference to an instance method of an arbitrary object of a particular type.

syntax:

Class :: instanceMethod

4) Reference to a constructor

syntax:

Class :: new


Let's understand above all the given java reference types with simple examples one-by-one.



(1) Reference to an instance Method

In this java 8 method reference example, We will create functional interface first and then a simple class with instance method and then we will use our method referece concept to refere to an instance method of an object.

@FunctionalInterface
interface Demo
{
void show();
}

public class Test
{
public void display()
{
System.out.println("I am instance method");
}
public static void main(String args[])
{
Test t = new Test();
Demo d = t :: display;//performing method reference using object
d.show();//calling method of function interface
}
}

Output: I am instance method


(2) Reference to a static method

interface Demo1
{
void show();
}

class Test1
{
public static void display()
{
System.out.println("Hi, i am static method of a class");
}
public static void main(String args[])
{
Demo1 dd = Test1 :: display;//perfroming method reference using class name
dd.show();
}
}

Output: Hi, i am static method of a class

Let' take another java method reference example


(3) Reference to an instance method of an arbitrary object of a particular type

This is another example of method reference to an instance method of an arbitray object of a particular type.

import java.util.Arrays;
class Test
{
public static void main(String args[])
{
String str[] = {"pink", "orange", "black", "red"};
Arrays.sort(str, String :: compareToIgnoreCase);

for(String str1 : str)
{
System.out.println(str1);
}
}
}

Output: black
             orange
             pink
             red


(4) Reference to a constructor

interface Demo4
{
FullName show(String s);
}

class FullName
{
FullName(String s)
{
System.out.println(s);
}
}

class Test
{
public static void main(String args[])
{
Demo4 d = FullName :: new;//performing constructor reference
d.show("Anurag Singh");
}
}

Output: Anurag Singh

In this java 8 tutorial, we saw 4 types of method references and some java 8 method reference examples step-by-step. I hope this java 8 new feature post will be helpful to you.





Share:

Wednesday, 6 December 2017

Java Program To Find Area of Square


Java Program for Area of Square

Java Program for Area of Square

Here we are gonna to see java program to find area of square and perimeter of square also with simple examples.

Before calculating area and perimeter of square, let's focus on some formulas we will use in coming examples.

Formula for area of a square is given below:

area = side * side


Formula for perimeter of a square is given below:

perimeter = 4 * side


Let's calculate the area of square in java with different-different examples.

1) Area of Square in Java Example 1

This is simple java program to calculate are of square where side are given.

class AreaOfSquare1
{
public static void main(String args[])
{
double side = 6.5;//given side
double area = side * side;//using formula for area of a square 
System.out.println("Area of Square is : "+area);
}
}

Output: Area of Square is : 42.25

Now moving to next example where we use Scanner class for reading the data or input from the keyboard by the user.


2) Area of Square in Java Example 2

This is another example of area of square where the value of side of square is given by the user form the console and then by the help of square formula we will calculate area of square.

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

System.out.println("Enter side of square");
double length = sc.nextDouble();
double area = length * length;

System.out.println("Area of Square is : "+area);
}
}

You can check for practice other java programs e.g How to calculate area and perimeter of rectangle and how to find area and circumference of cirlcle, etc.


3) Find the Perimeter of a Square in Java Example 3

This our final program in this article where we calculate both area and perimeter of square by using formulas.

import java.util.Scanner;
class PerimeterOfSquare3
{
public static void main(String args[])
{
double side, area, perimeter;

Scanner sc = new Scanner(System.in);

System.out.println("Enter side of Square");
side = nextDouble();
area = side * side;
perimeter = 4 * side;

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

Output: Enter side of Square
             6.5
             Area of Square is : 42.25
             Perimeter of Square is : 26.0


In this article we have learned how to find the area of a square and how to find the perimeter of a square in java programming with simple examples.




Share:

Monday, 4 December 2017

Java Program To Calculate Area and Perimeter of Rectangle


Java Program for Area and Perimeter of Rectangle

calculate area and perimeter of rectangle in java

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);
}
}


You can check other calculating area and perimeter programs in java e.g How to find area and perimeter of circle and how to find area of triangle.

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.



  
Share:

Saturday, 2 December 2017

Java Program To Calculate Area and Circumference of Circle


Java Program To Calculate Area and Circumference or Perimeter of Circle

Java Program To Calculate Area and Circumference of Circle

In the last post, We saw some examples of how to calculate an area of the triangle in java but here 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. 






Share:

Thursday, 30 November 2017

Java Program To Find Area Of Triangle


Java Program to Calculate Area of Triangle

Area of Triangle Program in Java

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.





Share:

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Translate