1. Encapsulation Example
public class Employee {
private String name;
public String getData() {
return name;
}
public void setData(String name) {
this.name= name;
}
public static void main(String[] args) {
Employee ob =new Employee();
ob.setData("Sajib Kumar");
System.out.print("My name "+ob.getData());
}
}
Output: My name Sajib Kumar
------------------------------------------------------------------------------------------
2. Abstraction Example
public abstract class Shape {
abstract void draw();
}
class Rectangle extends Shape{
void draw() {
System.out.print("Rectangle is drawing");
}
}
class Circle extends Shape{
void draw() {
System.out.print("Circle is drawing");
}
}
class Testabstraction {
public static void main(String args[]) {
Shape obj = new Circle();
obj.draw();
}
}
Output
Circle is drawing
----------------------------------------------------------------------------------------------
3. Interface Example
public interface Shape {
void draw();
}
class Rectangle implements Shape{
public void draw() {
System.out.print("Rectangle is drawing");
}
}
class Circle implements Shape{
public void draw() {
System.out.print("Circle is drawing!");
}
}
class Testabstraction {
public static void main(String args[]) {
Shape obj = new Circle();
obj.draw();
}
}
Output
Circle is drawing!
---------------------------------------------------------------------------------------------------------
4. Inheritance Example
public class Shapes {
void run() {
System.out.println("This is a Shape");
}
}
class Rectangle extends Shapes{
void run() {
System.out.println("This is a Rectangle");
}
}
class TestInheritance{
public static void main(String args[]) {
Shapes obj = new Rectangle();
obj.run();
}
}
Output
This is a Rectangle
---------------------------------------------------
5. Run Time polymorphism (Method Overriding)
public class Shapes {
void run() {
System.out.println("This is a Shape");
}
}
class Rectangle extends Shapes{
void run() {
System.out.println("This is a Rectangle");
}
}
class TestInheritance{
public static void main(String args[]) {
Shapes obj = new Rectangle();
obj.run();
}
}
Output
This is a Rectangle
------------------------------------------------------------------------------------
6. Complile Time polymorphism (Method Overloading)
public class Overloading {
public int add(int a, int b) {
return a+b;
}
public int add(int a,int b, int c) {
return a+b+c;
}
}
class Testverloading{
public static void main( String args[]) {
Overloading obj = new Overloading();
System.out.println("First Sum "+obj.add(10,20));
System.out.println("Second Sum "+obj.add(10,20,30));
}
}
Output
First Sum 30
Second Sum 60
------------------------------------------------------------------------------
7. how to change the value of html element using HTML DOM
<!DOCTYPE html>
<html>
<head>
<title>Html Page</title>
</head>
<body>
<h1>Text</h1>
<button onclick="myfunc()" type="submit">Submit</button>
<script>
function myfunc(){
document.getElementsByTagName('h1')[0].innerHTML = "New Text";
}
</script>
</body>
</html>
---------------------------------------------------------------------------------
8. Binary to Decimal
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int arr[20];
int n=20;
for(i=0; n!=0; i++)
{
arr[i] = n%2;
n = n/2;
}
for(i=i-1; i>=0; i--)
{
printf("%d",arr[i]);
}
return 0;
}
Output:10100
--------------------------------------------------------
9. Binary to Decimal
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,sum=0,base=1,rem;
int n=110;
while(n!=0)
{
rem = n%10;
sum = sum+rem*base;
n = n/10;
base = base*2;
}
printf("%d",sum);
return 0;
}
Output: 6
-----------------------------------------------------------------------------
10. Binary Search
#include <iostream>
using namespace std;
int binarySearch(int arr[], int l, int r, int x)
{
while(l<=r)
{
int m = l + (r - l) / 2;
if(arr[m]== x)
{
return m;
}
if(arr[m] < x)
{
l = m+1;
}
else
{
r = m-1;
}
}
return -1;
}
int main()
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
if(result == -1)
{
cout<<"Not present"<<endl;
}
else
{
cout<<result<<endl;
}
return 0;
}
11. Linear Search
S.NO | BFS | DFS |
---|---|---|
1. | BFS stands for Breadth First Search. | DFS stands for Depth First Search. |
2. | BFS uses Queue data structure for finding the shortest path. | DFS uses Stack data structure. |
3 | Vertex-based algorithm | Edge-based algorithm |
4. | BFS is better when target is closer to Source. | DFS is better when target is far from source. |
5. | BFS is slower than DFS. | DFS is faster than BFS. |
6. | Inefficient memory utilization | Efficient memory utilization |
-------------------------------------------------------------------------------
S.NO. | ROUTER | GATEWAY |
---|---|---|
1. | It is a hardware device which is responsible for receiving, analyzing and forwarding the data packets to other networks. | It is a device that is used for the communication among the networks which have a different set of protocols. |
2. | It supports the dynamic routing. | It does not support dynamic routing. |
3. | The main function of a router is routing the traffic from one network to the other. | The main function of a gateway is to translate one protocol to the other. |
4. | A router operates on layer 3 and layer 4 of the OSI model. | A gateway operates upto layer 5 of the OSI model. |
5. | Working principle of a router is to install routing details for multiple networks and routing traffic based upon the destination address. | 5. Working principle of a gateway is to differentiate what is inside the network and what is outside the network. |
6. | It is hosted on only the dedicated applications. | It is hosted on dedicated applications, physical servers or virtual applications. |
7. | The additional features provided by a router are Wireless networking, Static routing, NAT, DHCP server etc. | The additional features provided by a gateway are network access control, protocol conversion etc. |
No comments:
Post a Comment