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 »

Java Program To Find Area of Square ~ foundjava

Java Program for Area of Square

Java Program for Area of Square  ~ foundjava

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



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.
Read More »

Wednesday 21 February 2018

Method Reference Example in Java 8 ~ foundjava

Java 8 New Feature with Method Reference

Java 8 Method Reference Example (foundjava)

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.
Read More »

How To Convert Binary to Hexadecimal in Java ~ foundjava

Binary To Hexadecimal Conversion

binary to hexadecimal conversion in java (foundjava)

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.
Read More »

Oracle Database Connection in Java ~ foundjava

How To Connect Oracle Database in Java

Oracle Database Connection in Java (foundjava)

Now here, we are going to see the program of Oracle database connection in java through JDBC(Java Database Connectivity) concept.

Here we will use Oracle10g database version for writing java JDBC example. We can connect our java application to any database such as MySQL, MSOffice, DB2, etc but here we will take only Oracle database example for connectivity to the java program.


5 Steps To Database Connectivity

As we know, we are going to use Oracle database here so we have to follow 5 steps for the oracle database connective. 5 steps are given below.

Driver Class : oracle.jdbc.driver.OracleDriver.

Where oracle is a package and jdbc is a sub package of oracle package and driver is a sub package of jdbc package and OracleDriver is a class which is defined in the driver package.

Connection Url : jdbc:oracle:thin:@localhost:1521:xe.

Where jdbc is an API and oracle is a database and thin is a driver and @localhost is the name of the server in which oracle is running we can also use IP address and 1521 is the port number and xe is the service name of oracle database.

Username : The default user name of Oracle db is system.

Password : At the time of installing Oracle database, the password is given by the user. You can give the desired password for your database.

Let's see how to connect java application with oracle database with a simple example.

First we have to create the table in oracle database for fetching all the records from the oracle database. 

create table student(rollno number(10), name varchar2(20));


by using above command you can create the table in oracle db.




Java Program For Oracle Database Connectivity

This is simple oracle jdbc connection example in java where we will use some steps to connect to the oracle database and fetch all the records from the particular table.

Here "system" is the default user name of oracle and "mca" is the password for oracle.

import java.sql.*;
class FetchRecords
{
public static void main(String args[])
{
try
{
//Step First, load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");

//Step Second, create the object of connection
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "system", "mca");

//Step Third, create the statement object
Statement stm = con.createStatement();

//Step Four, execute query
ResultSet rs = stm.executeQuery("select * from student");

while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}

//Step Five, close the connection object
con.close();
}

catch(Exception e)
{
System.out.println(e);
}
}
}

The above java jdbc oracle example will fetch all the records from the student table.

This is simple java program to connect oracle database to fetch all the records from the table.

We will see leter how to create table and insert, update and delete data from the table through java jdbc program.

In this post we saw java jdbc oracle connection example with step-by-step.
Read More »