-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA Assignment-3
More file actions
1185 lines (602 loc) · 18.5 KB
/
DSA Assignment-3
File metadata and controls
1185 lines (602 loc) · 18.5 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
Question 1: Write a Java program to read your lucky number from the keyboard. Treat –ve no. as NumberFormatException. Write appropriate Exceptional handler.
Solution:
import java.util.*;;
public class Q1 {
int x;
public void input(int x)
{
this.x=x;
if(x<0)
throw new NumberFormatException("You entered negative number ");
else
System.out.println("Your lucky Number: "+x);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=0;
System.out.print("Enter your lucky number: ");
n=sc.nextInt();
Q1 ob=new Q1();
try
{
ob.input(n);
}
catch(Exception e)
{
System.out.println("Error "+e);
}
}
}
Output:
Enter your lucky number: -5
Error java.lang.NumberFormatException: You entered negative number
Question 2:Assign your favorite colors in an array. Identify 2 exceptions that may be generated & write exceptional handler in Java.
Solution:
import java.util.*;
public class Q2 {
public void insert(String colour[])throws ArrayIndexOutOfBoundsException
{
colour[colour.length]="Black";
}
public void display(String colour[])
{
System.out.println("Selected colours are:");
for(int i=0;i<colour.length;i++)
{
System.out.print(colour[i]+", ");
}
}
public void StringToInt(String colour[])
{
throw new NumberFormatException("Cannot convert String to int ");
}
public static void main(String[] args) {
String colour[]={"Red","Blue","Yellow","Green"};
Q2 ob=new Q2();
ob.display(colour);
try
{
ob.insert(colour);
}
catch(Exception e)
{
System.out.println("\nError "+e);
}
try
{
ob.StringToInt(colour);
}
catch(Exception e)
{
System.out.println("\nError "+e);
}
}
}
Output:
Selected colours are:
Red, Blue, Yellow, Green,
Error java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
Error java.lang.NumberFormatException: Cannot convert String to int
Question 3: Create a class Student & enter mark, name of the student. If mark is more than 100, create exception MarksOutOfBoundException & throw it using Java.
import java.util.Scanner;
class Student{
int marks; String name;
public Student(int marks,String name)throws Exception
{
this.marks=marks;
this.name=name;
if(marks<0 || marks>100)
{
throw new MarksOutOfBoundsException("Invalid Marks");
}
else{this.marks=marks;}
}
public void display()
{
System.out.println("Your name: "+this.name+"\tYour marks: "+this.marks);
}
}
class MarksOutOfBoundsException extends Exception
{
public MarksOutOfBoundsException(String str)
{
super(str);
}
}
public class Q3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Your name: ");
String s=sc.next();
System.out.print("Enter marks: ");
int m=sc.nextInt();
try
{
Student S=new Student(m,s);
S.display();
}
catch(Exception e)
{
System.out.println("Error "+e);
}
}
}
Output:
Your name: Surajit
Enter marks: 110
Error MarksOutOfBoundsException: Invalid Marks
Question 4. Write a simple main class in Java that contains an experiment that uses the generic Box<T> class to build boxes with different types and that verifies that this class works as advertised. Your experiment should include the following:
Create a boxed String object and two variables that refer to that box. Change the contents of one and determine the effect on the other.
Create a boxed Integer object and two variables that refer to that box. Change the contents of one and determine the effect on the other.
Create a boxed Object object and two variables that refer to that box. Determine what happens if you put a string in the box. Determine what happens if you put an integer in the box.
class Box<T> {
public T content;
public Box(T content) {
this.content = content;
}
public T getContent() {
return content;
}
public void setContent(T content) {
this.content = content;
}
}
public class Q5 {
public static void main(String[] args) {
// Create a boxed String object
Box<String> stringBox = new Box<>("Hello, World!");
// Create two variables that refer to the same string box
Box<String> variable1 = stringBox;
Box<String> variable2 = stringBox;
// Change the contents of one variable
variable1.setContent("Modified content");
// Verify the effect on the other variable
System.out.println("Variable 2 content: " + variable2.getContent());
// Create a boxed Integer object
Box<Integer> intBox = new Box<>(42);
// Create two variables that refer to the same integer box
Box<Integer> intVariable1 = intBox;
Box<Integer> intVariable2 = intBox;
// Change the contents of one variable
intVariable1.setContent(99);
// Verify the effect on the other variable
System.out.println("Int variable 2 content: " + intVariable2.getContent());
// Create a boxed Object object
Box<Object> objectBox = new Box<>(null);
// Put a string in the object box
objectBox.setContent("This is a string");
// Verify the content
System.out.println("Object box content (String): " + objectBox.getContent());
// Put an integer in the object box
objectBox.setContent(123);
// Verify the content
System.out.println("Object box content (Integer): " + objectBox.getContent());
}
}
Output:
Variable 2 content: Modified content
Int variable 2 content: 99
Object box content (String): This is a string
Object box content (Integer): 123
Question 5: Write a java program to print an array of different type using a single Generic method. The signature of printArray method is given below.
public static < E > void printArray( E[] inputArray)
public class Q5 {
public static < E > void printArray( E[] inputArray)
{
for(int i=0;i<inputArray.length ;i++) {
System.out.print(inputArray[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
Integer arr[]= {1,2,3,4,5};
printArray(arr);
String s[]= {"A","B","C","D","E"};
printArray(s);
}
}
Output:
1 2 3 4 5
A B C D E
Question 6:Write a Java method using Generics to count the occurrence of an element in an array of any type. The signature of count method is given below.
public static int count(T[] array, T item)
Solution:
public class Q6 {
public static void main(String[] args) {
Integer arr[]= {1,2,3,4,1,1,1};
Integer i=1;
System.out.println("Occurrence of "+i+" is "+count(arr,i)+" times");
}
public static <T>int count(T[] array, T item)
{
int c=0;
System.out.print("[");
for(int i=0;i<array.length;i++)
{
if(array[i]==item)
{
c++;
}
System.out.print(array[i]+" ");
}
System.out.println("]");
return c;
}
}
Output:
[1 2 3 4 1 1 1 ]
Occurrence of 1 is 4 times.
Question 7.Write a recursive method in Java that computes the factorial of a given integer.
import java.util.*;
public class Q7 {
public static int fact(int n)
{
if(n==1 || n==0)
return 1;
else
return n*fact(n-1);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a no: ");
int n=sc.nextInt();
System.out.println("Factorial= "+fact(n));
}
}
Output:
Enter a no: 5
Factorial= 120
Question 8.Write a recursive method in Java which, given real value x and a positive integer n, returns the value of x^n
import java.util.*;
public class Q8 {
public static int pow(int base ,int power)
{
if(base==0)
return 0;
else if(power==0)
return 1;
else
return base*pow(base,power-1);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the base: ");
int base=sc.nextInt();
System.out.println("Enter the power: ");
int power=sc.nextInt();
System.out.println(base+" to the power of "+power+" is "+pow(base,power));
}
}
Output:
Enter the base:
2
Enter the power:
12
2 to the power of 12 is 4096
Question 9.Write a recursive method in Java which, given an integer n, print it with its digits reversed.
For example , given 4735, it prints 5374.
import java.util.*;
public class Q9 {
public static String reverse(int n)
{
if(n==0)
return "";
else
return n%10+reverse(n/10);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a no. ");
int n=sc.nextInt();
System.out.println("Reverse "+reverse(n));
}
}
Alternative Solution:
import java.util.*;
public class Q9 {
public static int reverse(int n,int sum)
{
if(n==0)
return sum;
else
{
sum=sum*10+n%10;
return reverse(n/10,sum);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a no. ");
int n=sc.nextInt();
System.out.println("Reverse "+reverse(n,0));
}
}
Output:
Enter a no.
2024
Reverse 4202
Question 10. The sequence of numbers 1, 1, 2, 3, 5, 8, 13 etc are called Fibonacci numbers,each is the sum of the preceding two. Write a recursive method in Java which, given n, returns the nth Fibonacci number.
import java.util.*;
public class Q10 {
public static int fibonacci(int n)
{
if(n==0 || n==1)
return n;
else
return fibonacci(n-1)+fibonacci(n-2);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter no. of terms ");
int n=sc.nextInt();
for(int i=0;i<n;i++) {
System.out.print(fibonacci(i)+" ");
}
}
}
ALTERNATE SOLUTION:
import java.util.*;
public class fibonacci {
public static void printfibonacci(int a,int b,int n) {// a,b,c...n [c=a+b](logic)
if(n==0 || n==1)
{
return ;
}
int c=a+b;
System.out.print(c+" ");
printfibonacci(b,c,n-1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of terms: ");
int n=sc.nextInt();
int a=0,b=1;
System.out.print(a+" "+b+" ");
printfibonacci(a,b,n-2); //We have already printed 2terms 0,1
// so we are writing n-2
}
}
Output:
Enter no. of terms
10
0 1 1 2 3 5 8 13 21 34
HOME ASSIGNMENT SOLUTIONS :
Question 1. Write a recursive method in Java to return the greatest common divisor(gcd) of two integers m and n, given that in general, gcd(m,n)=gcd(n, m mod n).
import java.util.*;
class HA_Q1{
public static int gcd(int m, int n)
{
if(n==0)
return m;
else
return gcd(n,m%n);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number: ");
int m = sc.nextInt();
System.out.println("Enter the second number: ");
int n = sc.nextInt();
System.out.print("GCD between "+m+" and "+n+" is "+gcd(m,n));
}
}
Output:
Enter the first number:
12
Enter the second number:
136
GCD between 12 and 136 is 4
Question 2. Write a recursive method in Java to search an element of an array using binary search.
import java.util.*;
class HA_Q2
{
public static void BinarySearch(int arr[], int item, int start, int end)
{
int mid = (start+end)/2;
if(start>end)
System.out.println("Search element not found");
else if(arr[mid] == item)
System.out.println("Search element found");
else if(arr[mid]>item)
BinarySearch(arr, item,start, mid-1);
else
BinarySearch(arr, item, mid+1,end);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter no. of. elements you want to enter in the array ");
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter "+(i+1)+" element: \n");
arr[i]=sc.nextInt();
}
System.out.println("Enter item you want to search: ");
int item=sc.nextInt();
BinarySearch(arr, item, 0,arr.length-1);
}
}
Output:
Enter no. of. elements you want to enter in the array
5
Enter 1 element:
100
Enter 2 element:
150
Enter 3 element:
200
Enter 4 element:
250
Enter 5 element:
300
Enter item you want to search:
200
Search element found
Question 3. Write a recursive method in Java to find the binary equivalent of a positive decimal integer.
import java.util.*;
public class HA_Q3 {
public static int findBinary(int d) {
if (d == 0)
return 0;
else
return (d % 2) + 10 * findBinary(d/2);
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter a positive decimal number: ");
int n= sc.nextInt();
sc.close();
System.out.println("Binary equivalent: " + findBinary(n));
}
}
Output:
Enter a positive decimal number: 13
Binary equivalent: 1101
Question 4.Write a recursive method in Java to find the product of 2 numbers.
import java.util.*;
public class HAQ4 {
public static int product(int m,int n)
{
if (m < n)
return product(n, m); //Swap the numbers if m < n
else if (n != 0)
return m + product(m, n - 1); // Recursively calculate n times sum of m
else
return 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number: ");
int m = sc.nextInt();
System.out.println("Enter the second number: ");
int n = sc.nextInt();
System.out.print("Product of "+m+"x"+n+" is :"+product(m,n));
}
}
Output:
Enter the first number:
11
Enter the second number:
9
Product of 11x9 is :99
Question 5. Write a recursive Java method that takes a character string s and outputs its reverse. For example, the reverse of 'pots&pans' would be 'snap&stop'.
import java.util.*;
public class HAQ5 {
public static String reverse(String S, int index)
{
if(index<S.length())
return reverse(S, index+1)+S.charAt(index);
else
return " ";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");