0% found this document useful (0 votes)
286 views19 pages

Java Programming Practical Exercises

The document contains details about 9 practical assignments completed by Kumar Vaibhav with roll number 4177 TYCS. It includes the name, roll number, practical number, description of tasks, code snippets and output for each practical assignment ranging from bitwise operations to implementing a simple web crawler.

Uploaded by

Justin Sebastian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
286 views19 pages

Java Programming Practical Exercises

The document contains details about 9 practical assignments completed by Kumar Vaibhav with roll number 4177 TYCS. It includes the name, roll number, practical number, description of tasks, code snippets and output for each practical assignment ranging from bitwise operations to implementing a simple web crawler.

Uploaded by

Justin Sebastian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Name : Kumar Vaibhav

Roll No: 4177 TYCS


Practical No:1 Write a program to demonstrate bitwise
operation.

Code:

1
Name : Kumar Vaibhav
Roll No: 4177 TYCS

Output:

2
Name : Kumar Vaibhav
Roll No: 4177 TYCS
Practical No:2 Java Code for Page Rank
Algorithm :

Code:
import java.util.*;
import java.io.*;
public class PageRank {

public int path[][] = new int[10][10];


public double pagerank[] = new double[10];

public void calc(double totalNodes){

double InitialPageRank;
double OutgoingLinks=0;
double DampingFactor = 0.85;
double TempPageRank[] = new double[10];

int ExternalNodeNumber;
int InternalNodeNumber;
int k=1; // For Traversing
int ITERATION_STEP=1;

InitialPageRank = 1/totalNodes;
System.out.printf(" Total Number of Nodes :"+totalNodes+"\t Initial PageRank of
All Nodes :"+InitialPageRank+"\n");

// 0th ITERATION _ OR _ INITIALIZATION PHASE


for(k=1;k<=totalNodes;k++)
{
this.pagerank[k]=InitialPageRank;
}

System.out.printf("\n Initial PageRank Values , 0th Step \n");


for(k=1;k<=totalNodes;k++)
{
System.out.printf(" Page Rank of "+k+" is :\t"+this.pagerank[k]+"\n");

3
Name : Kumar Vaibhav
Roll No: 4177 TYCS
}

while(ITERATION_STEP<=2) // Iterations
{
// Store the PageRank for All Nodes in Temporary Array
for(k=1;k<=totalNodes;k++)
{
TempPageRank[k]=this.pagerank[k];
this.pagerank[k]=0;
}

for(InternalNodeNumber=1;InternalNodeNumber<=totalNodes;InternalNodeNum
ber++)
{

for(ExternalNodeNumber=1;ExternalNodeNumber<=totalNodes;ExternalNodeNu
mber++)
{
if(this.path[ExternalNodeNumber][InternalNodeNumber] == 1)
{
k=1;
OutgoingLinks=0; // Count the Number of Outgoing Links for each
ExternalNodeNumber
while(k<=totalNodes)
{
if(this.path[ExternalNodeNumber][k] == 1 )
{
OutgoingLinks=OutgoingLinks+1; // Counter for Outgoing Links
}
k=k+1;
}
// Calculate PageRank

this.pagerank[InternalNodeNumber]+=TempPageRank[ExternalNodeNumber]*(1/
OutgoingLinks);
}
}
}

4
Name : Kumar Vaibhav
Roll No: 4177 TYCS
System.out.printf("\n After "+ITERATION_STEP+"th Step \n");

for(k=1;k<=totalNodes;k++)
System.out.printf(" Page Rank of "+k+" is :\t"+this.pagerank[k]+"\n");

ITERATION_STEP = ITERATION_STEP+1;
}

// Add the Damping Factor to PageRank


for(k=1;k<=totalNodes;k++)
{
this.pagerank[k]=(1-DampingFactor)+ DampingFactor*this.pagerank[k];
}

// Display PageRank
System.out.printf("\n Final Page Rank : \n");
for(k=1;k<=totalNodes;k++)
{
System.out.printf(" Page Rank of "+k+" is :\t"+this.pagerank[k]+"\n");
}

public static void main(String args[])


{
int nodes,i,j,cost;
Scanner in = new Scanner(System.in);
System.out.println("Enter the Number of WebPages \n");
nodes = in.nextInt();
PageRank p = new PageRank();
System.out.println("Enter the Adjacency Matrix with 1->PATH & 0->NO
PATH Between two WebPages: \n");
for(i=1;i<=nodes;i++)
for(j=1;j<=nodes;j++)
{
p.path[i][j]=in.nextInt();
if(j==i)
p.path[i][j]=0;
}
p.calc(nodes);
5
Name : Kumar Vaibhav
Roll No: 4177 TYCS

Output:

6
Name : Kumar Vaibhav
Roll No: 4177 TYCS
Practical No-3
Implementing Dynamic Programming Algorithm for computing edit
distance between String s1 and s2.

a) Using Java
Code:
class EDIST
{
static int min(int x,int y,int z)
{
if (x<=y && x<=z) return x;
if (y<=x && y<=z) return y;
else return z;
}

static int editDist(String str1 , String str2 , int m ,int n)


{
// If first string is empty, the only option is to

// insert all characters of second string into first


if (m == 0) return n;

// If second string is empty, the only option is to

// remove all characters of first string


if (n == 0) return m;

// If last characters of two strings are same, nothing

// much to do. Ignore last characters and get count for

// remaining strings.
if (str1.charAt(m-1) == str2.charAt(n-1))
return editDist(str1, str2, m-1, n-1);

7
Name : Kumar Vaibhav
Roll No: 4177 TYCS
// If last characters are not same, consider all three

// operations on last character of first string, recursively

// compute minimum cost for all three operations and take

// minimum of three values.


return 1 + min ( editDist(str1, str2, m, n-1),
// Insert
editDist(str1, str2, m-1, n),
// Remove
editDist(str1, str2, m-1, n-1)
// Replace
);
}

public static void main(String args[])


{
String str1 = "sunday";
String str2 = "saturday";

System.out.println( editDist( str1 , str2 , str1.length(), str2.length()) );


}
}
Output:

8
Name : Kumar Vaibhav
Roll No: 4177 TYCS
B) Using Python
Code:

Output:

9
Name : Kumar Vaibhav
Roll No: 4177 TYCS
Practical No-4
Program to compute similarity between two text
documents

Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CompareTextFiles


{
public static void main(String[] args) throws IOException
{
BufferedReader reader1 = new BufferedReader(new
FileReader("D:\\file1.txt"));

BufferedReader reader2 = new BufferedReader(new


FileReader("D:\\file2.txt"));

String line1 = reader1.readLine();

String line2 = reader2.readLine();

boolean areEqual = true;

int lineNum = 1;

while (line1 != null || line2 != null)


{
if(line1 == null || line2 == null)
{
areEqual = false;

break;

10
Name : Kumar Vaibhav
Roll No: 4177 TYCS
}
else if(! line1.equalsIgnoreCase(line2))
{
areEqual = false;

break;
}

line1 = reader1.readLine();

line2 = reader2.readLine();

lineNum++;
}

if(areEqual)
{
System.out.println("Two files have same content.");
}
else
{
System.out.println("Two files have different content. They differ at line
"+lineNum);

System.out.println("File1 has "+line1+" and File2 has "+line2+" at line


"+lineNum);
}

reader1.close();

reader2.close();
}
}

11
Name : Kumar Vaibhav
Roll No: 4177 TYCS
File1.txt

File2.txt

12
Name : Kumar Vaibhav
Roll No: 4177 TYCS

Output:
Different Content

Same Content

13
Name : Kumar Vaibhav
Roll No: 4177 TYCS
Practical N0-5
Write a map-reduce program to count the number of occurrences of each
alphabetic character in the given dataset. The count for each letter
should be case-insensitive (i.e., include both upper-case and lower-case
versions of the letter; Ignore non-alphabetic characters)

Code

Output:

14
Name : Kumar Vaibhav
Roll No: 4177 TYCS
Practical N0-6
Implement a basic IR system using Lucene

Code:

Output:

15
Name : Kumar Vaibhav
Roll No: 4177 TYCS
Practical N0-7
Write a program for Pre-processing of a Text Document: stop word
removal.

Code:
Output:

16
Name : Kumar Vaibhav
Roll No: 4177 TYCS
Practical N0-8
Write a program for mining Twitter to identify tweets for a
specific period and identify trends and named entities

Code:

Output:

17
Name : Kumar Vaibhav
Roll No: 4177 TYCS
Practical N0-9
Write a program to implement simple web crawler

Code:

Output:

18
Name : Kumar Vaibhav
Roll No: 4177 TYCS
Practical N0-10
Write a program to parse XML text, generate Web graph and
compute topic specific page rank.

Code:

Output:

19

You might also like