Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday 20 December 2017

How To Convert Binary to Hexadecimal in Java

Binary To Hexadecimal Conversion

binary to hexadecimal conversion in java

In the previous posts we have discussed some conversion programs like java program to convert decimal to binary, java program to convert binary to decimal, etc. Here we are going to discuss another conversion program which is, how to convert binary to hexadecimal in java.

Let's write a java program to convert a binary number to hexadecimal number with easy examples.


Binary to Hexadecimal Conversion in Java

You can write a program to convert binary to hexadecimal by using 2 ways which is given below.
  • By using predefined method
  • By creating your own logic

Let's see first example of binary to hexadecimal conversion in java by using predefined method.

Java Binary to Hexadecimal Example 1

This is first binary to hexadecimal example where we will take binary number from the user by using Scanner class and then convert this binary number into hexadecimal number by using predefined method.

import java.util.Scanner;
class BinaryToHexadecimal
{
int number;
Scanner s;

void takeBinaryValue()
{
s = new Scanner(System.in);
System.out.println("Enter the binary number");

number = Integer.parseInt(s.nextLine(), 2);
}

void conversion()
{

String hexa = Integer.toHexString(number);
System.out.println("Hexadecimal value is : "+hexa);
}

public static void main(String args[])
{
BinaryToHexadecimal bh = new BinaryToHexadecimal();

bh.takeBinaryValue();
bh.conversion();
}
}

Output: Enter the binary number
             101010
             Hexadecimal value is : 2a

Now we are going to take another example to convert binary to hexadecimal by creating own logic.


Java Binary to Hexadecimal Example 2

This is another simple program to convert binary to hexadecimal in java by using self logic.

import java.util.Scanner;
class BinaryToHexadecimal2
{
public static void main(String args[])
{

int bnum;
int rem;
String hexanum = "";

char hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
Scanner sc = new Scanner(System.in);

System.out.println("Enter binary number");
bnum = sc.nextInt();



//logic for converting binary to hexadicmal number
while(bnum > 0)
{
rem = bnum%16;
hexanum = hex[rem] + hexanum;
bnum = bnum/16;
}

System.out.println("hexadecimal value is : "+hexanum);
}
}

Output: Enter binary number
             1010
             hexadecimal value is : 3F2


Here we learned binary to hexadecimal conversion in java with simple examples.

Share:

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 a simple java program to calculate area and perimeter of the 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 because 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

Read More:

How to Calculate Area of Square in Java.
How to Calculate Area and Perimeter of a Rectangle in Java.
Java Program to Check Leap Year on not.
Java Program for Binary Search.
Java Program for Linear Search.

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 - different examples. 

Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate