-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignmentOneJava.html
881 lines (815 loc) · 27.5 KB
/
AssignmentOneJava.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Java Exercises with Solutions - vCode">
<title>vCode</title>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/styles/night-owl.min.css"
/>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous"
/>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500&display=swap"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Josefin+Sans:wght@300;400;500&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="./exercise.css" />
<script
src="https://kit.fontawesome.com/f0235048b7.js"
crossorigin="anonymous"
></script>
</head>
<body>
<!-- Navbar -->
<section id="navbar">
<nav class="navbar navbar-expand-lg navbar-dark">
<div class="container-fluid">
<a class="navbar-brand" href="./index.html">vCode</a>
<button
class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<!-- <li class="nav-item">
<a class="nav-link" href="#">About</a>
</li> -->
<li class="nav-item">
<a class="nav-link" href="/vcode/AssignmentOneJava.html"
>Assignment</a
>
</li>
<li class="nav-item">
<a class="nav-link" href="/vcode/jdbc.html"
>JDBC</a
>
</li>
</ul>
</div>
</div>
</nav>
</section>
<section class="hero-section container">
<h2 class="text-center mt-3 herotxt">
Java Questions with Solutions for Assignment - 1
</h2>
<div class="links">
<a href="#first">1. Write a program to find area and perimeter of a rectangle.</a>
<a href="#second">2. Write a program to calculate Simple Interest and total amount.</a>
<a href="#third">3. Write a program to convert Fahrenheit to Celsius.</a>
<a href="#fourth">4. Write a program to swap two numbers without using a third variable.</a>
<a href="#fifth">5. Write a program to find the greatest among three numbers.</a>
<a href="#sixth">6. Write a program to check if a number is even or odd.</a>
<a href="#seventh">7. Write a program to check if a year is a leap year.</a>
<a href="#eighth">8. Write a program to find the sum of digits of a number.</a>
<a href="#ninth">9. Write a program to find the factorial of a number.</a>
<a href="#tenth">10. Write a program to reverse a number.</a>
<a href="#eleventh">11. Write a program to check if a number is a palindrome.</a>
<a href="#twelfth">12. Write a program to check if a number is an Armstrong number.</a>
<a href="#thirteenth">13. Write a program to check if a number is prime.</a>
<a href="#fourteenth">14. Write a program to display the Fibonacci series up to n terms.</a>
<a href="#fifteenth">15. Write a program to calculate the sum and average of 10 numbers in an array.</a>
<a href="#sixteenth">16. Write a program to sort 10 numbers in ascending order using bubble sort.</a>
<a href="#seventeenth">17. Write a program to search an element using linear search in a sorted array.</a>
<a href="#eighteenth">18. Write a program to perform matrix addition (2x2).</a>
<a href="#nineteenth">19. Write a program to perform matrix multiplication (2x2).</a>
<a href="#twentieth">20. Write a program to demonstrate classes and objects.</a>
<a href="#twentyfirst">21. Write a program to demonstrate function overloading.</a>
<a href="#twentysecond">22. Write a program to demonstrate constructor overloading.</a>
<a href="#twentythird">23. Write a program to demonstrate inheritance.</a>
<a href="#twentyfourth">24. Write a program to demonstrate different try-catch scenarios.</a>
<a href="#twentyfifth">25. Write a program to demonstrate interfaces.</a>
<a href="#twentysixth">26. Write a program to demonstrate packages.</a>
<a href="#twentyseventh">27. Write a program to demonstrate multithreading.</a>
</div>
</section>
<section class="solutions container">
<h5 id="first">1. Write a program to find area and perimeter of a rectangle.</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class Rectangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter length: ");
double length = sc.nextDouble();
System.out.print("Enter breadth: ");
double breadth = sc.nextDouble();
System.out.println("Area: " + (length * breadth));
System.out.println("Perimeter: " + (2 * (length + breadth)));
}
}</code>
</pre>
</div>
<h5 id="second">2. Write a program to calculate Simple Interest and total amount</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class SimpleInterest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter principal amount: ");
double principal = sc.nextDouble();
System.out.print("Enter rate of interest: ");
double rate = sc.nextDouble();
System.out.print("Enter time (in years): ");
double time = sc.nextDouble();
double interest = (principal * rate * time) / 100;
double totalAmount = principal + interest;
System.out.println("Simple Interest: " + interest);
System.out.println("Total Amount: " + totalAmount);
}
}</code>
</pre>
</div>
<h5 id="third">3. Write a program to convert Fahrenheit to Celsius</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class TemperatureConversion {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter temperature in Fahrenheit: ");
double fahrenheit = sc.nextDouble();
double celsius = (fahrenheit - 32) * 5 / 9;
System.out.println("Temperature in Celsius: " + celsius);
}
}</code>
</pre>
</div>
<h5 id="fourth">4. Write a program to swap two numbers without using a third variable</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class SwapNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
System.out.println("Before Swap: num1 = " + num1 + ", num2 = " + num2);
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("After Swap: num1 = " + num1 + ", num2 = " + num2);
}
}</code>
</pre>
</div>
<h5 id="fifth">5. Write a program to find the greatest among three numbers</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class GreatestNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
System.out.print("Enter third number: ");
int num3 = sc.nextInt();
if (num1 >= num2 && num1 >= num3) {
System.out.println("The greatest number is: " + num1);
} else if (num2 >= num1 && num2 >= num3) {
System.out.println("The greatest number is: " + num2);
} else {
System.out.println("The greatest number is: " + num3);
}
}
}</code>
</pre>
</div>
<h5 id="sixth">6. Write a program to check if a number is even or odd</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class EvenOddCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num % 2 == 0) {
System.out.println("The number is even.");
} else {
System.out.println("The number is odd.");
}
}
}</code>
</pre>
</div>
<h5 id="seventh">7. Write a program to check if a year is a leap year</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class LeapYearCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = sc.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println("The year is a leap year.");
} else {
System.out.println("The year is not a leap year.");
}
}
}</code>
</pre>
</div>
<h5 id="eighth">8. Write a program to find the sum of digits of a number</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class SumOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("The sum of digits is: " + sum);
}
}</code>
</pre>
</div>
<h5 id="ninth">9. Write a program to find the factorial of a number</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
System.out.println("The factorial is: " + factorial);
}
}</code>
</pre>
</div>
<h5 id="tenth">10. Write a program to reverse a number</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int reverse = 0;
while (num != 0) {
reverse = reverse * 10 + num % 10;
num /= 10;
}
System.out.println("The reversed number is: " + reverse);
}
}</code>
</pre>
<h5 id="eleventh">11. Write a program to check if a number is a palindrome</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int original = num;
int reverse = 0;
while (num != 0) {
reverse = reverse * 10 + num % 10;
num /= 10;
}
if (original == reverse) {
System.out.println("The number is a palindrome.");
} else {
System.out.println("The number is not a palindrome.");
}
}
}</code>
</pre>
</div>
<h5 id="twelfth">12. Write a program to check if a number is an Armstrong number</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class ArmstrongCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int original = num;
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += digit * digit * digit;
num /= 10;
}
if (original == sum) {
System.out.println("The number is an Armstrong number.");
} else {
System.out.println("The number is not an Armstrong number.");
}
}
}</code>
</pre>
</div>
<h5 id="thirteenth">13. Write a program to check if a number is prime</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println("The number is prime.");
} else {
System.out.println("The number is not prime.");
}
}
}</code>
</pre>
</div>
<h5 id="fourteenth">14. Write a program to display the Fibonacci series up to n terms</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int a = 0, b = 1;
System.out.print("Fibonacci series: " + a + ", " + b);
for (int i = 2; i < n; i++) {
int next = a + b;
System.out.print(", " + next);
a = b;
b = next;
}
System.out.println();
}
}</code>
</pre>
</div>
<h5 id="fifteenth">15. Write a program to calculate the sum and average of 10 numbers in an array</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class SumAndAverage {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Enter 10 numbers:");
int sum = 0;
for (int i = 0; i < 10; i++) {
numbers[i] = sc.nextInt();
sum += numbers[i];
}
double average = sum / 10.0;
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
}
}</code>
</pre>
</div>
<h5 id="sixteenth">16. Write a program to sort 10 numbers in ascending order using bubble sort</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class BubbleSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Enter 10 numbers:");
for (int i = 0; i < 10; i++) {
numbers[i] = sc.nextInt();
}
for (int i = 0; i < numbers.length - 1; i++) {
for (int j = 0; j < numbers.length - 1 - i; j++) {
if (numbers[j] > numbers[j + 1]) {
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
System.out.println("Sorted numbers in ascending order:");
for (int num : numbers) {
System.out.print(num + " ");
}
System.out.println();
}
}</code>
</pre>
</div>
<h5 id="seventeenth">17. Write a program to search an element using linear search in a sorted array</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class LinearSearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = sc.nextInt();
int[] array = new int[n];
System.out.println("Enter " + n + " sorted elements:");
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
}
System.out.print("Enter the element to search: ");
int target = sc.nextInt();
boolean found = false;
for (int i = 0; i < n; i++) {
if (array[i] == target) {
System.out.println("Element found at index " + i);
found = true;
break;
}
}
if (!found) {
System.out.println("Element not found in the array.");
}
}
}</code>
</pre>
</div>
<h5 id="eighteenth">18. Write a program to perform matrix addition (2x2)</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class MatrixAddition {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] matrix1 = new int[2][2];
int[][] matrix2 = new int[2][2];
int[][] sum = new int[2][2];
System.out.println("Enter elements of first 2x2 matrix:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
matrix1[i][j] = sc.nextInt();
}
}
System.out.println("Enter elements of second 2x2 matrix:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
matrix2[i][j] = sc.nextInt();
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
System.out.println("Sum of the matrices:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
}</code>
</pre>
</div>
<h5 id="nineteenth">19. Write a program to perform matrix multiplication (2x2)</h5>
<div class="container">
<pre class="box">
<code class="java">import java.util.Scanner;
class MatrixMultiplication {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] matrix1 = new int[2][2];
int[][] matrix2 = new int[2][2];
int[][] product = new int[2][2];
System.out.println("Enter elements of first 2x2 matrix:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
matrix1[i][j] = sc.nextInt();
}
}
System.out.println("Enter elements of second 2x2 matrix:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
matrix2[i][j] = sc.nextInt();
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
product[i][j] = 0;
for (int k = 0; k < 2; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
System.out.println("Product of the matrices:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(product[i][j] + " ");
}
System.out.println();
}
}
}</code>
</pre>
</div>
<h5 id="twentieth">20. Write a program to demonstrate classes and objects</h5>
<div class="container">
<pre class="box">
<code class="java">class Student {
String name;
int age;
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
Student student = new Student();
student.name = "John";
student.age = 20;
student.displayInfo();
}
}</code>
</pre>
</div>
<h5 id="twentyfirst">21. Write a program to demonstrate function overloading</h5>
<div class="container">
<pre class="box">
<code class="java">class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Sum (int): " + calc.add(10, 20));
System.out.println("Sum (double): " + calc.add(5.5, 4.5));
}
}</code>
</pre>
</div>
<h5 id="twentysecond">22. Write a program to demonstrate constructor overloading</h5>
<div class="container">
<pre class="box">
<code class="java">class Rectangle {
int length, breadth;
Rectangle() {
length = 0;
breadth = 0;
}
Rectangle(int l, int b) {
length = l;
breadth = b;
}
int area() {
return length * breadth;
}
public static void main(String[] args) {
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle(5, 10);
System.out.println("Default area: " + rect1.area());
System.out.println("Parameterized area: " + rect2.area());
}
}</code>
</pre>
</div>
<h5 id="twentythird">23. Write a program to demonstrate inheritance</h5>
<div class="container">
<pre class="box">
<code class="java">class Animal {
void sound() {
System.out.println("Animals make sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dogs bark");
}
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}</code>
</pre>
</div>
<h5 id="twentyfourth">24. Write a program to demonstrate different try-catch scenarios.</h5>
<div class="container">
<h5 class="mt-3">a. Single try with single catch</h5>
<pre class="box">
<code class="java">public class ExceptionDemo {
public static void main(String[] args) {
try {
int result = 10 / 0; // This will throw an ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Error: " + e);
}
}
}
</code>
</pre>
<h5>b. Single try with multiple catches java</h5>
<pre class="box">
<code class="java">public class ExceptionDemo {
public static void main(String[] args) {
try {
int[] arr = new int[2];
arr[3] = 10; // This will throw an ArrayIndexOutOfBoundsException
int result = 10 / 0; // This will throw an ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Arithmetic Error: " + e);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index Error: " + e);
}
}
}
</code>
</pre>
<h5>c. Multiple try blocks with a single catch</h5>
<pre class="box">
<code class="java">public class ExceptionDemo {
public static void main(String[] args) {
try {
int result = 10 / 0; // This will throw an ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Error: " + e);
}
try {
String str = null;
System.out.println(str.length()); // This will throw a NullPointerException
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
</code>
</pre>
<h5>d. Multiple try blocks with multiple catches</h5>
<pre class="box">
<code class="java">public class ExceptionDemo {
public static void main(String[] args) {
try {
int result = 10 / 0; // This will throw an ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Arithmetic Error: " + e);
}
try {
int[] arr = new int[2];
arr[3] = 10; // This will throw an ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index Error: " + e);
}
}
}
</code>
</pre>
</div>
<h5 id="twentyfifth">25. Write a program to demonstrate interfaces.</h5>
<div class="container">
<pre class="box">
<code class="java">// Interface
interface Animal {
void sound(); // Abstract method
void sleep(); // Abstract method
}
// Concrete class implementing the interface
class Dog implements Animal {
@Override
public void sound() {
System.out.println("Woof Woof");
}
@Override
public void sleep() {
System.out.println("Dog is sleeping.");
}
}
class Cat implements Animal {
@Override
public void sound() {
System.out.println("Meow");
}
@Override
public void sleep() {
System.out.println("Cat is sleeping.");
}
}
public class InterfaceDemo {
public static void main(String[] args) {
Animal dog = new Dog();
dog.sound();
dog.sleep();
Animal cat = new Cat();
cat.sound();
cat.sleep();
}
}
</code>
</pre>
</div>
<h5 id="twentysixth">26. Write a program to demonstrate packages.</h5>
<div class="container">
<pre class="box">
<code class="java">// In package animals
package animals;
public class Dog {
public void sound() {
System.out.println("Woof Woof");
}
}
// In package test
package test;
import animals.Dog;
public class PackageDemo {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
</code>
</pre>
</div>
<h5 id="twentyseventh">27. Write a program to demonstrate multithreading.</h5>
<div class="container">
<pre class="box">
<code class="java">// Extending Thread class
class MyThread extends Thread {
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getId() + " Value " + i);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class MultiThreadingDemo {
public static void main(String[] args) {
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
thread1.start(); // Start thread1
thread2.start(); // Start thread2
}
}
</code>
</pre>
</div>
</section>
<script
src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB"
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13"
crossorigin="anonymous"
></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/highlight.min.js"></script>
<script>
hljs.highlightAll();
</script>
</body>
</html>