-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayCode1.java
More file actions
174 lines (144 loc) · 4.46 KB
/
ArrayCode1.java
File metadata and controls
174 lines (144 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.util.Scanner;
public class ArrayCode1 {
public static void main(String[] args) {
System.out.println("-----------Static Approach-------------");
//single-dimensional array using static approach
int a[]= {10,20,30,40,50};
for(int i=0;i<5;i++) {
System.out.println("Elements of array a: "+a[i]);
}
System.out.println("-----------Dynamic Approach-------------");
//single-dimensional array using dynamic approach
System.out.println("ENter the array size");
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int a1[] = new int[size];
for(int i=0;i<a1.length;i++) {
System.out.println("Enter the element no:"+(i+1));
a1[i]=sc.nextInt();
}
//Displaying the array elements
for(int i=0;i<a1.length;i++) {
System.out.println("the element no:"+(i+1)+" is: "+a1[i]);
}
sc.close();
}
}
Scanner sc = new Scanner(System.in);
int [][] a1 = new int[2][2];
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
System.out.println("Enter the element no:"+(j+1));
a1[i][j]=sc.nextInt();
}
}
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
System.out.println("the element no:"+(j+1)+" is: "+a1[i][j]);
}
}
//Thread is a built in class in java present in java.lang package
public class ThreadBasic {
public static void main(String[] args) {
Thread t = new Thread();
System.out.println(t);
t.setName("Shaktiman");
t.setPriority(1);
System.out.println(t);
System.out.println(Thread.currentThread().getName());
System.out.println(t.getName());
System.out.println(t.getId());
System.out.println(t.getPriority());
System.out.println(t.getClass());
}
}
// Thread[Thread-0,5,main]
// Thread-0 (name of the thread)
// 5 (priority of the thread)
// main (method associated to the thread)
class Operations extends Thread
{
public void run() {
if(Thread.currentThread().getName().equals("Addition")) {
add();
}
else if(Thread.currentThread().getName().equals("Subtraction")) {
sub();
}
else if(Thread.currentThread().getName().equals("Multiplication")) {
mul();
}
else {
div();
}
}
void add() {
System.out.println("Addition is executed");
}
void sub() {
System.out.println("Subtraction is executed");
}
void mul() {
System.out.println("Multiplication is executed");
}
void div() {
System.out.println("Division is executed");
}
}
public class ThreadingCode1 {
public static void main(String[] args) {
Operations op1 = new Operations();//thread-1
Operations op2 = new Operations();//thread-2
Operations op3 = new Operations();//thread-3
Operations op4 = new Operations();//thread-4
op1.setName("Addition");
op2.setName("Subtraction");
op3.setName("Multiplication");
op4.setName("Division
op4.setName("Division");
op1.start();//op1 is given to the scheduler
op2.start();//op2 is given to the scheduler
op3.start();//op3 is given to the scheduler
op4.start();//op4 is given to the scheduler
}
}
public class ThreadingCode2 {
public static void main(String[] args) {
Operations1 op1 = new Operations1();//not a thread
Operations1 op2 = new Operations1();//not a thread
Operations1 op3 = new Operations1();//not a thread
Operations1 op4 = new Operations1();//not a thread
Thread t1 = new Thread(op1);
Thread t2 = new Thread(op2);
Thread t3 = new Thread(op3);
Thread t4 = new Thread(op4);
t1.setName("Addition");
t2.setName("Subtraction");
t3.setName("Multiplication");
t4.setName("Division");
t1.start();//t1 is given to the scheduler
t2.start();//t2 is given to the scheduler
t3.start();//t3 is given to the scheduler
t4.start();//t4 is given to the scheduler
}
}
public class ThreadingCode2 {
public static void main(String[] args) {
Operations1 op1 = new Operations1();//not a thread
Operations1 op2 = new Operations1();//not a thread
Operations1 op3 = new Operations1();//not a thread
Operations1 op4 = new Operations1();//not a thread
Thread t1 = new Thread(op1);
Thread t2 = new Thread(op2);
Thread t3 = new Thread(op3);
Thread t4 = new Thread(op4);
t1.setName("Addition");
t2.setName("Subtraction");
t3.setName("Multiplication");
t4.setName("Division");
t1.start();//t1 is given to the scheduler
t2.start();//t2 is given to the scheduler
t3.start();//t3 is given to the scheduler
t4.start();//t4 is given to the scheduler
}
}