Daa Lab Manual 2021
Daa Lab Manual 2021
(i) USN
(ii) Name
(iii) Branch
(iv) Phone
Write a Java program to create nStudent objects and print the USN, Name, Branch,
and Phoneof these objects with suitable headings.
1. B. Write a Java program to implement the Stack using arrays. Write Push(), Pop(),
and Display() methods to demonstrate its working.
2. A. Design a superclass called Staff with details as StaffId, Name, Phone, Salary.
Extend this class by writing three subclasses namely Teaching (domain, publications),
Technical (skills), and Contract (period). Write a Java program to read and display at
least 3 staff objects of all three categories.
2 B. Write a Java class called Customer to store their name and date_of_birth. The
date_of_birth format should be dd/mm/yyyy. Write methods to read customer data as
<name, dd/mm/yyyy> and display as <name, dd, mm, yyyy> using StringTokenizer
class considering the delimiter character as “/”.
3 A. Write a Java program to read two integers a andb. Compute a/b and print, when b
is not zero. Raise an exception when b is equal to zero.
3 B. Write a Java program that implements a multi-thread application that has three
threads. First thread generates a random integer for every 1 second; second thread
computes the square of the number andprints; third thread will print the value of cube
of the number.
4 Sort a given set of n integer elements using Quick Sort method and compute its time
complexity. Run the program for varied values of n> 5000 and record the time taken
to sort. Plot a graph of the time taken versus non graph sheet. The elements can be
read from a file or can be generated using the random number generator. Demonstrate
using Java how the divide-and-conquer method works along with its time complexity
analysis: worst case, average case and best case.
5 Sort a given set of n integer elements using Merge Sort method and compute its time
complexity. Run the program for varied values of n> 5000, and record the time taken
to sort. Plot a graph of the time taken versus non graph sheet. The elements can be
read from a file or can be generated using the random number generator. Demonstrate
using Java how the divide-and-conquer method works along with its time complexity
analysis: worst case, average case and best case.
6 Implement in Java, the 0/1 Knapsack problem using (a) Dynamic Programming
method (b) Greedy method.
7 From a given vertex in a weighted connected graph, find shortest paths to other
vertices using Dijkstra's algorithm. Write the program in Java.
8 Find Minimum Cost Spanning Tree of a given connected undirected graph using
Kruskal's algorithm. Use Union-Find algorithms in your program.
9 Find Minimum Cost Spanning Tree of a given connected undirected graph using
Prim's algorithm.
10 Write Java programs to
(a) Implement All-Pairs Shortest Paths problem using Floyd's algorithm.
(b) Implement Travelling Sales Person problem using Dynamic programming.
11 Design and implement in Java to find a subset of a given set S = {Sl, S2,.....,Sn} of n
positive integers whose SUM is equal to a given positive integer d. For example, if S
={1, 2, 5, 6, 8} and d= 9, there are two solutions {1,2,6}and {1,8}. Display a suitable
message, if the given problem instance doesn't have a solution.
12 Design and implement in Java to find all Hamiltonian Cycles in a connected
undirected Graph G of n vertices using backtracking principle.
1 A. Create a Java class called Student with the following details as variables
within it.
(i) USN
(ii) Name
(iii) Branch
(iv) Phone
Write a Java program to create n Student objects and print the USN, Name,
Branch, and Phone of these objects with suitable headings.
import java.util.Scanner;
// Define Student Class
public class Student
{
String USN;
String Name;
String branch;
int phone;
void insertRecord(String reg,String name, String brnch,int ph)
{
USN=reg;
Name=name;
branch=brnch;
phone=ph;
}
void displayRecord()
{
System.out.println(USN+" "+Name+" "+branch+" "+phone);
}
public static void main(String args[])
{
// initialize an array that holds 100 objects of type Student
Student s[]=new Student [100];
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of students");
int n=sc.nextInt();
for(int i=0;i<n;i++)
s[i]=new Student();
for(int j=0;j<n;j++) // read in the data
{
System.out.println("enter the usn,name,branch,phone");
String USN=sc.next();
String Name=sc.next();
String branch=sc.next();
int phone=sc.nextInt();
s[j].insertRecord(USN,Name,branch,phone);
}
for( int m=0;m<n;m++)// print results
{
s[m].displayRecord();
}
}
}
1. B. Write a Java program to implement the Stack using arrays. Write Push(),
Pop(), and Display() methods to demonstrate its working.
import java.util.Scanner;
public class Stack
{
final int max=100;
int s[]=new int[max];
int top=-1;
void push(int ele) // Member function to insert element into an Array
{
if(top>=max-1)
System.out.println("stack overflow");
else
s[++top]=ele;
}
int pop()// Member function to Delete an element from Stack Array
{
int z=0;
if(top==-1)
System.out.println("stack underflow");
else
z=s[top--];
return z;
}
void display()// Display Contents of Stack Array
{
if(top==-1)
System.out.println("stack empty");
else
{
for(int i=top;i>-1;i--)
System.out.println(s[i]+" ");
}
}
2 A. Write a Java program to read two integers a and b. Compute a/b and print,
when b is not zero. Raise an exception when b is equal to zero.
import java.util.Scanner;
public class Division
{
public static void main(String[] args)
{
int a,b,result;
Scanner input =new Scanner(System.in);
System.out.println("Input two integers");
a=input.nextInt();
b=input.nextInt();
try
{
result=a/b;
System.out.println("Result="+result);
}
catch(ArithmeticException e)
{
3 Sort a given set of n integer elements using Quick Sort method and compute its
time complexity. Run the program for varied values of n> 5000 and record the
time taken to sort. Plot a graph of the time taken versus non graph sheet. The
elements can be read from a file or can be generated using the random number
generator. Demonstrate using Java how the divide -and-conquer method works
along with its time complexity analysis: worst case, average case and best case.
import java.util.Random;
import java.util.Scanner;
public class quicksort
{
static int max=2000;
int partition (int[] a, int low,int high)
{
int p,i,j,temp;
p=a[low];
i=low+1;
j=high;
while(low<high)
{
while(a[i]<=p&&i<high)
i++;
while(a[j]>p)
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp = a[low];
a[low] = a[j];
a[j] = temp;
return j;
}
}
return j;
}
void sort(int[] a,int low,int high)
{
if (low < high)
{
int s = partition(a, low, high);
sort(a, low, s - 1);
sort(a, s + 1, high);
}
}
public static void main(String[] args)
{
int[] a;
int i;
System.out.println("Enter the array size");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
a = new int[max];
Random generator = new Random();
for (i = 0; i < n; i++)
a[i] = generator.nextInt(20);
System.out.println("Array before sorting");
for (i = 0; i < n; i++)
System.out.println(a[i] + " ");
long startTime = System.nanoTime();
quicksort m = new quicksort();
m.sort(a, 0, n - 1);
long stopTime = System.nanoTime();
long elapseTime = (stopTime - startTime);
System.out.println("Time taken to sort array is:" + elapseTime +
"nano seconds");
System.out.println("Sorted array is");
for ( i = 0; i < n; i++)
System.out.println(a[i]);
}
}
6. A) Implement in Java, the 0/1 Knapsack problem using (a) Dynamic Programming
method
import java.util.Scanner;
public class knapsackDP
{
public void solve(int[] wt, int[] val, int W, int N)
{
int i, j;
int[][] sol = new int[N + 1][W + 1];
for (i = 0; i <= N; i++)
{
for (j = 0; j <= W; j++)
{
if (i == 0 || j == 0)
sol[i][j] = 0;
else if (wt[i] > j)
sol[i][j] = sol[i - 1][j];
else
sol[i][j] = Math.max((sol[i - 1][j]), (sol[i - 1][j - wt[i]] + val[i]));
}
}
System.out.println("The optimal solution is" + sol[N][W]);
int[] selected = new int[N + 1];
for (i = 0; i < N + 1; i++)
selected[i] = 0;
i = N;
j = W;
while (i > 0 && j > 0)
{
if (sol[i][j] != sol[i - 1][j])
{
selected[i] = 1;
j = j - wt[i];
}
i--;
}
System.out.println("\nItems selected : ");
for (i = 1; i < N + 1; i++)
if (selected[i] == 1) System.out.print(i + " ");
System.out.println();
}
7 From a given vertex in a weighted connected graph, find shortest paths to other
vertices using Dijkstra's algorithm. Write the program in Java.
import java.util.Scanner;
}
void path(int v,int s)
{
if(p[v]!=-1)
path(p[v],s);
if(v!=s)
System.out.print("->"+v+" ");
}
}
}
8 Find Minimum Cost Spanning Tree of a given connected undirected graph using
Kruskal's algorithm. Use Union-Find algorithms in your program.
import java.util.Scanner;
}
i=find(u); j=find(v); if(i!=j)
{
min=a[i][j]; u=i;
v=j;
union(i,j); System.out.println("("+u+","+v+")"+"="+a[u][v]);
sum=sum+a[u][v];
k++;
}
a[u][v]=a[v][u]=99;
}
System.out.println("The cost of minimum spanning tree = "+sum);
setToBePassedOnToNextCallOfCOST[k++]=inputSet[j];
}
int
temp=COST(inputSet[i],setToBePassedOnToNextCallOfCOST,setSize-
1);
if((weight[currentNode][inputSet[i]]+temp) < min)
{
min=weight[currentNode][inputSet[i]]+temp;
}
}
return min;
}
public int MIN(int currentNode,int inputSet[],int setSize)
{
if(setSize==0)
return weight[currentNode][0];
int min=INF,minindex=0;
int setToBePassedOnToNextCallOfCOST[]=new int[n-1];
for(int i=0;i<setSize;i++)
{
int k=0;
for(int j=0;j<setSize;j++)
{
if(inputSet[i]!=inputSet[j])
setToBePassedOnToNextCallOfCOST[k++]=inputSet[j];
}
int
temp=COST(inputSet[i],setToBePassedOnToNextCallOfCOST,setSize-1);
if((weight[currentNode][inputSet[i]]+temp) < min)
{
min=weight[currentNode][inputSet[i]]+temp; minindex=inputSet[i];
}
}
return minindex;
}
public void eval()
{
int dummySet[]=new int[n-1]; for(int i=1;i<n;i++)
dummySet[i-1]=i; finalCost=COST(0,dummySet,n-1);
constructTour();
}
public void constructTour()
{
int previousSet[]=new int[n-1];
int nextSet[]=new int[n-2];
for(int i=1;i<n;i++)
previousSet[i-1]=i;
int setSize=n-1;
tour[0]=MIN(0,previousSet,setSize);
for(int i=1;i<n-1;i++)
{
int k=0;
for(int j=0;j<setSize;j++)
{
if(tour[i-1]!=previousSet[j]) nextSet[k++]=previousSet[j];
}
--setSize;
tour[i]=MIN(tour[i-1],nextSet,setSize);
for(int j=0;j<setSize;j++)
previousSet[j]=nextSet[j];
}
display();
}
public void display()
{
System.out.println();
System.out.print("The tour is 1-");
for(int i=0;i<n-1;i++)
System.out.print((tour[i]+1)+"-");
System.out.print("1");
System.out.println();
System.out.println("The final cost is "+finalCost);
}
}
class TSP
{
public static void main(String args[])
{
TSPExp obj=new TSPExp();
11 Design and implement in Java to find a subset of a given set S = {Sl, S2,.....,Sn} of n
positive integers whose SUM is equal to a given positive integer d. For example, if S
={1, 2, 5, 6, 8} and d= 9, there are two solutions {1,2,6}and {1,8}. Display a suitable
message, if the given problem instance doesn't have a solution.
import java.util.Scanner;
import static java.lang.Math.pow;
System.out.print("}="+d); System.out.println();
}
}
}
if(present==0)
System.out.println("Solution does not exists");
}
}
}
class HamiltoniancycleExp
{
public static void main(String args[])
{
Hamiltoniancycle obj=new Hamiltoniancycle(); obj.getHCycle(1);
}
}