Wednesday, 10 April 2019

Program To Demonstrate Name Resolution Using Java

import java.net.*;
import java.*;
import java.util.*;


class GFG {
public static void main(String args[])
throws UnknownHostException
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the URL for which IP address needs to be fetched : ");
// The URL for which IP address needs to be fetched
String s = sc.next();
System.out.print("Enter the Ip for which address needs to be fetched : ");
// The IP address for which Domain Name needs to be fetched
String ips = sc.next();
try {
// Fetch IP address by getByName()
InetAddress ip = InetAddress.getByName(new URL(s).getHost());


// Print the IP address
System.out.println("Public IP Address of: " + ip);


//Fetch Domain Name/Server Name by getHostName()
System.out.println("Public Address of IP : " + InetAddress.getByName(ips).getHostName());
}
catch (MalformedURLException e) {
// It means the URL is invalid
System.out.println("Invalid URL");
}
}
}
Output:
Enter the URL for which IP address needs to be fetched : https://www.google.com
Public IP Address of: www.google.com/172.217.26.228
Enter the Ip for which address needs to be fetched : 172.217.26.228
Public Address of IP : bom05s09-in-f4.1e100.net

Program to demonstrate Load Balancing Algorithm (Java)

import java.io.*;
import java.util.*;


class loadbal
{
public void loadbalancer(int servers,int processes )
{
int[] s=new int[servers];
int eachProcess = processes/servers;


for(int i=0;i<servers;i++)
{
s[i]=eachProcess;
}


int remainProcess= processes % servers;
for(int j=0,i=remainProcess;i>0 && j<servers;i--,j++)
{
s[j] = s[j] +1;
}
for(int i=0; i<servers;i++)
{
System.out.println( "\nServer " + (i+1) +" = " + s[i] + " " );
}
}
public static void main(String args[])throws IOException
{
loadbal lb= new loadbal();


int servers=0,processes=0,input;
int choice;


Scanner scrinp = new Scanner(System.in);


do
{


System.out.println("Please enter your choice => \n1. Add Servers 2. Remove Servers 3. Add Process 4. Remove Process 5.Exit ");


choice= scrinp.nextInt();


switch(choice)
{
case 1: System.out.println("ENter the no. of servers to be added ");
input = scrinp.nextInt();
while(input < 0)
{
System.out.println("\nInvalid input.");
System.out.println("\nPlease reENter the no. of servers to be added ");


input = scrinp.nextInt();
}
servers += input;


lb.loadbalancer(servers,processes);
break;


case 2: System.out.println("ENter the no. of servers to be removed ");
input = scrinp.nextInt();
while(input < 0 || input > servers)
{
System.out.println("\nInvalid input.");
System.out.println("\nPlease reENter the no. of servers to be removed ");
input = scrinp.nextInt();
}
servers -= input;


lb.loadbalancer(servers,processes);
break;
case 3: System.out.println("ENter the no. of processes to be added ");
input = scrinp.nextInt();
while(input < 0)
{
System.out.println("\nInvalid input.");
System.out.println("\nPlease reENter the no. of processes to be added ");
input = scrinp.nextInt();
}
processes += input;


lb.loadbalancer(servers,processes);
break;


case 4: System.out.println("ENter the no. of processes to be removed ");
input = scrinp.nextInt();
while(input < 0 || input > servers)
{
System.out.println("\nInvalid input.");
System.out.println("\nPlease reENter the no. of processes to be removed ");


input = scrinp.nextInt();


}


processes -= input;


lb.loadbalancer(servers,processes);
break;


case 5: System.exit(0);


default: System.out.println("\nInvalid Choice ");
}
}while(true);
}
}


Output:
// C:\Users\GRIMM\Desktop>javac loadbal.java
// C:\Users\GRIMM\Desktop>java loadbal
// Please enter your choice =>
// 1. Add Servers 2. Remove Servers 3. Add Process 4. Remove Process 5.Exit
// 1
// ENter the no. of servers to be added
// 4


// Server 1 = 0


// Server 2 = 0


// Server 3 = 0


// Server 4 = 0
// Please enter your choice =>
// 1. Add Servers 2. Remove Servers 3. Add Process 4. Remove Process 5.Exit
// 2
// ENter the no. of servers to be removed
// 1


// Server 1 = 0


// Server 2 = 0


// Server 3 = 0
// Please enter your choice =>
// 1. Add Servers 2. Remove Servers 3. Add Process 4. Remove Process 5.Exit
// 4
// ENter the no. of processes to be removed
// 2


// Server 1 = 0


// Server 2 = 0


