-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava_Lab_Programs.txt
More file actions
1573 lines (1228 loc) · 39 KB
/
Copy pathJava_Lab_Programs.txt
File metadata and controls
1573 lines (1228 loc) · 39 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
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
################## part-A #####################
1. Design a java Program to add two matrix using multi-dimensional arrays, pass
two-dimensional array as parameter to the method.
import java.util.*;
public class matrix_add {
static void printMatrix(int M[][], int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print(M[i][j] + " ");
}
System.out.println();
}
}
static void Multiply(int row1, int col1, int arr1[][], int row2, int col2, int arr2[][]) {
System.out.println("First Matrix:");
printMatrix(arr1, row1, col1);
System.out.println("Second Matrix:");
printMatrix(arr2, row2, col2);
int arr3[][] = new int[row1][col2];
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
arr3[i][j] = arr1[i][j] + arr2[i][j];
}
}
System.out.println();
System.out.println("Resultant Matrix:");
printMatrix(arr3, row1, col2);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows in first matrix");
int row1 = sc.nextInt();
System.out.println("Enter the number of columns in first matrix");
int col1 = sc.nextInt();
System.out.println("Enter the number of rows in second matrix");
int row2 = sc.nextInt();
System.out.println("Enter the number of columns in second matrix");
int col2 = sc.nextInt();
int arr1[][] = new int[row1][col1];
int arr2[][] = new int[row2][col2];
System.out.println("Enter the elements of first matrix:");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
arr1[i][j] = sc.nextInt();
}
}
System.out.println("");
System.out.println("Enter the elements of second matrix:");
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
arr2[i][j] = sc.nextInt();
}
}
Multiply(row1, col1, arr1, row2, col2, arr2);
}
}
###############Matrix Multy###################
import java.util.*;
public class matrix_multiply {
static void printMatrix(int M[][], int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print(M[i][j] + " ");
}
System.out.println();
}
}
static void Multiply(int row1, int col1, int arr1[][], int row2, int col2, int arr2[][]) {
System.out.println("First Matrix:");
printMatrix(arr1, row1, col1);
System.out.println("Second Matrix:");
printMatrix(arr2, row2, col2);
int arr3[][] = new int[row1][col2];
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
for(int k=0; k<row2; k++) {
arr3[i][j] += arr1[i][j] * arr2[i][j];
}
}
}
System.out.println("Resultant Matrix:");
printMatrix(arr3, row1, col2);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows in first matrix");
int row1 = sc.nextInt();
System.out.println("Enter the number of columns in first matrix");
int col1 = sc.nextInt();
System.out.println("Enter the number of rows in second matrix");
int row2 = sc.nextInt();
System.out.println("Enter the number of columns in second matrix");
int col2 = sc.nextInt();
if (row2 != col1) {
System.out.println("Multiplication Not Possible");
}
else {
int arr1[][] = new int[row1][col1];
int arr2[][] = new int[row2][col2];
System.out.println("Enter the elements of first matrix:");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
arr1[i][j] = sc.nextInt();
}
}
System.out.println("");
System.out.println("Enter the elements of second matrix:");
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
arr2[i][j] = sc.nextInt();
}
}
Multiply(row1, col1, arr1, row2, col2, arr2);
}
}
}
2. Design a java program with one method to put even & odd elements of an array
in 2 separate arrays, and another method to find the transpose of the matrix by
implementing the concept of method overriding
import java.util.*;
class EvenOdd {
void meth() {
System.out.print("Enter size of matrix: ");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int l = 0, k = 0;
int a[] = new int[x];
int even[] = new int[x];
int odd[] = new int[x];
System.out.println("Enter the elements of matrix: ");
for (int i = 0; i < x; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < x; i++) {
if (a[i] % 2 == 0) {
even[k] = a[i];
k++;
}
else {
odd[l] = a[i];
l++;
}
}
System.out.println("Even matrix: ");
for (int i = 0; i < k; i++) {
System.out.print(even[i] + " ");
}
System.out.println();
System.out.println("Odd matrix: ");
for (int i = 0; i < l; i++) {
System.out.print(odd[i] + " ");
}
System.out.println();
}
}
class Transpose extends EvenOdd {
void meth() {
super.meth();
int m, n;
System.out.println();
System.out.print("Enter number of rows: ");
Scanner track = new Scanner(System.in);
m = track.nextInt();
System.out.print("Enter number of columns: ");
n = track.nextInt();
int a[][] = new int[m][n];
int b[][] = new int[m][n];
System.out.println("Enter a 2D matrix: ");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = track.nextInt();
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
b[i][j] = a[j][i];
}
}
System.out.println("Entered matrix is: ");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("Transpose of matrix is");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
}
class matrix_transpose {
public static void main(String[] args) {
Transpose obj = new Transpose();
obj.meth();
}
}
3. Design a java code to accept a value for ‘n’ and calculate the total number of all
possible squares in a square matrix
// Design a java code to accept a value for n and calculate the total number of all possible squares in a square matrix
import java.util.*;
public class square_matrix
{
public static void main(String[] args)
{
int size, sum=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the matrix");
size = sc.nextInt();
if (size<1)
{
System.out.println("Invalid Input");
}
else
{
while(size!=0)
{
sum += size*size;
size--;
}
}
System.out.println("Total number of square matrices are "+ sum);
}
}
4. Design a code for a simple calculator which takes input from the user and also
details of what operation must be performed. The user can input only 2 operands.
import java.util.*;
public class simple_calculator {
public static void main(String args[]) {
int op1, op2, res;
Scanner sc = new Scanner(System.in);
System.out.println("Enter ther first operand");
op1 = sc.nextInt();
System.out.println("Enter the second operand");
op2 = sc.nextInt();
char operator;
System.out.print("Enter the operator (+,-,*,/,%): ");
System.out.println();
operator = sc.next().charAt(0);
switch (operator) {
case '+':
res = op1 + op2;
System.out.println("The addition is " + res);
break;
case '-':
res = op1 - op2;
System.out.println("The subtraction is " + res);
break;
case '*':
res = op1 * op2;
System.out.println("The multiplication is " + res);
break;
case '/':
res = op1 / op2;
System.out.println("The division is " + res);
break;
case '%':
res = op1 % op2;
System.out.println("The division is " + res);
break;
default:
System.out.println("Invalid Input");
break;
}
}
}
5. Design a java program to calculate the difference between the sum of the odd
level and even level nodes of a binary tree
class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
class binary_tree {
Node root;
int getLevelDiff(Node node) {
if (node == null)
return 0;
return node.data - getLevelDiff(node.left) - getLevelDiff(node.right);
}
public static void main(String[] args) {
binary_tree tree = new binary_tree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.right.left = new Node(6);
tree.root.right.right = new Node(7);
int levelDiff = tree.getLevelDiff(tree.root);
System.out.println("Difference between sum of odd and even level nodes: " + levelDiff);
}
}
6.Design a java code to accept 5 strings from the user and print them in lexicographical
order
import java.util.*;
public class lexicographic {
public static void main(String[] args) {
System.out.println("Enter five names: ");
String array[] = new String[5]; // Declaration of string array
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
array[i] = sc.nextLine();
}
Arrays.sort(array);
System.out.print("Sorted list is: ");
for (int i = 0; i < 5; i++) {
System.out.print(array[i] + " ");
}
}
}
7. Design a java code for implementing Binary Search, pass array as parameter to
the method
import java.util.*;
public class binary_search {
int binarySearch(int arr[], int low, int high, int x) {
if (high >= low) {
int mid = low+(high-low)/2;
if (arr[mid] == x)
return mid;
else if (arr[mid] > x)
return binarySearch(arr, low, mid-1, x);
else
return binarySearch(arr, mid+1, high, x);
}
return -1;
}
public static void main(String args[]) {
binary_search ob = new binary_search();
int arr[] = { 10, 20, 30, 40, 50 };
int n = arr.length;
int x = 30;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element is not present in array");
else
System.out.println("Element is present at index " + result);
}
}
8. Design a java code to implement method overloading.
import java.util.*;
public class method_overloading
{
public void add(int a, int b)
{
int sum = a + b;
System.out.println("Sum of two integers: " + sum);
}
public void add(double a, double b)
{
double sum = a + b;
System.out.println("Sum of two doubles: " + sum);
}
public void add(String a, String b)
{
String result = a + b;
System.out.println("Concatenation of two strings: " + result);
}
public static void main(String[] args)
{
method_overloading example = new method_overloading();
// Call the add() method with different argument types
example.add(5, 10);
example.add(3.5, 2.8);
example.add("Hello, ", "world!");
}
}
9. Design a java program to implement multiple inheritance using an interface.
import java.util.*;
interface motar1 {
int speed = 90;
public void distance();
}
interface motar2 {
int distance = 200;
public void speed();
}
class Vehicle implements motar1, motar2 {
public void distance() {
int distance = speed * 100;
System.out.println("Distance is: " + distance);
}
public void speed() {
int speed = distance / 100;
System.out.println("Speed is: " + speed);
}
}
public class multiple_inheritance {
public static void main(String[] args) {
Vehicle obj = new Vehicle();
obj.distance();
obj.speed();
}
}
10. Design a code to print a pyramid based on level entered by the user
import java.util.*;
public class Pattern
{
public static void main(String[] args)
{
int rows = 5, k = 0, count = 0, count1 = 0;
for (int i = 1; i <= rows; ++i)
{
for (int space = 1; space <= rows - i; ++space)
{
System.out.print(" ");
++count;
}
while (k != 2 * i - 1)
{
if (count <= rows-1)
{
System.out.print((i + k) + " ");
++count;
}
else
{
++count1;
System.out.print((i + k - 2 * count1) + " ");
}
++k;
}
count1 = count = k = 0;
System.out.println();
}
}
}
11. Write a Java program to find digit sum two digit number until the digit sum is less
than 10.
import java.util.*;
public class sumofdigit {
public static void main(String[] args) {
int num, temp, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
num = sc.nextInt();
while (num != 0) {
while (num != 0) {
temp = num % 10;
sum = sum + temp;
num = num/10;
}
if (sum >= 10) {
System.out.println("Sum " + sum);
num = sum;
sum = 0;
}
}
System.out.println("Sum in single digit is: "+ sum);
}
}
12. Design a java code which accepts a number (which is non-zero and positive) from
the user and then checks if it is a happy number or not. Implement it using nested
interface concept
import java.util.*;
interface yes {
int check(int num);
}
class Happy implements yes {
public int check(int num) {
int rem = 0, sum = 0;
// calculate the sum of squares of each digit
while (num > 0) {
rem = num % 10;
sum = sum + (rem * rem);
num = num / 10;
}
return sum;
}
}
public class happy_number {
public static void main(String[] args) {
Happy obj = new Happy();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int finalVal = num;
while (finalVal != 1 && finalVal != 4) {
finalVal = obj.check(finalVal);
}
if (finalVal == 1) {
System.out.println(num + " is a Happy Number");
}
else {
System.out.println(num + " is not a Happy Number");
}
}
}
13. Design an java code which accepts a Room Number, Mobile Number and Name
of the Customer and generate a 6 Character Unique Password
import java.util.*;
public class password_generator {
public static void main(String args[]) {
int rno, sum = 0, temp;
long ph;
String pw = new String();
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Room Number");
rno = sc.nextInt();
int rno1 = rno;
System.out.println("Enter the Mobile Number");
ph = sc.nextLong();
System.out.println("Enter the Name");
name = sc.next();
char sym[] = { ')', '@', '#', '$', '%', '^', '!', '&', '(', '*' };
while(rno != 0) {
while (rno != 0) {
temp = rno % 10;
sum = sum + temp;
rno = rno / 10;
}
if (sum >= 10) {
rno = sum;
sum = 0;
}
}
int c = rno1 / 100;
int len = name.length();
System.out.println("Password is: ");
pw = "" + name.charAt(0) + (ph % 10) + sum + sym[c] + name.charAt(len - 1);
System.out.println(pw);
}
}
################## part-B #####################
1.Develop a small java application, which accepts employee id from the command prompt
and displays the details using arrays.
->Emp No
->Emp Name
->Department
->Designation and Salary
Your may assume that the array is initialized with the following details
Salary is calculated as Basic+HRA+DA-IT. (DA details are given in the Designation table)
Designation details:
Use Switch-Case to print Designation in the output and to find the value of DA for a
particular employee
import java.util.*;
public class prac{
public void calc(int id){
int[] EmpId = { 1001, 1002, 1003, 1004, 1005, 1006, 1007 };
String[] EmpName = { "Abc", "Opqr", "Ghi", "Wxyz", "Jklmn", "Stuv", "Def" };
String[] JoinDate = { "01/04/2009", "23/08/2012", "12/11/2008", "29/01/2013", "16/07/2005", "01/01/2000","12/06/2006" };
char[] DesigCode = { 'e', 'c', 'k', 'r', 'm', 'e', 'c' };
String[] Department = { "R&D", "PM", "Acct", "Front Desk", "Engg", "Manufacturing", "PM" };
double[] Basic = { 20000, 30000, 10000, 12000, 50000, 23000, 29000 };
double[] HRA = { 8000, 12000, 8000, 6000, 20000, 9000, 12000 };
double[] IT = { 3000, 9000, 1000, 2000, 20000, 4400, 10000 };
char[] DesignationCode = { 'e', 'c', 'k', 'r', 'm' };
String[] Designation = { "Engineer", "Consultant", "Clerk", "Receptionist", "Manager" };
double[] DA = { 20000, 32000, 12000, 15000, 40000 };
for (int i = 0; i < EmpId.length; i++) {
if (EmpId[i] == id) {
System.out.println("Emp Id\tEmp Name Department\tDesignation\tDA\tSalary");
System.out.print(EmpId[i]+"\t"+EmpName[i]+"\t\t"+Department[i]);
for (int j = 0; j < DesignationCode.length; j++) {
if (DesigCode[i] == DesignationCode[j]) {
System.out.print("\t"+Designation[j]+"\t"+DA[i]+"\t");
double sum = Basic[i]+HRA[i]+DA[j]-IT[i];
System.out.println(sum);
}
}
}
}
}
public static void main(String[] args) {
prac obj = new prac();
Scanner sc = new Scanner(System.in);
System.out.println("Enter Employee ID: ");
int id = sc.nextInt();
switch (id) {
case 1001: obj.calc(id); break;
case 1002: obj.calc(id); break;
case 1003: obj.calc(id); break;
case 1004: obj.calc(id); break;
case 1005: obj.calc(id); break;
case 1006: obj.calc(id); break;
case 1007: obj.calc(id); break;
default:
System.out.println("There is no employee with EmpId: " + id);
break;
}
}
}
2.Develop a producer-consumer problem using the concept of multithreading
Producer Consumer-- file
program1 file ---psvm -- Threa1 andd thread 2
3. Design and Implement GUI for managing Employee Details using concepts of Files
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class EmployeeFile {
private static Color black;
public static void main(String[] args) {
JFrame frameobj = new JFrame(); // creating frame
frameobj.setSize(500, 500); // declaring frame size
GridLayout g1 = new GridLayout(5, 2); // layout of the frame
frameobj.setLayout(g1); // layout is set to the frame created
JPanel p1 = new JPanel(); // creating panels
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JPanel p6 = new JPanel();
JPanel p7 = new JPanel();
JPanel p8 = new JPanel();
JPanel p9 = new JPanel();
JPanel p10 = new JPanel();
JLabel l1 = new JLabel("NAME"); // creating labels
JLabel l2 = new JLabel("ID");
JLabel l3 = new JLabel("DOJ");
JLabel l4 = new JLabel("DOB");
JTextField f1 = new JTextField(); // create obj for txtfield
JTextField f2 = new JTextField();
JTextField f3 = new JTextField();
JTextField f4 = new JTextField();
f1.setPreferredSize(new Dimension(200, 30)); // size of txtfield
f2.setPreferredSize(new Dimension(200, 30));
f3.setPreferredSize(new Dimension(200, 30));
f4.setPreferredSize(new Dimension(200, 30));
JButton b1 = new JButton("SUBMIT");
JButton b2 = new JButton("RESET");
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File fileobj = new File("C:\\Users\\Piyush Narayan\\OneDrive\\Desktop\file.txt");
try {
FileWriter fw = new FileWriter(fileobj.getAbsoluteFile(), true);
System.out.println("\nNAME: "+f1.getText()+"\n"+"ID : "+f2.getText()+"\n"+"DOJ:"+f3.getText()+"\n"+"DOB"+f4.getText()+"\n");
fw.write("Name:"+f1.getText()+"\n"+"ID:"+f2.getText()+"\n"+"DOJ:"+f3.getText()+"\n"+"DOB"+f4.getText()+"\n");
fw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f1.setText("");
f2.setText(null);
f3.setText(null);
f4.setText(null);
}
});
p1.add(l1); // add labels to panels where labels=name,id,doj,dob
p3.add(l2);
p5.add(l3);
p7.add(l4);
p2.add(f1); // add textfield to panels where txtfield is user defined
p4.add(f2);
p6.add(f3);
p8.add(f4);
p9.add(b1); // add buttons to panel
p10.add(b2);
// l1.setBorder(BorderFactory.createLineBorder(Color.black));
l1.setBorder(BorderFactory.createLineBorder(black, 10));
l2.setBorder(BorderFactory.createLineBorder(black, 10));
l3.setBorder(BorderFactory.createLineBorder(black, 10));
l4.setBorder(BorderFactory.createLineBorder(black, 10));
frameobj.add(p1); // add panels to frames
frameobj.add(p2);
frameobj.add(p3);
frameobj.add(p4);
frameobj.add(p5);
frameobj.add(p6);
frameobj.add(p7);
frameobj.add(p8);
frameobj.add(p9);
frameobj.add(p10);
frameobj.setVisible(true);
}
}
4.Write a program to implement different exception handling methods in java.
import java.util.*;
public class exception_handling {
public static void main(String[] args) {
try {
try {
int[] a = { 1, 2, 3 };
System.out.println(a[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Exception Type: "+ e);
}
System.out.println(4 / 0);
} catch (ArithmeticException e) {
System.out.println("Exception Type: "+ e);
}
}
}
5.Design and implement a simple inventory central system for a small video rental store
using constructors and Object List
##############Video##########################
public class Video {
String Name;
boolean status;
double rating;
public Video(String n, boolean s, double r) {
super();
this.Name = n;
this.status = s;
this.rating = r;
}
public String getmName() {
return Name;
}
public void setmName(String mName) {
this.Name = mName;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public double getRating() {
return rating;
}
public double setRating(double rating) {
return this.rating = rating;
}
}
#################VideoMethods ####################
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;
public class VideoMethods{
List<Video> MovieList = new ArrayList<Video>();
public void AddMovies() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the name of the movie:");
String mName = in.nextLine();
System.out.print("Enter the status of the movie(True/False):");
boolean status = in.nextBoolean();
System.out.print("Enter the ratings for the movie(0-5):");
double rating = in.nextDouble();
Video v = new Video(mName, status, rating);
MovieList.add(v);
System.out.println("Library Initialized");
}
public void DisplayAll() {
if (MovieList.isEmpty()) {
System.out.println("No movies in the library");
}
for (Video m : MovieList) {
System.out.println("Movie : " + m.getmName() + " " + "Status : " + m.isStatus() + " " + "Rating " + m.getRating());
}
}
boolean RentOut(String name)
{
for(Video m :MovieList){
if(m.getmName().equalsIgnoreCase(name)){
if(m.isStatus()){
m.setStatus(false);
return true;
}
}
return false;
}
return false;
}
public void CollectIn(String name,double rat)
{
boolean flag=false;
for(Video m :MovieList)
{
if(m.getmName().equalsIgnoreCase(name)){
m.setStatus(true);
flag=true;
Math.round(m.setRating((m.getRating() + rat)/2));
}