-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerated.html
991 lines (876 loc) · 27.1 KB
/
generated.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
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
<link rel="stylesheet" href="style.css"><h1>Write a program to print hello world.</h1><h3>HelloWorld.java</h3><div class='code'><code>public class HelloWorld{
public static void main(String[] args){
System.out.println(" By Madhurendra");
System.out.print("Hello World!");
}
}
</code></div><img src="HelloWorld.png"/><h1>Write a program to print average of an array</h1><h3>Average.java</h3><div class='code'><code>public class Average{
public static void main(String[] args){
System.out.println(" By Madhurendra");
int a[] = {1,2,4,12,456};
int sum=0;
for(int i=0;i<a.length;++i)
sum+=a[i];
int avg = sum/a.length;
System.out.println("Average of "+a.length+" elements is "+avg);
}
}
</code></div><img src="Average.png"/><h1>Write a program to print a pattern</h1><h3>PatternNumber.java</h3><div class='code'><code>public class PatternNumber{
public static void main(String[] args){
System.out.println(" By Madhurendra");
int num = 5;
for(int i=0;i<num;++i){
for(int j=0;j<i;++j)
System.out.print(j+" ");
System.out.print("\n");
}
}
}
</code></div><img src="PatternNumber.png"/><h1>Write a program to print a pattern</h1><h3>PatternStar.java</h3><div class='code'><code>public class PatternStar{
public static void main(String[] args){
System.out.println(" By Madhurendra");
int num = 5;
for(int i=0;i<num;++i){
for(int j=0;j<i;++j)
System.out.print("*");
System.out.print("\n");
}
}
}
</code></div><img src="PatternStar.png"/><h1>Write a program to print a pattern</h1><h3>PatternSweet.java</h3><div class='code'><code>public class PatternSweet{
public static void main(String[] args){
System.out.println(" By Madhurendra");
int c = 6;
for(int i=0;i<c;++i){
for(int j=0;j<(c-i);++j)
System.out.print(" ");
for(int j=0;j<=i;++j)
System.out.print("* ");
System.out.println("");
}
for(int i=0;i<c;++i){
for(int j=0;j<=i;++j)
System.out.print(" ");
for(int j=0;j<(c-i);++j)
System.out.print("* ");
System.out.println("");
}
}
}
</code></div><img src="PatternSweet.png"/><h1>Write a program to find areas.</h1><h3>Area.java</h3><div class='code'><code>import java.util.*;
class Area
{
public static void main(String args[])
{
System.out.println(" By Madhurendra");
Scanner sc=new Scanner(System.in);
int choice;
System.out.println("1:- Area of circle \n2:- Area of Rectangle \n3:- Area of Triangle ");
System.out.println("Enter your choice ");
System.out.println("Calculate area ");
choice=sc.nextInt();
switch(choice)
{
case 1:
int r;
double area1;
System.out.println("Enter radius for Circle :: ");
r=sc.nextInt();
area1=3.14*r*r;
System.out.println("Area of Circle is :: "+area1);
break;
case 2:
int l,b;
double area2;
System.out.println("Enter Length :: ");
l=sc.nextInt();
System.out.println("Enter Breadth :: ");
b=sc.nextInt();
area2=l*b;
System.out.println("Area of rectangle is :: "+area2);
break;
case 3:
int base,h;
double area3;
System.out.println("Enter Base :: ");
base=sc.nextInt();
System.out.println("Enter Height :: ");
h=sc.nextInt();
area3=0.5*base*h;
System.out.println("Area of Triangle is :: "+area3);
break;
default:
System.out.println("Invalid input");
}
}
}
</code></div><img src="Area.png"/><h1>Write a program to find armstrong.</h1><h3>Armstrong.java</h3><div class='code'><code>import java.util.Scanner;
class Armstrong
{
public static void main(String[]args)
{
System.out.println(" By Madhurendra");
int c=0,a,temp,n;
System.out.println("Enter the number");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
{
System.out.println("The number is an Armstrong number");
}
else
{
System.out.println("Not an Armstrong number");
}
}
}
</code></div><img src="Armstrong.png"/><h1>Write a program to demonstrate array.</h1><h3>ArrayDemo.java</h3><div class='code'><code>import java.util.*;
public class ArrayDemo{
public static void main(String []args){
System.out.println(" By Madhurendra");
int [] arr=new int[5];
System.out.println("Enter the value of array: ");
Scanner sc=new Scanner(System.in);
for(int i=0;i<5;i++){
arr[i]=sc.nextInt();
}
System.out.println("You have entered these values: ");
for(int i=0;i<5;i++){
System.out.println(" "+arr[i]);
}
}
}
</code></div><img src="ArrayDemo.png"/><h1>Write a program to print factorial of a number</h1><h3>Factorial.java</h3><div class='code'><code>import java.util.Scanner;
public class Factorial{
public static void main(String[] args){
System.out.println(" By Madhurendra");
Scanner s = new Scanner(System.in);
System.out.println("Enter a number : ");
int num = s.nextInt();
int fact=1;
for(int i=2;i<=num;++i)
fact*=i;
System.out.println("Factorial of "+num+" is "+fact);
}
}
</code></div><img src="Factorial.png"/><h1>Write a program to sort using bubble sort</h1><h3>BubbleSort.java</h3><div class='code'><code>public class BubbleSort{
public static void main(String[] args){
System.out.println(" By Madhurendra");
int a[] = {1,2,4,2,3,5,5,6,8};
for(int i=0;i<a.length;++i){
for(int j=1;j<a.length;++j){
if(a[j]<a[j-1]){
int tmp = a[j-1];
a[j-1]= a[j];
a[j] = tmp;
}
}
}
System.out.println("Sorted array is : ");
for(int i=0;i<a.length;++i)
System.out.print(a[i]+" ");
}
}
</code></div><img src="BubbleSort.png"/><h1>Write a program to add two matrix</h1><h3>MatrixAdd.java</h3><div class='code'><code>public class MatrixAdd{
public static void main(String[] args){
System.out.println(" By Madhurendra");
int a[][] = {{1,2,4},{2,3,5},{5,6,8}};
int b[][] = {{1,3,4},{2,3,5},{15,6,8}};
int sum[][] = new int[a.length][a[0].length];
for(int i=0;i<a.length;++i){
for(int j=0;j<a[i].length;++j)
sum[i][j]= a[i][j]+b[i][j];
}
for(int i=0;i<a.length;++i){
for(int j=0;j<a[i].length;++j)
System.out.print(sum[i][j]+" ");
System.out.println("");
}
}
}
</code></div><img src="MatrixAdd.png"/><h1>Write a program to check if number is palindrome</h1><h3>Palindrome.java</h3><div class='code'><code>import java.util.Scanner;
public class Palindrome{
public static void main(String[] args){
System.out.println(" By Madhurendra");
Scanner s = new Scanner(System.in);
System.out.println("Enter a number : ");
int num = s.nextInt();
int temp = num,rev=0;
while(temp>0){
rev=rev*10+temp%10;
temp /=10;
}
if(num==rev)
System.out.println("it's a palindrome.");
else
System.out.println("it's not a palindrome.");
}
}
</code></div><img src="Palindrome.png"/><h1>Write a program to show usage of final</h1><h3>FinalDemo.java</h3><div class='code'><code>public class FinalDemo{
public static void main(String[] args){
System.out.println(" By Madhurendra");
FinalUsage f = new FinalUsage();
FinalUsageAnother fa = new FinalUsageAnother();
System.out.println("FinalUsage Value : "+f.UPPER_CHAR);
System.out.println("FinalUsageAnother Value : "+fa.UPPER_CHAR);
System.out.println("\nNormal method call.");
f.test();
System.out.println("Method call which uses super.");
fa.anotherTest();
}
}
class FinalUsage{
//this can be overriden, but can't be modified.
final int UPPER_CHAR = 'A';
//this can't be overriden.
final void test(){
System.out.println("FinalUsage.test method.");
}
//called by GC
protected void finalize() throws Throwable{
System.out.println("Finalize called.");
}
}
class FinalUsageAnother extends FinalUsage{
final int UPPER_CHAR= 'B';
void anotherTest(){
//a test for super.
super.test();
System.out.println("FinalUsage.anotherTest method.");
}
}</code></div><img src="FinalDemo.png"/><h1>Write a program to show final method behaviour.</h1><h3>FinalError.java</h3><div class='code'><code>public class FinalError{
public static void main(String[] args){
System.out.println(" By Madhurendra");
//not compiled
}
}
class FinalUsage{
final void test(){
System.out.println("FinalUsage.test method.");
}
}
class FinalUsageAnother extends FinalUsage{
void test(){
System.out.println("FinalUsage.anotherTest method.");
}
}</code></div><img src="FinalError.png"/><h1>Write a program to demostrate inheritance</h1><h3>Inheritance.java</h3><div class='code'><code>public class Inheritance{
public static void main(String[] args){
System.out.println(" By Madhurendra");
Man m = new Man();
Tiger t = new Tiger();
Child c = new Child();
System.out.println("Man.isDangerous "+(m.isDangerous()));
System.out.println("Tiger.isDangerous "+(t.isDangerous()));
System.out.println("Child.isDangerous "+(c.isDangerous()));
c.walk = true;
System.out.println("Child.canWalk "+(c.canWalk()));
}
}
class Mammal{
final boolean dangerous=false;
int AGE = 1;
boolean isDangerous(){
return dangerous;
}
}
class Human extends Mammal{
final boolean dangerous = false;
}
class Man extends Human{
Man(){
AGE = 18;
}
boolean earn = false;
boolean canEarn(){
return earn;
}
}
class Child extends Human{
boolean walk = false;
boolean canWalk(){
return walk;
}
}
class Tiger extends Mammal{
final boolean dangerous = true;
}</code></div><img src="Inheritance.png"/><h1>Write a program to show implementation of stack.</h1><h3>StackDemo.java</h3><div class='code'><code>public class StackDemo{
public static void main(String[] args){
System.out.println(" By Madhurendra");
Stack s = new Stack();
s.push(10);
s.push(1);
s.push(2);
System.out.println("Content of stack:");
s.printStack();
s.pop();
s.pop();
System.out.println("\nContent of stack:");
s.printStack();
}
}
class Stack{
int store[] = new int[20];
int top =-1;
boolean push(int num){
if(store.length-1==top){
System.out.println("Stack overflow.");
return false;
}
store[++top]=num;
return true;
}
int pop(){
if(top==-1){
System.out.println("Stack underflow");
return -1;
}
return store[top--];
}
void printStack(){
if(top==-1)
return;
for(int i=0;i<=top;++i)
System.out.print(store[i]+" ");
}
}</code></div><img src="StackDemo.png"/><h1>Write a program to demonstrate string functions.</h1><h3>StringFunction.java</h3><div class='code'><code>import java.*;
class StringFunction
{
public static void main(String args[])
{
System.out.println(" By Madhurendra");
String s1=" SHEMARK 1234567890";
String s2=" shemark 1234567890";
System.out.println("Replace s1() :: "+s1.replace('A','S'));
System.out.println("Replace s2 :: "+s2.replace('a','s'));
System.out.println("To Upper Case s2() :: "+s2.toUpperCase());
System.out.println("To Lower Case s1() :: "+s1.toLowerCase());
System.out.println("Sub String1 () :: "+s1.substring(3,10));
System.out.println("Sub String2 () :: "+s2.substring(3,10));
System.out.println("ReplaceCharSequence1 () :: "+s1.replace("SH","DE"));
System.out.println("ReplaceCharSequence2 () :: "+s2.replace("sh","de"));
System.out.println("CharAt for s1 () :: "+s1.charAt(4));
System.out.println("CharAt for s2 () :: "+s2.charAt(5));
System.out.println(s1 + " <<Equals>> " + s2 + s1.equals(s2));
System.out.println("Without Trim s1 () :: "+s1);
System.out.println("With Trim s1 () :: "+s1.trim());
System.out.println("Without Trim s2 () :: "+s2);
System.out.println("With Trim s2 () :: "+s2.trim());
}
}
</code></div><img src="StringFunction.png"/><h1>Write a program to show threads usage.</h1><h3>thread.java</h3><div class='code'><code>//Program for Mutithread
class NewThread implements Runnable{
String name;
Thread t;
boolean suspendFlag;
NewThread(String t_name){
name=t_name;
t=new Thread(this,name);
System.out.println("New Thread "+t+" has begun");
suspendFlag=false;
t.start();
}
public void run(){
try{
for(int i=5;i>0;i--){
System.out.println(name+" : "+i);
Thread.sleep(1000);
synchronized(this){
while(suspendFlag){
wait();
}
}
}
}catch(InterruptedException e){
System.out.println("Thread Interrupted "+e);
}
System.out.println("Thread "+name+" has exited.");
}
void threadSuspend(){
suspendFlag=true;
}
synchronized void threadResume(){
suspendFlag=false;
notify();
}
}
class thread{
public static void main(String args[]){
System.out.println(" By Madhurendra");
NewThread One=new NewThread("ThreadOne");
NewThread Two=new NewThread("ThreadTwo");
try{
Thread.sleep(1000);
One.threadSuspend();
System.out.println("Thread Two Suspended");
Thread.sleep(1000);
One.threadResume();
System.out.println("Thread Two Resumed");
Thread.sleep(1000);
Two.threadSuspend();
System.out.println("Thread One Suspended");
Thread.sleep(1000);
Two.threadResume();
System.out.println("Thread Two Resumed");
}catch(InterruptedException e){
System.out.println("Thread Interrupted "+e);
}
try{
System.out.println("Waiting for thread for finish");
One.t.join();
Two.t.join();
}catch(InterruptedException e){
System.out.println("Thread Interrupted "+e);
}
}
}
</code></div><img src="thread.png"/><h1>Write a program to demonstrate multithreading</h1><h3>MultiThread.java</h3><div class='code'><code>class A extends Thread{
public void run(){
int i;
for(i=1;i<=10;i++){
System.out.println("i="+i+" Thred A");
}
}
}
class B extends Thread{
public void run(){
int i;
for(i=1;i<=10;i++){
System.out.println("i="+i+" Thread B");
}
}
}
public class MultiThread{
public static void main(String []args){
System.out.println(" By Madhurendra");
A o1=new A();
B o2=new B();
o1.start();
o2.start();
}
}
</code></div><img src="MultiThread.png"/><h1>Write a program to find greatest number.</h1><h3>Greatest.java</h3><div class='code'><code>import java.util.*;
class Greatest
{
public static void main(String args[])
{
System.out.println(" By Madhurendra");
Scanner sc=new Scanner(System.in);
int num1,num2,num3;
System.out.println("Enter the number");
num1=sc.nextInt();
num2=sc.nextInt();
num3=sc.nextInt();
if(num1>num2 && num1>num3)
{
System.out.println("Greater number is :: "+num1);
}
else if(num2>num1 && num2>num3)
{
System.out.println("Greater number is :: "+num2);
}
else
{
System.out.println("Greater number is :: "+num3);
}
}
}
</code></div><img src="Greatest.png"/><h1>Write a program to use switch case.</h1><h3>Cal.java</h3><div class='code'><code>import java.util.*;
class Cal
{
public static void main(String args[])
{
System.out.println(" By Madhurendra");
Scanner sc=new Scanner(System.in);
int choice,num1,num2;
System.out.println("Enter your choice : ");
System.out.println("1:- Addition\n2:- Substraction\n3:- Multiplication\n4:- Division:- ");
choice=sc.nextInt();
switch(choice)
{
case 1:
int add;
System.out.println("Enter your number :: ");
num1=sc.nextInt();
num2=sc.nextInt();
add=num1+num2;
System.out.println("Addition is :: "+add);
break;
case 2:
int sub;
System.out.println("Enter your number :: ");
num1=sc.nextInt();
num2=sc.nextInt();
sub=num1-num2;
System.out.println("Subtraction is :: "+sub);
break;
case 3:
int mul;
System.out.println("Enter your number :: ");
num1=sc.nextInt();
num2=sc.nextInt();
mul=num1*num2;
System.out.println("Multiplication is :: "+mul);
break;
case 4:
int div;
System.out.println("Enter your number :: ");
num1=sc.nextInt();
num2=sc.nextInt();
div=num1/num2;
System.out.println("Division is :: "+div);
break;
default:
System.out.println("Invalid input");
}
}
}
</code></div><img src="Cal.png"/><h1>Write a program to show File Handling.</h1><h3>FileHandling.java</h3><div class='code'><code>import java.io.*;
class FileHandling{
public static void main(String args[]){
System.out.println(" By Madhurendra");
char[] in=new char[30];
int size=0;
try{
File direx=new File("Direx.txt");
direx.mkdir();
if(direx.exists()){
System.out.println("The directory already exists");
File file=new File(direx,"Human.txt");
file.createNewFile();
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
bw.write("Test content.");
bw.flush();
bw.close();
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
size=br.read(in);
System.out.println(size);
for(char c:in){
System.out.print(c);
}
}else{
System.out.println("Sorry this directory does not exist");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
</code></div><img src="FileHandling.png"/><h1>Write a program to implement stack.</h1><h3>Stack.java</h3><div class='code'><code>import java.util.*;
class Stack
{
int top;
int size=10;
int a[]=new int[100];
void Push()
{
if(top==size-1)
{
System.out.println("Stack Overflow");
}
else
{
int element;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the elements to be inserted");
element=sc.nextInt();
top=top+1;
a[top]=element;
}
}
int Pop()
{
if(top==-1)
{
System.out.println("Stack underflow");
return 0;
}
else
{
top=top-1;
}
return top;
}
void Display()
{
for(int i=1;i<=top;i++)
{
System.out.println("Array elements="+a[i]);
}
}
public static void main(String args[])
{
System.out.println(" By Madhurendra");
int choice;
int ch,n;
Stack stack=new Stack();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of Stack ");
n=sc.nextInt();
do
{
System.out.println("Enter your choice");
System.out.println(" 1:Push\n 2:Pop\n 3:Display");
choice=sc.nextInt();
switch(choice)
{
case 1:
stack.Push();
break;
case 2:
stack.Pop();
break;
case 3:
stack.Display();
break;
default:
System.out.println("Invalid Input");
}
System.out.println("Do you want to continue");
ch=sc.nextInt();
}
while(ch==1);
}
}
</code></div><img src="Stack.png"/><h1>Write a program to show Constructor Overloading</h1><h3>ConstructorOverloading.java</h3><div class='code'><code>public class ConstructorOverloading{
public static void main(String args[]){
System.out.println(" By Madhurendra");
System.out.println("\nWith 1 int param");
(new Server(80)).print();
System.out.println("\nWith 1 String param.");
(new Server("c:\\")).print();
}
}
class Server {
int port;
String path;
public Server(){
}
public Server(int port){
this.port = port;
}
public Server(String path){
this.path = path;
}
public Server(String path, int port){
this.port = port;
this.path = path;
}
public Server(ServerConfig serverConfig){
this.port = serverConfig.port;
this.path = serverConfig.path;
}
public void print(){
System.out.println("Server's Port is "+port);
System.out.println("Server's Path is "+path);
}
}
class ServerConfig{
//default constructor is being called.
public int port; //set to 0
public String path; //set to null
public void print(){
System.out.println("Port is "+port);
System.out.println("Path is "+path);
}
}
</code></div><img src="ConstructorOverloading.png"/><h1>Write a program to differentiate between constructor & method.</h1><h3>ConstructorVsMethod.java</h3><div class='code'><code>public class ConstructorVsMethod{
public static void main(String args[]){
System.out.println(" By Madhurendra");
System.out.println("Constructor vs Method\n");
ServerHandler s= new ServerHandler();
System.out.println("\nCalling method print().");
s.print();
}
}
class ServerHandler{
ServerHandler(){
System.out.println("This is contructor called automatically.");
}
void print(){
System.out.println("This is explicitly called by program.");
}
}
</code></div><img src="ConstructorVsMethod.png"/><h1>Write a program to show default contructor.</h1><h3>DefaultConstructor.java</h3><div class='code'><code>public class DefaultConstructor{
public static void main(String args[]){
System.out.println(" By Madhurendra");
System.out.println("Default constructor");
ServerConfig sc = new ServerConfig();
sc.print();
sc.port = 80;
sc.path="c:\\sec\\";
System.out.println("After manually updating value.");
sc.print();
}
}
</code></div><h3>ServerConfig.java</h3><div class='code'><code>
public class ServerConfig{
//default constructor is being called.
public int port; //set to 0
public String path; //set to null
public void print(){
System.out.println("Port is "+port);
System.out.println("Path is "+path);
}
}
</code></div><img src="DefaultConstructor.png"/><h1>Write a program to show parameterized contructor.</h1><h3>ServerConfig.java</h3><div class='code'><code>
public class ServerConfig{
//default constructor is being called.
public int port; //set to 0
public String path; //set to null
public void print(){
System.out.println("Port is "+port);
System.out.println("Path is "+path);
}
}
</code></div><h3>Server.java</h3><div class='code'><code>public class Server {
int port;
String path;
public Server(){
}
public Server(int port){
this.port = port;
}
public Server(String path){
this.path = path;
}
public Server(String path, int port){
this.port = port;
this.path = path;
}
public Server(ServerConfig serverConfig){
this.port = serverConfig.port;
this.path = serverConfig.path;
}
public void print(){
System.out.println("Server's Port is "+port);
System.out.println("Server's Path is "+path);
}
}
</code></div><h3>ParametarizedConstructor.java</h3><div class='code'><code>public class ParametarizedConstructor{
public static void main(String args[]){
System.out.println(" By Madhurendra");
System.out.println("Non parametarized");
(new Server()).print();;
System.out.println("\nParametarized with 1 param.");
(new Server(80)).print();
System.out.println("\nParametarized with 2 param.");
(new Server("c:\\",80)).print();
}
}
</code></div><img src="ParametarizedConstructor.png"/><h1>Write a program to implement insertion sort.</h1><h3>Insertion.java</h3><div class='code'><code>import java.util.*;
class Insertion
{
public static void main(String args[])
{
System.out.println(" By Madhurendra");
int a[]=new int[50];
int n,i,k,j,temp,smallest;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of elements::");
n=sc.nextInt();
System.out.println("Enter the elements::");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j]) && (j>0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
System.out.print("After passes "+i+" :: ");
for(k=0;k<n;k++)
{
System.out.print(a[k]+"\t");
}
System.out.println();
}
System.out.print("Sorted Array :: ");
for(i=0;i<n;i++)
{
System.out.print(a[i]+"\t");
}
}
}
</code></div><img src="Insertion.png"/><h1>Write a program to use FileHandling.</h1><h3>FileHandling.java</h3><div class='code'><code>import java.io.*;
class FileHandling{
public static void main(String args[]){
System.out.println(" By Madhurendra");
char[] in=new char[30];
int size=0;
try{
File direx=new File("Direx.txt");
direx.mkdir();
if(direx.exists()){
System.out.println("The directory already exists");
File file=new File(direx,"Human.txt");
file.createNewFile();
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
bw.write("Test content.");
bw.flush();
bw.close();
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
size=br.read(in);
System.out.println(size);
for(char c:in){
System.out.print(c);
}
}else{
System.out.println("Sorry this directory does not exist");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
</code></div><img src="FileHandling.png"/><h1>Write a program to show method overloading.</h1><h3>Overloading.java</h3><div class='code'><code>class Over
{
int rollno;
String name;
void M1()
{
System.out.println("No arguments.");
this.M1(10,"Student");
}
void M1(int rollno, String name)
{
this.rollno=rollno;
this.name=name;
}
void M1(String name)
{
System.out.println("Name is::"+name);
}
void show()
{
System.out.println("Roll no::"+rollno);
System.out.println("Name is::"+name);
}
}
class Overloading
{
public static void main(String args[])
{
System.out.println(" By Madhurendra");
Over obj=new Over();
obj.M1();
obj.show();
}
}
</code></div><img src="Overloading.png"/>