-
Notifications
You must be signed in to change notification settings - Fork 0
/
Java full lab.java
1190 lines (1148 loc) · 24.5 KB
/
Java full lab.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//1. W. P. that reverse a number.
import java.util.Scanner;
class Rev
{
public static void main(String a[])
{
Scanner scanf=new Scanner(System.in);
System.out.print("Enter The No: ");
int x=scanf.nextInt();
int rev=0;
while(x!=0)
{
rev*=10;
rev+=x%10;
x/=10;
}
System.out.println(rev + " is the reversed No");
}
}
//3. W. P. To prints Fibonacci series.
import java.util.Scanner;
class Fibonac
{
public static void main(String A[])
{
Scanner sc=new Scanner(System.in);
int a=0,b=1;
System.out.print("Enter Number of terms: ");
int n=sc.nextInt();
System.out.print(a + ", " + b);
for(int i=2;i<n;i++)
{
int x=a+b;
System.out.print(", " + x);
a=b;b=x;
}
}
}
//4. W.P. to merge two arrays in third array .also sort that array in ascending order.
import java.util.Scanner;
class Array
{
int a[];
Array()
{
a=new int[5];
for(int i=0;i<a.length;i++)
a[i]=i;
}
Array(int x[])
{
a=x;
}
void read()
{
Scanner sc=new Scanner(System.in);
for(int i=0;i<a.length;i++)
a[i]=sc.nextInt();
}
void disp()
{
for(int i=0;i<a.length;i++)
System.out.print(a[i] + " ");
}
int[] merge(int x[])
{
int temp[]=new int[a.length+x.length];
for(int i=0;i<temp.length;i++)
{
if(i<a.length)
temp[i]=a[i];
else
temp[i]=x[i-a.length];
}
for(int i=0;i<temp.length;i++)
{
for(int j=0;j<temp.length-i-1;j++)
{
if(temp[j]>temp[j+1])
{
int temp1=temp[j];
temp[j]=temp[j+1];
temp[j+1]=temp1;
}
}
}
return temp;
}
void sort()
{
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length-i-1;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
}
class MAIN
{
public static void main(String A[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Sizes of arrays: ");
int m=sc.nextInt();
int n=sc.nextInt();
int a[],b[];
a=new int[m];
b=new int[n];
System.out.println("Enter array 1:");
for(int i=0;i<m;i++)
a[i]=sc.nextInt();
System.out.println("Enter array 2:");
for(int i=0;i<n;i++)
b[i]=sc.nextInt();
Array A_ob=new Array(a);
Array res=new Array(A_ob.merge(b));
System.out.println("After Merge ang Sort:");
res.disp();
}
}
javalab2.java
Type
Java
Size
359 bytes
Storage used
359 bytes
Location
Java_lab
Owner
me
Modified
Jun 26, 2018 by me
Opened
8:26 PM by me
Created
Jun 26, 2018 with Google Drive Web
Add a description
Viewers can download
import java.util.*;
class Matrix
{
int a[][];
int row,col;
Matrix()
{
a=new int[3][3];
row=col=3;
}
Matrix(int r,int c)
{
a=new int[r][c];
row=r;col=c;
}
Matrix(int x[][])
{
a=x;
row=x.length;
col=x[0].length;
}
Matrix add(Matrix M)
{
if(this.row!=M.row || this.col!=M.col)
{
System.out.print("Can't Add Two matrix as Dimensions are not equal !!!");
return this;
}
Matrix t = new Matrix(row,col);
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
t.a[i][j]=this.a[i][j]+M.a[i][j];
}
}
return t;
}
Matrix mul(Matrix M)
{
if(this.col!=M.row)
{
System.out.print("Matrix can not be multiplied !!!");
return this;
}
Matrix t = new Matrix(this.row,M.col);
for(int i=0;i<this.row;i++) //I is row of A
{
for(int j=0;j<M.col;j++) //J is col of B
{
for(int k=0;k<M.row;k++) //K is row of B as well as col of A
{
t.a[i][j]+=this.a[i][k]*M.a[k][j];
}
}
}
return t;
}
void read()
{
Scanner sc=new Scanner(System.in);
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
a[i][j]=sc.nextInt();
}
}
}
void display()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.print("\n");
}
}
}
class MAIN
{
public static void main(String args[])
{
Matrix m1,m2,m3;
//ADDITION OF TWO MATRICES
m1=new Matrix(2,3);
m2=new Matrix(2,3);
System.out.println("Enter M1:");
m1.read();
System.out.println("Enter M2:");
m2.read();
m3=m1.add(m2);
System.out.println("After addition:");
m3.display();
//MULTIPLICATION OF TWO MATRICES
m1=new Matrix(2,3);
m2=new Matrix(3,4);
System.out.println("Enter M1:");
m1.read();
System.out.println("Enter M2:");
m2.read();
m3=m1.mul(m2);
System.out.println("After MULTIPLICATION:");
m3.display();
}
}
javalab2.java
Type
Java
Size
359 bytes
Storage used
359 bytes
Location
Java_lab
Owner
me
Modified
Jun 26, 2018 by me
Opened
8:26 PM by me
Created
Jun 26, 2018 with Google Drive Web
Add a description
Viewers can download
/*
• Programs for polymorphism and Inheritance
1. Create a class OverLoadDemo and also create method test () in it.
Overload test () in four ways. First version takes no parameter,
the second takes one integer parameter, and the
third takes two integer parameters and fourth takes one double parameter.
Call all methods from main() function.
*/
class OverLoadDemo
{
void test()
{
System.out.println("Test with no parameter called\n");
}
void test(int x)
{
System.out.println("Test with int parameter("+x+")called\n");
}
void test(int x,int y)
{
System.out.println("Test with two int parameters("+x+","+y+")called\n");
}
void test(double x)
{
System.out.println("Test with double parameter("+x+")called");
}
}
class Polymorf
{
public static void main(String args[])
{
OverLoadDemo ob = new OverLoadDemo();
ob.test();
ob.test(345);
ob.test(30,54);
ob.test(54.67000);
}
}
/*
2. Create class ‘Point3D’.
There are three constructor in a Class ‘Point3D’.
The first form accepts one double argument.
The second form accepts two double arguments.
The third form accepts three double arguments.
These are used to initialize all of the instance variables.
Also prints instance variables.
Demonstrate use of overloaded constructors in main function.
*/
class Point3D
{
double x,y,z;
Point3D(double x)
{
this.x = x;
this.y = 0;
this.z = 0;
}
Point3D(double x,double y)
{
this.x = x;
this.y = y;
this.z = 0;
}
Point3D(double x,double y,double z)
{
this.x = x;
this.y = y;
this.z = z;
}
void disp()
{
System.out.println(x +"\t"+ y +"\t"+ z);
}
}
class Polyconst
{
public static void main(String args[])
{
Point3D p1;
p1=new Point3D(30);
p1.disp();
p1=new Point3D(30,40);
p1.disp();
p1=new Point3D(30,40,50);
p1.disp();
}
}
/*
2. Interface named Multiplication has two methods, multiplication and display.
Class MatrixMultiplication and IntegerNumberMultiplication implements Multiplication.
Write a program for the above condition. Provide appropriate data for the calculation and show output.
*/
import java.util.*;
public interface Multiplication{
void multiplication();
void display();
}
class MatrixMultiplication implements Multiplication{
int a[][];
int b[][];
int rowa,cola;
int rowb,colb;
MatrixMultiplication()
{
a=new int[3][3];
rowa=cola=3;
b=new int[3][3];
rowb=colb=3;
}
MatrixMultiplication(int r1,int c1,int r2,int c2)
{
a=new int[r1][c1];
rowa=r1;cola=c1;
b=new int[r2][c2];
rowb=r2;colb=c2;
}
public void multiplication()
{
//verify the condition
if(cola!=rowb)
{
System.out.print("Matrix can not be multiplied !!!");
}
//multiplication
int[][] t=new int[rowa][colb];
for(int i=0;i<rowa;i++) //I is row of A
{
for(int j=0;j<colb;j++) //J is col of B
{
for(int k=0;k<rowb;k++) //K is row of B as well as col of A
{
t[i][j]+=a[i][k]*b[k][j];
}
}
}
//print result
for(int i=0;i<rowa;i++)
{
for(int j=0;j<colb;j++)
{
System.out.print(t[i][j] + "\t");
}
System.out.print("\n");
}
}
void read()
{
Scanner sc=new Scanner(System.in);
System.out.println("Matrix 1 : ");
for(int i=0;i<rowa;i++)
{
for(int j=0;j<cola;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Matrix 2 : ");
for(int i=0;i<rowb;i++)
{
for(int j=0;j<colb;j++)
{
b[i][j]=sc.nextInt();
}
}
}
public void display()
{
System.out.println("Matrix 1 : ");
for(int i=0;i<rowa;i++)
{
for(int j=0;j<cola;j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.print("\n");
}
System.out.println("Matrix 1 : ");
for(int i=0;i<rowb;i++)
{
for(int j=0;j<colb;j++)
{
System.out.print(b[i][j] + "\t");
}
System.out.print("\n");
}
}
}
class IntegerNumberMultiplication implements Multiplication{
int no1,no2;
IntegerNumberMultiplication(int a,int b)
{
no1=a;no2=b;
}
public void multiplication()
{
System.out.println("After multipliying: " + no1*no2);
}
public void display()
{
System.out.println("Number 1: " + no1 + "\tNumber 2: " + no2);
}
}
class intrfc2{
public static void main(String args[])
{
//instansiation
MatrixMultiplication mp1=new MatrixMultiplication(2,3,3,4);
IntegerNumberMultiplication np1 = new IntegerNumberMultiplication(236,576);
//multipliying intigers and displaying
np1.multiplication();
np1.display();
//multipliying matrix and displaying
mp1.read();
mp1.multiplication();
mp1.display();
}
}
/*
2. Interface named Multiplication has two methods, multiplication and display.
Class MatrixMultiplication and IntegerNumberMultiplication implements Multiplication.
Write a program for the above condition. Provide appropriate data for the calculation and show output.
*/
import java.util.*;
public interface Multiplication{
void multiplication();
void display();
}
class MatrixMultiplication implements Multiplication{
int a[][];
int b[][];
int rowa,cola;
int rowb,colb;
MatrixMultiplication()
{
a=new int[3][3];
rowa=cola=3;
b=new int[3][3];
rowb=colb=3;
}
MatrixMultiplication(int r1,int c1,int r2,int c2)
{
a=new int[r1][c1];
rowa=r1;cola=c1;
b=new int[r2][c2];
rowb=r2;colb=c2;
}
public void Multiplication()
{
//verify the condition
if(cola!=rowb)
{
System.out.print("Matrix can not be multiplied !!!");
}
//multiplication
int[][] t=new int[rowa][colb];
for(int i=0;i<rowa;i++) //I is row of A
{
for(int j=0;j<colb;j++) //J is col of B
{
for(int k=0;k<rowb;k++) //K is row of B as well as col of A
{
t[i][j]+=a[i][k]*b[k][j];
}
}
}
//print result
for(int i=0;i<rowa;i++)
{
for(int j=0;j<colb;j++)
{
System.out.print(t[i][j] + "\t");
}
System.out.print("\n");
}
}
void read()
{
Scanner sc=new Scanner(System.in);
System.out.println("Matrix 1 : ");
for(int i=0;i<rowa;i++)
{
for(int j=0;j<cola;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Matrix 2 : ");
for(int i=0;i<rowb;i++)
{
for(int j=0;j<colb;j++)
{
a[i][j]=sc.nextInt();
}
}
}
public void display()
{
System.out.println("Matrix 1 : ");
for(int i=0;i<rowa;i++)
{
for(int j=0;j<cola;j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.print("\n");
}
System.out.println("Matrix 1 : ");
for(int i=0;i<rowb;i++)
{
for(int j=0;j<colb;j++)
{
System.out.print(b[i][j] + "\t");
}
System.out.print("\n");
}
}
}
class IntegerNumberMultiplication implements Multiplication{
int no1,no2;
IntegerNumberMultiplication(int a,int b)
{
no1=a;no2=b;
}
public void multiplication()
{
System.out.println("After multipliying: " + no1*no2);
}
public void display()
{
System.out.println("Number 1: " + no1 + "\tNumber 2: " + no2);
}
}
class intrfc2{
public static void main(String args[])
{
//instansiation
MatrixMultiplication mp1=new MatrixMultiplication(2,3,3,4);
IntegerNumberMultiplication np1 = new IntegerNumberMultiplication(236,576);
//multipliying intigers and displaying
IntegerNumberMultiplication.multiplication();
IntegerNumberMultiplication.display();
//multipliying matrix and displaying
MatrixMultiplication.read();
MatrixMultiplication.multiplication();
MatrixMultiplication.display();
}
}
/*
1. Declare an interface called Property containing a method computePrice to compute and return the price.
The interface is to be implemented by following two classes i) Bungalow and ii) Flat.
Both the classes have following data members
- name
- constructionArea
The class Bungalow has an additional data member called landArea.
Define computePrice for both classes for computing total price.
Use following rules for computing total price by summing up sub-costs:
Construction cost(for both classes):Rs.500/- per sq.feet
Additional cost ( for Flat) : Rs. 200000/-
( for Bungalow ) : Rs. 200/- per sq.feet
for landArea
Land cost ( only for Bungalow ): Rs. 400/- per sq. feet
Define method main to show usage of method computePrice.
2. Interface named Multiplication has two methods, multiplication and display.
Class MatrixMultiplication and IntegerNumberMultiplication implements Multiplication.
Write a program for the above condition. Provide appropriate data for the calculation and show output.
jdk jvm jre
how to set claSS PATH(temp/permanent) WHY?
*/
import java.util.Scanner;
public interface property{
double computeprice();
}
class Flat implements property{
string name;
int area;
Flat()
{
Scanner sc = new Scanner(system.in);
System.out.print("Enter Name:");
name=sc.next();
System.out.print("Enter Area:");
area=sc.nextInt();
}
public double computeprice(){
double cost;
cost = 500*area + 200000;
return cost;
}
}
class Bungalow implements property{
String name;
int area;
int landArea;
/*
double computelandcost(){
double cost;
cost=400*area;
return cost;
}*/
Bungalow(){
Scanner sc = new Scanner(system.in);
System.out.print("Enter Name:");
name=sc.next();
System.out.print("Enter Area:");
area=sc.nextInt();
System.out.print("Enter LandArea:");
landArea=sc.nextInt();
}
public double computeprice(){
double cost;
cost=(500+200)*area + 400*landArea;
return cost;
}
}
class intrfc{
public static void main(String args[])
{
Flat f1=new Flat();
Bungalow b1=new Bungalow();
System.out.println("Price of Flat1 :" + f1.computeprice());
System.out.println("Price of Bungalow1 :" + b1.computeprice());
}
}/*
1. Declare an interface called Property containing a method computePrice to compute and return the price.
The interface is to be implemented by following two classes i) Bungalow and ii) Flat.
Both the classes have following data members
- name
- constructionArea
The class Bungalow has an additional data member called landArea.
Define computePrice for both classes for computing total price.
Use following rules for computing total price by summing up sub-costs:
Construction cost(for both classes):Rs.500/- per sq.feet
Additional cost ( for Flat) : Rs. 200000/-
( for Bungalow ) : Rs. 200/- per sq.feet
for landArea
Land cost ( only for Bungalow ): Rs. 400/- per sq. feet
Define method main to show usage of method computePrice.
2. Interface named Multiplication has two methods, multiplication and display.
Class MatrixMultiplication and IntegerNumberMultiplication implements Multiplication.
Write a program for the above condition. Provide appropriate data for the calculation and show output.
jdk jvm jre
how to set claSS PATH(temp/permanent) WHY?
*/
import java.util.Scanner;
public interface property{
double computeprice();
}
class Flat implements property{
String name;
int area;
Flat()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name:");
name=sc.next();
System.out.print("Enter Area:");
area=sc.nextInt();
}
public double computeprice(){
double cost;
cost = 500*area + 200000;
return cost;
}
}
class Bungalow implements property{
String name;
int area;
int landArea;
/*
double computelandcost(){
double cost;
cost=400*area;
return cost;
}*/
Bungalow(){
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name:");
name=sc.next();
System.out.print("Enter Area:");
area=sc.nextInt();
System.out.print("Enter LandArea:");
landArea=sc.nextInt();
}
public double computeprice(){
double cost;
cost=(500+200)*area + 400*landArea;
return cost;
}
}
class intrfc{
public static void main(String args[])
{
System.out.println("Flat1 Details:");
Flat f1=new Flat();
System.out.println("Bungalow1 Details:");
Bungalow b1=new Bungalow();
System.out.println("Price of Flat1 :" + f1.computeprice());
System.out.println("Price of Bungalow1 :" + b1.computeprice());
}
}
/*
2. Write a program to increment the value of one variable by one and display it after one second using thread.
*/
class Mythread extends Thread{
public void run(){
for(int i=0;i<40;i++)
{
System.out.println(i);
try{
sleep(1000);
}
catch(InterruptedException e)
{
System.out.print("error 1");
}
}
}
}
class DelayCounter{
public static void main(String a[]){
Mythread t1=new Mythread();
Mythread t2=new Mythread();
System.out.println("Thread 1");
t1.start();
System.out.println("Thread 2");
t2.start();
}
}
class PrimeThread extends Thread {
int a,b;
PrimeThread(){
a=0;
b=10;
}
PrimeThread(int a,int b)
{
this.a=a;
this.b=b;
}
@Override
public void run(){
for(int i=a;i<b;i++)
{
int flag=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
System.out.print(i + "\t");
}
}
}
class PrimeThread2 implements Runnable{
int a,b;
PrimeThread2(){
a=0;
b=10;
}
PrimeThread2(int a,int b)
{
this.a=a;
this.b=b;
}
@Override
public void run(){
for(int i=a;i<b;i++)
{
int flag=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
System.out.print(i + "\t");
}
}
}
public class Multithread{
public static void main(String a[]){
PrimeThread t1=new PrimeThread(3,30),t2=new PrimeThread();
PrimeThread2 tt1=new PrimeThread2(3,30),tt2=new PrimeThread2();
Thread T1=new Thread(tt1),T2=new Thread(tt2);
System.out.println("\nThread 1.1");
t1.start();
try{
t1.join();
}
catch(InterruptedException e){
System.out.print("Error 1");
}
System.out.println("\nThread 1.2");
t2.start();
try{
t2.join();
}
catch(InterruptedException e){
System.out.print("Error 2");
}
System.out.println("\nThread 2.1");
T1.start();
try{
T1.join();
}
catch(InterruptedException e){
System.out.print("Error 21");
}
System.out.println("\nThread 2.2");
T2.start();
try{
T2.join();
}
catch(InterruptedException e){
System.out.print("Error 2");
}
}
}
class Exception2{
public static void main(String args[])
{
try{
int a=4,b=0,c=1;
a=b/c;
Exception e=new Exception();
throw e;
}
catch(Exception e){
System.out.println("Divide by zero error");
}
}
}
/*
3. Assume one class Queue that defines queue of fix size says 15.
• Assume one class producer which implements Runnable, having priority NORM_PRIORITY +1
• One more class consumer implements Runnable, having priority NORM_PRIORITY-1
• Class TestThread is having main method with maximum priority, which creates 1 thread for producer and 2 threads for consumer.
• Producer produces number of elements and put on the queue. when queue becomes full it notifies other threads.
• Consumer consumes number of elements and notifies other thread when queue become empty..
*/
class Queue{
int[] a;
int f,r,l;
Queue()
{
a=new int[10];
f=-1;r=-1;
l=10;
}
Queue(int n)
{
a=new int[n];
f=-1;r=-1;
l=n;
}
synchronized void produce()
{
try{
while(size()>l)
{
System.out.println("Producer waiting !!");
wait();
}
while(true)
{
if(f==-1)
{
f=0;r=0;
a[r]=1;
//System.out.println("\nItem produced: " + 1);
disp();
notifyAll();
try{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.print("error 1");
}
}
else if((r+1)%l==f)
{
System.out.println("\nItem not produced(overflow)!");
//disp();
notifyAll();
//while(size()>l){
System.out.println("Producer waiting !!");
wait();