Professional Documents
Culture Documents
CODE: 15CSL47
DEPARTMENT OF
INFORMATION SCIENCE AND ENGINEERING
class Student
{
String USN, Name, Branch, Phone;
Scanner input = new Scanner(System.in);
void read()
{
System.out.println("Enter Student Details");
System.out.println("Enter USN");
USN = input.nextLine();
System.out.println("Enter Name");
Name = input.nextLine();
System.out.println("Enter Branch");
Branch = input.nextLine();
System.out.println("Enter Phone");
Phone = input.nextLine();
}
void display()
{
System.out.format("%-20s %-20s %-20s %-20s", USN, Name, Branch,
Phone);
}
}
class studentdetails
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter number of student details to be created");
int number = input.nextInt();
Student s[] = new Student[number];
OUTPUT:
Enter Name
VEDA
Enter Branch
ISE
Enter Phone
9234566789
Enter Student Details
Enter USN
IBI10IS11
Enter Name
SREE
Enter Branch
ISE
Enter Phone
9876543217
Enter Student Details
Enter USN
1BI10IS13
Enter Name
MERCY
Enter Branch
ISE
Enter Phone
1234567898
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.*;
class arrayStack {
int arr[];
int top, max;
arrayStack(int n) {
max = n;
arr = new int[max];
top = -1;
}
void push(int i) {
if (top == max - 1)
System.out.println("Stack Overflow");
else
arr[++top] = i;
}
void pop() {
if (top == -1) {
System.out.println("Stack Underflow");
} else {
int element = arr[top--];
System.out.println("Popped Element: " + element);
}
}
void display() {
System.out.print("\nStack = ");
if (top == -1) {
System.out.print("Empty\n");
return;
}
for (int i = top; i >= 0; i--)
System.out.print(arr[i] + " ");
System.out.println();
}
}
class Stack
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter Size of Integer Stack ");
int n = scan.nextInt();
boolean done = false;
case 3:
stk.display();
break;
case 4:
done = true;
break;
default:
System.out.println("Wrong Entry \n ");
break;
}
} while (!done);
}
}
OUTPUT:
Stack Operations
1. push
2. pop
3. display
4. Exit
1
Enter integer element to push
5
Stack Operations
1. push
2. pop
3. display
4. Exit
1
Enter integer element to push
10
Stack Operations
1. push
2. pop
3. display
4. Exit
1
Enter integer element to push
15
Stack Operations
1. push
2. pop
3. display
4. Exit
1
Enter integer element to push
20
Stack Operations
1. push
2. pop
3. display
4. Exit
1
Stack Operations
1. push
2. pop
3. display
4. Exit
3
Stack = 25 20 15 10 5
Stack Operations
1. push
2. pop
3. display
4. Exit
2
Popped Element: 25
Stack Operations
1. push
2. pop
3. display
4. Exit
2
Popped Element: 20
Stack Operations
1. push
2. pop
3. display
4. Exit
2
Popped Element: 15
Stack Operations
1. push
2. pop
3. display
4. Exit
2
Popped Element: 10
Stack Operations
1. push
2. pop
3. display
4. Exit
2
Popped Element: 5
Stack Operations
1. push
2. pop
3. display
4. Exit
2
Stack Underflow
Stack Operations
1. push
2. pop
3. display
4. Exit
1
Enter integer element to push
3
Stack Operations
1. push
2. pop
3. display
4. Exit
1
Enter integer element to push
5
Stack Operations
1. push
2. pop
3. display
4. Exit
1
Enter integer element to push
10
Stack Operations
1. push
2. pop
3. display
4. Exit
1
Enter integer element to push
15
Stack Operations
1. push
2. pop
3. display
4. Exit
1
Enter integer element to push
25
Stack Operations
1. push
2. pop
3. display
4. Exit
1
Enter integer element to push
25
Stack Overflow
Stack Operations
1. push
2. pop
3. display
4. Exit
3
Stack = 25 15 10 5 3
Stack Operations
1. push
2. pop
3. display
4. Exit
import java.util.Scanner;
class Staff {
void read()
{
System.out.println("Enter StaffID");
StaffID = input.nextLine();
System.out.println("Enter Name");
Name = input.nextLine();
System.out.println("Enter Phone");
Phone = input.nextLine();
System.out.println("Enter Salary");
Salary = input.nextLine();
void display()
{
System.out.format("%-15s", StaffID);
System.out.println();
System.out.format("%-15s", Name);
System.out.println();
System.out.format("%-15s", "PHONE:");
System.out.format("%-15s", Phone);
System.out.println();
System.out.format("%-15s", "SALARY:");
System.out.format("%-15s", Salary);
System.out.println();
void read_Teaching() {
System.out.println("Enter Domain");
Domain = input.nextLine();
System.out.println("Enter Publication");
Publication = input.nextLine();
void display() {
System.out.println();
System.out.format("%-15s", "DOMAIN:");
System.out.format("%-15s", Domain);
System.out.println();
System.out.format("%-15s", "PUBLICATION:");
System.out.format("%-15s", Publication);
System.out.println();
String Skills;
void read_Technical() {
System.out.println("Enter Skills");
Skills = input.nextLine();
void display() {
System.out.println();
System.out.format("%-15s", "SKILLS:");
System.out.format("%-15s", Skills);
System.out.println();
}
}
String Period;
void read_Contract() {
System.out.println("Enter Period");
Period = input.nextLine();
}
void display()
{
System.out.println();
System.out.format("%-15s", "PERIOD:");
System.out.format("%-15s", Period);
System.out.println();
}
}
class Staffdetails
{
int n = input.nextInt();
{
System.out.println("Enter Teaching staff information");
steach[i].read_Teaching();
}
stech[i].read_Technical();
scon[i].read_Contract();
}
// Display Staff Information
System.out.println("Staff Details:");
System.out.println();
{
steach[i].display();
System.out.println();
System.out.println();
scon[i].display();
}
}
}
OUTPUT:
Enter Publication
IEEE
Enter Teaching staff information
Enter StaffID
02
Enter Name
PRALASH
Enter Phone
9876543219
Enter Salary
35000
Enter Domain
MICROPROCESSOR
Enter Publication
IEEE
Enter Teaching staff information
Enter StaffID
03
Enter Name
VEDA
Enter Phone
1234567898
Enter Salary
2000
Enter Domain
NETWORKS
Enter Publication
IEEE
Enter Technical staff information
Enter StaffID
04
Enter Name
3
Enter Phone
1234567898
Enter Salary
20000
Enter Skills
Enter StaffID
02
Enter Name
VIJAY
Enter Phone
1234567898
Enter Salary
2000
Enter Skills
BASIC COMPUTER
Enter Technical staff information
Enter StaffID
03
Enter Name
RAHUL
Enter Phone
9876543217
Enter Salary
10000
Enter Skills
COMPUTER APPLICATION
Enter Contract staff information
Enter StaffID
01
Enter Name
VEDA
Enter Phone
9876543218
Enter Salary
1000
Enter Period
1YEAR
Enter Contract staff information
Enter StaffID
02
Enter Name
SHILPA
Enter Phone
8976543216
Enter Salary
1000
Enter Period
1YEAR
Enter Contract staff information
Enter StaffID
03
Enter Name
MOHAN
Enter Phone
8976543215
Enter Salary
1000
Enter Period
1YEAR
Staff Details:
STAFFID: 01
NAME: NAGARAJU
PHONE: 1234567898
SALARY: 25000
DOMAIN: NETWORKS
PUBLICATION: IEEE
STAFFID: 02
NAME: PRALASH
PHONE: 9876543219
SALARY: 35000
DOMAIN: MICROPROCESSOR
PUBLICATION: IEEE
STAFFID: 03
NAME: VEDA
PHONE: 1234567898
SALARY: 2000
DOMAIN: NETWORKS
PUBLICATION: IEEE
STAFFID: 04
NAME: SHILPA
PHONE: 1234567898
SALARY: 20000
SKILLS:
STAFFID: 02
NAME: VIJAY
PHONE: 1234567898
SALARY: 2000
SKILLS: BASIC COMPUTER
STAFFID: 03
NAME: RAHUL
PHONE: 9876543217
SALARY: 10000
SKILLS: COMPUTER APPLICATION
STAFFID: 01
NAME: VEDA
PHONE: 9876543218
SALARY: 1000
PERIOD: 1YEAR
STAFFID: 02
NAME: SHILPA
PHONE: 8976543216
SALARY: 1000
PERIOD: 1YEAR
STAFFID: 03
NAME: MOHAN
PHONE: 8976543215
SALARY: 1000
PERIOD: 1YEAR
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 and display as using StringTokenizer class considering the delimiter
character as “/”.
import java.util.Scanner;
import java.util.StringTokenizer;
String name;
Scanner scan = new Scanner(System.in);
name = scan.next();
System.out.print(st.nextToken());
if (i < count)
System.out.print(",");
}
}
}
OUTPUT:
VIJAY,02/03/1988
VIJAY,02,03,1988
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.
import java.util.Scanner;
class exception {
a = input.nextInt();
b = input.nextInt();
try {
result = a / b;
System.out.println("Result = " + result);
}
catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero.");
}
}
}
OUTPUT:
import java.util.Random;
SquareThread(int x) {
this.x = x;
}
CubeThread(int x) {
this.x = x;
}
{
int num = 0;
r = new Random();
try
{
while (true) {
num = r.nextInt(100);
System.out.println("Main Thread and Generated Number is " + num);
t1 = new Thread(new SquareThread(num));
t1.start();
Thread.sleep(1000);
System.out.println("--------------------------------------");
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
OUTPUT:
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 divideand-conquer method works
along with its time complexity analysis: worst case, average case and best case.
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;
System.out.println("Input Array:");
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
OUTPUT:
Input Array:
0 0 6 6 9 10 10 12 14 15 17 19 23 27 29 32 34 34 36 37 39 40 41 42 44 46 56 58 62 65 65
66 70 71 72 79 80 84 84 84 85 85 92 95 103 103 104 105 105 106 108 108 108 109 110
110 111 111 112 118 120 121 121 121 127 127 129 130 130 132 133 135 138 138 139
139 141 146 152 154 154 154 155 160 162 164 169 169 171 172 172 172 175 176 177
178 179 180 180 183 183 184 186 188 190 190 191 191 191 193 193 196 202 203 208
209 210 212 212 214 217 219 228 229 230 230 238 239 239 239 242 244 248 248 249
249 251 254 258 261 263 265 265 265 265 265 270 273 274 275 276 276 283 286 286
288 290 293 294 294 295 298 300 308 310 310 311 313 316 317 317 319 324 326 327
329 331 335 336 338 339 341 342 343 347 347 348 351 352 354 354 357 360 363 364
366 369 372 374 379 379 380 384 384 386 386 387 388 389 393 395 402 402 403 408
410 412 415 416 418 422 424 428 431 431 433 434 435 436 437 438 439 444 444 444
445 454 455 460 462 462 464 464 464 464 468 468 469 475 478 478 480 481 485 485
488 492 493 495 497 498 499 500 501 502 504 504 507 508 508 509 509 510 512 512
512 514 519 520 521 526 529 529 530 531 535 536 539 545 546 551 552 553 557 558
560 561 564 569 570 576 579 579 581 582 582 582 583 585 586 589 589 590 595 597
597 599 599 600 604 609 611 614 615 615 617 618 619 619 621 621 624 629 630 630
634 638 639 640 640 642 644 644 644 650 654 657 660 665 666 668 668 670 677 680
681 682 683 687 689 689 689 690 693 693 693 695 695 695 697 698 702 703 704 706
707 707 712 717 718 719 737 737 738 739 739 740 740 741 741 742 742 742 744 744
745 750 757 759 763 765 767 769 774 778 780 780 784 784 785 786 786 787 791 796
798 798 800 801 805 807 816 816 818 821 823 827 829 830 832 833 837 838 838 844
845 849 849 851 851 852 856 859 859 863 865 873 873 873 883 890 891 892 896 900
902 903 906 907 912 913 913 917 920 928 929 929 931 932 934 937 940 944 945 946
946 949 951 951 953 954 955 962 963 963 966 973 974 978 979 980 981 988 990 991
994 995 996 997 998
Sorted Array:
0 0 6 6 9 10 10 12 14 15 17 19 23 27 29 32 34 34 36 37 39 40 41 42 44 46 56 58 62 65 65
66 70 71 72 79 80 84 84 84 85 85 92 95 103 103 104 105 105 106 108 108 108 109 110
110 111 111 112 118 120 121 121 121 127 127 129 130 130 132 133 135 138 138 139
139 141 146 152 154 154 154 155 160 162 164 169 169 171 172 172 172 175 176 177
178 179 180 180 183 183 184 186 188 190 190 191 191 191 193 193 196 202 203 208
209 210 212 212 214 217 219 228 229 230 230 238 239 239 239 242 244 248 248 249
249 251 254 258 261 263 265 265 265 265 265 270 273 274 275 276 276 283 286 286
288 290 293 294 294 295 298 300 308 310 310 311 313 316 317 317 319 324 326 327
329 331 335 336 338 339 341 342 343 347 347 348 351 352 354 354 357 360 363 364
366 369 372 374 379 379 380 384 384 386 386 387 388 389 393 395 402 402 403 408
410 412 415 416 418 422 424 428 431 431 433 434 435 436 437 438 439 444 444 444
445 454 455 460 462 462 464 464 464 464 468 468 469 475 478 478 480 481 485 485
488 492 493 495 497 498 499 500 501 502 504 504 507 508 508 509 509 510 512 512
512 514 519 520 521 526 529 529 530 531 535 536 539 545 546 551 552 553 557 558
560 561 564 569 570 576 579 579 581 582 582 582 583 585 586 589 589 590 595 597
597 599 599 600 604 609 611 614 615 615 617 618 619 619 621 621 624 629 630 630
634 638 639 640 640 642 644 644 644 650 654 657 660 665 666 668 668 670 677 680
681 682 683 687 689 689 689 690 693 693 693 695 695 695 697 698 702 703 704 706
707 707 712 717 718 719 737 737 738 739 739 740 740 741 741 742 742 742 744 744
745 750 757 759 763 765 767 769 774 778 780 780 784 784 785 786 786 787 791 796
798 798 800 801 805 807 816 816 818 821 823 827 829 830 832 833 837 838 838 844
845 849 849 851 851 852 856 859 859 863 865 873 873 873 883 890 891 892 896 900
902 903 906 907 912 913 913 917 920 928 929 929 931 932 934 937 940 944 945 946
946 949 951 951 953 954 955 962 963 963 966 973 974 978 979 980 981 988 990 991
994 995 996 997 998
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 divideand-conquer method works
along with its time complexity analysis: worst case, average case and best case.
import java.util.Random;
import java.util.Scanner;
int n = input.nextInt();
// uniform distribution
MergeSortAlgorithm(0, n - 1);
input.close();
int mid;
MergeSortAlgorithm(low, mid);
MergeSortAlgorithm(mid + 1, high);
int i, h, j, k;
h = i = low;
j = mid + 1;
b[i++] = a[h++];
else
b[i++] = a[j++];
if (h > mid)
b[i++] = a[k];
else
b[i++] = a[k];
a[k] = b[k];
OUTPUT:
import java.util.Scanner;
System.out.println();
return V[n][M];
}
OUTPUT:
Optimal solution = 35
import java.util.Scanner;
class KObject
{ // Knapsack object details
float w;
float p;
float r;
}
ReadObjects(obj);
Knapsack(obj);
scanner.close();
}
OUTPUT:
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.*;
S[s] = 1;
d[s] = 1;
i = 2;
while (i <= n)
{
u = Extract_Min(S, d);
S[u] = 1;
i++;
for (v = 1; v <= n; v++)
{
if (((d[u] + a[u][v] < d[v]) && (S[v] == 0)))
d[v] = d[u] + a[u][v];
}
}
for (i = 1; i <= n; i++)
if (i != s)
System.out.println(i + ":" + d[i]);
}
{
int i, j = 1, min;
min = infinity;
for (i = 1; i <= n; i++) {
if ((d[i] < min) && (S[i] == 0))
{
min = d[i];
j = i;
}
}
return (j);
}
}
OUTPUT:
6
1:49
2:14
3:29
4:4
5:34
8. Find Minimum Cost Spanning Tree of a given connected undirected graph using
Kruskal'salgorithm. Use Union-Find algorithms in your program.
import java.util.Scanner;
int i, j;
cost = new int[MAX][MAX];
OUTPUT:
6
Enter the cost adjacency matrix
0 60 10 999 999 999
60 0 999 20 40 70
10 999 0 999 999 50
999 20 999 0 999 80
999 40 999 999 0 30
999 70 50 80 30 0
9. Find Minimum Cost Spanning Tree of a given connected undirected graph using
Prim's algorithm.
import java.util.Scanner;
visited[1] = 1;
while (ne < n)
{
for (i = 1, min = 999; i <= n; i++)
for (j = 1; j <= n; j++)
if (cost[i][j] < min)
if (visited[i] != 0)
{
min = cost[i][j];
a = u = i;
b = v = j;
}
if (visited[u] == 0 || visited[v] == 0)
{
System.out.println("Edge" + ne++ + ":(" + a + "," + b + ")" +
"cost:" + min);
mincost += min;
visited[b] = 1;
}
cost[a][b] = cost[b][a] = 999;
}
OUTPUT:
Edge1:(1,3)cost:10
Edge2:(3,6)cost:50
Edge3:(6,5)cost:30
Edge4:(5,2)cost:40
Edge5:(2,4)cost:20
Minimum cost150
import java.util.Scanner;
public class FloydsClass {
static final int MAX = 20; // max. size of cost matrix
static int a[][]; // cost matrix
static int n; // actual matrix size
OUTPUT:
4
Enter the Cost Matrix (999 for infinity)
0 999 3 999
2 0 999 999
999 7 0 1
6 999 999 0
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
import java.util.Scanner;
OUTPUT:
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
Tour:
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;
{
S = new int[MAX];
soln = new int[MAX];
int sum = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number of elements: ");
n = scanner.nextInt();
{
for (int j = 1; j <= i; j++)
{
if (soln[j] == 1)
System.out.print(S[j] + " ");
}
System.out.println();
}
else
{
soln[i + 1] = 1;
SumofSub(i + 1, weight + S[i + 1], total - S[i + 1]);
soln[i + 1] = 0;
SumofSub(i + 1, weight, total - S[i + 1]);
}
}
OUTPUT:
12. Design and implement in Java to find all Hamiltonian Cycles in a connected
undirected Graph G of n vertices using backtracking principle.
import java.util.Scanner;
{
if (found == false)
System.out.println("No Solution possible!");
}
{
if ((i != j) && (i < j)) {
System.out.print(i + " and " + j + ": ");
G[j][i] = G[i][j] = scanner.nextInt();
}
if (i == j)
G[i][j] = 0;
}
for (int i = 1; i <= n; i++)
x[i] = 0;
x[1] = 1;
scanner.close();
}
void HamiltonianMethod(int k)
{
while (true) {
NextValue(k, G, x, n);
if (x[k] == 0)
return;
if (k == n)
{
for (int i = 1; i <= k; i++)
System.out.print(x[i] + " ");
System.out.println(x[1]);
System.out.println();
found = true;
return;
}
else
HamiltonianMethod(k + 1);
}
}
if (x[k] == x[j])
break;
if (j == k)
if ((k < n) || ((k == n) && G[x[n]][x[1]] != 0))
return;
}
}
}
}
OUTPUT:
1 and 2: 1
1 and 3: 1
1 and 4: 1
1 and 5: 0
1 and 6: 0
2 and 3: 1
2 and 4: 0
2 and 5: 1
2 and 6: 1
3 and 4: 1
3 and 5: 1
3 and 6: 0
4 and 5: 1
4 and 6: 0
5 and 6: 1
Solution:
1265341
1326541
1345621
1435621
1456231