// Server 3 = 0
// Please enter your choice =>
// 1. Add Servers 2. Remove Servers 3. Add Process 4. Remove Process 5.Exit
// 1
// ENter the no. of servers to be added
// 4


// Server 1 = 0


// Server 2 = 0


// Server 3 = 0


// Server 4 = 0


// Server 5 = 0


// Server 6 = 0


// Server 7 = 0
// Please enter your choice =>
// 1. Add Servers 2. Remove Servers 3. Add Process 4. Remove Process 5.Exit



Program to demonstrate Election Algorithm (Java)

import java.io.*;
import java.util.*;


class testpgm
{
int MaxProcess=100; //max no of processes allowed
int p[] = new int[MaxProcess]; //array to store status of process 1-alive 0-crashed
int actualProcesses=0; //actual no of proess by user
int coord=0; //stores coordinator


Scanner scrinp = new Scanner(System.in); //for taking input


public void performElection(int candidate )
{
int flag=0; // coordinator to be elected


while(flag == 0)
{
if(p[candidate]==1) //should be alive
{
int j = candidate +1 ;


while( j< actualProcesses)
{
if(j>candidate && p[j]==1)
{
System.out.println("\nProcess " + j + "is in election process.");
flag =1;
break;
}
j++;
}


if(flag==0)
{
coord = candidate;


System.out.println("\nNew elected coordinator Process " + coord );
flag =1;


}
else
{
flag = 0;
candidate = j;
}
}
}
}


void bully()
{


System.out.println("ENter the no. of processes (Max 100) ");
actualProcesses = scrinp.nextInt();


int i=0;
coord = actualProcesses-1;


for(i=0; i< actualProcesses ; i++)
{
p[i]=1; //make alive all the processes
}


int choice;


do
{


System.out.println("Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit ");


choice= scrinp.nextInt();


switch(choice)
{
case 1: System.out.println("ENter the process to be crashed");
int pc= scrinp.nextInt();
p[pc] = 0;


System.out.println("\nProcess "+ pc + " crashed ");


if( pc == coord)
{
int crash =0;
for( i=0 ; i< actualProcesses; i++)
{
if(p[i] == 0)
crash++;
}


if(crash == actualProcesses)
{
System.out.println("\nAll processes are crashed");
break;
}


System.out.println("\nCoordinator crashed.");
System.out.println("\nPlease enter the initiator node ");


int initiator= scrinp.nextInt();


while(p[initiator]==0)
{
System.out.println("\nThe entered node is already crashed.");
System.out.println("\nPlease enter another initiator node ");
initiator= scrinp.nextInt();
}
performElection(initiator );
}
break;


case 2: System.out.println("\nENter the process to be recovered");
int pr= scrinp.nextInt();


if(p[pr] == 0) //check if proecss is crashed or not
{
p[pr] =1; //make iit alive
System.out.println("\nProcess "+ pr + " recovered ");
if( pr > coord)
{
performElection(pr );
}
}
else
{
System.out.println("\nProcess is already running");
}
break;
case 3: System.out.println("\nThe coordinator is " + coord);
break;
case 4:
System.out.println("\n\n Status of processes - ");
for( i=0 ; i< actualProcesses; i++)
System.out.println( i +"-" + p[i] + " " );
break;
case 5: System.exit(0);
default: System.out.println("\nInvalid Choice ");
}
}while(true);
}
public static void main(String args[])throws IOException
{
testpgm test1 = new testpgm();
test1.bully();
}
}
Output:

// C:\Users\GRIMM\Desktop>javac election.java


// C:\Users\GRIMM\Desktop>java testpgm
// ENter the no. of processes (Max 100)


// 4
// Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit
// 3


// The coordinator is 3
// Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit
// 4




// Status of processes -
// 0-1
// 1-1
// 2-1
// 3-1
// Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit
// 4




// Status of processes -
// 0-1
// 1-1
// 2-1
// 3-1
// Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit
// 1
// ENter the process to be crashed
// 2


// Process 2 crashed
// Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit
// 4




// Status of processes -
// 0-1
// 1-1
// 2-0
// 3-1
// Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit

Implement K-Mean Algorithm (Python)

import pandas as pd

df=pd.read_csv('new.csv')

from sklearn.cluster import KMeans

ob=KMeans(n_clusters=3)

ob.fit(df)

print('centroids',ob.cluster_centers_)

print('cluster for each label',ob.labels_)


print('number of clusters',ob.n_clusters)

Output:
Training Data
2
4
6
3
31
12
15
16
38
35
14
21
23
25
30

centroids [[18.] [33.5] [4.33333333]]
cluster for each label [2 2 2 1 0 0 0 1 1 0 0 0 0 1]
number of clusters 3