Skip to content

Commit 3558739

Browse files
authored
Add files via upload
1 parent 82369de commit 3558739

7 files changed

Lines changed: 848 additions & 0 deletions

File tree

DigitalMax.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package javacodes;
2+
3+
import java.util.*;
4+
5+
public class DigitalMax {
6+
7+
public static void main(String[] agrs)
8+
{
9+
Scanner in=new Scanner(System.in);
10+
11+
System.out.println("Enter a number :");
12+
13+
int n=in.nextInt();
14+
15+
int temp=0,a;
16+
17+
18+
int[] arr={8,9,8,9,9,9,8,9,8,9};
19+
20+
while(n>0)
21+
22+
{
23+
a=n%10;
24+
25+
temp=temp*10+arr[a];
26+
27+
n=n/10;
28+
}
29+
int ans=0;
30+
31+
while(temp>0)
32+
{
33+
a=temp%10;
34+
35+
ans=ans*10+a;
36+
37+
temp=temp/10;
38+
}
39+
System.out.println("Maximum no:"+" "+ans);
40+
}
41+
}

Dijkstra.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package javacodes;
2+
3+
import java.util.*;
4+
5+
public class Dijkstra {
6+
7+
static int V;
8+
9+
int minDistance(int dist[], Boolean que[])
10+
{
11+
12+
int min = Integer.MAX_VALUE, index=-1;
13+
14+
for (int i = 0; i < V; i++)
15+
16+
if (que[i] == false && dist[i] <= min)
17+
{
18+
min = dist[i];
19+
20+
index = i;
21+
}
22+
23+
return index;
24+
}
25+
26+
27+
void printSolution(int dist[], int n)
28+
{
29+
System.out.println("Vertex Distance from Source");
30+
31+
for (int i = 0; i < V; i++)
32+
33+
System.out.println(i+" \t\t "+dist[i]);
34+
}
35+
36+
37+
void dijkstra(int graph[][], int src)
38+
{
39+
int dist[] = new int[V];
40+
41+
Boolean que[] = new Boolean[V];
42+
43+
44+
for (int i = 0; i < V; i++)
45+
{
46+
dist[i] = Integer.MAX_VALUE;
47+
48+
que[i] = false;
49+
}
50+
51+
52+
dist[src] = 0;
53+
54+
55+
for (int count = 0; count < V-1; count++)
56+
{
57+
58+
int u = minDistance(dist, que);
59+
60+
61+
que[u] = true;
62+
63+
64+
for (int i = 0; i < V; i++)
65+
66+
67+
if (!que[i] && graph[u][i]!=0 &&
68+
69+
dist[u] != Integer.MAX_VALUE &&
70+
71+
dist[u]+graph[u][i] < dist[i])
72+
73+
dist[i] = dist[u] + graph[u][i];
74+
75+
}
76+
77+
78+
printSolution(dist, V);
79+
}
80+
81+
82+
public static void main (String[] args)
83+
{
84+
Scanner in=new Scanner(System.in);
85+
86+
System.out.println("Number of vertices:");
87+
88+
V=in.nextInt();
89+
90+
int [][]graph = new int[V][V];
91+
92+
System.out.println("Rows and Column of matrix:");
93+
94+
for(int i=0;i<V;i++)
95+
{
96+
for(int j=0;j<V;j++)
97+
98+
graph[i][j]=in.nextInt();
99+
}
100+
Dijkstra ob=new Dijkstra();
101+
102+
ob.dijkstra(graph, 0);
103+
}
104+
}

ProduConsum.java

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
package javacodes;
3+
4+
import java.util.*;
5+
6+
public class ProduConsum{
7+
8+
public static void main(String []args)throws InterruptedException
9+
10+
{
11+
Queue q=new Queue();
12+
13+
Producer p=new Producer(q);
14+
15+
Consumer c=new Consumer(q);
16+
17+
Thread pt=new Thread(p);
18+
19+
Thread ct=new Thread(c);
20+
21+
pt.start();
22+
23+
ct.start();
24+
25+
pt.join();
26+
27+
ct.join();
28+
29+
System.out.println("Existing main Thread");
30+
}
31+
}
32+
class Producer implements Runnable
33+
{
34+
Queue queue;
35+
36+
Producer(Queue queue)
37+
{
38+
this.queue=queue;
39+
40+
41+
}
42+
public void run()
43+
{
44+
int i=0;
45+
46+
while(i<=20)
47+
{
48+
queue.set(i);
49+
50+
i++;
51+
}
52+
System.out.println("Existing Producer Thread");
53+
54+
queue.producerAlive=false;
55+
}
56+
}
57+
class Consumer implements Runnable
58+
{
59+
Queue queue;
60+
61+
Consumer(Queue queue)
62+
{
63+
this.queue=queue;
64+
}
65+
public void run()
66+
{
67+
while(queue.producerAlive)
68+
{
69+
queue.get();
70+
}
71+
System.out.println("Existing Consumer Thread");
72+
}
73+
}
74+
class Queue
75+
{
76+
int n=0;
77+
78+
boolean valueset=false;
79+
80+
boolean producerAlive=true;
81+
82+
synchronized void get()
83+
{
84+
if(!valueset)
85+
{
86+
try {
87+
wait();
88+
89+
} catch (InterruptedException e) {
90+
91+
System.out.println("Error");
92+
}
93+
}
94+
valueset=false;
95+
96+
System.out.println("Got"+" "+this.n);
97+
98+
notify();
99+
}
100+
synchronized void set(int n)
101+
{
102+
if(valueset)
103+
{
104+
try {
105+
106+
wait();
107+
108+
} catch (InterruptedException e) {
109+
110+
System.out.println("Value set");
111+
}
112+
}
113+
this.n=n;
114+
115+
valueset=true;
116+
117+
notify();
118+
}
119+
}

Root.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package javacodes;
2+
3+
import java.util.*;
4+
5+
public class Root{
6+
7+
public static void main(String[] args)
8+
{
9+
Scanner in=new Scanner(System.in);
10+
11+
System.out.println("Number of vertices in tree");
12+
13+
int v=in.nextInt();
14+
15+
int sumt=0,sumc=0,tmp;
16+
17+
System.out.println("Values of vertices");
18+
19+
for(int i=1;i<=v;i++)
20+
{
21+
tmp=in.nextInt();
22+
23+
sumt+=tmp;
24+
}
25+
26+
System.out.println("Enter sum of child's values");
27+
28+
for(int i=1;i<=v;i++)
29+
30+
{
31+
tmp=in.nextInt();
32+
33+
sumc+=tmp;
34+
}
35+
36+
System.out.println("Value of root node is :"+(sumt-sumc));
37+
}
38+
}

0 commit comments

Comments
 (0)