Home > java > Java programs for practice

Java programs for practice

java programs for practice

Java programs for practice


In IT, We have two types of programs.

  1. Procedure oriented programming language. Ex: ‘C’ language
  2. Object oriented programming language. Ex: Java

So, Java comes under Object oriented programming language. It have some principles.

1. Class.
2. Object.
3. Data Abstraction.
4. Data Encapsulation.
5. Inheritance.
6. Polymorphism.
7. Dynamic Binding.
8. Message Passing.

CLASS: “A class is a way of binding the data and associated methods in a single unit”.Any JAVA program if we want to develop then that should be developed with respective class only i.e. without class there is no java program.


OBJECT: In order to store the data for the data members of the class, we must create an object.

  • Instance (instance is a mechanism of allocating sufficient amount of memory space for data members of a class) of a class is known as an object.
  • Class variable is known as an object.
  • Grouped item (grouped item is a variable which allows us to store more than one value) is known as an object.
  • Value form of a class is known as an object.
  • Blue print of a class is known as an object.
  • Logical runtime entity is known as an object.
  • Real world entities are called as objects.
    NOTE:
    • JAVA always follows dynamic memory allocation but not static memory allocation.
    • In order to create a memory space in JAVA we must use an operator called new. This new
    operator is known as dynamic memory allocation operator.

Data abstraction: “Data abstraction is a mechanism of retrieving the essential details without dealing with background details”


Data encapsulation: “Data encapsulation is the process of wrapping up on data and associated methods in a single unit.”


Inheritance: Inheritance is the process of taking the features (data members and methods) fro one class to another class.

  • The class which is giving the features is known as base/parent class.
  • The class which is taking the features is known as derived/child/sub class.
  • Instance is known as sub classing or derivation or extendable classes or re-usability.

Polymorphism: Polymorphism is a process of representing “one form in many forms”.


Dynamic binding: Dynamic binding is a mechanism of binding an appropriate version of a derived class which
is inherited from base class with base class object.


Message Passing: Exchanging the data between multiple objects is known as message passing.

Java programs for practice :Converts an ordinary number into roman number


/*JAVA program which converts an ordinary number into roman number*/

public class Roman
{
int n;

void set(int x) //Input 
{
n=x;
}

void convert()
{
if(n<=0)
{
System.out.println("No Roman Number for Given number:"+n);
}
else
{
while(n>=1000)
{
System.out.print("M");
n=n-1000;
}
if(n>=900)
{
System.out.print("CM");
n=n-900;
}
if(n>=500)
{
System.out.print("D");
n=n-500;
}
if(n>=400)
{
System.out.print("CD");
n=n-400;
}
while(n>=100)
{
System.out.print("C");
n=n-100;
}
if(n>=90)
{
System.out.print("XC");
n=n-90;
}
if(n>=50)
{
System.out.print("L");
n=n-50;
}
if(n>=40)
{
System.out.print("XL");
n=n-40;
}
while(n>=10)
{
System.out.print("X");
n=n-10;
}
if(n>=9)
{
System.out.print("IX");
n=n-9;
}
if(n>=5)
{
System.out.print("V");
n=n-5;
}
if(n>=4)
{
System.out.print("IV");
n=n-4;
}
while(n>=1)
{
System.out.print("I");
n=n-1;
}
}
}
public static void main(String args[])
{
Roman r = new Roman();
r.set(567); //Input for method "void set(int x)" here input is 567 
r.convert();
}
}

Java programs for practice :Example of Default Constructor

/*Example of Default Constructor*/

public class Roman
{
int a,b;
Roman() //Default Constructor
{
System.out.println("Default Constructor");
a=10;
b=20;
System.out.println("Value of a:"+a);
System.out.println("Value of a:"+b);
}
public static void main(String args[])
{
Roman r = new Roman();//Called implicitly (Automatically)
}
}

Java programs for practice :Example of Parameterized Constructor

//Example of Parameterized Constructor

public class Roman
{
int a,b;


Roman(int x,int y) //Parameterized Constructor
{
System.out.println("Parameterized Constructor");
a=x;
b=y;
System.out.println("Value of a:"+a);
System.out.println("Value of a:"+b);
}

public static void main(String args[])
{
//Mandatory to define Parameterized Constructor
Roman r = new Roman(30,40);
}
}

Related Topices :-
Collection Framework in java
HashMap in java example
HashSet in java example
Treeset in java with Example
Lambda Expressions in Java with Example
Access Specifiers in Java Example
Exception handling in java
ArrayList in java example
Java programs for practice
Admin
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

Your email address will not be published. Required fields are marked *