· 4 years ago · Jan 22, 2021, 06:12 AM
1
2
3DEPARTMENT OF COMPUTER SCIENCE
4RAJAGIRI COLLEGE OF SOCIAL SCIENCES
5(Autonomous)
6KALAMASSERY - KOCHI - 683104
7
8
9
10
11
12
13
14
15MASTER OF COMPUTER APPLICATIONS
16
17
18
19
20
21JAVA LAB RECORD
22
23
24
25
26
27
28
29
30
31
32
33
34
35NAME : _MATHEW REJI____
36
37SEMESTER : _THIRD SEMESTER_
38
39
40
41REGISTER NO. : _1924025____________
42
43
44
45
46
47
48
49
50DEPARTMENT OF COMPUTER SCIENCE
51RAJAGIRI COLLEGE OF SOCIAL SCIENCES
52 (Autonomous)
53KALAMASSERY - KOCHI – 683104
54
55
56
57MASTER OF COMPUTER APPLICATIONS
58
59
60
61CERTIFICATE
62
63NAME : MATHEW REJI
64
65SEMESTER : THIRD
66
67
68REGISTER NO. : 1924025
69
70
71Certified that this is a bonafide record of work done by the student in the
72Software Laboratory of Department of Computer Science, Rajagiri College of
73Social Sciences, Kalamassery.
74
75
76
77 Dr. Ann Baby Dr. Bindiya M. Varghese
78 Dean, Department of Computer Science
79
80 External Examiner
81
82
83Faculty in Charge
84
85
86
87
88Internal Examiner
89
90Place : Kalamassery
91Date :
92
93
94
95
96
97
98
99
100
101JAVA LAB
102
103
104
105
106
1071
108
109
110
111Table of Contents
112Program to print ‘Welcome to Java’ ................................................................................... 5
113Program to display two numbers received as command line argument, and print its
114product. ............................................................................................................................... 6
115Read two numbers and display the output in the form of ‘Sum of 2 and 3 is 5’. ............... 7
116Program to accept two numbers from keyboard and swap them. ....................................... 9
117Program to read three numbers and find maximum. ........................................................ 11
118Program to read 3 numbers from keyboard. Find the minimum using a single
119statement. .......................................................................................................................... 12
120Program to search for a given element in an array. .......................................................... 13
121Program to sort elements in an array in ascending order.................................................. 15
122Program to print the row wise and column wise sum of 2D array. .................................. 17
123Program with 2 functions to check for an integer palindrome. (Function1 should
124reverse the integer. Function2 should return 1, if it is a palindrome or else 0). ............... 19
125Program to display numbers from m to n using single while loop. .................................. 21
126Program to find the sum of 1+(1+2)+(1+2+3)+…………..+(1+2+3+……..+n)
127using a single while loop. ................................................................................................. 22
128Program to find the sum of 1+2/2!+3/3!+4/4!+.....+n/n! Using single for loop. .............. 23
129Program to calculate area of a circle (functions with no argument and no return
130type). ................................................................................................................................. 24
131Program to reverse a number (functions with argument and no return type). .................. 25
132Program to calculate sum of digits of a number (functions with argument and return
133type). ................................................................................................................................. 27
134Program to calculate sum of n even numbers (functions with no argument and
135return type). ...................................................................................................................... 29
136Program with nested functions to find the maximum of three numbers. Function1
137should take in two arguments and find the maximum. Function2 should take in the
138third number and the maximum from function1 to find the maximum.) ......................... 31
139Program to find the factorial of n, using recursion. .......................................................... 33
140Program to display from n to 1 and vice versa, using recursion ....................................... 35
141Create a class complex having real and imaginary as data members and provide
142function for read, display, add and multiplying two complex numbers. .......................... 37
143Program to display numbers using a static function. ........................................................ 39
144Program (menu driven) to demonstrate function overriding in java, by displaying
145details of a student, and a teacher. .................................................................................... 40
146
147
148JAVA LAB
149
150
151
152
153
1542
155
156
157
158Create a class for employee having eno, ename and esal as data members. Provide
159function for reading and displaying employee details. (Accept information of n
160employees in main function, display the same and search for an emp (using eno)). ....... 44
161Program to implement run time polymorphism in java using interface, write
162calculating area of a triangle. ............................................................................................ 47
163Create an interface shape having two prototypes disp() and calc(), to display the
164shape and calculate area respectively. Create two classes circle and rectangle which
165implements the above interface. In the main function create a reference of Shape
166depending on the user-choice. .......................................................................................... 49
167Program to implement a function using call by value to swap two float numbers. .......... 52
168Program to implement a function using call by reference to find the square root of a
169given number. ................................................................................................................... 54
170Program to create a class for Cstring having a string data member and provide
171functions for read, display, compare (return Boolean value), add and concatenate. ........ 56
172Program to implement object cloning for the class Distance which has inch and feet
173as data members. .............................................................................................................. 59
174Program to create a menu driven program for performing the following operations. ...... 61
175Convert to uppercase and lowercase ................................................................................. 61
176Program to reverse a string. .............................................................................................. 64
177Program to calculate the prime factors of a given number, using packages. .................... 65
178Program to calculate the value of nPr for given value of n & r, using packages. ............ 67
179Program to read numbers into an array. Perform validations using multiple catch
180statements / predefined Exceptions. ................................................................................. 69
181Program to implement a user defined Exception, which will throw an Exception
182when a given number is prime. ......................................................................................... 71
183Program to implement throw and finally. ......................................................................... 73
184Program to create multiple threads by extending the Thread class. ................................. 74
185Program to implement Synchronization using inter-thread communication. ................... 77
186Program to Implement the Producer - Consumer Problem, using Threads. ..................... 79
187Program to display the contents of a directory by displaying the subdirectory’s
188name first, then the file names. ......................................................................................... 82
189Program to display the contents of a directory including its subdirectory content
190using recursive function. .................................................................................................. 84
191Program to search for a given file name in a directory (including its
192subdirectory) content. ....................................................................................................... 86
193Menu driven program to demonstrate Random Access File handling, with options
194for creating, deleting, writing, appending and reading the file. ....................................... 88
195
196
197JAVA LAB
198
199
200
201
202
2033
204
205
206
207Program to implement a Generic method, which can display the elements of
208various arrays of different data types, and find the length of each array. ........................ 92
209Program to implement a Generic class (with constructors), and display the types of
210various parameters passed. ............................................................................................... 94
211Program to compare the averages of various arrays, using bounded type
212parameters......................................................................................................................... 96
213Program to implement Serialization and De-Serialization, for an object of Student
214class. ................................................................................................................................. 98
215Program to implement IS-A Serialization and De-Serialization, for a Maruti Car
216inherited from Vehicle. ................................................................................................... 101
217Program to implement HAS-A Serialization and De-Serialization for the Engine of
218a Vehicle. ........................................................................................................................ 105
219Program to Serialize/De-Serialize selected attributes of an Employee. ......................... 109
220Program to implement various methods of a StringBuffer class. ................................... 112
221Program to implement communication between a client and server client, via
222Socket Programming. ..................................................................................................... 114
223Program to implement one-one chatting using the TCP protocol. ................................. 116
224Program to accept a file name given in the client, and to download the content from
225the server side. ................................................................................................................ 118
226Program to accept a number from the client, and print its factorial in the server. ......... 121
227Program to implement connection less communication. ................................................ 123
228Program to get protocol, file name, host, path and port of a given URL. ...................... 125
229Program to download a file from a given URL. ............................................................. 126
230Program to implement Two- way Communication using UDP Protocol. ...................... 128
231Program to create a table Citizen(Id (Primary), Name, age, address, DOB), insert
232records, and display the records. .................................................................................... 130
233Assume that login is a table which has Uname, Upass. Check whether a record with
234“Uname=”Bob” and “UPass=”Alice123#”is present in the table. ................................. 133
235Program to construct the following tables: .................................................................... 135
236Department (dno(Primary), dname, dloc) ....................................................................... 135
237Emp ( eno(Primary), ename, esal ,dno(Foreign)) ........................................................... 135
238Program for displaying information in the following order: .......................................... 136
239eno ename esal dname dloc .................................................... 136
240101 Rani 10,000 MCA Kochi ................................................. 136
241102 Vani 20,000 MSW Delhi ................................................. 136
242
243
244JAVA LAB
245
246
247
248
249
2504
251
252
253
254Program to implement database connectivity using object oriented concepts. .............. 138
255JDBC program with Parametrized queries to update a given record (Rani’s salary to
25615,000) in the Emp table. ............................................................................................... 143
257JDBC program with Parametrized queries to list the records of Emp table which
258has records whose names start with the alphabet “R”. ................................................... 145
259JDBC program with PreparedStatement to delete the records of Emp table which
260has records whose salary is less than 10,000. ................................................................. 146
261Implement a JDBC program which uses a Stored Procedure to insert records into
262Department table. ........................................................................................................... 147
263Program to use Callable statement to implement a Stored Procedure to display the
264Ename and Salary of all employees................................................................................ 149
265JDBC program to implement Choice Based Transaction Management in the
266Department table. ........................................................................................................... 151
267JDBC program to depict the usage of SQL Exception Class and SQL Warning
268Class. .............................................................................................................................. 153
269Using java AWT components, read text from a text box, and insert it into a list.
270Similarly, remove selected items from the list, and place it into another list. ................ 154
271Implement a numeric calculator using java AWT. ......................................................... 157
272Using AWT, create a menu for file and edit options. ............................................. 163
273Replicate a Notepad editor using AWT. ......................................................................... 166
274Design a login frame for a shopping site, using GUI - Database Connectivity.............. 169
275Using AWT, draw a house/ car....................................................................................... 172
276Using applets, create a colored image of your choice, and move it in a zig zag
277motion. ............................................................................................................................ 174
278With music in the background, use AWT components to find the sum and product
279of two numbers. .............................................................................................................. 176
280Using applets, draw a Christmas tree. ............................................................................ 178
281With parameter passing, check whether the sum of passed numbers is less than,
282greater than, or equal to zero. Based upon the result, display different images from
283the folder. ........................................................................................................................ 180
284MINI – PROJECT: Project Planner ................................................................................ 183
285
286
287
288
289
290
291JAVA LAB
292
293
294
295
296
2975
298
299
300
301Program #1
302Program to print ‘Welcome to Java’
303
304
305
306SOURCE CODE
307
308
309
310import java.io.*;
311
312public class welcome
313{
314 public static void main( String args[] )
315 {
316 System.out.println("\nMCA325\n");
317 System.out.println("---------Welcome to JAVA!!!---------");
318 }
319}
320
321
322
323OUTPUT
324
325
326
327
328
329
330
331
332JAVA LAB
333
334
335
336
337
3386
339
340
341
342Program #2
343Program to display two numbers received as command line argument, and print its
344product.
345
346
347
348SOURCE CODE
349
350
351
352public class cli_product
353{
354 public static void main(String[] args)
355 {
356 int count,x;
357 int i=0,prod=1;
358 count=args.length;
359 String text1;
360
361 System.out.println("\nMCA325\n");
362 System.out.print("Numbers : ");
363 while(i<count)
364 {
365 text1=args[i];
366 x=Integer.parseInt(text1);
367 prod=x*prod;
368 System.out.print(x+ " ");
369 i=i+1;
370 }
371 System.out.println("\nProduct : " +prod);
372 }
373}
374
375
376
377OUTPUT
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394JAVA LAB
395
396
397
398
399
4007
401
402
403
404Program #3
405Read two numbers and display the output in the form of ‘Sum of 2 and 3 is 5’.
406
407
408
409SOURCE CODE
410import java.io.*;
411public class NumSum
412{
413 public static void main(String args[])
414 {
415 float x,y,z;
416 String str;
417 try
418 {
419 DataInputStream dis = new DataInputStream(System.in);
420 System.out.println("\nMCA325\n");
421 System.out.print("Enter first number : ");
422 str=dis.readLine();
423 x=Float.parseFloat(str);
424 System.out.print("Enter second number : ");
425 str=dis.readLine();
426 y=Float.parseFloat(str);
427 z=x+y;
428 System.out.println("\n-----------RESULT-----------");
429 System.out.println("First number : " +x);
430 System.out.println("Second number : " +y);
431 System.out.println("Sum of " +x+ " and " +y+ " is " +z);
432 }
433 catch(Exception e)
434 {
435 System.out.println("Error : " +e);
436 }
437 }
438}
439
440
441
442OUTPUT
443
444
445
446
447JAVA LAB
448
449
450
451
452
4538
454
455
456
457
458
459
460
461
462JAVA LAB
463
464
465
466
467
4689
469
470
471
472Program #4
473Program to accept two numbers from keyboard and swap them.
474
475
476
477SOURCE CODE
478
479
480
481import java.io.*;
482public class SwapNum
483{
484 public static void main(String args[])
485 {
486 int x,y,temp;
487 String str;
488 try
489 {
490 DataInputStream dis = new DataInputStream(System.in);
491 System.out.println("\nMCA325\n");
492 System.out.print("Enter first number : ");
493 str=dis.readLine();
494 x=Integer.parseInt(str);
495 System.out.print("Enter second number : ");
496 str=dis.readLine();
497 y=Integer.parseInt(str);
498 System.out.println("\n-----------BEFORE SWAPPING-----------
499");
500 System.out.println("First number : " +x);
501 System.out.println("Second number : " +y);
502 temp=x;
503 x=y;
504 y=temp;
505 System.out.println("\n-----------AFTER SWAPPING-----------");
506 System.out.println("First number : " +x);
507 System.out.println("Second number : " +y);
508 }
509
510 catch(Exception e)
511 {
512 System.out.println("Error : " +e);
513 }
514 }
515}
516
517
518
519JAVA LAB
520
521
522
523
524
52510
526
527
528
529OUTPUT
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
570JAVA LAB
571
572
573
574
575
57611
577
578
579
580Program #5
581Program to read three numbers and find maximum.
582
583
584
585SOURCE CODE
586
587
588
589import java.io.*;
590class larg3
591{
592 public static void main(String args[])
593 {
594 int num1,num2,num3;
595 try
596 {
597 DataInputStream dis = new DataInputStream(System.in);
598 System.out.println("\nMCA325\n");
599 System.out.print("Enter First Number : ");
600 num1=Integer.parseInt(dis.readLine());
601 System.out.print("Enter Second Number : ");
602 num2=Integer.parseInt(dis.readLine());
603 System.out.print("Enter Third Number:");
604 num3=Integer.parseInt(dis.readLine());
605 if(num1>=num2 && num1>=num3)
606 System.out.println("\n" +num1+" is the largest Number");
607 else if(num2>=num1 && num2>=num3)
608 System.out.println("\n" +num2+" is the largest Number");
609 else
610 System.out.println("\n" +num3+" is the largest Number");
611 }
612 catch(Exception e)
613 {
614 System.out.println("Error : " +e);
615 }
616 }
617}
618OUTPUT
619
620
621
622
623JAVA LAB
624
625
626
627
628
62912
630
631
632
633Program #6
634Program to read 3 numbers from keyboard. Find the minimum using a single
635statement.
636
637
638
639SOURCE CODE
640
641
642
643import java.io.*;
644class min3
645{
646 public static void main(String args[])
647 {
648 int num1,num2,num3,min;
649 try
650 {
651 DataInputStream dis = new DataInputStream(System.in);
652 System.out.println("\nMCA325\n");
653 System.out.print("Enter First Number : ");
654 num1=Integer.parseInt(dis.readLine());
655 System.out.print("Enter Second Number : ");
656 num2=Integer.parseInt(dis.readLine());
657 System.out.print("Enter Third Number:");
658 num3=Integer.parseInt(dis.readLine());
659 min=(num1<num2?(num1<num3?num1:num3):(num2<num3?num2:num3));
660 System.out.println("Smallest Number is : "+min);
661 }
662 catch(Exception e)
663 {
664 System.out.println("Error : " +e);
665 }
666 }
667}
668
669
670
671OUTPUT
672
673
674
675
676
677
678
679
680JAVA LAB
681
682
683
684
685
68613
687
688
689
690Program #7
691Program to search for a given element in an array.
692
693
694
695SOURCE CODE
696
697
698
699import java.io.*;
700class arraysrch
701{
702 public static void main(String args[])
703 {
704 int n,key,flag=1,ch;
705 int a[]; //array declaration
706 try
707 {
708 DataInputStream dis = new DataInputStream(System.in);
709 System.out.println("\nMCA325\n");
710 System.out.print("Enter the size of the array : ");
711 n = Integer.parseInt(dis.readLine());
712 System.out.print("Enter the array : ");
713 a = new int[n]; //creating an array
714 for(int i=0;i<n;i++)
715 {
716 a[i] = Integer.parseInt(dis.readLine());
717 }
718 //searching process
719 do
720 {
721 System.out.print("Enter the element to be searched : ");
722 key=Integer.parseInt(dis.readLine());
723 for(int i=0;i<n;i++)
724 {
725 if(a[i]==key)
726 {
727 System.out.println("Element found at position " +
728(i+1));
729 flag=0;
730 }
731 }
732 if(flag>0)
733 {
734 System.out.println("Element not found");
735 }
736 DataInputStream dis1 = new DataInputStream(System.in);
737
738
739JAVA LAB
740
741
742
743
744
74514
746
747
748
749 System.out.println("Do you want to search another element
750(0/1) : ");
751 ch=Integer.parseInt(dis.readLine());
752 flag=1;
753 }while(ch==1);
754 }
755 catch(Exception e)
756 {
757 System.out.println("Error : " +e);
758 }
759 }
760}
761
762OUTPUT
763
764
765
766
767
768
769
770
771
772
773JAVA LAB
774
775
776
777
778
77915
780
781
782
783Program #8
784Program to sort elements in an array in ascending order.
785
786
787
788SOURCE CODE
789
790
791
792import java.io.*;
793class sortarray
794{
795 public static void main(String args[])
796 {
797 int n,a[],temp,j; //array declaration
798 try
799 {
800 DataInputStream dis = new DataInputStream(System.in);
801 System.out.println("\nMCA325\n");
802 System.out.print("Enter the size of the array : ");
803 n=Integer.parseInt(dis.readLine());
804 System.out.print("Enter the array : ");
805 a=new int[n]; //creating an array
806 for(int i=0;i<n;i++)
807 {
808 a[i]=Integer.parseInt(dis.readLine()); //reading into
809the array
810 }
811 System.out.print("ARRAY ELEMENTS ARE : ");
812 for(int i=0;i<n;i++)
813 {
814 System.out.print(a[i]+ "\t");
815 }
816 //sorting process
817 for(int i=0;i<n;i++)
818 {
819 for(j=i+1;j<n;j++)
820 {
821 if(a[i]>a[j])
822 {
823 temp=a[i];
824 a[i]=a[j];
825 a[j]=temp;
826 }
827 }
828 }
829 System.out.print("\nTHE SORTED ARRAY ELEMENTS ARE : ");
830 for(int i=0;i<n;i++)
831
832
833JAVA LAB
834
835
836
837
838
83916
840
841
842
843 {
844 System.out.print(a[i]+ "\t");
845 }
846
847 }
848 catch(Exception e)
849 {
850 System.out.println("Error : " +e);
851 }
852 }
853}
854
855
856
857OUTPUT
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872JAVA LAB
873
874
875
876
877
87817
879
880
881
882Program #9
883Program to print the row wise and column wise sum of 2D array.
884 1 2 3 | 6
885 2 1 1 |4
886 ---------
887 3 3 4
888
889
890
891SOURCE CODE
892
893
894
895import java.io.*;
896class rowcolsum
897{
898 public static void main(String args[])
899 {
900 int m,n,i,j,rowsum,colsum;
901 int a[][]; //array declaration
902 try
903 {
904 DataInputStream dis = new DataInputStream(System.in);
905 System.out.println("\nMCA325\n");
906 System.out.print("Enter the order of the matrix : ");
907 m=Integer.parseInt(dis.readLine());
908 n=Integer.parseInt(dis.readLine());
909 System.out.print("Enter the matrix : ");
910 a=new int[m][n]; //creating an array
911 for(i=0;i<m;i++)
912 {
913 for(j=0;j<n;j++)
914 a[i][j]=Integer.parseInt(dis.readLine());
915 }
916
917 //for row and column sum
918 System.out.println("-----MATRIX ROW AND COLUMN SUM----");
919 for(i=0;i<n;i++)
920 {
921 rowsum=0;
922 System.out.println("");
923 for(j=0;j<n;j++)
924 {
925 System.out.print(a[i][j]+ "\t");
926 rowsum=rowsum+a[i][j];
927 }
928 System.out.print("|" +rowsum);
929 }
930
931
932JAVA LAB
933
934
935
936
937
93818
939
940
941
942 System.out.print("\n-------------------");
943 System.out.print("\n");
944 for(j=0;j<n;j++)
945 {
946 colsum=0;
947 for(i=0;i<n;i++)
948 {
949 colsum=colsum+a[i][j];
950 }
951 System.out.print(colsum+ "\t");
952 }
953 }
954 catch(Exception e)
955 {
956 System.out.println("Error : " +e);
957 }
958 }
959}
960
961OUTPUT
962
963
964
965
966
967
968
969
970
971
972JAVA LAB
973
974
975
976
977
97819
979
980
981
982Program #10
983Program with 2 functions to check for an integer palindrome. (Function1 should
984reverse the integer. Function2 should return 1, if it is a palindrome or else 0).
985
986
987
988SOURCE CODE
989
990
991
992import java.io.*;
993class palin
994{
995 public int rev(int n)
996 {
997 int rev=0,rem;
998 while(n>0)
999 {
1000 rem=n%10;
1001 rev=rev*10+rem;
1002 n=n/10;
1003 }
1004 return rev;
1005 }
1006 public int checkpalin(int num)
1007 {
1008 int temp=rev(num);
1009 if(num==temp)
1010 return 1;
1011 else
1012 return 0;
1013 }
1014}
1015class palinfunc
1016{
1017 public static void main(String args[])
1018 {
1019 int n,temp;
1020 palin p = new palin();
1021 DataInputStream din = new DataInputStream(System.in);
1022 System.out.println("\nMCA325\n");
1023 try
1024 {
1025 System.out.print("Enter the number : ");
1026 n=Integer.parseInt(din.readLine());
1027 temp=p.checkpalin(n);
1028 if(temp==1)
1029 System.out.println(n+" is palindrome");
1030
1031
1032JAVA LAB
1033
1034
1035
1036
1037
103820
1039
1040
1041
1042 else
1043 System.out.println(n+" is not palindrome");
1044 }
1045 catch(Exception e)
1046 {
1047 System.out.println("ERROR "+e);
1048 }
1049 }
1050}
1051
1052OUTPUT
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063JAVA LAB
1064
1065
1066
1067
1068
106921
1070
1071
1072
1073Program #11
1074Program to display numbers from m to n using single while loop.
1075
1076
1077
1078SOURCE CODE
1079
1080
1081
1082import java.io.*;
1083class mton
1084{
1085 public static void main(String args[])
1086 {
1087 int m,n;
1088 try
1089 {
1090 DataInputStream dis = new DataInputStream(System.in);
1091 System.out.println("\nMCA325\n");
1092 System.out.print("Enter the start limit : ");
1093 m=Integer.parseInt(dis.readLine());
1094 System.out.print("Enter the end limit : ");
1095 n=Integer.parseInt(dis.readLine());
1096 System.out.println("-----NUMBERS FROM "+m+ " TO " +n+ "----
1097");
1098 while(m<=n)
1099 {
1100 System.out.print(m + " ");
1101 m++;
1102 }
1103 }
1104 catch(Exception e)
1105 {
1106 System.out.println("Error : " +e);
1107 }
1108 }
1109}
1110OUTPUT
1111
1112
1113
1114
1115
1116
1117
1118
1119JAVA LAB
1120
1121
1122
1123
1124
112522
1126
1127
1128
1129Program #12
1130Program to find the sum of 1+(1+2)+(1+2+3)+…………..+(1+2+3+……..+n) using a
1131single while loop.
1132
1133
1134
1135SOURCE CODE
1136
1137
1138
1139import java.io.*;
1140class seriessum
1141{
1142 public static void main(String args[])
1143 {
1144 int i=1,n,prev_sum=0, sum = 0;
1145 try
1146 {
1147 DataInputStream dis = new DataInputStream(System.in);
1148 System.out.println("\nMCA325\n");
1149 System.out.print("Enter the limit : ");
1150 n=Integer.parseInt(dis.readLine());
1151 while(i<=n)
1152 {
1153 prev_sum = prev_sum + i;
1154 //tsum=tsum+i;
1155 i++;
1156 sum = sum + prev_sum;
1157 }
1158 System.out.println("Sum of series (1)+(1+2)+(1+2+3)+........+
1159(1+2+3+...+n) : " + sum);
1160 }
1161 catch(Exception e)
1162 {
1163 System.out.println("Error : " +e);
1164 }
1165 }
1166}
1167
1168
1169
1170OUTPUT
1171
1172
1173
1174
1175
1176
1177JAVA LAB
1178
1179
1180
1181
1182
118323
1184
1185
1186
1187Program #13
1188Program to find the sum of 1+2/2!+3/3!+4/4!+.....+n/n! Using single for loop.
1189
1190
1191
1192SOURCE CODE
1193
1194
1195
1196import java.io.*;
1197class sumfactseries
1198{
1199 public static void main(String args[])
1200 {
1201 int n,i;
1202 double sum=0.0,fact=1;
1203 DataInputStream dis = new DataInputStream(System.in);
1204 System.out.println("\nMCA325\n");
1205 try
1206 {
1207 System.out.print("Enter the Limit : ");
1208 n=Integer.parseInt(dis.readLine());
1209 for(i=1;i<=n;i++)
1210 {
1211 fact=fact*i;
1212 sum=sum+(i/fact);
1213 }
1214 System.out.println("\nSum of series (1+2/2!+3/3!+---
1215+n/n!) : "+sum);
1216 }
1217 catch(Exception e) { System.out.println("ERROR "+e); }
1218 }
1219}
1220
1221
1222
1223OUTPUT
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236JAVA LAB
1237
1238
1239
1240
1241
124224
1243
1244
1245
1246Program #14
1247Program to calculate area of a circle (functions with no argument and no return
1248type).
1249
1250
1251
1252SOURCE CODE
1253
1254
1255
1256import java.io.*;
1257class func
1258{
1259 public void area()
1260 {
1261 double r,res;
1262 DataInputStream dis = new DataInputStream(System.in);
1263 System.out.println("\nMCA325\n");
1264 try
1265 {
1266 System.out.print("Enter the radius of circle : ");
1267 r=Double.parseDouble(dis.readLine());
1268 res=3.14*r*r;
1269 System.out.println("Area of circle : " +res);
1270 }
1271 catch(Exception e)
1272 {
1273 System.out.println("Error : " +e);
1274 }
1275 }
1276}
1277class circlefctn
1278{
1279 public static void main(String args[])
1280 {
1281 func f = new func();
1282 f.area();
1283 }
1284}
1285
1286
1287
1288OUTPUT
1289
1290
1291
1292
1293
1294
1295JAVA LAB
1296
1297
1298
1299
1300
130125
1302
1303
1304
1305Program #15
1306Program to reverse a number (functions with argument and no return type).
1307
1308
1309
1310SOURCE CODE
1311
1312
1313
1314import java.io.*;
1315class function
1316{
1317 public void rev(int n)
1318 {
1319 int res=0,temp;
1320 while(n>0)
1321 {
1322 temp=n%10;
1323 res=temp+(res*10);
1324 n=n/10;
1325 }
1326 System.out.println("Reverse of the number : " +res);
1327 }
1328}
1329class reverseno
1330{
1331 public static void main(String args[])
1332 {
1333 function f = new function();
1334 int n;
1335 try
1336 {
1337 DataInputStream dis = new DataInputStream(System.in);
1338 System.out.println("\nMCA325\n");
1339 System.out.print("Enter the number : ");
1340 n=Integer.parseInt(dis.readLine());
1341 f.rev(n);
1342 }
1343 catch(Exception e)
1344 {
1345 System.out.println("Error : " +e);
1346 }
1347 }
1348}
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359JAVA LAB
1360
1361
1362
1363
1364
136526
1366
1367
1368
1369OUTPUT
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384JAVA LAB
1385
1386
1387
1388
1389
139027
1391
1392
1393
1394Program #16
1395Program to calculate sum of digits of a number (functions with argument and
1396return type).
1397
1398
1399
1400SOURCE CODE
1401
1402
1403
1404import java.io.*;
1405class func0
1406{
1407 public int sumno1(int n)
1408 {
1409 int res=0,temp;
1410 while(n>0)
1411 {
1412 temp=n%10;
1413 res=res+temp;
1414 n=n/10;
1415 }
1416 return(res);
1417 }
1418}
1419class sumno
1420{
1421 public static void main(String args[])
1422 {
1423 int sum,num;
1424 func0 f = new func0();
1425 try
1426 {
1427 DataInputStream dis = new DataInputStream(System.in);
1428 System.out.println("\nMCA325\n");
1429 System.out.print("Enter the number : ");
1430 num=Integer.parseInt(dis.readLine());
1431 sum = f.sumno1(num);
1432 System.out.println("Sum of digits of number " +num+ " is " +s
1433um);
1434 }
1435 catch(Exception e)
1436 {
1437 System.out.println("Error : " +e);
1438 }
1439 }
1440}
1441
1442
1443
1444
1445JAVA LAB
1446
1447
1448
1449
1450
145128
1452
1453
1454
1455OUTPUT
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476JAVA LAB
1477
1478
1479
1480
1481
148229
1483
1484
1485
1486Program #17
1487Program to calculate sum of n even numbers (functions with no argument and
1488return type).
1489
1490
1491
1492SOURCE CODE
1493
1494
1495
1496import java.io.*;
1497class func1
1498{
1499 public int sumofn()
1500 {
1501 int n,sum=0,i=0,a=2;
1502 DataInputStream dis = new DataInputStream(System.in);
1503 System.out.println("\nMCA325\n");
1504 try
1505 {
1506 System.out.print("Enter the limit : ");
1507 n=Integer.parseInt(dis.readLine());
1508 while(i<n)
1509 {
1510 sum=sum+a;
1511 a=a+2;
1512 i++;
1513 }
1514 }
1515 catch(Exception e)
1516 {
1517 System.out.println("Error : " +e);
1518 }
1519 return(sum);
1520 }
1521}
1522class evensum
1523{
1524 public static void main(String args[])
1525 {
1526 func1 f = new func1();
1527 int s = f.sumofn();
1528 System.out.println("Sum : " +s);
1529 }
1530}
1531
1532
1533
1534
1535
1536
1537JAVA LAB
1538
1539
1540
1541
1542
154330
1544
1545
1546
1547OUTPUT
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576JAVA LAB
1577
1578
1579
1580
1581
158231
1583
1584
1585
1586Program #18
1587Program with nested functions to find the maximum of three numbers. Function1
1588should take in two arguments and find the maximum. Function2 should take in the
1589third number and the maximum from function1 to find the maximum.)
1590
1591
1592
1593SOURCE CODE
1594
1595
1596
1597import java.io.*;
1598
1599class max
1600{
1601 public int maxfunc(int a,int b)
1602 {
1603 if(a>b)
1604 return a;
1605 else
1606 return b;
1607 }
1608 public int maxfunc1(int x,int y,int z)
1609 {
1610 int m=maxfunc(x,y);
1611 if(m>z)
1612 return m;
1613 else
1614 return z;
1615 }
1616}
1617class maximumfunc
1618{
1619 public static void main(String arg[])
1620 {
1621 max m = new max();
1622 int a,b,c;
1623 DataInputStream dis = new DataInputStream(System.in);
1624 System.out.println("\nMCA325\n");
1625 try
1626 {
1627 System.out.print("Enter 3 numbers : ");
1628 a=Integer.parseInt(dis.readLine());
1629 b=Integer.parseInt(dis.readLine());
1630 c=Integer.parseInt(dis.readLine());
1631 int larg=m.maxfunc1(a,b,c);
1632 System.out.println("Largest number : " +larg);
1633 }
1634
1635
1636JAVA LAB
1637
1638
1639
1640
1641
164232
1643
1644
1645
1646 catch(Exception e)
1647 {
1648 System.out.println("Error"+e);
1649 }
1650 }
1651}
1652
1653
1654
1655OUTPUT
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684JAVA LAB
1685
1686
1687
1688
1689
169033
1691
1692
1693
1694Program #19
1695Program to find the factorial of n, using recursion.
1696
1697
1698
1699SOURCE CODE
1700
1701
1702
1703import java.io.*;
1704class fact
1705{
1706 public int rfact(int n)
1707 {
1708 if(n>=1)
1709 return (n*rfact(n-1));
1710 else
1711 return 1;
1712 }
1713}
1714class factorial
1715{
1716 public static void main(String args[])
1717 {
1718 fact f = new fact();
1719 int n,rf;
1720 try
1721 {
1722 DataInputStream dis = new DataInputStream(System.in);
1723 System.out.println("\nMCA325\n");
1724 System.out.print("Enter the number : ");
1725 n=Integer.parseInt(dis.readLine());
1726 rf=f.rfact(n);
1727 System.out.println("Factorial : " +rf);
1728 }
1729 catch(Exception e)
1730 {
1731 System.out.println("Error" +e);
1732 }
1733 }
1734}
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744JAVA LAB
1745
1746
1747
1748
1749
175034
1751
1752
1753
1754OUTPUT
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791JAVA LAB
1792
1793
1794
1795
1796
179735
1798
1799
1800
1801Program #20
1802Program to display from n to 1 and vice versa, using recursion
1803
1804
1805
1806SOURCE CODE
1807
1808
1809
1810import java.io.*;
1811class cdisp
1812{
1813 public void disp(int n)
1814 {
1815 if(n>0)
1816 {
1817 disp(n-1);
1818 System.out.print(n+" ");
1819 }
1820 }
1821 public void dispn(int n)
1822 {
1823 if(n>0)
1824 {
1825 System.out.print(n+" ");
1826 dispn(n-1);
1827 }
1828 }
1829}
1830class onetonto1
1831{
1832 public static void main(String args[])
1833 {
1834 cdisp d = new cdisp();
1835 DataInputStream dis = new DataInputStream(System.in);
1836 System.out.println("\nMCA325\n");
1837 try
1838 {
1839 System.out.print("Enter the limit : ");
1840 int n=Integer.parseInt(dis.readLine());
1841 System.out.println("-------NUMBERS 1 TO N-------");
1842 d.disp(n);
1843 System.out.println("\n");
1844 System.out.println("-------NUMBERS N TO 1-------");
1845 d.dispn(n);
1846 }
1847 catch(Exception e)
1848 {
1849
1850
1851JAVA LAB
1852
1853
1854
1855
1856
185736
1858
1859
1860
1861 System.out.println("ERROR"+e);
1862 }
1863 }
1864}
1865
1866OUTPUT
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901JAVA LAB
1902
1903
1904
1905
1906
190737
1908
1909
1910
1911Program #21
1912Create a class complex having real and imaginary as data members and provide
1913function for read, display, add and multiplying two complex numbers.
1914
1915
1916
1917SOURCE CODE
1918
1919
1920
1921import java.io.*;
1922class complex
1923{
1924 int real,imag;
1925 public void read()
1926 {
1927 DataInputStream dis = new DataInputStream(System.in);
1928 try
1929 {
1930 System.out.print("Enter the real part : ");
1931 real=Integer.parseInt(dis.readLine());
1932 System.out.print("Enter the imaginary part : ");
1933 imag=Integer.parseInt(dis.readLine());
1934 }
1935 catch(Exception e) { System.out.println("ERROR"+e); }
1936 }
1937 public void disp()
1938 {
1939 System.out.println(real+" + " +imag+" i");
1940 }
1941 public complex add(complex c)
1942 {
1943 complex t=new complex();
1944 t.real=real+c.real;
1945 t.imag=imag+c.imag;
1946 return t;
1947 }
1948 public complex mul(complex d)
1949 {
1950 complex t1=new complex();
1951 t1.real = (real*d.real - imag*d.imag);
1952 t1.imag = (real*d.imag + d.real*imag);
1953 return t1;
1954 }
1955}
1956class complexfctn
1957{
1958 public static void main(String args[])
1959
1960
1961JAVA LAB
1962
1963
1964
1965
1966
196738
1968
1969
1970
1971 {
1972 complex c1,c2,c3,c4;
1973 c1=new complex();
1974 c2=new complex();
1975 System.out.println("\nMCA325\n");
1976 System.out.println("Enter the first complex number :");
1977 c1.read();
1978 System.out.println("Enter the second complex number :");
1979 c2.read();
1980 System.out.print("\n\nFIRST COMPLEX NO :");
1981 c1.disp();
1982 System.out.print("SECOND COMPLEX NO :");
1983 c2.disp();
1984 System .out.print("SUM OF COMPLEX NUMBERS :");
1985 c3=c1.add(c2);
1986 c3.disp();
1987 System .out.print("PRODUCT OF COMPLEX NUMBERS :");
1988 c4=c1.mul(c2);
1989 c4.disp();
1990 }
1991}
1992
1993
1994
1995OUTPUT
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006JAVA LAB
2007
2008
2009
2010
2011
201239
2013
2014
2015
2016Program #22
2017Program to display numbers using a static function.
2018
2019
2020
2021SOURCE CODE
2022
2023
2024
2025import java.io.*;
2026class staticfctn
2027{
2028 static void evendisp(int a) //static func
2029 {
2030 for(int i=1;i<=a;i++)
2031 {
2032 if(i%2==0)
2033 System.out.print(i+" ");
2034 }
2035 }
2036 public static void main(String args[]) //main() is static
2037 {
2038 try
2039 {
2040 DataInputStream d=new DataInputStream(System.in);
2041 System.out.println("\nMCA325\n");
2042 System.out.print("Enter a number : ");
2043 int n=Integer.parseInt(d.readLine());
2044 System.out.print("\nEven number upto " +n+ " : ");
2045 evendisp(n);
2046 }
2047 catch (Exception e)
2048 {
2049 System.out.println("Error");
2050 }
2051 }
2052}
2053
2054
2055
2056OUTPUT
2057
2058
2059
2060
2061
2062
2063
2064
2065JAVA LAB
2066
2067
2068
2069
2070
207140
2072
2073
2074
2075Program #23
2076Program (menu driven) to demonstrate function overriding in java, by displaying
2077details of a student, and a teacher.
2078
2079
2080
2081SOURCE CODE
2082
2083
2084
2085import java.io.*;
2086class person
2087{
2088 int id;
2089 String name;
2090 void read(){}
2091 void disp(){}
2092}
2093
2094class teacher extends person
2095{
2096 String sub;
2097 void read()
2098 {
2099 try
2100 {
2101 DataInputStream dis = new DataInputStream(System.in);
2102 System.out.print("Enter id : ");
2103 id=Integer.parseInt(dis.readLine());
2104 System.out.print("Enter name : ");
2105 name=dis.readLine();
2106 System.out.print("Enter subject : ");
2107 sub=dis.readLine();
2108 }
2109 catch (Exception e)
2110 {
2111 System.out.println("error");
2112 }
2113 }
2114 void disp()
2115 {
2116 System.out.println("...........TEACHER DETAILS...........");
2117 System.out.println("ID : " +id);
2118 System.out.println("Name : " +name);
2119 System.out.println("Subject : " +sub);
2120 }
2121}
2122
2123
2124
2125JAVA LAB
2126
2127
2128
2129
2130
213141
2132
2133
2134
2135class student extends person
2136{
2137 float m1,m2;
2138 void read()
2139 {
2140 try
2141 {
2142 DataInputStream dis = new DataInputStream(System.in);
2143 System.out.println("Enter id : ");
2144 id=Integer.parseInt(dis.readLine());
2145 System.out.println("Enter name : ");
2146 name=dis.readLine();
2147 System.out.println("Enter mark1 : ");
2148 m1=Float.parseFloat(dis.readLine());
2149 System.out.println("Enter mark2 : ");
2150 m2=Float.parseFloat(dis.readLine());
2151 }
2152 catch (Exception e)
2153 {
2154 System.out.println("error");
2155 }
2156 }
2157 void disp()
2158 {
2159 System.out.println("...........STUDENT DETAILS...........");
2160 System.out.println("ID : " +id);
2161 System.out.println("Name : " +name);
2162 System.out.println("Mark1 : " +m1);
2163 System.out.println("Mark2 : " +m2);
2164 }
2165}
2166
2167class studteach_fctnoverld
2168{
2169 public static void main(String args[])
2170 {
2171 student s = new student();
2172 teacher t = new teacher();
2173 System.out.println("\nMCA325");
2174 try
2175 {
2176 DataInputStream dis=new DataInputStream(System.in);
2177 do
2178 {
2179 System.out.println("-------CHOICE MENU-------");
2180
2181
2182JAVA LAB
2183
2184
2185
2186
2187
218842
2189
2190
2191
2192 System.out.println("1. Enter Student details");
2193 System.out.println("2. Enter Teacher details");
2194 System.out.println("3. display student details");
2195 System.out.println("4. display teacher details");
2196 System.out.println("5. exit");
2197 System.out.println("Enter your choice : ");
2198 int c=Integer.parseInt(dis.readLine());
2199 switch(c)
2200 {
2201 case 1: s.read();
2202 break;
2203 case 2: t.read();
2204 break;
2205 case 3: s.disp();
2206 break;
2207 case 4: t.disp();
2208 break;
2209 case 5: System.exit(0);
2210 break;
2211 }
2212 }
2213 while(true);
2214 }
2215 catch (Exception e)
2216 {
2217 System.out.println("error");
2218 }
2219 }
2220}
2221
2222
2223
2224
2225
2226
2227
2228JAVA LAB
2229
2230
2231
2232
2233
223443
2235
2236
2237
2238OUTPUT
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267JAVA LAB
2268
2269
2270
2271
2272
227344
2274
2275
2276
2277Program #24
2278Create a class for employee having eno, ename and esal as data members. Provide
2279function for reading and displaying employee details. (Accept information of n
2280employees in main function, display the same and search for an emp (using eno)).
2281
2282
2283
2284SOURCE CODE
2285
2286
2287
2288import java.io.*;
2289class employee
2290{
2291 int eno;
2292 String ename;
2293 float esal;
2294
2295 public void read()
2296 {
2297 DataInputStream dis = new DataInputStream(System.in);
2298 try
2299 {
2300 System.out.print("Enter the Employee no : ");
2301 eno=Integer.parseInt(dis.readLine());
2302 System.out.print("Enter the Employee name : ");
2303 ename=dis.readLine();
2304 System.out.print("Enter the Salary : ");
2305 esal=Float.parseFloat(dis.readLine());
2306 }
2307 catch(Exception e)
2308 {
2309 System.out.println("ERROR"+e);
2310 }
2311 }
2312 public void disp()
2313 {
2314 System.out.println("EMPLOYEE NO : "+eno);
2315 System.out.println("EMPLOYEE NAME : "+ename);
2316 System.out.println("SALARY : "+esal);
2317 }
2318 public void compare_emp(employee a[],int s)
2319 {
2320 int i,flag=0,index;
2321 for(i=0;i<a.length;i++)
2322 {
2323 if(a[i].eno==s)
2324 {
2325
2326
2327JAVA LAB
2328
2329
2330
2331
2332
233345
2334
2335
2336
2337 flag=1;
2338 System.out.println("\nFound element!!!!");
2339 System.out.println("\n--------EMPLOYEE DETAILS--------");
2340 System.out.println("Name : "+a[i].ename);
2341 System.out.println("Salary : "+a[i].esal);
2342 break;
2343 }
2344 }
2345 if(flag==0)
2346 {
2347 System.out.println("NOT FOUND!!!");
2348 }
2349 }
2350}
2351
2352class empfctn
2353{
2354 public static void main(String args[])
2355 {
2356 employee e[];
2357 employee x = new employee();
2358 int n,i,temp,ch;
2359 DataInputStream dis = new DataInputStream(System.in);
2360 System.out.println("\nMCA325\n");
2361
2362 try
2363 {
2364 System.out.print("Enter the no of Employees : ");
2365 n=Integer.parseInt(dis.readLine());
2366 e = new employee[n];
2367 for(i=0;i<n;i++)
2368 {
2369 e[i]=new employee();
2370 System.out.print("Enter the Employee Details of "+(i+1)+"
2371:\n");
2372 e[i].read();
2373 }
2374
2375 for(i=0;i<n;i++)
2376 {
2377 System.out.println("---EMPLOYEE DETAILS"+(i+1)+"---");
2378 e[i].disp();
2379 }
2380 do
2381 {
2382
2383
2384JAVA LAB
2385
2386
2387
2388
2389
239046
2391
2392
2393
2394 System.out.print("\nEnter the employee no to be searched : ")
2395;
2396 temp=Integer.parseInt(dis.readLine());
2397 x.compare_emp(e,temp);
2398 System.out.print("Do you want to search another employee(1/0)
2399 : ");
2400 ch=Integer.parseInt(dis.readLine());
2401 }while(ch==1);
2402 }
2403 catch(Exception er)
2404 {
2405 System.out.println("ERROR"+er);
2406 }
2407 }
2408}
2409
2410
2411
2412OUTPUT
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425JAVA LAB
2426
2427
2428
2429
2430
243147
2432
2433
2434
2435Program #25
2436Program to implement run time polymorphism in java using interface, write
2437calculating area of a triangle.
2438
2439
2440
2441SOURCE CODE
2442
2443
2444
2445import java.io.*;
2446
2447interface shape
2448{
2449 float area();
2450}
2451
2452class triangle implements shape
2453{
2454 int l,h;
2455 void read()
2456 {
2457 try
2458 {
2459 DataInputStream dis=new DataInputStream(System.in);
2460 System.out.print("Enter lenght and height of triangle : ");
2461 l=Integer.parseInt(dis.readLine());
2462 h=Integer.parseInt(dis.readLine());
2463 }
2464 catch (Exception e)
2465 {
2466 System.out.println("error");
2467 }
2468 }
2469 public float area()
2470 {
2471 float area = (float) 0.5*l*h;
2472 return area;
2473 }
2474}
2475
2476class tri_interface
2477{
2478 public static void main(String args[])
2479 {
2480 triangle t = new triangle();
2481 System.out.println("\nMCA325\n");
2482 System.out.println("...........TRAINGLE...........\n");
2483
2484
2485JAVA LAB
2486
2487
2488
2489
2490
249148
2492
2493
2494
2495 t.read();
2496 System.out.println("Area : " +t.area());
2497 }
2498}
2499
2500
2501
2502OUTPUT
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539JAVA LAB
2540
2541
2542
2543
2544
254549
2546
2547
2548
2549Program #26
2550Create an interface shape having two prototypes disp() and calc(), to display the
2551shape and calculate area respectively. Create two classes circle and rectangle which
2552implements the above interface. In the main function create a reference of Shape
2553depending on the user-choice.
2554
2555
2556
2557SOURCE CODE
2558
2559
2560
2561import java.io.*;
2562interface shape
2563{
2564 void read();
2565 void calc();
2566}
2567
2568class circle implements shape
2569{
2570 int r;
2571 public void read()
2572 {
2573 try
2574 {
2575 System.out.println("\n-----------CIRCLE-----------");
2576 DataInputStream dis = new DataInputStream(System.in);
2577 System.out.print("Enter radius of circle : ");
2578 r = Integer.parseInt(dis.readLine());
2579 }
2580 catch (Exception e)
2581 {
2582 System.out.println("Error");
2583 }
2584 }
2585
2586 public void calc()
2587 {
2588 float area = (float) 3.14 * r * r;
2589 System.out.println("AREA : " + area);
2590 }
2591}
2592
2593class rectangle implements shape
2594{
2595 int l, b;
2596
2597
2598
2599JAVA LAB
2600
2601
2602
2603
2604
260550
2606
2607
2608
2609 public void read()
2610 {
2611 try
2612 {
2613 System.out.println("\n-----------RECTANGLE-----------");
2614 DataInputStream dis = new DataInputStream(System.in);
2615 System.out.print("Enter lenght and breath of rectangle : ");
2616 l = Integer.parseInt(dis.readLine());
2617 b = Integer.parseInt(dis.readLine());
2618 }
2619 catch (Exception e)
2620 {
2621 System.out.println("error");
2622 }
2623 }
2624
2625 public void calc()
2626 {
2627 float area = (float) l * b;
2628 System.out.println("AREA : " + area);
2629 }
2630}
2631
2632class shape_interfc
2633{
2634 public static void main(String args[])
2635 {
2636 shape s;
2637 try
2638 {
2639 DataInputStream dis = new DataInputStream(System.in);
2640 System.out.println("\nMCA325");
2641 do
2642 {
2643 System.out.println("\n.........CHOICE MENU.........");
2644 System.out.println(" 1. Circle \n 2. Rectangle \n 3. Exit
2645");
2646 System.out.print("Enter your choice : ");
2647 int c = Integer.parseInt(dis.readLine());
2648 switch (c)
2649 {
2650 case 1:
2651 s = new circle();
2652 s.read();
2653 s.calc();
2654
2655
2656JAVA LAB
2657
2658
2659
2660
2661
266251
2663
2664
2665
2666 break;
2667 case 2:
2668 s = new rectangle();
2669 s.read();
2670 s.calc();
2671 break;
2672 case 3:
2673 System.exit(0);
2674 break;
2675 }
2676 } while (true);
2677 }
2678 catch (Exception e)
2679 {
2680 System.out.println("error");
2681 }
2682 }
2683}
2684OUTPUT
2685
2686
2687
2688
2689
2690
2691JAVA LAB
2692
2693
2694
2695
2696
269752
2698
2699
2700
2701Program #27
2702Program to implement a function using call by value to swap two float numbers.
2703
2704
2705
2706SOURCE CODE
2707
2708
2709
2710import java.io.*;
2711
2712class funct
2713{
2714 public void swapfn(float a,float b)
2715 {
2716 float temp;
2717 temp=a;
2718 a=b;
2719 b=temp;
2720 System.out.println("inside function");
2721 System.out.println("a = " +a+ "\nb = " +b);
2722 }
2723}
2724
2725class callval_swapfloat
2726{
2727 public static void main(String args[])
2728 {
2729 funct f = new funct();
2730 float a,b;
2731 DataInputStream dis = new DataInputStream(System.in);
2732 System.out.println("\nMCA325");
2733 try
2734 {
2735 System.out.print("Enter first number : " );
2736 a=Float.parseFloat(dis.readLine());
2737 System.out.print("Enter second number : " );
2738 b=Float.parseFloat(dis.readLine());
2739 System.out.println("-----------BEFORE SWAPPING-----------");
2740 System.out.println("a = " +a+ "\nb = " +b);
2741 System.out.println("-----------AFTER SWAPPING-----------");
2742 f.swapfn(a,b);
2743 System.out.println("outside function");
2744 System.out.println("a = " +a+ "\nb = " +b);
2745 }
2746 catch(Exception e)
2747 {
2748 System.out.println("error"+e);
2749
2750
2751JAVA LAB
2752
2753
2754
2755
2756
275753
2758
2759
2760
2761 }
2762 }
2763}
2764
2765
2766
2767OUTPUT
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800JAVA LAB
2801
2802
2803
2804
2805
280654
2807
2808
2809
2810Program #28
2811Program to implement a function using call by reference to find the square root of
2812a given number.
2813
2814
2815
2816SOURCE CODE
2817
2818
2819
2820import java.lang.Math;
2821import java.io.*;
2822
2823class num
2824{
2825 public double f;
2826}
2827
2828class sqroot
2829{
2830 public static double fctn(num n1)
2831 {
2832 double r;
2833 r = Math.sqrt(n1.f);
2834 return r;
2835 }
2836}
2837
2838class callref_sqroot
2839{
2840 public static void main(String args[])
2841 {
2842 num n = new num();
2843 sqroot obj = new sqroot();
2844 double res;
2845 DataInputStream dis = new DataInputStream(System.in);
2846 System.out.println("\nMCA325\n");
2847
2848 try
2849 {
2850 System.out.print("Enter the number : " );
2851 n.f=Double.parseDouble(dis.readLine());
2852 res=obj.fctn(n);
2853 System.out.println("Square root : " +res);
2854 }
2855 catch(Exception e)
2856 {
2857 System.out.println("error"+e);
2858
2859
2860JAVA LAB
2861
2862
2863
2864
2865
286655
2867
2868
2869
2870 }
2871 }
2872}
2873
2874
2875
2876OUTPUT
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937JAVA LAB
2938
2939
2940
2941
2942
294356
2944
2945
2946
2947Program #29
2948Program to create a class for Cstring having a string data member and provide
2949functions for read, display, compare (return Boolean value), add and concatenate.
2950
2951
2952
2953SOURCE CODE
2954
2955
2956
2957import java.io.*;
2958
2959class string
2960{
2961 String str;
2962 public void read()
2963 {
2964 DataInputStream dis = new DataInputStream(System.in);
2965 try
2966 {
2967 System.out.print("Enter the string : ");
2968 str = dis.readLine();
2969 }
2970 catch(Exception e)
2971 {
2972 System.out.println("ERROR" + e);
2973 }
2974 }
2975 public void display()
2976 {
2977 System.out.println(str);
2978 }
2979 public boolean compare(string z)
2980 {
2981 boolean x;
2982 if(str.equals(z.str))
2983 x = true;
2984 else
2985 x = false;
2986 return x;
2987 }
2988 public string concat(string p)
2989 {
2990 string q = new string();
2991 q.str = str + p.str;
2992 return q;
2993 }
2994 public void add(String s)
2995
2996
2997JAVA LAB
2998
2999
3000
3001
3002
300357
3004
3005
3006
3007 {
3008 str=s+str;
3009 }
3010}
3011
3012class Cstringfctns
3013{
3014 public static void main(String args[])
3015 {
3016 string s1,s2,s3,s4;
3017 s1 = new string();
3018 s2 = new string();
3019 System.out.println("\nMCA325\n");
3020
3021 s1.read();
3022 s2.read();
3023
3024 System.out.print("\nString1 : ");
3025 s1.display();
3026 System.out.print("String2 : ");
3027 s2.display();
3028
3029 if(s1.compare(s2)==true)
3030 {
3031 System.out.println("2 strings are equal");
3032 }
3033 else
3034 {
3035 System.out.println("Not equal!!!");
3036 }
3037
3038 s3 = s1.concat(s2);
3039 System.out.print("Concatenated String : ");
3040 s3.display();
3041
3042 System.out.print("String after adding hi : ");
3043 s1.add("Hi ");
3044 s1.display();
3045 }
3046}
3047
3048
3049
3050
3051
3052
3053
3054JAVA LAB
3055
3056
3057
3058
3059
306058
3061
3062
3063
3064OUTPUT
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115JAVA LAB
3116
3117
3118
3119
3120
312159
3122
3123
3124
3125Program #30
3126Program to implement object cloning for the class Distance which has inch and feet
3127as data members.
3128
3129
3130
3131SOURCE CODE
3132
3133
3134
3135import java.io.*;
3136
3137class distance1
3138{
3139 int inch, feet;
3140
3141 public Object clone() // method clone (keyword) of class Object(keywo
3142rd)
3143 {
3144 try
3145 {
3146 return super.clone(); // return clone to mian()
3147 }
3148 catch (CloneNotSupportedException e)
3149 {
3150 System.out.println("Error" + e);
3151 return this;
3152 }
3153 }
3154
3155 void getdata()
3156 {
3157 DataInputStream dis = new DataInputStream(System.in);
3158 try
3159 {
3160 System.out.print("Enter the length in feet and inch : ");
3161 feet = Integer.parseInt(dis.readLine());
3162 inch = Integer.parseInt(dis.readLine());
3163 }
3164 catch (Exception e)
3165 {
3166 System.out.println("Error!!" + e);
3167 }
3168 }
3169
3170 void putdata()
3171 {
3172 System.out.println("\nLength...");
3173
3174
3175JAVA LAB
3176
3177
3178
3179
3180
318160
3182
3183
3184
3185 System.out.println("Feet : " + feet + " inch : " + inch);
3186 }
3187}
3188
3189class clone_distance
3190{
3191 public static void main(String args[]) throws CloneNotSupportedExcept
3192ion
3193 {
3194 System.out.println("\nMCA325\n");
3195
3196 distance1 d1 = new distance1();
3197 d1.getdata();
3198 d1.putdata();
3199
3200 distance1 d2 = (distance1) d1.clone();
3201 // creating clone of object t1 and typecast it to class test
3202 System.out.println("\nAfter Cloning...........");
3203 d2.putdata();
3204 }
3205}
3206
3207
3208
3209OUTPUT
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230JAVA LAB
3231
3232
3233
3234
3235
323661
3237
3238
3239
3240Program #31
3241Program to create a menu driven program for performing the following operations.
3242· Length of a given string
3243· Compare for equality
3244· Extract a substring from a string.
3245 · Convert to uppercase and lowercase
3246
3247
3248
3249SOURCE CODE
3250
3251
3252
3253import java.io.*;
3254
3255class stringoptn
3256{
3257 public static void main(String args[])
3258 {
3259 System.out.println("\nMCA325\n");
3260 int ch;
3261 DataInputStream dis = new DataInputStream(System.in);
3262
3263 try
3264 {
3265 System.out.print("Enter string1 : ");
3266 String s1 = dis.readLine();
3267 System.out.print("Enter string2 : ");
3268 String s2 = dis.readLine();
3269 do
3270 {
3271 System.out.println("\n...........CHOICE MENU...........")
3272;
3273 System.out.print("\n1. Length of the string");
3274 System.out.print("\n2. Compare for equality");
3275 System.out.print("\n3. Extract a substring from a string"
3276);
3277 System.out.print("\n4. Convert to uppercase and lowercase
3278");
3279 System.out.print("\nEnter your choice : ");
3280 int option = Integer.parseInt(dis.readLine());
3281
3282 switch (option)
3283 {
3284 case 1:
3285 System.out.println("\nLength of string1 : " + s1.
3286length());
3287
3288
3289JAVA LAB
3290
3291
3292
3293
3294
329562
3296
3297
3298
3299 System.out.println("Length of string2 : " + s2.le
3300ngth());
3301 break;
3302 case 2:
3303 if (s2.equalsIgnoreCase(s1) == true)
3304 System.out.println("\nStrings are equal!!");
3305 else
3306 System.out.println("\nStrings are not equal!!
3307");
3308 break;
3309 case 3:
3310 System.out.println("\nSubstring of string1 : " +
3311s1.substring(1, 5));
3312 System.out.println("Substring of string2 : " + s2
3313.substring(1, 5));
3314 break;
3315 case 4:
3316 System.out.println("\nString1 (uppercase) : " + s
33171.toUpperCase());
3318 System.out.println("String1 (lowercase) : " + s1.
3319toLowerCase());
3320 System.out.println("String2 (uppercase) : " + s2.
3321toUpperCase());
3322 System.out.println("String2 (lowercase) : " + s2.
3323toLowerCase());
3324 break;
3325 }
3326 System.out.print("Do you want to try another Operation(y=
33271/n=0) : ");
3328 ch = Integer.parseInt(dis.readLine());
3329 }
3330 while (ch == 1);
3331 }
3332 catch (Exception e)
3333 {
3334 System.out.println("ERROR : " + e);
3335 }
3336 }
3337}
3338
3339
3340
3341
3342JAVA LAB
3343
3344
3345
3346
3347
334863
3349
3350
3351
3352OUTPUT
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365JAVA LAB
3366
3367
3368
3369
3370
337164
3372
3373
3374
3375Program #32
3376
3377Program to reverse a string.
3378
3379
3380
3381SOURCE CODE
3382
3383
3384
3385import java.util.*;
3386
3387class reverstr
3388{
3389 public static void main(String args[])
3390 {
3391 String str,rev="";
3392 Scanner dis = new Scanner(System.in);
3393 System.out.println("\nMCA325\n");
3394
3395 System.out.print("Enter a string : ");
3396 str=dis.nextLine();
3397
3398 int n=str.length();
3399
3400 for(int i=n-1;i>=0;i--)
3401 rev=rev+str.charAt(i);
3402
3403 System.out.println("Reverse of the string : " +rev);
3404 }
3405}
3406
3407
3408
3409OUTPUT
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424JAVA LAB
3425
3426
3427
3428
3429
343065
3431
3432
3433
3434Program #33
3435Program to calculate the prime factors of a given number, using packages.
3436
3437
3438
3439SOURCE CODE
3440//PACKAGE
3441package PRIME1;
3442
3443import java.lang.Math;
3444
3445public class prime_check
3446{
3447 public void factors(int n)
3448 {
3449 System.out.print("\nFactors : ");
3450 for (int i = 2; i <= n; i++)
3451 {
3452 while (n % i == 0)
3453 {
3454 System.out.print(i + " ");
3455 n = n / i;
3456 }
3457 }
3458 if (n > 2)
3459 System.out.print(n);
3460 }
3461}
3462// DRIVER PROGRAM
3463import PRIME1.*;
3464import java.io.*;
3465
3466public class prime1 extends prime_check
3467{
3468 public static void main(String args[])
3469 {
3470 int num;
3471 System.out.println("\nMCA325\n");
3472 prime_check obj1 = new prime_check();
3473 DataInputStream dis = new DataInputStream(System.in);
3474 try
3475 {
3476 System.out.println("............PRIME FACTORS...........");
3477
3478
3479JAVA LAB
3480
3481
3482
3483
3484
348566
3486
3487
3488
3489 System.out.print("Enter the number : ");
3490 num = Integer.parseInt(dis.readLine());
3491
3492 obj1.factors(num);
3493 }
3494 catch(Exception e)
3495 { System.out.println("Error : " +e); }
3496 }
3497}
3498
3499
3500
3501OUTPUT
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544JAVA LAB
3545
3546
3547
3548
3549
355067
3551
3552
3553
3554Program #34
3555Program to calculate the value of nPr for given value of n & r, using packages.
3556
3557
3558
3559SOURCE CODE
3560//PACKAGE
3561package NPR_CALC;
3562
3563import java.lang.Math;
3564
3565public class check
3566{
3567 public int fact(int n)
3568 {
3569 if (n <= 1)
3570 return 1;
3571 else
3572 return n * fact(n - 1);
3573 }
3574
3575 public int nPr(int n, int r)
3576 {
3577 return fact(n) / fact(n - r);
3578 }
3579}
3580// DRIVER PROGRAM
3581import NPR_CALC.*;
3582import java.io.*;
3583
3584public class permutation1 extends check
3585{
3586 public static void main(String args[])
3587 {
3588 System.out.println("\nMCA325\n");
3589 check p = new check();
3590 DataInputStream dis = new DataInputStream(System.in);
3591 try
3592 {
3593 System.out.println("............PERMUTATION...........");
3594 System.out.print("Enter the value of n : ");
3595 int n = Integer.parseInt(dis.readLine());
3596 System.out.print("Enter the value of r : ");
3597
3598
3599JAVA LAB
3600
3601
3602
3603
3604
360568
3606
3607
3608
3609 int r = Integer.parseInt(dis.readLine());
3610 if (n >= r)
3611 System.out.println("\nThe value of " + n + "P" + r + " :
3612" + p.nPr(n, r));
3613 else
3614 System.out.println("\nn value should be greater than or e
3615quals to r value!!!");
3616 }
3617 catch (Exception e)
3618 {
3619 System.out.println("Error : " + e);
3620 }
3621 }
3622}
3623
3624
3625
3626OUTPUT
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659JAVA LAB
3660
3661
3662
3663
3664
366569
3666
3667
3668
3669Program #35
3670Program to read numbers into an array. Perform validations using multiple catch
3671statements / predefined Exceptions.
3672
3673
3674
3675
3676
3677SOURCE CODE
3678
3679
3680
3681import java.io.*;
3682
3683class multycatch_array2
3684{
3685 public static void main(String args[])
3686 {
3687 System.out.println("\nMCA325\n");
3688 DataInputStream dis = new DataInputStream(System.in);
3689 try
3690 {
3691 System.out.print("Enter the limit : ");
3692 int n = Integer.parseInt(dis.readLine());
3693 int a[] = new int[n];
3694
3695 System.out.print("Enter the array elements : ");
3696 for(int i=0;i<n;i++)
3697 a[i] = Integer.parseInt(dis.readLine());
3698
3699 System.out.println("Element of a[3] : " +a[3]);
3700 System.out.println("Element of a[n+10] : " +a[n+10]);
3701 }
3702 catch(ArrayIndexOutOfBoundsException e)
3703 {
3704 System.out.println("Error occured at first catch block");
3705 System.out.println(e);
3706 }
3707 catch(ArithmeticException e)
3708 {
3709 System.out.println("Error occured at second catch block");
3710 System.out.println(e);
3711 }
3712 catch(NullPointerException e)
3713 {
3714 System.out.println("Error occured at third catch block");
3715 System.out.println(e);
3716 }
3717 catch(IOException e)
3718
3719
3720JAVA LAB
3721
3722
3723
3724
3725
372670
3727
3728
3729
3730 {
3731 System.out.println("Error occured at fourth catch block");
3732 System.out.println(e);
3733 }
3734 }
3735}
3736
3737
3738
3739OUTPUT
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782JAVA LAB
3783
3784
3785
3786
3787
378871
3789
3790
3791
3792Program #36
3793Program to implement a user defined Exception, which will throw an Exception
3794when a given number is prime.
3795
3796
3797
3798SOURCE CODE
3799
3800
3801
3802import java.io.*;
3803
3804class throw_own_prime
3805{
3806 public static void main(String args[]) throws IOException
3807 {
3808 try
3809 {
3810 int flag=0;
3811 System.out.println("\nMCA325\n");
3812
3813 DataInputStream dis = new DataInputStream(System.in);
3814 System.out.print("Entered a number : ");
3815 int n = Integer.parseInt(dis.readLine());
3816
3817 for(int i=2;i<=n/2;++i)
3818 {
3819 if (n%i==0)
3820 {
3821 flag=1;
3822 break;
3823 }
3824 }
3825 if(flag==0) throw new MyException("......The number is prime.
3826.....");
3827 }
3828 catch(MyException e)
3829 {
3830 System.out.println("Caught MyException!!");
3831 System.out.println(e);
3832 }
3833 }
3834}
3835
3836class MyException extends Exception
3837{
3838 MyException(String message)
3839 {
3840
3841
3842JAVA LAB
3843
3844
3845
3846
3847
384872
3849
3850
3851
3852 super(message);
3853 }
3854}
3855
3856
3857
3858OUTPUT
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915JAVA LAB
3916
3917
3918
3919
3920
392173
3922
3923
3924
3925Program #37
3926Program to implement throw and finally.
3927
3928
3929
3930SOURCE CODE
3931
3932
3933
3934import java.io.*;
3935class throw_finally
3936{
3937 public static void main(String args[])
3938 {
3939 try
3940 {
3941 DataInputStream dis = new DataInputStream(System.in);
3942 System.out.println("\nMCA325\n");
3943 System.out.print("Entered a number : ");
3944 int n = Integer.parseInt(dis.readLine());
3945 if(n%2==0)
3946 {
3947 throw new ArrayIndexOutOfBoundsException();
3948 }
3949 }
3950 catch(ArrayIndexOutOfBoundsException e)
3951 {
3952 System.out.print("Entered n4umber is even!!" +e);
3953 }
3954 catch(Exception e)
3955 { System.out.print("Error"); }
3956 finally
3957 { System.out.println("\nFINALLY Code executed"); }
3958 }
3959}
3960
3961
3962
3963OUTPUT
3964
3965
3966
3967
3968
3969
3970
3971
3972JAVA LAB
3973
3974
3975
3976
3977
397874
3979
3980
3981
3982Program #38
3983Program to create multiple threads by extending the Thread class.
3984
3985
3986
3987SOURCE CODE
3988
3989
3990
3991class threaddemo extends Thread
3992{
3993 private String threadname;
3994 threaddemo(String name)
3995 {
3996 threadname=name;
3997 System.out.println("The creating thread is : "+threadname);
3998 }
3999 public void run()
4000 {
4001 System.out.println("The running thread is : "+threadname);
4002 try
4003 {
4004 for(int n=3;n>0;n--)
4005 {
4006 System.out.println("Thread: "+threadname+" "+n);
4007 Thread.sleep(1000);
4008 }
4009 }
4010 catch(InterruptedException e)
4011 {
4012 System.out.println(e);
4013 }
4014 }
4015}
4016class threads_extend_thrdcls
4017{
4018 public static void main(String args[])
4019 {
4020 System.out.println("\nMCA325\n");
4021 threaddemo t1=new threaddemo("A");
4022 t1.start();
4023 threaddemo t2=new threaddemo("B");
4024 t2.start();
4025 threaddemo t3=new threaddemo("c");
4026 t3.start();
4027 }
4028}
4029
4030
4031
4032
4033JAVA LAB
4034
4035
4036
4037
4038
403975
4040
4041
4042
4043OUTPUT
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064JAVA LAB
4065
4066
4067
4068
4069
407076
4071
4072
4073
4074Program #39
4075Program to implement threads by implementing the Runnable interface.
4076
4077
4078
4079SOURCE CODE
4080
4081
4082
4083class thread1 implements Runnable
4084{
4085 public void run()
4086 {
4087 System.out.println("Thread is running");
4088 }
4089}
4090public class thread_implements_runnable
4091{
4092 public static void main(String args[])
4093 {
4094 System.out.println("\nMCA325\n");
4095 Thread t1=new Thread();
4096 Thread t2=new Thread(t1);
4097 t2.start();
4098 t2.setName("hello there thread");
4099 t2.setPriority(4);
4100 System.out.println("Thread priority : " +t2.getPriority());
4101 System.out.println("Thread name : " +t2.getName());
4102 System.out.println("Thread Alive : " +t2.isAlive());
4103 }
4104}
4105
4106
4107
4108OUTPUT
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119JAVA LAB
4120
4121
4122
4123
4124
412577
4126
4127
4128
4129Program #40
4130Program to implement Synchronization using inter-thread communication.
4131
4132
4133
4134SOURCE CODE
4135
4136
4137
4138class first //normal class
4139{
4140 public void display(String msg)
4141 {
4142 System.out.print(" [ " +msg);
4143 try
4144 {
4145 Thread.sleep(1000);
4146 }
4147 catch(InterruptedException e)
4148 {
4149 System.out.println(e);
4150 }
4151 System.out.println(" ] ");
4152 }
4153}
4154
4155class second extends Thread //thread class
4156{
4157 first fobj;
4158 String msg;
4159 second(first f1, String str)
4160 {
4161 fobj=f1;
4162 msg=str;
4163 }
4164 public void run()
4165 {
4166 synchronized(fobj) //synchronized block
4167 {
4168 fobj.display(msg); //synchronized statement
4169 }
4170 }
4171}
4172public class synchro_inter_thread
4173{
4174 public static void main(String args[])
4175 {
4176 System.out.println("\nMCA325\n");
4177
4178
4179JAVA LAB
4180
4181
4182
4183
4184
418578
4186
4187
4188
4189
4190 first f = new first();
4191 second s1 = new second(f, " I");
4192 second s2 = new second(f, "Love");
4193 second s3 = new second(f, "Java");
4194
4195 s1.start();
4196 s2.start();
4197 s3.start();
4198 }
4199}
4200
4201
4202
4203OUTPUT
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218JAVA LAB
4219
4220
4221
4222
4223
422479
4225
4226
4227
4228Program #41
4229Program to Implement the Producer - Consumer Problem, using Threads.
4230
4231
4232
4233SOURCE CODE
4234
4235
4236
4237class Q
4238{
4239 int n;
4240 boolean flag = false;
4241 synchronized int get()
4242 {
4243 while(flag==false)
4244 try
4245 {
4246 wait();
4247 Thread.sleep(1000);
4248 }
4249 catch (InterruptedException e ){}
4250 System.out.println("Consumed: " +n);
4251 flag=false;
4252 notify();
4253 return n;
4254 }
4255 synchronized void put(int n)
4256 {
4257 while(flag==true)
4258 try
4259 {
4260 wait();
4261 Thread.sleep(1000);
4262 }
4263 catch (InterruptedException e )
4264 {}
4265 this.n=n;
4266 flag=true;
4267 System.out.println("Produced: " +n);
4268 notify();
4269 }
4270}
4271
4272class producer extends Thread
4273{
4274 Q q ;
4275 producer ( Q q)
4276
4277
4278JAVA LAB
4279
4280
4281
4282
4283
428480
4285
4286
4287
4288 {
4289 this.q=q;
4290 }
4291 public void run()
4292 {
4293 for(int i=1;i<=3;i++)
4294 {
4295 q.put(i);
4296 }
4297 }
4298}
4299
4300class consumer extends Thread
4301{
4302 Q q ;
4303 consumer ( Q q)
4304 {
4305 this.q=q;
4306 }
4307 public void run()
4308 {
4309 for(int i=1;i<=3;i++)
4310 {
4311 q.get();
4312 }
4313 }
4314}
4315
4316public class producer_consumer
4317{
4318 public static void main(String args[])
4319 {
4320 System.out.println("\nMCA325\n");
4321 Q q = new Q();
4322 producer p = new producer(q);
4323 p.start();
4324 consumer c = new consumer(q);
4325 c.start();
4326 }
4327}
4328
4329
4330
4331JAVA LAB
4332
4333
4334
4335
4336
433781
4338
4339
4340
4341OUTPUT
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400JAVA LAB
4401
4402
4403
4404
4405
440682
4407
4408
4409
4410Program #42
4411Program to display the contents of a directory by displaying the subdirectory’s name
4412first, then the file names.
4413
4414
4415
4416SOURCE CODE
4417
4418
4419
4420import java.io.*;
4421
4422public class dir_list_subdir_subfile
4423{
4424 public static void main(String args[])
4425 {
4426 try
4427 {
4428 String path;
4429 System.out.println("\nMCA325\n");
4430 DataInputStream dis = new DataInputStream(System.in);
4431 System.out.print("Enter the path : ");
4432 path = dis.readLine();
4433
4434 File dir = new File(path);
4435 String a[] = dir.list();
4436
4437 if(dir.exists() && dir.isDirectory())
4438 {
4439 System.out.println("\nSUB DIRECTORIES.........");
4440 for(int i=0;i<a.length;i++)
4441 {
4442 dir = new File(path.concat(a[i]));
4443 if(dir.isDirectory())
4444 System.out.println("\t" +a[i]);
4445 }
4446 System.out.println("\nFILES...................");
4447 for(int i=0;i<a.length;i++)
4448 {
4449 dir = new File(path.concat(a[i]));
4450 if(dir.isFile())
4451 System.out.println("\t" +a[i]);
4452 }
4453 }
4454 else
4455 System.out.println("Directory not exists!!");
4456 }
4457 catch(Exception e)
4458
4459
4460JAVA LAB
4461
4462
4463
4464
4465
446683
4467
4468
4469
4470 {
4471 System.out.println(e);
4472 }
4473 }
4474}
4475
4476
4477
4478OUTPUT
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515JAVA LAB
4516
4517
4518
4519
4520
452184
4522
4523
4524
4525Program #43
4526Program to display the contents of a directory including its subdirectory content
4527using recursive function.
4528
4529
4530
4531SOURCE CODE
4532
4533
4534
4535import java.io.*;
4536import java.util.Scanner;
4537class dir_list
4538{
4539 static int depth = 0;
4540 public void explore_dir(File folder)
4541 {
4542 depth++;
4543 File [] fileList = folder.listFiles();
4544 for(File file : fileList)
4545 {
4546 for(int d = 0; d < depth-1; d++)
4547 System.out.print("\t"); //apply indent
4548
4549 if(file.isFile())
4550 {
4551 System.out.println(file.getName());
4552 }
4553 else
4554 {
4555 System.out.println("[" + file.getName() + "]");
4556 explore_dir(file);
4557 }
4558 }
4559 depth--;
4560 }
4561}
4562
4563class recusive_dir_list
4564{
4565 public static void main(String[] args)
4566 {
4567 System.out.println("\nMCA325\n");
4568
4569 System.out.print("Enter the path :");
4570 Scanner s = new Scanner(System.in);
4571 String path = s.nextLine();
4572
4573
4574
4575JAVA LAB
4576
4577
4578
4579
4580
458185
4582
4583
4584
4585 System.out.println("Listing...\n");
4586
4587 File file = new File(path);
4588 s.close();
4589 dir_list obj = new dir_list();
4590 obj.explore_dir(file);
4591 }
4592}
4593
4594
4595
4596OUTPUT
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611JAVA LAB
4612
4613
4614
4615
4616
461786
4618
4619
4620
4621Program #44
4622Program to search for a given file name in a directory (including its
4623subdirectory) content.
4624
4625
4626
4627SOURCE CODE
4628
4629
4630
4631import java.io.*;
4632
4633public class file_exists
4634{
4635 static int flag = 0;
4636
4637 static void sub(File arr, String name)
4638 {
4639 String str;
4640 if (arr.listFiles() != null)
4641 {
4642 for (File f : arr.listFiles())
4643 {
4644 if (f.isFile())
4645 {
4646 str = f.getName();
4647 if (str.equals(name))
4648 {
4649 System.out.println("\nFile found!!!");
4650 System.out.println("file path : " + f.getPath());
4651 flag = 1;
4652 return;
4653 }
4654 }
4655 else if (f.isDirectory())
4656 {
4657 sub(f, name);
4658 }
4659 }
4660 }
4661 }
4662
4663 public static void main(String[] args) throws IOException
4664 {
4665 String filename, dir;
4666 System.out.println("\nMCA325\n");
4667 DataInputStream dis = new DataInputStream(System.in);
4668 System.out.print("Enter the directory name : ");
4669
4670
4671JAVA LAB
4672
4673
4674
4675
4676
467787
4678
4679
4680
4681 dir = dis.readLine();
4682 File file = new File(dir);
4683 if (file.exists() && file.isDirectory())
4684 {
4685 System.out.print("Enter the file name which is needed to sear
4686ch : ");
4687 filename = dis.readLine();
4688 sub(file, filename);
4689 if (flag < 1)
4690 System.out.println("\nFile not found!!!");
4691 }
4692 else
4693 System.out.println("Directory not exists!!");
4694 }
4695}
4696
4697
4698
4699OUTPUT
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714JAVA LAB
4715
4716
4717
4718
4719
472088
4721
4722
4723
4724Program #45
4725Menu driven program to demonstrate Random Access File handling, with options
4726for creating, deleting, writing, appending and reading the file.
4727
4728
4729
4730SOURCE CODE
4731
4732
4733
4734import java.io.*;
4735import java.util.*;
4736
4737class RAF_options
4738{
4739 public static void main(String args[]) throws IOException
4740 {
4741 System.out.println("\nMCA325\n");
4742 Scanner sc = new Scanner(System.in);
4743 char c,c1;
4744 do
4745 {
4746 System.out.println("\n...........CHOICE MENU...........");
4747 System.out.println("1. Create a file \n2. Delete a file ");
4748 System.out.println("3. Write into file \n4. Read from file");
4749 System.out.println("5. Append at a given position");
4750 System.out.print("Enter your choice : ");
4751 int choice = Integer.parseInt(sc.nextLine());
4752
4753 switch(choice)
4754 {
4755 case 1 :
4756 System.out.print("\nEnter the filename : ");
4757 String filename1 = sc.nextLine();
4758
4759 File file1 = new File(filename1);
4760 if(file1.exists())
4761 {
4762 System.out.println("File already exits");
4763 }
4764 else
4765 {
4766 RandomAccessFile f1 = new RandomAccessFile(filename1,
4767"rw");
4768 System.out.println("File created successfully!!!");
4769 }
4770 break;
4771
4772
4773
4774JAVA LAB
4775
4776
4777
4778
4779
478089
4781
4782
4783
4784 case 2 :
4785 System.out.print("\nEnter the file name : ");
4786 String filename2 = sc.nextLine();
4787
4788 File file2 = new File(filename2);
4789 if(file2.exists())
4790 {
4791 if(file2.delete())
4792 System.out.println("File deleted successfully");
4793 }
4794 else
4795 {
4796 System.out.println("File does not exist!!");
4797 }
4798 break;
4799
4800 case 3 :
4801 System.out.print("\nEnter the file name : ");
4802 String filename3 = sc.nextLine();
4803 RandomAccessFile f3 = new RandomAccessFile(filename3,"rw"
4804);
4805 do
4806 {
4807 String s1 = "\n";
4808 f3.write(s1.getBytes());
4809 System.out.print("\nEnter the data : ");
4810 String data = sc.nextLine();
4811 f3.write(data.getBytes());
4812 System.out.print("Do you want to write more : ");
4813 String ch1 = sc.nextLine();
4814 c1=ch1.charAt(0);
4815 }while(c1=='y');
4816 System.out.println("The data is written successfully");
4817 f3.close();
4818 break;
4819
4820 case 4 :
4821 System.out.print("\nEnter the file name : ");
4822 String filename4 = sc.nextLine();
4823 File f4 = new File(filename4);
4824 if(f4.exists())
4825 {
4826 RandomAccessFile file4 = new RandomAccessFile(filenam
4827e4,"rw");
4828 file4.seek(0);
4829
4830
4831JAVA LAB
4832
4833
4834
4835
4836
483790
4838
4839
4840
4841 byte[] bytes = new byte[100];
4842 file4.read(bytes);
4843 System.out.println(new String(bytes));
4844 file4.close();
4845 }
4846 else
4847 {
4848 System.out.println("File does not exist!!");
4849 }
4850 break;
4851
4852 case 5 :
4853 System.out.print("\nEnter the file name : ");
4854 String filename5 = sc.nextLine();
4855
4856 File f5 = new File(filename5);
4857 if(f5.exists())
4858 {
4859 RandomAccessFile file5 = new RandomAccessFile(filenam
4860e5,"rw");
4861 file5.seek(file5.length());
4862 System.out.print("\nEnter the data to append : ");
4863 String s = sc.nextLine();
4864 file5.write(s.getBytes());
4865 System.out.println("Data is successfully appended");
4866 file5.close();
4867 }
4868 else
4869 {
4870 System.out.println("File does not exist!!");
4871 }
4872 break;
4873
4874 default:
4875 System.out.println("Enter valid choice!!!");
4876 break;
4877 }
4878 System.out.print("\nDo you wish to continue (y/n): ");
4879 String ch = sc.nextLine();
4880 c=ch.charAt(0);
4881 }while(c=='y');
4882 sc.close();
4883 }
4884}
4885
4886
4887
4888
4889JAVA LAB
4890
4891
4892
4893
4894
489591
4896
4897
4898
4899OUTPUT
4900
4901
4902
4903
4904
4905
4906JAVA LAB
4907
4908
4909
4910
4911
491292
4913
4914
4915
4916Program #46
4917Program to implement a Generic method, which can display the elements of various
4918arrays of different data types, and find the length of each array.
4919
4920
4921
4922SOURCE CODE
4923
4924
4925
4926class generic_mthd_array
4927{
4928 public static <E> void printArray(E[] array) //generic method decla
4929ration
4930 {
4931 int i=0;
4932 for(E element : array)
4933 {
4934 System.out.print(element+ " ");
4935 i++;
4936 }
4937 System.out.println("\nLength of array : " +i);
4938 }
4939
4940 public static void main(String args[])
4941 {
4942 System.out.println("\nMCA325\n");
4943
4944 Integer[] intArray = {1,2,3,4,5};
4945 Double[] dbArray = {-1.1,2.2,4.4,5.5};
4946 Float[] ftArray = {1.3f,3.0f,5.0f,8.0f,-15.3f,-2.22f};
4947 Character[] charArray = {'A','B','C'};
4948 String[] strArray = {"Mathew","Reji"};
4949
4950 System.out.print("Integer elements : ");
4951 printArray(intArray); //pass a integer array
4952
4953 System.out.print("\nDouble elements : ");
4954 printArray(dbArray); //pass a double array
4955
4956 System.out.print("\nFloat elements : ");
4957 printArray(ftArray); //pass a float array
4958
4959 System.out.print("\nCharacter elements : ");
4960 printArray(charArray); //pass a character array
4961
4962 System.out.print("\nString elements : ");
4963 printArray(strArray); //pass a string array
4964
4965
4966JAVA LAB
4967
4968
4969
4970
4971
497293
4973
4974
4975
4976 }
4977}
4978
4979
4980
4981OUTPUT
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026JAVA LAB
5027
5028
5029
5030
5031
503294
5033
5034
5035
5036Program #47
5037Program to implement a Generic class (with constructors), and display the types of
5038various parameters passed.
5039
5040
5041
5042SOURCE CODE
5043
5044
5045
5046class generic_constructor <T1,T2>
5047{
5048 T1 obj1;
5049 T2 obj2;
5050
5051 generic_constructor(T1 o1,T2 o2)
5052 {
5053 this.obj1 = o1;
5054 this.obj2 = o2;
5055 System.out.println("\nValue of obj1 : " +obj1);
5056 System.out.println("Value of obj2 : " +obj2);
5057 }
5058
5059 public void disp()
5060 {
5061 System.out.println("\nType of " +obj1+ " is " +obj1.getClass());
5062 System.out.println("Type of " +obj2+ " is " +obj2.getClass());
5063 }
5064
5065 public static void main(String args[])
5066 {
5067 System.out.println("\nMCA325");
5068
5069 generic_constructor<String, Integer> ob1 = new generic_constructo
5070r<String, Integer> ("hello", 123);
5071 generic_constructor <String, Double> ob2 = new generic_constructo
5072r<String, Double> ("world", 123.123);
5073
5074 ob1.disp(); //string,int
5075 ob2.disp(); //string,double
5076 }
5077}
5078
5079
5080
5081
5082
5083
5084JAVA LAB
5085
5086
5087
5088
5089
509095
5091
5092
5093
5094OUTPUT
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153JAVA LAB
5154
5155
5156
5157
5158
515996
5160
5161
5162
5163Program #48
5164Program to compare the averages of various arrays, using bounded type parameters.
5165
5166
5167
5168SOURCE CODE
5169
5170
5171
5172public class generic_bounded_avg
5173{
5174 public static <E extends Number> double avgArray(E[] array)
5175 {
5176 double sum=0,avg;
5177 for(E element : array)
5178 {
5179 System.out.print(element+" ");
5180 sum = sum+element.doubleValue();
5181 }
5182 avg=sum/6;
5183 return avg;
5184 }
5185
5186 public static void cmpArray(double a1,double a2,double a3)
5187 {
5188 if((a1>a2)&&(a1>a3))
5189 System.out.println("\nInteger array is greatest");
5190 else
5191 {
5192 if(a2>a3)
5193 System.out.println("\nFloat array is greatest");
5194 else
5195 System.out.println("\nDouble array is greatest");
5196 }
5197 }
5198 public static void main(String args[])
5199 {
5200 System.out.println("\nMCA325\n");
5201
5202 Integer[] intArray = {1,2,3};
5203 Double[] dbArray = {12.34,5.678};
5204 Float[] fltArray = {1.2f,2.0f,3.0f};
5205 System.out.print("Integer elements : ");
5206 double a1 = avgArray(intArray);
5207 System.out.println("\nAverage value of integer elements : "+a1);
5208
5209 System.out.print("\nFloat elements : ");
5210 double a2 = avgArray(fltArray);
5211
5212
5213JAVA LAB
5214
5215
5216
5217
5218
521997
5220
5221
5222
5223 System.out.println("\nAverage value of float elements : " +a2);
5224
5225 System.out.print("\nDouble elements : ");
5226 double a3 = avgArray(dbArray);
5227 System.out.println("\nAverage value of double elements : " +a3);
5228
5229 cmpArray(a1,a2,a3);
5230 }
5231}
5232
5233
5234
5235OUTPUT
5236
5237
5238
5239
5240
5241
5242
5243
5244JAVA LAB
5245
5246
5247
5248
5249
525098
5251
5252
5253
5254Program #49
5255Program to implement Serialization and De-Serialization, for an object of Student
5256class.
5257
5258
5259
5260SOURCE CODE
5261// STUDENT DETAILS
5262import java.io.*;
5263public class student implements Serializable
5264{
5265 public String name;
5266 public String address;
5267 public String course;
5268 public String sid;
5269 public int age;
5270 public void getdata()
5271 {
5272 DataInputStream d = new DataInputStream(System.in);
5273 try
5274 {
5275 System.out.print("Enter Student name : ");
5276 name = d.readLine();
5277 System.out.print("Enter Student id : ");
5278 sid = d.readLine();
5279 System.out.print("Enter address : ");
5280 address = d.readLine();
5281 System.out.print("Enter Course : ");
5282 course = d.readLine();
5283 System.out.print("Enter Student age : ");
5284 age = Integer.parseInt(d.readLine());
5285 }
5286 catch(Exception e){ System.out.println(e);}
5287 }
5288 public void disp()
5289 {
5290 System.out.println("...........STUDENT DETAILS...........");
5291 System.out.println("Name : "+name);
5292 System.out.println("ID : "+sid);
5293 System.out.println("Address : "+address);
5294 System.out.println("Course : "+course);
5295 System.out.println("Age : "+age);
5296 }
5297}
5298
5299
5300JAVA LAB
5301
5302
5303
5304
5305
530699
5307
5308
5309
5310//SERIALIZATION AND DESERIALIZATION
5311import java.io.*;
5312public class ser_student
5313{
5314 public static void main(String args[])
5315 {
5316 student s=new student();
5317 System.out.println("\nMCA325");
5318 System.out.println("---Implementation of serialization and
5319Deserializaton--");
5320 s.getdata();
5321
5322 try
5323 {
5324 FileOutputStream fout = new
5325FileOutputStream("stud.txt");
5326 ObjectOutputStream out = new ObjectOutputStream(fout);
5327 out.writeObject(s);
5328 out.close();
5329 fout.close();
5330 System.out.println("\nSerialization happend!!\n");
5331
5332 FileInputStream fin = new FileInputStream("stud.txt");
5333 ObjectInputStream in = new ObjectInputStream(fin);
5334 s=(student) in.readObject();
5335 s.disp();
5336 in.close();
5337 fin.close();
5338 System.out.println("\nDeserializaton happended");
5339 }
5340 catch(IOException e)
5341 {
5342 System.out.println(e);
5343 }
5344 catch(ClassNotFoundException e1)
5345 {
5346 System.out.println(e1);
5347 }
5348 }
5349}
5350
5351
5352
5353JAVA LAB
5354
5355
5356
5357
5358
5359100
5360
5361
5362
5363OUTPUT
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382JAVA LAB
5383
5384
5385
5386
5387
5388101
5389
5390
5391
5392Program #50
5393Program to implement IS-A Serialization and De-Serialization, for a Maruti Car
5394inherited from Vehicle.
5395
5396
5397
5398SOURCE CODE
5399//VEHICLE DETAILS
5400import java.io.*;
5401public class vehicle implements Serializable
5402{
5403 public String type;
5404 public String vno;
5405 public String color;
5406
5407 public void getdata()
5408 {
5409 DataInputStream d=new DataInputStream(System.in);
5410 try
5411 {
5412 System.out.print("Enter vehicle type : ");
5413 type = d.readLine();
5414 System.out.print("Enter vehicle number : ");
5415 vno = d.readLine();
5416 System.out.print("Enter vehicle color : ");
5417 color = d.readLine();
5418 }
5419 catch(Exception e)
5420 {
5421 System.out.println(e);
5422 }
5423 }
5424 public void disp()
5425 {
5426 System.out.println("............VEHICLE DETAILS...........");
5427 System.out.println("Type : "+type);
5428 System.out.println("Vehicle No : "+vno);
5429 System.out.println("Color : "+color);
5430 }
5431}
5432
5433
5434
5435
5436
5437
5438JAVA LAB
5439
5440
5441
5442
5443
5444102
5445
5446
5447
5448//MARUTI DETAILS
5449import java.io.*;
5450public class maruti extends vehicle
5451{
5452 public String model;
5453 public int price;
5454 public String fueltype;
5455 public int seatcap;
5456
5457 public void getdata()
5458 {
5459 super.getdata();
5460 DataInputStream d=new DataInputStream(System.in);
5461 try
5462 {
5463 System.out.print("Enter vehicle model : ");
5464 model = d.readLine();
5465 System.out.print("Enter vehicle price : ");
5466 price = Integer.parseInt(d.readLine());
5467 System.out.print("Enter vehicle fueltype : ");
5468 fueltype = d.readLine();
5469 System.out.print("Enter vehicle seat capacity : ");
5470 seatcap = Integer.parseInt(d.readLine());
5471 }
5472 catch(Exception e)
5473 {
5474 System.out.println(e);
5475 }
5476 }
5477
5478 public void disp()
5479 {
5480 super.disp();
5481 System.out.println("Model : "+model);
5482 System.out.println("Price : "+price);
5483 System.out.println("Fuel type : "+fueltype);
5484 System.out.println("Seating capacity : "+seatcap);
5485 }
5486}
5487
5488
5489
5490JAVA LAB
5491
5492
5493
5494
5495
5496103
5497
5498
5499
5500//IS-A SERIALIZATION AND DESERIALIZATION
5501import java.io.*;
5502public class ISA_ser_vehicle
5503{
5504 public static void main(String args[])
5505 {
5506 maruti m = new maruti();
5507 System.out.println("\nMCA325");
5508 System.out.println(".....IS-A Serialization and De-
5509Serialization…..");
5510 m.getdata();
5511
5512 try
5513 {
5514 FileOutputStream fout = new
5515FileOutputStream("vehicle.txt");
5516 ObjectOutputStream out = new ObjectOutputStream(fout);
5517 out.writeObject(m);
5518 out.close();
5519 fout.close();
5520 System.out.println("\nIS-A Serialization happend!!\n");
5521
5522 FileInputStream fin = new
5523FileInputStream("vehicle.txt");
5524 ObjectInputStream in = new ObjectInputStream(fin);
5525 m = (maruti)in.readObject();
5526 m.disp();
5527 in.close();
5528 fin.close();
5529 System.out.println("\nIS-A Deserialization
5530happend!!\n");
5531 }
5532 catch(IOException e)
5533 {
5534 System.out.println(e);
5535 }
5536 catch(ClassNotFoundException e1)
5537 {
5538 System.out.println(e1);
5539 }
5540 }
5541}
5542
5543
5544
5545
5546
5547
5548
5549
5550JAVA LAB
5551
5552
5553
5554
5555
5556104
5557
5558
5559
5560OUTPUT
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605JAVA LAB
5606
5607
5608
5609
5610
5611105
5612
5613
5614
5615Program #51
5616Program to implement HAS-A Serialization and De-Serialization for the Engine of a
5617Vehicle.
5618
5619
5620
5621SOURCE CODE
5622//VEHICLE DETAILS
5623import java.io.*;
5624public class vehicle implements Serializable
5625{
5626 public String type;
5627 public String vno;
5628 public String color;
5629
5630 public void getdata()
5631 {
5632 DataInputStream d=new DataInputStream(System.in);
5633 try
5634 {
5635 System.out.print("Enter vehicle type : ");
5636 type = d.readLine();
5637 System.out.print("Enter vehicle number : ");
5638 vno = d.readLine();
5639 System.out.print("Enter vehicle color : ");
5640 color = d.readLine();
5641 }
5642 catch(Exception e)
5643 {
5644 System.out.println(e);
5645 }
5646 }
5647
5648 public void disp()
5649 {
5650 System.out.println("............VEHICLE DETAILS...........");
5651 System.out.println("Type : "+type);
5652 System.out.println("Vehicle No : "+vno);
5653 System.out.println("Color : "+color);
5654 }
5655}
5656
5657
5658
5659JAVA LAB
5660
5661
5662
5663
5664
5665106
5666
5667
5668
5669//ENGINE DETAILS
5670import java.io.*;
5671public class engine extends vehicle
5672{
5673 vehicle v = new vehicle();
5674 public String status;
5675 public void getdata()
5676 {
5677 v.getdata();
5678 DataInputStream d = new DataInputStream(System.in);
5679 try
5680 {
5681 System.out.print("Enter vehicle status : ");
5682 status = d.readLine();
5683 }
5684 catch(Exception e)
5685 {
5686 System.out.println(e);
5687 }
5688 }
5689 public void disp()
5690 {
5691 v.disp();
5692 System.out.println("Engine status : "+status);
5693 }
5694}
5695
5696
5697
5698
5699
5700
5701JAVA LAB
5702
5703
5704
5705
5706
5707107
5708
5709
5710
5711// HAS-A SERIALIZATION AND DESERIALIZATION
5712import java.io.*;
5713public class HASA_ser_vehicle
5714{
5715 public static void main(String args[])
5716 {
5717 engine e = new engine();
5718 System.out.println("\nMCA325");
5719 System.out.println("…….HAS-A Serialization and De-
5720Serialization…….");
5721 e.getdata();
5722 try
5723 {
5724 FileOutputStream fout = new
5725FileOutputStream("engine.txt");
5726 ObjectOutputStream out = new ObjectOutputStream(fout);
5727 out.writeObject(e);
5728 out.close();
5729 fout.close();
5730 System.out.println("\nHAS-A Serialization
5731happend!!\n");
5732
5733 FileInputStream fin = new
5734FileInputStream("engine.txt");
5735 ObjectInputStream in = new ObjectInputStream(fin);
5736 e = (engine)in.readObject();
5737 e.disp();
5738 in.close();
5739 fin.close();
5740 System.out.println("\nHAS-A Deserialization
5741happend!!");
5742 }
5743 catch(IOException i)
5744 {
5745 System.out.println(i);
5746 }
5747 catch(ClassNotFoundException i1)
5748 {
5749 System.out.println(i1);
5750 }
5751 }
5752}
5753
5754
5755
5756
5757
5758
5759JAVA LAB
5760
5761
5762
5763
5764
5765108
5766
5767
5768
5769OUTPUT
5770
5771
5772
5773
5774
5775
5776
5777
5778JAVA LAB
5779
5780
5781
5782
5783
5784109
5785
5786
5787
5788Program #52
5789Program to Serialize/De-Serialize selected attributes of an Employee.
5790
5791
5792
5793SOURCE CODE
5794//EMPLOYEE DETAILS
5795import java.io.*;
5796public class employee implements Serializable
5797{
5798 public String empid;
5799 public String name;
5800 public String address;
5801 public transient int age;
5802 public void getdata()
5803 {
5804 DataInputStream d=new DataInputStream(System.in);
5805 try
5806 {
5807 System.out.print("Enter employee name : ");
5808 name = d.readLine();
5809 System.out.print("Enter employee id : ");
5810 empid = d.readLine();
5811 System.out.print("Enter employee address : ");
5812 address = d.readLine();
5813 System.out.print("Enter employee age : ");
5814 age = Integer.parseInt(d.readLine());
5815 }
5816 catch(Exception e) { System.out.println(e); }
5817 }
5818 public void disp()
5819 {
5820 System.out.println("...........EMPLOYEE DETAILS...........");
5821 System.out.println("ID : " +empid);
5822 System.out.println("Name : " +name);
5823 System.out.println("Address : " +address);
5824 System.out.println("Age : " +age);
5825 }
5826}
5827
5828
5829
5830
5831
5832
5833JAVA LAB
5834
5835
5836
5837
5838
5839110
5840
5841
5842
5843//SERIALIZATION AND DESERIALIZATION
5844import java.io.*;
5845public class ser_employee
5846{
5847 public static void main(String[] args)
5848 {
5849 employee e = new employee();
5850 System.out.println("\nMCA325");
5851 System.out.println("\n......Implementation of transient
5852keyword......\n");
5853 e.getdata();
5854
5855 try
5856 {
5857 FileOutputStream fout = new
5858FileOutputStream("employee.txt");
5859 ObjectOutputStream out = new ObjectOutputStream(fout);
5860 out.writeObject(e);
5861 out.close();
5862 fout.close();
5863 System.out.println("\nSerializaton happened!!\n");
5864
5865 FileInputStream fin=new
5866FileInputStream("employee.txt");
5867 ObjectInputStream in= new ObjectInputStream(fin);
5868 e = (employee)in.readObject();
5869 e.disp();
5870 in.close();
5871 fin.close();
5872 System.out.println("\nDeserializaton happened!!");
5873 }
5874 catch(Exception e1)
5875 {
5876 System.out.println(e1);
5877 }
5878 }
5879}
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890JAVA LAB
5891
5892
5893
5894
5895
5896111
5897
5898
5899
5900OUTPUT
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953JAVA LAB
5954
5955
5956
5957
5958
5959112
5960
5961
5962
5963Program #53
5964Program to implement various methods of a StringBuffer class.
5965
5966
5967
5968SOURCE CODE
5969
5970
5971public class string_buffer
5972{
5973 public static void main(String[] args)
5974 {
5975 System.out.println("\nMCA325\n");
5976 StringBuffer s = new StringBuffer();
5977 System.out.println("String : " +s);
5978 System.out.println("Length of string buffer : "
5979+s.length());
5980 System.out.println("Default capacity of string buffer :
5981"+s.capacity());
5982
5983 s.append("Mathew Reji");
5984 System.out.println("\nAfter append() String : "+s);
5985 System.out.println("Length of string buffer : "
5986+s.length());
5987 System.out.println("capacity of string buffer : "
5988+s.capacity());
5989
5990 s.insert(3,493);
5991 System.out.println("\nAfter insert() String : "+s);
5992 System.out.println("Length of string buffer : "
5993+s.length());
5994 System.out.println("capacity of string buffer : "
5995+s.capacity());
5996 System.out.println("Reverse of string :
5997"+s.reverse());
5998
5999 s.reverse();
6000 s.delete(9,11);
6001 System.out.println("\nAfter delete() String : "+s);
6002 System.out.println("Length of string buffer : "
6003+s.length());
6004 System.out.println("capacity of string buffer : "
6005+s.capacity());
6006
6007 s.deleteCharAt(7);
6008 System.out.println("\nAfter deleteCharAt() String : "+s);
6009
6010
6011JAVA LAB
6012
6013
6014
6015
6016
6017113
6018
6019
6020
6021 System.out.println("Length of string buffer : "
6022+s.length());
6023 System.out.println("capacity of string buffer : "
6024+s.capacity());
6025
6026 s.replace(6,8,"***");
6027 System.out.println("\nAfter replace() String : "+s);
6028 System.out.println("Length of string buffer : "
6029+s.length());
6030 System.out.println("capacity of string buffer : "
6031+s.capacity());
6032 }
6033}
6034
6035
6036
6037OUTPUT
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048JAVA LAB
6049
6050
6051
6052
6053
6054114
6055
6056
6057
6058Program #54
6059Program to implement communication between a client and server client, via Socket
6060Programming.
6061
6062
6063
6064SOURCE CODE
6065//SERVER PROGRAM
6066import java.net.*;
6067import java.io.*;
6068public class sock_server
6069{
6070 public static void main(String[] args) throws Exception
6071 {
6072 ServerSocket ss = new ServerSocket(1221);
6073 System.out.println("\nMCA325\n");
6074 System.out.println("Waiting"); //1
6075
6076 Socket s = ss.accept();
6077 System.out.println("Connection made"); //3
6078
6079 ObjectInputStream in = new
6080ObjectInputStream(s.getInputStream());
6081 String str = (String)in.readUTF(); //5
6082 System.out.println("Message from alice : " +str);
6083 ss.close();
6084 }
6085}
6086//CLIENT PROGRAM
6087import java.net.*;
6088import java.io.*;
6089public class sock_client_keyboard
6090{
6091 public static void main(String[] args) throws Exception
6092 {
6093 Socket s = new Socket("localhost",1221);
6094 //created a client socket in address, port number
6095 System.out.println("\nMCA325\n");
6096 System.out.println("Alice waiting for a connection with
6097bob"); //2
6098
6099 //imagine bob accepts the connection, Alice will send data
6100
6101
6102JAVA LAB
6103
6104
6105
6106
6107
6108115
6109
6110
6111
6112 ObjectOutputStream out = new
6113ObjectOutputStream(s.getOutputStream());
6114
6115 DataInputStream dis = new DataInputStream(System.in);
6116 System.out.print("Enter the message : ");
6117 String str = dis.readLine();
6118 out.writeUTF(str); //4
6119
6120 out.close();
6121 s.close();
6122 }
6123}
6124
6125
6126
6127OUTPUT
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140JAVA LAB
6141
6142
6143
6144
6145
6146116
6147
6148
6149
6150Program #55
6151Program to implement one-one chatting using the TCP protocol.
6152
6153
6154
6155SOURCE CODE
6156//TCP SERVER PROGRAM
6157import java.net.*;
6158import java.io.*;
6159public class TCP_server_chat
6160{
6161 public static void main(String[] args) throws Exception
6162 {
6163 System.out.println("\nMCA325\n");
6164 ServerSocket ss=new ServerSocket(2);
6165 System.out.println("Waiting!!");
6166 Socket as=ss.accept();
6167 System.out.println("Connection made!!");
6168
6169 DataInputStream din=new DataInputStream(System.in);
6170 DataInputStream sin=new DataInputStream(as.getInputStream());
6171 DataOutputStream sout=new
6172DataOutputStream(as.getOutputStream());
6173 String str;
6174 sout.writeUTF("Welcome!!");
6175 while(true)
6176 {
6177 str=sin.readUTF();
6178 System.out.println("Alice says : "+str);
6179 if(str.equals("bye")) break;
6180 System.out.print("Enter data/bye for Alice : ");
6181 str=din.readLine();
6182 sout.writeUTF(str);
6183 if(str.equals("bye")) break;
6184 }
6185 sout.close();
6186 ss.close();
6187 }
6188}
6189
6190
6191
6192JAVA LAB
6193
6194
6195
6196
6197
6198117
6199
6200
6201
6202//TCP CLIENT PROGRAM
6203import java.net.*;
6204import java.io.*;
6205public class TCP_client_chat
6206{
6207 public static void main(String[] args) throws Exception
6208 {
6209 Socket cs=new Socket("localhost",2);
6210 System.out.println("\nMCA325\n");
6211 System.out.println("Alice waiting for connection with
6212Bob!!");
6213
6214 DataInputStream kin=new DataInputStream(System.in);//read
6215from KB
6216 DataInputStream sin=new DataInputStream(cs.getInputStream());
6217 DataOutputStream sout=new
6218DataOutputStream(cs.getOutputStream());
6219 String str;
6220 while(true)
6221 {
6222 str=sin.readUTF();
6223 if(str.equals("bye")) break;
6224 System.out.println("Bob says : "+str);
6225 System.out.print("Enter data for Bob : ");
6226 str=kin.readLine();
6227 sout.writeUTF(str);
6228 if(str.equals("bye"))
6229 break;
6230 }
6231 sout.close();
6232 cs.close();
6233 }
6234}
6235
6236
6237
6238OUTPUT
6239
6240
6241
6242
6243
6244
6245JAVA LAB
6246
6247
6248
6249
6250
6251118
6252
6253
6254
6255Program #56
6256Program to accept a file name given in the client, and to download the content from
6257the server side.
6258
6259
6260
6261SOURCE CODE
6262//SERVER PROGRAM
6263import java.net.*;
6264import java.io.*;
6265public class server_file_dwnld
6266{
6267 static int flag = 0;
6268 String fcontent;
6269 static byte[] bytes = new byte[100];
6270
6271 static void sub(File arr,String name)
6272 {
6273 String str;
6274 if(arr.listFiles() != null)
6275 {
6276 for(File f : arr.listFiles())
6277 {
6278 if(f.isFile())
6279 {
6280 str = f.getName();
6281 if(str.equals(name))
6282 {
6283 File f4 = new File(f.getPath());
6284 if(f4.exists())
6285 {
6286 try
6287 {
6288 RandomAccessFile file4=new
6289RandomAccessFile(f4,"rw");
6290 file4.seek(0);
6291
6292 file4.read(bytes);
6293 file4.close();
6294 flag=1;
6295 return;
6296 }
6297 catch (Exception e) {}
6298 }
6299 }
6300
6301
6302JAVA LAB
6303
6304
6305
6306
6307
6308119
6309
6310
6311
6312 }
6313 else if(f.isDirectory())
6314 {
6315 sub(f,name);
6316 }
6317 }
6318 }
6319 }
6320
6321 public static void main(String[] args) throws Exception
6322 {
6323 String filename;
6324 File file = new File("E:\\");
6325
6326 ServerSocket s2=new ServerSocket(23);
6327 System.out.println("\nMCA325\n");
6328 System.out.println("Waiting!!");
6329 Socket c2=s2.accept();
6330 System.out.println("Connection made!");
6331 DataInputStream sin2 =new
6332DataInputStream(c2.getInputStream());
6333 DataOutputStream sout2=new
6334DataOutputStream(c2.getOutputStream());
6335
6336 filename=(String)sin2.readUTF();
6337 if(file.exists() && file.isDirectory())
6338 {
6339 sub(file,filename);
6340 if(flag<1)
6341 sout2.writeUTF("\nFile not found!!!");
6342 else
6343 sout2.writeUTF(new String(bytes));
6344 }
6345 else
6346 sout2.writeUTF("Directory not exists!!");
6347
6348 s2.close();
6349 }
6350}
6351//CLIENT PROGRAM
6352import java.io.*;
6353import java.net.*;
6354public class client_file_dwnld
6355{
6356
6357
6358JAVA LAB
6359
6360
6361
6362
6363
6364120
6365
6366
6367
6368 public static void main(String[] args) throws Exception
6369 {
6370 Socket cs=new Socket("localhost",23);
6371 System.out.println("\nMCA325\n");
6372 System.out.println("waiting for connection!!");
6373
6374 DataInputStream kin =new DataInputStream(System.in);
6375 DataInputStream sin =new
6376DataInputStream(cs.getInputStream());
6377 DataOutputStream sout=new
6378DataOutputStream(cs.getOutputStream());
6379 String str,cstr;
6380
6381 System.out.print("Enter file name :" );
6382 cstr=kin.readLine ();
6383 sout.writeUTF(cstr);
6384 str=(String)sin.readUTF();
6385
6386 System.out.println("File content: \n"+str);
6387 cs.close();
6388 }
6389}
6390
6391
6392
6393OUTPUT
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404JAVA LAB
6405
6406
6407
6408
6409
6410121
6411
6412
6413
6414Program #57
6415Program to accept a number from the client, and print its factorial in the server.
6416
6417
6418
6419SOURCE CODE
6420//SOCK SERVER
6421import java.net.*;
6422import java.io.*;
6423
6424public class sock_server_factorial
6425{
6426 public static void main(String[] args) throws Exception
6427 {
6428 ServerSocket ss = new ServerSocket(12111);
6429 System.out.println("\nMCA325\n");
6430 Socket s = ss.accept();
6431
6432 ObjectInputStream in = new
6433ObjectInputStream(s.getInputStream());
6434 String str = (String)in.readUTF();
6435 int num = Integer.parseInt(str.trim());
6436
6437 int i,fact=1;
6438 for(i=1;i<=num;i++)
6439 {
6440 fact=fact*i;
6441 }
6442
6443 System.out.println("Factorial : " +fact);
6444 }
6445}
6446//SOCK CLIENT
6447import java.net.*;
6448import java.io.*;
6449
6450public class sock_client_factorial
6451{
6452 public static void main(String[] args) throws Exception
6453 {
6454 Socket s = new Socket("localhost",12111);
6455 System.out.println("\nMCA325\n");
6456 //created a client socket in address, port number
6457
6458
6459JAVA LAB
6460
6461
6462
6463
6464
6465122
6466
6467
6468
6469
6470 ObjectOutputStream out = new
6471ObjectOutputStream(s.getOutputStream());
6472 DataInputStream dis = new DataInputStream(System.in);
6473
6474 System.out.println("-----------FACTORIAL-----------");
6475 System.out.print("Enter the number : ");
6476 String str = dis.readLine();
6477 out.writeUTF(str);
6478
6479 out.close();
6480 s.close();
6481 }
6482}
6483
6484
6485
6486OUTPUT
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505JAVA LAB
6506
6507
6508
6509
6510
6511123
6512
6513
6514
6515Program #58
6516Program to implement connection less communication.
6517
6518
6519
6520SOURCE CODE
6521//UDP SERVER - RECEIVING DATA IN PACKET VIA SOCKET
6522import java.net.*;
6523public class UDP_serv
6524{
6525 public static void main(String[] args) throws Exception
6526 {
6527 DatagramSocket ds = new DatagramSocket(22255);
6528 System.out.println("\nMCA325\n");
6529
6530 byte[] b1 = new byte[1024];
6531 DatagramPacket dp = new DatagramPacket(b1,b1.length);
6532 ds.receive(dp);
6533
6534 String str = new String(dp.getData());
6535 int num = Integer.parseInt(str.trim());
6536
6537 System.out.println("Number(Server) : " +num);
6538 int result = num * num;
6539 System.out.println("Result(Server) : " +result);
6540 }
6541}
6542
6543
6544
6545JAVA LAB
6546
6547
6548
6549
6550
6551124
6552
6553
6554
6555//UDP CLIENT - SIMPLY GO ON SENDING DATA IN PACKETS THROUGH A
6556SOCKET
6557import java.net.*;
6558public class UDP_cli
6559{
6560 public static void main(String[] args) throws Exception
6561 {
6562 DatagramSocket ds = new DatagramSocket();
6563 int i = 12; //can send "i" only in a packet through ds
6564
6565 byte b[] = String .valueOf(i).getBytes();
6566 InetAddress ilh = InetAddress.getLocalHost();
6567 DatagramPacket dp = new DatagramPacket(b,b.length,ilh,22255);
6568 ds.send(dp);
6569 }
6570}
6571
6572
6573
6574OUTPUT
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597JAVA LAB
6598
6599
6600
6601
6602
6603125
6604
6605
6606
6607Program #59
6608Program to get protocol, file name, host, path and port of a given URL.
6609
6610
6611
6612SOURCE CODE
6613
6614
6615
6616import java.net.*;
6617import java.util.*;
6618
6619public class URLCon_demo
6620{
6621 public static void main(String[] args) throws Exception
6622 {
6623 URL url = new URL("https://www.google.com/");
6624 URLConnection urlc = url.openConnection();
6625 System.out.println("\nMCA325\n");
6626
6627 System.out.println("URL : " +urlc.getURL());
6628 System.out.println("User Interaction : "
6629+urlc.getAllowUserInteraction());
6630 System.out.println("Content type : "
6631+urlc.getContentType());
6632 System.out.println("Url do input :" +urlc.getDoInput());
6633//bool..default - true
6634 System.out.println("Url do output:"+urlc.getDoOutput());
6635//bool-default-false
6636 System.out.println("Last modified date:"+new
6637Date(urlc.getLastModified()));
6638 System.out.println("Content length : "
6639+urlc.getContentLength());
6640 System.out.println("Content encoding : "
6641+urlc.getContentEncoding());
6642 }
6643}
6644OUTPUT
6645
6646
6647
6648
6649JAVA LAB
6650
6651
6652
6653
6654
6655126
6656
6657
6658
6659
6660
6661
6662
6663Program #60
6664Program to download a file from a given URL.
6665
6666
6667
6668SOURCE CODE
6669
6670
6671
6672import java.net.*;
6673import java.io.*;
6674public class URL_download
6675{
6676 public static void main(String[] args) throws Exception
6677 {
6678 URL url = new
6679URL("http://africau.edu/images/default/sample.pdf");
6680 InputStream is = url.openStream();
6681 FileOutputStream os = new FileOutputStream("dwnld.pdf");
6682
6683 int length;
6684 byte b[] = new byte[2048];
6685 while((length = is.read(b))!=-1)
6686 {
6687 os.write(b,0,length); // 1->byte, 2->offset, 3->length
6688of byte
6689 }
6690 os.close();
6691 is.close();
6692 }
6693}
6694
6695
6696
6697OUTPUT
6698
6699
6700
6701
6702JAVA LAB
6703
6704
6705
6706
6707
6708127
6709
6710
6711
6712
6713
6714
6715
6716
6717JAVA LAB
6718
6719
6720
6721
6722
6723128
6724
6725
6726
6727Program #61
6728Program to implement Two- way Communication using UDP Protocol.
6729
6730
6731
6732SOURCE CODE
6733//SERVER PROGRAM
6734import java.net.*;
6735public class UDPserver_2way
6736{
6737 public static void main(String[] args) throws Exception
6738 {
6739 DatagramSocket ds = new DatagramSocket(29);
6740 System.out.println("\nMCA325\n");
6741
6742 //receiving
6743 byte[] b1 = new byte[1024];
6744 DatagramPacket dp = new DatagramPacket(b1,b1.length);
6745 //2 arguments - byte array, array length
6746 ds.receive(dp);
6747
6748 String str = new String(dp.getData());
6749 int num = Integer.parseInt(str.trim());
6750 System.out.println("Number(Server) : " +num);
6751 int result = num * num;
6752 System.out.println("Result(Server) : " +result);
6753
6754
6755 //sender
6756 byte[] b2 = String.valueOf(result).getBytes();
6757 InetAddress ilh = InetAddress.getLocalHost();
6758 DatagramPacket dp1 = new
6759DatagramPacket(b2,b2.length,ilh,dp.getPort());
6760 //sender part - 4 arguments - byte array, array length
6761 ds.send(dp1);
6762 }
6763}
6764
6765
6766
6767//CLIENT PROGRAM
6768import java.net.*;
6769public class UDPcli_2way
6770{
6771 public static void main(String[] args) throws Exception
6772
6773
6774JAVA LAB
6775
6776
6777
6778
6779
6780129
6781
6782
6783
6784 {
6785 DatagramSocket ds = new DatagramSocket();
6786 System.out.println("\nMCA325\n");
6787 //sending
6788 int i = 12;
6789 byte b[] = String .valueOf(i).getBytes();
6790 InetAddress ilh = InetAddress.getLocalHost();
6791 DatagramPacket dp = new DatagramPacket(b,b.length,ilh,29);
6792 //4 arguments - byte array, byte array length, InetAddress,
6793Port no
6794 ds.send(dp);
6795
6796 //receiving
6797 byte b1[] = new byte[1024];
6798 DatagramPacket dp1 = new DatagramPacket(b1,b1.length);
6799 ds.receive(dp1);
6800
6801 String str = new String(dp1.getData());
6802 int r = Integer.parseInt(str.trim());
6803 System.out.println("Result(client) : " +r);
6804 int rnew = r+10;
6805 System.out.println("New Result(client) : " +rnew);
6806 }
6807}
6808
6809
6810
6811OUTPUT
6812
6813
6814
6815
6816
6817
6818
6819
6820JAVA LAB
6821
6822
6823
6824
6825
6826130
6827
6828
6829
6830Program #62
6831Program to create a table Citizen(Id (Primary), Name, age, address, DOB), insert
6832records, and display the records.
6833
6834
6835
6836SOURCE CODE
6837
6838
6839
6840import java.sql.*;
6841import java.io.*;
6842
6843public class Citizen
6844{
6845 public static void main(String[] args) throws Exception
6846 {
6847 Class.forName("oracle.jdbc.driver.OracleDriver");
6848 Connection con =
6849DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
6850"hr", "tiger");
6851 Statement stmt = con.createStatement();
6852 DataInputStream d = new DataInputStream(System.in);
6853 System.out.println("\nMCA325");
6854
6855 int c,id,age;
6856 String name,address,dob;
6857 ResultSet rs;
6858
6859 do
6860 {
6861 System.out.println("\n---------------CHOICE TABLE------
6862---------");
6863 System.out.print("1. Create table \n2. Insert
6864values\n3. Display record\n4.");
6865 System.out.print("Exit\nEnter your choice : ");
6866 c=Integer.parseInt(d.readLine());
6867
6868 switch(c)
6869 {
6870 case 1 : stmt.executeUpdate("create table citizen(id
6871int NOT NULL PRIMARY KEY, name varchar(50), age int, address varchar(50),
6872DOB date)");
6873 System.out.println("Table created!!");
6874 break;
6875
6876 case 2: System.out.println("Enter 5 records");
6877 for(int i=0;i<5;i++)
6878
6879
6880JAVA LAB
6881
6882
6883
6884
6885
6886131
6887
6888
6889
6890 {
6891 System.out.println("\nENTER DETAILS OF CITIZEN "
6892+(i+1));
6893 System.out.print("Enter id : ");
6894 id = Integer.parseInt(d.readLine());
6895 System.out.print("Enter name : ");
6896 name = d.readLine();
6897 System.out.print("Enter age : ");
6898 age = Integer.parseInt(d.readLine());
6899 System.out.print("Enter address : ");
6900 address = d.readLine();
6901 System.out.print("Enter DOB : ");
6902 dob = d.readLine();
6903 stmt.executeUpdate("insert into citizen
6904values("+id+",'"+name+"',"+age+",'"+address+"','"+dob+"')");
6905 System.out.println("Value inserted!!");
6906 }
6907 break;
6908
6909 case 3: rs = stmt.executeQuery("select * from
6910citizen");
6911 ResultSetMetaData md = rs.getMetaData();
6912 for(int i=1; i<md.getColumnCount(); i++)
6913 {
6914 System.out.print(md.getColumnName(i)+ "\t\t");
6915 }
6916 System.out.print("\t DATE OF BIRTH");
6917 System.out.println("\n--------------------
6918--------------------------------");
6919 while(rs.next()) //print
6920 {
6921 for(int i=1; i<=md.getColumnCount(); i++)
6922 System.out.print(rs.getString(i) + "\t\t");
6923 System.out.println("\n");
6924 }
6925 break;
6926
6927 case 4: System.exit(0);
6928 break;
6929 }
6930 }while(true);
6931 }
6932}
6933
6934
6935
6936
6937
6938
6939JAVA LAB
6940
6941
6942
6943
6944
6945132
6946
6947
6948
6949OUTPUT
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960JAVA LAB
6961
6962
6963
6964
6965
6966133
6967
6968
6969
6970Program #63
6971Assume that login is a table which has Uname, Upass. Check whether a record with
6972“Uname=”Bob” and “UPass=”Alice123#”is present in the table.
6973
6974
6975
6976SOURCE CODE
6977
6978
6979
6980import java.io.*;
6981import java.sql.*;
6982
6983public class login_srch
6984{
6985 public static void main(String[] args) throws Exception
6986 {
6987 Class.forName("oracle.jdbc.driver.OracleDriver");
6988 Connection con =
6989DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
6990"hr", "tiger");
6991 Statement stmt = con.createStatement();
6992 DataInputStream d = new DataInputStream(System.in);
6993 System.out.println("\nMCA325\n");
6994 do
6995 {
6996 System.out.print("Enter username to search : ");
6997 String name = d.readLine();
6998 System.out.print("Enter password to search : ");
6999 String pass = d.readLine();
7000
7001 ResultSet rs = stmt.executeQuery("select * from login
7002where uname = '"+name+"' and upass = '"+pass+"'");
7003
7004 if(rs.next()==true)
7005 System.out.println("Record exists!!");
7006 else
7007 System.out.println("Record not exists!!");
7008
7009 System.out.print("Do you want to check another(y/n) :
7010");
7011 String c = d.readLine();
7012 if(c.startsWith("n")) break;
7013 }while(true);
7014 }
7015}
7016
7017
7018
7019JAVA LAB
7020
7021
7022
7023
7024
7025134
7026
7027
7028
7029OUTPUT
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040JAVA LAB
7041
7042
7043
7044
7045
7046135
7047
7048
7049
7050Program #64
7051Program to construct the following tables:
7052 Department (dno(Primary), dname, dloc)
7053 Emp ( eno(Primary), ename, esal ,dno(Foreign))
7054
7055
7056
7057SOURCE CODE
7058
7059
7060
7061import java.sql.*;
7062public class deparment_emp
7063{
7064 public static void main(String[] args) throws Exception
7065 {
7066 Class.forName("oracle.jdbc.driver.OracleDriver");
7067 Connection
7068con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","
7069hr","tiger");
7070 Statement stmt=con.createStatement();
7071 System.out.println("\nMCA325\n");
7072
7073 stmt.executeUpdate("create table Department(dno int NOT NULL
7074PRIMARY KEY, dname varchar(50), dloc varchar(50))");
7075 System.out.println("Department table created!!");
7076
7077 stmt.executeUpdate("create table Employee(eno number(5) NOT
7078NULL PRIMARY KEY, ename varchar(20), esal number(7), dno references
7079Department(dno))");
7080 System.out.println("Employee table created!!");
7081
7082 con.close();
7083 }
7084}
7085OUTPUT
7086
7087
7088
7089
7090
7091
7092
7093
7094JAVA LAB
7095
7096
7097
7098
7099
7100136
7101
7102
7103
7104Program #65
7105Program for displaying information in the following order:
7106eno ename esal dname dloc
7107101 Rani 10,000 MCA Kochi
7108102 Vani 20,000 MSW Delhi
7109
7110
7111
7112SOURCE CODE
7113
7114
7115import java.sql.*;
7116
7117public class dept_emp_disp
7118{
7119 public static void main(String[] args) throws Exception
7120 {
7121 Class.forName("oracle.jdbc.driver.OracleDriver");
7122 Connection
7123con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","
7124hr","tiger");
7125 Statement stmt=con.createStatement();
7126 System.out.println("\nMCA325\n");
7127
7128 stmt.executeUpdate("insert into Department
7129values(3111,'MCA','Kochi')");
7130 stmt.executeUpdate("insert into Department
7131values(3112,'MSW','Delhi')");
7132
7133 stmt.executeUpdate("insert into Employee
7134values(101,'Rani',10000,3111)");
7135 stmt.executeUpdate("insert into Employee
7136values(102,'Vani',20000,3112)");
7137
7138 ResultSet rs = stmt.executeQuery("select e.eno, e.ename,
7139e.esal, d.dname, d.dloc from Employee e, Department d where
7140e.dno=d.dno");
7141 ResultSetMetaData md = rs.getMetaData();
7142
7143 System.out.println("ID \tNAME \tSALARY DEP. NAME DEP.
7144LOCATION");
7145 System.out.println("-----------------------------------------
7146----------");
7147 while(rs.next())
7148 {
7149 for(int i=1;i<md.getColumnCount();i++)
7150 {
7151
7152
7153JAVA LAB
7154
7155
7156
7157
7158
7159137
7160
7161
7162
7163 System.out.print(rs.getString(i)+" \t");
7164 }
7165 System.out.print(" " +rs.getString(5));
7166 System.out.println("\n");
7167 }
7168 con.close();
7169 }
7170}
7171
7172OUTPUT
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183JAVA LAB
7184
7185
7186
7187
7188
7189138
7190
7191
7192
7193Program #66
7194Program to implement database connectivity using object oriented concepts.
7195
7196
7197
7198SOURCE CODE
7199
7200
7201
7202import java.io.*;
7203import java.sql.*;
7204
7205class stud {
7206 void create() {
7207 try {
7208 Class.forName("oracle.jdbc.driver.OracleDriver");
7209 Connection con =
7210DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
7211"hr", "tiger");
7212 Statement stmt = con.createStatement();
7213 stmt.executeUpdate("create table stud(sid int NOT NULL,
7214sname varchar(20), sage int, saddr varchar(20))");
7215 System.out.println("Table created!!");
7216 con.close();
7217 } catch (Exception e) {
7218 System.out.println(e);
7219 }
7220 }
7221
7222 void insert(int id, String name, int age, String addr) {
7223 try {
7224 Class.forName("oracle.jdbc.driver.OracleDriver");
7225 Connection con =
7226DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
7227"hr", "tiger");
7228 Statement stmt = con.createStatement();
7229 stmt.executeUpdate("insert into stud values(" + id +
7230",'" + name + "'," + age + ",'" + addr + "')");
7231 System.out.println("Value inserted!!");
7232 con.close();
7233 } catch (Exception e) {
7234 System.out.println(e);
7235 }
7236 }
7237
7238 void delete(int id) {
7239 try {
7240 Class.forName("oracle.jdbc.driver.OracleDriver");
7241
7242
7243JAVA LAB
7244
7245
7246
7247
7248
7249139
7250
7251
7252
7253 Connection con =
7254DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
7255"hr", "tiger");
7256 Statement stmt = con.createStatement();
7257 stmt.executeUpdate("delete from stud where sid = " + id
7258+ "");
7259 System.out.println("Record deleted!!");
7260 con.close();
7261 } catch (Exception e) {
7262 System.out.println(e);
7263 }
7264 }
7265
7266 void search(int id) {
7267 try {
7268 Class.forName("oracle.jdbc.driver.OracleDriver");
7269 Connection con =
7270DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
7271"hr", "tiger");
7272 Statement stmt = con.createStatement();
7273 ResultSet rs;
7274 rs = stmt.executeQuery("select * from stud where sid =
7275" + id + " ");
7276 System.out.println("ID\t\tNAME\t\tAGE\t\tADDRESS");
7277 while (rs.next())
7278 System.out.println(rs.getString(1) + "\t\t" +
7279rs.getString(2) + "\t\t" + rs.getString(3) + "\t\t"
7280 + rs.getString(4));
7281 con.close();
7282 } catch (Exception e) {
7283 System.out.println(e);
7284 }
7285 }
7286
7287 void update(int id, String name, int age, String addr) {
7288 try {
7289 Class.forName("oracle.jdbc.driver.OracleDriver");
7290 Connection con =
7291DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
7292"hr", "tiger");
7293 Statement stmt = con.createStatement();
7294 stmt.executeUpdate(
7295 "update stud set sname='" + name +
7296"',sage=" + age + ",saddr='" + addr + "' where sid=" + id + " ");
7297 System.out.println("table updated");
7298
7299
7300JAVA LAB
7301
7302
7303
7304
7305
7306140
7307
7308
7309
7310 con.close();
7311 } catch (Exception e) {
7312 System.out.println(e);
7313 }
7314 }
7315
7316 void disp() {
7317 try {
7318 Class.forName("oracle.jdbc.driver.OracleDriver");
7319 Connection con =
7320DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
7321"hr", "tiger");
7322 Statement stmt = con.createStatement();
7323 ResultSet rs;
7324 rs = stmt.executeQuery("select * from stud");
7325 System.out.println("ID\t\tNAME\t\tAGE\t\tADDRESS");
7326 System.out.println("-----------------------------------
7327--------------------------");
7328 while (rs.next())
7329 System.out.println(rs.getString(1) + "\t\t" +
7330rs.getString(2) + "\t\t" + rs.getString(3) + "\t\t"
7331 + rs.getString(4));
7332 con.close();
7333 } catch (Exception e) {
7334 System.out.println(e);
7335 }
7336 }
7337}
7338
7339public class OOP_db_connectivity {
7340 public static void main(String[] args)
7341 throws NumberFormatException, IOException,
7342SQLException, ClassNotFoundException {
7343 stud s = new stud();
7344 int id, age, n;
7345 String name, addr;
7346 DataInputStream d = new DataInputStream(System.in);
7347 int i;
7348 System.out.println("\nMCA325");
7349 System.out.println("...........DATABASE connectivity using
7350OPP concept...........");
7351 do {
7352 System.out.println("\n-------CHOICE MENU-------");
7353 System.out.print("\n1. Create table\n2. Insert
7354record\n3. Search record\n4. Delete ");
7355
7356
7357JAVA LAB
7358
7359
7360
7361
7362
7363141
7364
7365
7366
7367 System.out.println("record\n5. Update record\n6.
7368Display all records\n7. Exit");
7369 System.out.print("Enter choice : ");
7370 i = Integer.parseInt(d.readLine());
7371 switch (i) {
7372 case 1:
7373 s.create();
7374 break;
7375 case 2:
7376 System.out.print("Enter no. of records to insert
7377: ");
7378 n = Integer.parseInt(d.readLine());
7379 System.out.println("Enter " + n + " records");
7380 for (i = 0; i < n; i++) {
7381 System.out.print("\nEnter id: ");
7382 id = Integer.parseInt(d.readLine());
7383 System.out.print("Enter name: ");
7384 name = d.readLine();
7385 System.out.print("Enter age: ");
7386 age = Integer.parseInt(d.readLine());
7387 System.out.print("Enter address: ");
7388 addr = d.readLine();
7389 s.insert(id, name, age, addr);
7390 }
7391 break;
7392 case 3:
7393 System.out.print("Enter id to search: ");
7394 id = Integer.parseInt(d.readLine());
7395 s.search(id);
7396 break;
7397 case 4:
7398 System.out.print("Enter id to delete: ");
7399 id = Integer.parseInt(d.readLine());
7400 s.delete(id);
7401 break;
7402 case 5:
7403 System.out.print("Enter id to update: ");
7404 id = Integer.parseInt(d.readLine());
7405 System.out.print("Enter name: ");
7406 name = d.readLine();
7407 System.out.print("Enter age: ");
7408 age = Integer.parseInt(d.readLine());
7409 System.out.print("Enter address: ");
7410 addr = d.readLine();
7411 s.update(id, name, age, addr);
7412
7413
7414JAVA LAB
7415
7416
7417
7418
7419
7420142
7421
7422
7423
7424 break;
7425 case 6:
7426 s.disp();
7427 break;
7428 case 7:
7429 System.exit(0);
7430 break;
7431 }
7432 } while (true);
7433 }
7434}
7435
7436OUTPUT
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447JAVA LAB
7448
7449
7450
7451
7452
7453143
7454
7455
7456
7457Program #67
7458JDBC program with Parametrized queries to update a given record (Rani’s salary to
745915,000) in the Emp table.
7460
7461
7462
7463SOURCE CODE
7464
7465
7466
7467import java.sql.*;
7468public class dep_emp_parameter_update
7469{
7470 public static void main(String[] args) throws Exception
7471 {
7472 Class.forName("oracle.jdbc.driver.OracleDriver");
7473 Connection
7474con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","
7475hr","tiger");
7476 Statement stmt=con.createStatement();
7477 System.out.println("\nMCA325\n");
7478 PreparedStatement pt = con.prepareStatement("update Employee
7479set esal=? where ename=?");
7480 pt.setInt(1, 15000);
7481 pt.setString(2, "Rani");
7482 pt.executeUpdate();
7483 System.out.println("Records updated!!");
7484 con.close();
7485 }
7486}
7487
7488
7489
7490JAVA LAB
7491
7492
7493
7494
7495
7496144
7497
7498
7499
7500OUTPUT
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517JAVA LAB
7518
7519
7520
7521
7522
7523145
7524
7525
7526
7527Program #68
7528JDBC program with Parametrized queries to list the records of Emp table which has
7529records whose names start with the alphabet “R”.
7530
7531
7532
7533SOURCE CODE
7534
7535
7536
7537import java.sql.*;
7538public class dep_emp_parameter_srch{
7539 public static void main(String[] args) throws Exception {
7540 Class.forName("oracle.jdbc.driver.OracleDriver");
7541 Connection
7542con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","
7543hr","tiger");
7544 Statement stmt=con.createStatement();
7545 ResultSet rs;
7546 System.out.println("\nMCA325\n");
7547 PreparedStatement pt = con.prepareStatement("select * from
7548Employee where ename like ?");
7549 pt.setString(1, "R%");
7550 rs = pt.executeQuery();
7551 ResultSetMetaData md = rs.getMetaData();
7552 for(int i=1;i<=md.getColumnCount();i++)
7553 {
7554 System.out.print(md.getColumnName(i)+"\t");
7555 }
7556 System.out.println("\n------------------------------");
7557 while(rs.next())
7558 {
7559 for(int i=1;i<=md.getColumnCount();i++)
7560 System.out.print(rs.getString(i)+"\t");
7561 System.out.println("\n");
7562 }
7563 con.close();
7564 }
7565}
7566OUTPUT
7567
7568
7569
7570
7571
7572
7573
7574
7575JAVA LAB
7576
7577
7578
7579
7580
7581146
7582
7583
7584
7585Program #69
7586JDBC program with PreparedStatement to delete the records of Emp table which
7587has records whose salary is less than 10,000.
7588
7589
7590
7591SOURCE CODE
7592
7593
7594
7595import java.sql.*;
7596public class dep_emp_dlt
7597{
7598 public static void main(String[] args) throws Exception
7599 {
7600 Class.forName("oracle.jdbc.driver.OracleDriver");
7601 Connection con =
7602DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","hr",
7603"tiger");
7604 Statement stmt = con.createStatement();
7605 System.out.println("\nMCA325\n");
7606 PreparedStatement ps = con.prepareStatement("delete from
7607Employee where esal<?");
7608 ps.setInt(1,10000);
7609 ps.executeUpdate();
7610 System.out.println("Records Deleted!!");
7611 con.close();
7612 }
7613}
7614
7615OUTPUT
7616
7617
7618
7619
7620
7621
7622
7623
7624JAVA LAB
7625
7626
7627
7628
7629
7630147
7631
7632
7633
7634Program #70
7635Implement a JDBC program which uses a Stored Procedure to insert records into
7636Department table.
7637
7638
7639
7640SOURCE CODE
7641
7642
7643
7644import java.io.*;
7645import java.sql.*;
7646
7647public class dep_insert_SP
7648{
7649 public static void main(String[] args) throws Exception
7650 {
7651 System.out.println("\nMCA325\n");
7652 Class.forName("oracle.jdbc.driver.OracleDriver");
7653 Connection
7654con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","
7655hr","tiger");
7656 Statement stmt=con.createStatement();
7657 BufferedReader br = new BufferedReader(new
7658InputStreamReader(System.in));
7659 CallableStatement cst = con.prepareCall("{call
7660INSDEP(?,?,?)}");
7661
7662 do
7663 {
7664 System.out.print("Enter Department ID : ");
7665 int id = Integer.parseInt(br.readLine());
7666 System.out.print("Enter Department Name : ");
7667 String name = br.readLine();
7668 System.out.print("Enter Department Location : ");
7669 String loc = br.readLine();
7670
7671 cst.setInt(1, id);
7672 cst.setString(2, name);
7673 cst.setString(3, loc);
7674 cst.executeUpdate();
7675
7676 System.out.print("Do you want to conyinue(y/n) : ");
7677 String s = br.readLine();
7678 if(s.startsWith("n")) { break;}
7679 }while(true);
7680 System.out.println("Records updated!!");
7681 con.close();
7682
7683
7684JAVA LAB
7685
7686
7687
7688
7689
7690148
7691
7692
7693
7694 }
7695}
7696
7697/*
7698
7699 create or replace procedure "INSDEP"
7700 2 (dno IN NUMBER,
7701 3 dname IN VARCHAR2,
7702 4 dloc IN VARCHAR2)
7703 5 is begin
7704 6 insert into department values(dno,dname,dloc);
7705 7 end;
7706 8 /
7707
7708*/
7709
7710OUTPUT
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723JAVA LAB
7724
7725
7726
7727
7728
7729149
7730
7731
7732
7733Program #71
7734Program to use Callable statement to implement a Stored Procedure to display the
7735Ename and Salary of all employees.
7736
7737
7738
7739SOURCE CODE
7740
7741
7742
7743import java.sql.*;
7744import oracle.jdbc.*;
7745
7746public class EmpStoredProc
7747{
7748 public static void main(String args[])
7749 {
7750 try
7751 {
7752 Class.forName("oracle.jdbc.driver.OracleDriver");
7753 Connection
7754con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","
7755hr","tiger");
7756 Statement st=con.createStatement();
7757 CallableStatement cst=con.prepareCall("{call DispEmp(?)}");
7758 cst.registerOutParameter(1, OracleTypes.CURSOR);
7759 cst.execute();
7760 ResultSet rs = ((OracleCallableStatement)cst).getCursor(1);
7761 ResultSetMetaData md=rs.getMetaData();
7762 System.out.println("--------------------");
7763 for(int i=1;i<=md.getColumnCount();i++)
7764 {
7765 System.out.print(md.getColumnName(i)+"\t");
7766 }
7767 System.out.print("\n--------------------\n");
7768 while (rs.next())
7769 {
7770 System.out.println(rs.getString("ename") + "\t" +
7771rs.getString("esal"));
7772 }
7773
7774 }
7775 catch(Exception e)
7776 {
7777 e.printStackTrace();
7778 }
7779 }
7780}
7781
7782
7783JAVA LAB
7784
7785
7786
7787
7788
7789150
7790
7791
7792
7793
7794/*
7795
7796CREATE OR REPLACE PROCEDURE DispEmp
7797(p_DispEmp OUT SYS_REFCURSOR)
7798AS
7799BEGIN
7800OPEN p_DispEmp FOR
7801SELECT ename,esal from employee;
7802END DispEmp;
7803/
7804
7805*/
7806
7807OUTPUT
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820JAVA LAB
7821
7822
7823
7824
7825
7826151
7827
7828
7829
7830Program #72
7831JDBC program to implement Choice Based Transaction Management in the
7832Department table.
7833
7834
7835
7836SOURCE CODE
7837
7838
7839import java.io.*;
7840import java.sql.*;
7841
7842public class dep_transaction_menu_based
7843{
7844 public static void main(String[] args) throws Exception
7845 {
7846 System.out.println("\nMCA325\n");
7847 Class.forName("oracle.jdbc.driver.OracleDriver");
7848 Connection
7849con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:
7850orcl","hr","tiger");
7851 PreparedStatement pst = con.prepareStatement("insert into
7852Department values(?,?,?)");
7853 con.setAutoCommit(false);
7854 BufferedReader br = new BufferedReader(new
7855InputStreamReader(System.in));
7856
7857 do
7858 {
7859 System.out.print("Enter Department No : ");
7860 int id = Integer.parseInt(br.readLine());
7861 System.out.print("Enter Department Name : ");
7862 String name = br.readLine();
7863 System.out.print("Enter Department Location : ");
7864 String loc = br.readLine();
7865
7866 pst.setInt(1, id);
7867 pst.setString(2, name);
7868 pst.setString(3, loc);
7869 pst.executeUpdate();
7870
7871 System.out.print("Commit/Rollback : ");
7872 String ans = br.readLine();
7873 if(ans.startsWith("c")) { con.commit();}
7874 if(ans.startsWith("r")) { con.rollback();}
7875
7876 System.out.print("Do you want to conyinue(y/n) : ");
7877
7878
7879JAVA LAB
7880
7881
7882
7883
7884
7885152
7886
7887
7888
7889 String s = br.readLine();
7890 if(s.startsWith("n")) { break;}
7891 }while(true);
7892
7893 System.out.println("Records inserted!!");
7894 con.close();
7895 }
7896}
7897
7898OUTPUT
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913JAVA LAB
7914
7915
7916
7917
7918
7919153
7920
7921
7922
7923Program #73
7924JDBC program to depict the usage of SQL Exception Class and SQL Warning Class.
7925
7926
7927
7928SOURCE CODE
7929
7930
7931
7932import java.sql.*;
7933
7934public class SQL_Excep{
7935 public static void main(String[] args) throws
7936ClassNotFoundException{
7937 try{
7938 System.out.println("\nMCA325\n");
7939 Class.forName("oracle.jdbc.driver.OracleDriver");
7940 Connection
7941con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","
7942hr","tiger");
7943 Statement stmt=con.createStatement();
7944 stmt.executeUpdate("select * from Employee where
7945ename=Rani");
7946 }
7947 catch(SQLException e)
7948 {
7949 System.out.println("SQL Message : " +e.getMessage());
7950 System.out.println("SQL State : " +e.getSQLState());
7951 System.out.println("SQL Error code : "
7952+e.getErrorCode());
7953 System.out.println("SQL Cause : " +e.getCause());
7954 e.printStackTrace();
7955 }
7956 }
7957}
7958
7959OUTPUT
7960
7961
7962
7963
7964
7965
7966
7967
7968JAVA LAB
7969
7970
7971
7972
7973
7974154
7975
7976
7977
7978Program #74
7979Using java AWT components, read text from a text box, and insert it into a list.
7980Similarly, remove selected items from the list, and place it into another list.
7981
7982
7983
7984SOURCE CODE
7985
7986
7987
7988import java.awt.*;
7989import java.awt.event.*;
7990public class list_insert_remove implements ActionListener, WindowListener
7991{
7992 Frame f;
7993 Label l,nl;
7994 TextField t;
7995 List l1,l2;
7996 Button b1,b2;
7997 list_insert_remove()
7998 {
7999 f = new Frame("LIST");
8000 l = new Label("Enter text to insert");
8001 l.setBounds(30,50,100,30);
8002 t = new TextField();
8003 t.setBounds(150,50,100,30);
8004 b1=new Button("INSERT");
8005 b1.setBounds(105,100,75,30);
8006 b1.addActionListener(this);
8007
8008 l1 = new List();
8009 l1.setBounds(30,150,100,50);
8010 b2 = new Button("REMOVE");
8011 b2.setBounds(140,160,100,30);
8012 b2.addActionListener(this);
8013
8014 nl = new Label("New List");
8015 nl.setBounds(30,220,70,50);
8016 l2 = new List();
8017 l2.setBounds(110,240,100,50);
8018
8019 f.add(l);
8020 f.add(t);
8021 f.add(b1);
8022 f.add(l1);
8023 f.add(b2);
8024 f.add(nl);
8025 f.add(l2);
8026
8027
8028JAVA LAB
8029
8030
8031
8032
8033
8034155
8035
8036
8037
8038
8039 f.addWindowListener(this);
8040 f.setSize(400,400);
8041 f.setLayout(null);
8042 f.setVisible(true);
8043 }
8044
8045 public void actionPerformed(ActionEvent e)
8046 {
8047 String str;
8048 if(e.getSource()==b2)
8049 {
8050 str = l1.getItem(l1.getSelectedIndex());
8051 if(str!=null)
8052 l2.add(str);
8053 l1.remove(l1.getSelectedItem());
8054 }
8055 else if(e.getSource()==b1)
8056 {
8057 str = t.getText();
8058 l1.add(str);
8059 t.setText(null);
8060 }
8061 }
8062 public void windowClosing(WindowEvent we)
8063 {
8064 System.exit(0); //event handler
8065 }
8066 public static void main(String[] args)
8067 {
8068 new list_insert_remove();
8069 }
8070
8071 @Override public void windowClosed(WindowEvent e) {}
8072 @Override public void windowIconified(WindowEvent e) {}
8073 @Override public void windowDeiconified(WindowEvent e) {}
8074 @Override public void windowDeactivated(WindowEvent e) {}
8075 @Override public void windowOpened(WindowEvent e) {}
8076 @Override public void windowActivated(WindowEvent e) {}
8077}
8078
8079
8080
8081JAVA LAB
8082
8083
8084
8085
8086
8087156
8088
8089
8090
8091OUTPUT
8092
8093
8094
8095
8096
8097
8098
8099
8100JAVA LAB
8101
8102
8103
8104
8105
8106157
8107
8108
8109
8110Program #75
8111Implement a numeric calculator using java AWT.
8112
8113
8114
8115SOURCE CODE
8116
8117
8118
8119import java.awt.*;
8120import java.lang.*;
8121import java.awt.event.*;
8122import javax.swing.*;
8123
8124public class numeric_calculator implements ActionListener, WindowListener
8125{
8126 int add=0, sub=0, mul=0, div=0, mod=0;
8127 Double n1,n2,res=0.0;
8128 String data;
8129 TextField t;
8130 Button bc, b, bmo, bd, bm, bs, ba, bo, be, b0, b1, b2, b3, b4,
8131b5,b6, b7, b8, b9;
8132 numeric_calculator()
8133 {
8134 Frame f = new Frame("Calculator");
8135 f.setBackground(Color.WHITE);
8136
8137 t = new TextField(25); t.setBounds(50,50,255,35);
8138
8139 bc = new Button("AC"); bc.setBounds(50,100,60,50);
8140 b = new Button("+/-"); b.setBounds(115,100,60,50);
8141 bmo = new Button("%"); bmo.setBounds(180,100,60,50);
8142 bd = new Button("/"); bd.setBounds(245,100,60,50);
8143 b9 = new Button("9"); b9.setBounds(50,155,60,50);
8144 b8 = new Button("8"); b8.setBounds(115,155,60,50);
8145 b7 = new Button("7"); b7.setBounds(180,155,60,50);
8146 bm = new Button("*"); bm.setBounds(245,155,60,50);
8147 b6 = new Button("6"); b6.setBounds(50,210,60,50);
8148 b5 = new Button("5"); b5.setBounds(115,210,60,50);
8149 b4 = new Button("4"); b4.setBounds(180,210,60,50);
8150 bs = new Button("-"); bs.setBounds(245,210,60,50);
8151 b3 = new Button("3"); b3.setBounds(50,265,60,50);
8152 b2 = new Button("2"); b2.setBounds(115,265,60,50);
8153 b1 = new Button("1"); b1.setBounds(180,265,60,50);
8154 ba = new Button("+"); ba.setBounds(245,265,60,50);
8155 b0 = new Button("0"); b0.setBounds(50,320,125,50);
8156 bo = new Button("."); bo.setBounds(180,320,60,50);
8157 be = new Button("="); be.setBounds(245,320,60,50);
8158
8159
8160JAVA LAB
8161
8162
8163
8164
8165
8166158
8167
8168
8169
8170
8171 f.add(t);
8172 f.add(bc);
8173 f.add(b);
8174 f.add(bmo);
8175 f.add(bd);
8176 f.add(b9);
8177 f.add(b8);
8178 f.add(b7);
8179 f.add(bm);
8180 f.add(b6);
8181 f.add(b5);
8182 f.add(b4);
8183 f.add(bs);
8184 f.add(b3);
8185 f.add(b2);
8186 f.add(b1);
8187 f.add(ba);
8188 f.add(b0);
8189 f.add(bo);
8190 f.add(be);
8191
8192 bc.addActionListener(this);
8193 b.addActionListener(this);
8194 bmo.addActionListener(this);
8195 bd.addActionListener(this);
8196 b9.addActionListener(this);
8197 b8.addActionListener(this);
8198 b7.addActionListener(this);
8199 bm.addActionListener(this);
8200 b6.addActionListener(this);
8201 b5.addActionListener(this);
8202 b4.addActionListener(this);
8203 bs.addActionListener(this);
8204 b3.addActionListener(this);
8205 b2.addActionListener(this);
8206 b1.addActionListener(this);
8207 ba.addActionListener(this);
8208 b0.addActionListener(this);
8209 bo.addActionListener(this);
8210 be.addActionListener(this);
8211
8212 f.addWindowListener(this);
8213 f.setSize(350,400);
8214 f.setLayout(null);
8215
8216
8217JAVA LAB
8218
8219
8220
8221
8222
8223159
8224
8225
8226
8227 f.setVisible(true);
8228 }
8229
8230 public void actionPerformed(ActionEvent e)
8231 {
8232 Object source = e.getSource();
8233
8234 if(source==bc)
8235 {
8236 n1 = 0.0;
8237 n2 = 0.0;
8238 res = 0.0;
8239 t.setText(null);
8240 }
8241 if(source==b0)
8242 t.setText(t.getText()+"0");
8243
8244 if(source==b1)
8245 t.setText(t.getText()+"1");
8246
8247 if(source==b2)
8248 t.setText(t.getText()+"2");
8249
8250 if(source==b3)
8251 t.setText(t.getText()+"3");
8252
8253 if(source==b4)
8254 t.setText(t.getText()+"4");
8255
8256 if(source==b5)
8257 t.setText(t.getText()+"5");
8258
8259 if(source==b6)
8260 t.setText(t.getText()+"6");
8261
8262 if(source==b7)
8263 t.setText(t.getText()+"7");
8264
8265 if(source==b8)
8266 t.setText(t.getText()+"8");
8267
8268 if(source==b9)
8269 t.setText(t.getText()+"9");
8270
8271 if(source==bo)
8272
8273
8274JAVA LAB
8275
8276
8277
8278
8279
8280160
8281
8282
8283
8284 t.setText(t.getText()+".");
8285
8286 if(source==b)
8287 t.setText("-"+t.getText());
8288
8289 if(source==ba)
8290 {
8291 n1 = Double.valueOf(t.getText());
8292 add=1;
8293 t.setText("");
8294 }
8295 if(source==bs)
8296 {
8297 n1 = Double.valueOf(t.getText());
8298 sub=1;
8299 t.setText("");
8300 }
8301
8302 if(source==bm)
8303 {
8304 n1 = Double.valueOf(t.getText());
8305 mul=1;
8306 t.setText("");
8307 }
8308
8309 if(source==bd)
8310 {
8311 n1 = Double.valueOf(t.getText());
8312 div=1;
8313 t.setText("");
8314 }
8315
8316 if(source==bmo)
8317 {
8318 n1 = Double.valueOf(t.getText());
8319 mod=1;
8320 t.setText("");
8321 }
8322
8323 if(source==be)
8324 {
8325 n2 = Double.valueOf(t.getText());
8326 if(add>0)
8327 res = n1 + n2;
8328
8329
8330
8331JAVA LAB
8332
8333
8334
8335
8336
8337161
8338
8339
8340
8341 if(sub>0)
8342 res = n1 - n2;
8343
8344 if(mul>0)
8345 res = n1 * n2;
8346
8347 if(div>0)
8348 res = n1 / n2;
8349
8350 if(mod>0)
8351 res = n1 % n2;
8352
8353 data = Double.toString(res);
8354 t.setText(data);
8355 }
8356 }
8357
8358 public void windowActivated(WindowEvent we){}
8359 public void windowOpened(WindowEvent we){}
8360 public void windowClosing(WindowEvent we)
8361 {
8362 System.exit(0); //event handler
8363 }
8364
8365 public static void main(String[] args)
8366 {
8367 new numeric_calculator();
8368 }
8369 @Override public void windowClosed(WindowEvent e) {}
8370 @Override public void windowIconified(WindowEvent e) {}
8371 @Override public void windowDeiconified(WindowEvent e) {}
8372 @Override public void windowDeactivated(WindowEvent e) {}
8373}
8374
8375
8376
8377JAVA LAB
8378
8379
8380
8381
8382
8383162
8384
8385
8386
8387OUTPUT
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400JAVA LAB
8401
8402
8403
8404
8405
8406163
8407
8408
8409
8410Program #76
8411Using AWT, create a menu for file and edit options.
8412
8413
8414
8415SOURCE CODE
8416
8417
8418import java.awt.*;
8419import java.awt.event.*;
8420
8421public class Menubar implements ActionListener,WindowListener
8422{
8423 Button b;
8424 Label l;
8425 Frame f;
8426 Panel p;
8427 Menubar()
8428 {
8429 f = new Frame("MENUBAR");
8430
8431 MenuBar m = new MenuBar();
8432 Menu m1 = new Menu("File");
8433 Menu m2 = new Menu("Edit");
8434 m.add(m1);
8435 m.add(m2);
8436
8437 MenuItem f1 = new MenuItem("New");
8438 MenuItem f2 = new MenuItem("Open");
8439 MenuItem f3 = new MenuItem("Save");
8440 MenuItem f4 = new MenuItem("Save As");
8441 MenuItem e1 = new MenuItem("Undo");
8442 MenuItem e2 = new MenuItem("Redo");
8443 MenuItem e3 = new MenuItem("Cut");
8444 MenuItem e4 = new MenuItem("Copy");
8445 MenuItem e5 = new MenuItem("Paste");
8446 m1.add(f1);
8447 m1.add(f2);
8448 m1.add(f3);
8449 m1.add(f4);
8450 m2.add(e1);
8451 m2.add(e2);
8452 m2.add(e3);
8453 m2.add(e4);
8454 m2.add(e5);
8455 f.setMenuBar(m);
8456 f2.addActionListener(this);
8457
8458
8459JAVA LAB
8460
8461
8462
8463
8464
8465164
8466
8467
8468
8469
8470 l = new Label();
8471 l.setBounds(5,50,480,30);
8472 l.setBackground(Color.white);
8473 f.add(l);
8474
8475 p = new Panel();
8476 p.setBounds(0,50,500,500);
8477 p.setBackground(Color.white);
8478 f.add(p);
8479
8480 f.setSize(500,500);
8481 f.setVisible(true);
8482 f.addWindowListener(this);
8483 }
8484
8485 public void windowClosing(WindowEvent e) { System.exit(0); }
8486 public void windowActivated(WindowEvent e) {}
8487 public void windowClosed(WindowEvent e) {}
8488 public void windowDeactivated(WindowEvent e) {}
8489 public void windowDeiconified(WindowEvent e) {}
8490 public void windowIconified(WindowEvent e) {}
8491 public void windowOpened(WindowEvent arg0) {}
8492
8493 public void actionPerformed(ActionEvent e)
8494 {
8495 FileDialog fd = new FileDialog(f, "OpenFile",
8496FileDialog.LOAD);
8497 fd.setVisible(true);
8498 String str1 = fd.getFile();
8499 l.setText(str1);
8500 }
8501 public static void main(String[] args)
8502 {
8503 new Menubar();
8504 }
8505}
8506
8507
8508
8509
8510
8511
8512JAVA LAB
8513
8514
8515
8516
8517
8518165
8519
8520
8521
8522OUTPUT
8523
8524
8525
8526
8527
8528
8529
8530
8531JAVA LAB
8532
8533
8534
8535
8536
8537166
8538
8539
8540
8541Program #77
8542Replicate a Notepad editor using AWT.
8543
8544
8545
8546SOURCE CODE
8547
8548
8549
8550import java.awt.*;
8551import java.awt.event.*;
8552
8553public class Notepad implements ActionListener, WindowListener
8554{
8555 Frame f;
8556 TextArea t;
8557 Label l;
8558 MenuItem f1,f2,f3,f4,e1,e2,e3,e4,e5,e6;
8559 String selected;
8560 Notepad()
8561 {
8562 f = new Frame("Notepad");
8563 t = new TextArea();
8564 t.setBounds(0,0,300,300);
8565 f.add(t);
8566
8567 MenuBar m = new MenuBar();
8568 Menu m1 = new Menu("File");
8569 Menu m2 = new Menu("Edit");
8570 Menu m3 = new Menu("Format");
8571 Menu m4 = new Menu("View");
8572 Menu m5 = new Menu("Help");
8573
8574 m.add(m1);
8575 m.add(m2);
8576 m.add(m3);
8577 m.add(m4);
8578 m.add(m5);
8579
8580 MenuItem f1 = new MenuItem("New");
8581 MenuItem f2 = new MenuItem("Open");
8582 MenuItem f3 = new MenuItem("Save");
8583 MenuItem f4 = new MenuItem("Save As");
8584 MenuItem e1 = new MenuItem("Undo");
8585 MenuItem e2 = new MenuItem("Redo");
8586 MenuItem e3 = new MenuItem("Cut");
8587 MenuItem e4 = new MenuItem("Copy");
8588 MenuItem e5 = new MenuItem("Paste");
8589
8590
8591JAVA LAB
8592
8593
8594
8595
8596
8597167
8598
8599
8600
8601 MenuItem e6 = new MenuItem("Select All");
8602
8603 m1.add(f1);
8604 m1.add(f2);
8605 m1.add(f3);
8606 m1.add(f4);
8607 m2.add(e1);
8608 m2.add(e2);
8609 m2.add(e3);
8610 m2.add(e4);
8611 m2.add(e5);
8612 m3.add(new MenuItem("Font"));
8613 m4.add(new MenuItem("Zoom"));
8614 m5.add(new MenuItem("View Help"));
8615 m5.add(new MenuItem("Send Feedback"));
8616 m5.add(new MenuItem("About"));
8617
8618 f.setMenuBar(m);
8619 f2.addActionListener(this);
8620 f.addWindowListener(this);
8621 f.setSize(500,500);
8622 f.setVisible(true);
8623 }
8624
8625 public void windowClosing(WindowEvent e) { System.exit(0); }
8626 public void windowActivated(WindowEvent e) {}
8627 public void windowClosed(WindowEvent e) {}
8628 public void windowDeactivated(WindowEvent e) {}
8629 public void windowDeiconified(WindowEvent e) {}
8630 public void windowIconified(WindowEvent e) {}
8631 public void windowOpened(WindowEvent arg0) {}
8632
8633 public void actionPerformed(ActionEvent e)
8634 {
8635 FileDialog fd = new FileDialog(f,"openFIle",FileDialog.LOAD);
8636 fd.setVisible(true);
8637 String str1=fd.getFile();
8638 l.setText(str1);
8639 }
8640
8641 public static void main(String[] args)
8642 {
8643 new Notepad();
8644 }
8645}
8646
8647
8648JAVA LAB
8649
8650
8651
8652
8653
8654168
8655
8656
8657
8658OUTPUT
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671JAVA LAB
8672
8673
8674
8675
8676
8677169
8678
8679
8680
8681Program #78
8682Design a login frame for a shopping site, using GUI - Database Connectivity.
8683
8684
8685
8686SOURCE CODE
8687
8688
8689
8690import java.awt.*;
8691import java.awt.event.*;
8692import java.sql.*;
8693
8694public class shopping_login implements ActionListener, WindowListener
8695{
8696 TextField t1, t2;
8697 Label l1, l2, l3;
8698 Button b1, b2;
8699 Frame f;
8700 Dialog d1;
8701 int flag = 0;
8702
8703 shopping_login() {
8704 f = new Frame("Login Form");
8705 Font font = new Font("Serif", Font.BOLD, 15);
8706 l1 = new Label("USERNAME");
8707 l1.setBounds(50, 100, 100, 30);
8708 l1.setFont(font);
8709 f.add(l1);
8710 l2 = new Label("PASSWORD");
8711 l2.setBounds(50, 150, 100, 30);
8712 l2.setFont(font);
8713 f.add(l2);
8714 t1 = new TextField();
8715 t1.setBounds(200, 100, 100, 30);
8716 f.add(t1);
8717 t2 = new TextField();
8718 t2.setBounds(200, 150, 100, 30);
8719 f.add(t2);
8720 t2.setEchoChar('*');
8721 b1 = new Button("LOGIN");
8722 b1.setBounds(140, 210, 80, 30);
8723 b1.setFont(font);
8724 f.add(b1);
8725
8726 b1.addActionListener(this);
8727 f.addWindowListener(this);
8728 f.setSize(400, 400);
8729
8730
8731JAVA LAB
8732
8733
8734
8735
8736
8737170
8738
8739
8740
8741 f.setLayout(null);
8742 f.setVisible(true);
8743 }
8744
8745 public void actionPerformed(ActionEvent e) {
8746 try {
8747 Class.forName("oracle.jdbc.driver.OracleDriver");
8748 Connection con =
8749DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
8750"hr", "tiger");
8751 Statement stmt = con.createStatement();
8752
8753 ResultSet rs = stmt.executeQuery("select * from
8754login");
8755 String uname = t1.getText();
8756 String upass = t2.getText();
8757 while (rs.next() == true) {
8758 if ((uname.equals(rs.getString(1))) &&
8759(upass.equals(rs.getString(2)))) {
8760 flag = 1;
8761 System.out.println(uname);
8762 System.out.println(upass);
8763 d1 = new Dialog(f, "SUCCESS",
8764Dialog.DEFAULT_MODALITY_TYPE);
8765 d1.add(new Label("Successfully Logged
8766in!"));
8767 d1.setBounds(200, 150, 200, 150);
8768 d1.setVisible(true);
8769 break;
8770 } else {
8771 continue;
8772 }
8773 }
8774 if (flag != 1) {
8775 flag = 0;
8776 d1 = new Dialog(f, "FAILURE",
8777Dialog.DEFAULT_MODALITY_TYPE);
8778 d1.add(new Label("Incorrect password or
8779username!"));
8780 d1.setBounds(200, 150, 200, 150);
8781 d1.setVisible(true);
8782 }
8783 } catch (Exception e1) {
8784 System.out.println(e1);
8785 }
8786
8787
8788JAVA LAB
8789
8790
8791
8792
8793
8794171
8795
8796
8797
8798 }
8799
8800 public void windowActivated(WindowEvent we) { }
8801 public void windowOpened(WindowEvent we) { }
8802 public void windowClosing(WindowEvent we) { System.exit(0); }
8803
8804 public static void main(String[] args)
8805 {
8806 shopping_login s = new shopping_login();
8807 }
8808
8809 @Override public void windowClosed(WindowEvent e) { }
8810 @Override public void windowIconified(WindowEvent e) { }
8811 @Override public void windowDeiconified(WindowEvent e) { }
8812 @Override public void windowDeactivated(WindowEvent e) { }
8813}
8814
8815OUTPUT
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830JAVA LAB
8831
8832
8833
8834
8835
8836172
8837
8838
8839
8840Program #79
8841
8842Using AWT, draw a house/ car.
8843
8844
8845
8846SOURCE CODE
8847
8848
8849import java.awt.*;
8850import java.awt.event.MouseAdapter;
8851import java.awt.event.MouseEvent;
8852import java.awt.event.WindowEvent;
8853import java.awt.event.WindowListener;
8854
8855public class draw_house extends MouseAdapter implements WindowListener
8856{
8857 Frame f;
8858 Color rgb;
8859 draw_house()
8860 {
8861 f = new Frame("HOUSE");
8862 f.addMouseListener(this);
8863 f.addWindowListener(this);
8864 f.setSize(500,500);
8865 f.setVisible(true);
8866 f.setLayout(null);
8867 }
8868 public void mouseClicked(MouseEvent e)
8869 {
8870 Graphics g = f.getGraphics();
8871 rgb = new Color(25,0,55);
8872 g.setColor(rgb);
8873 g.fillRect(0,0, 500, 500);
8874
8875 int x[] = {0, 250, 500};
8876 int y[] = {200, 100, 200};
8877 int n = 3;
8878 rgb = new Color(220,20,60);
8879 g.setColor(rgb);
8880 g.fillPolygon(x, y, n);
8881
8882 int x1[] = {50, 450, 450,50};
8883 int y1[] = {200, 200, 400,400};
8884 int n1 = 4;
8885 rgb = new Color(255,140,0);
8886 g.setColor(rgb);
8887 g.fillPolygon(x1, y1, n1);
8888
8889
8890JAVA LAB
8891
8892
8893
8894
8895
8896173
8897
8898
8899
8900
8901 int x2[] = {220, 280, 280,220};
8902 int y2[] = {300, 300, 400,400};
8903 int n2 = 4;
8904 rgb = new Color(255,40,0);
8905 g.setColor(rgb);
8906 g.fillPolygon(x2, y2, n2);
8907 }
8908
8909
8910
8911 public void windowClosing(WindowEvent we)
8912 {
8913 System.exit(0); //event handler
8914 }
8915 public static void main(String args[])
8916 {
8917 new draw_house();
8918 }
8919 @Override public void windowOpened(WindowEvent e) {}
8920 @Override public void windowClosed(WindowEvent e) {}
8921 @Override public void windowIconified(WindowEvent e) {}
8922 @Override public void windowDeiconified(WindowEvent e) {}
8923 @Override public void windowActivated(WindowEvent e) {}
8924 @Override public void windowDeactivated(WindowEvent e) {}
8925}
8926OUTPUT
8927
8928
8929
8930
8931
8932
8933JAVA LAB
8934
8935
8936
8937
8938
8939174
8940
8941
8942
8943Program #80
8944Using applets, create a colored image of your choice, and move it in a zig zag
8945motion.
8946
8947
8948
8949SOURCE CODE
8950
8951
8952
8953import java.applet.*;
8954import java.awt.*;
8955import java.lang.Math;
8956
8957/*<applet code="Waves" width="700" height="700"></applet>*/
8958
8959public class Waves extends Applet
8960{
8961 double freq = 0.025;
8962 Image pic, a;
8963 int x = 0, y = 0, mag = 200, offset = 200;
8964 boolean zig = true;
8965
8966 public void init() {}
8967
8968 public void paint(Graphics g)
8969 {
8970 while (y < 700) {
8971 double dx;
8972 dx = Math.sin(y * freq) * mag;
8973 dx += mag + offset;
8974 g.setColor(Color.red);
8975 g.fillOval((int) dx, y, 40, 40);
8976 try
8977 {
8978 Thread.sleep(10);
8979 g.setColor(Color.WHITE);
8980 g.fillRect(0, 0, 1000, 1000);
8981 }
8982 catch (Exception e)
8983 {
8984 e.printStackTrace();
8985 }
8986 y += 1;
8987 }
8988 }
8989}
8990
8991
8992
8993
8994JAVA LAB
8995
8996
8997
8998
8999
9000175
9001
9002
9003
9004OUTPUT
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018JAVA LAB
9019
9020
9021
9022
9023
9024176
9025
9026
9027
9028Program #81
9029With music in the background, use AWT components to find the sum and product
9030of two numbers.
9031
9032
9033
9034SOURCE CODE
9035
9036
9037
9038import java.applet.*;
9039import java.awt.*;
9040import java.awt.event.*;
9041
9042/*<applet code="AudioPgm" width="400" height="400"></applet>*/
9043
9044public class AudioPgm extends Applet {
9045 AudioClip ac;
9046 TextField t1, t2;
9047 Button b1, b2;
9048 Label l;
9049
9050 public void init() {
9051 setBackground(Color.WHITE);
9052 ac = getAudioClip(getCodeBase(), "audio.wav");
9053 ac.play();
9054 t1 = new TextField();
9055 add(t1);
9056 t2 = new TextField();
9057 add(t2);
9058 b1 = new Button("Sum");
9059 add(b1);
9060 b2 = new Button("Product");
9061 add(b2);
9062 l = new Label("");
9063 l.setBounds(50, 200, 100, 100);
9064 add(l);
9065 b1.addActionListener(S);
9066 b2.addActionListener(M);
9067 }
9068
9069 public ActionListener S = new ActionListener() {
9070 public void actionPerformed(ActionEvent e) {
9071 {
9072 int i = Integer.parseInt(t1.getText());
9073 int j = Integer.parseInt(t2.getText());
9074 int sum = i + j;
9075 l.setText(String.valueOf(sum));
9076
9077
9078JAVA LAB
9079
9080
9081
9082
9083
9084177
9085
9086
9087
9088 }
9089 }
9090 };
9091 public ActionListener M = new ActionListener() {
9092 public void actionPerformed(ActionEvent e) {
9093 {
9094 int i = Integer.parseInt(t1.getText());
9095 int j = Integer.parseInt(t2.getText());
9096 int p = i * j;
9097 l.setText(String.valueOf(p));
9098 }
9099 }
9100 };
9101}
9102
9103OUTPUT
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126JAVA LAB
9127
9128
9129
9130
9131
9132178
9133
9134
9135
9136Program #82
9137Using applets, draw a Christmas tree.
9138
9139
9140
9141SOURCE CODE
9142
9143
9144
9145import java.awt.*;
9146import java.applet.*;
9147
9148/*<applet code="Ctree" width="700" height="700"></applet>*/
9149public class Ctree extends Applet
9150{
9151 public void init()
9152 {
9153 setBackground(Color.WHITE);
9154 }
9155
9156 public void paint(Graphics g)
9157 {
9158 Color G = new Color(0, 153, 0);
9159 g.setColor(G);
9160 int x1[] = { 275, 478, 675 };
9161 int y1[] = { 540, 380, 540 };
9162 int n = 3;
9163 g.fillPolygon(x1, y1, n);
9164 int x2[] = { 300, 478, 640 };
9165 int y2[] = { 420, 260, 420 };
9166 g.fillPolygon(x2, y2, n);
9167 int x3[] = { 330, 478, 620 };
9168 int y3[] = { 300, 140, 300 };
9169
9170 g.fillPolygon(x3, y3, n);
9171 //Rectangle
9172 Color Brown = new Color(102, 51, 0);
9173 g.setColor(Brown);
9174 g.fillRect(455, 540, 50, 110);
9175 //BALLSs
9176 g.setColor(Color.RED);
9177 g.fillOval(465, 129, 35, 35);
9178 g.fillOval(330, 300, 15, 15);
9179 g.fillOval(620, 300, 15, 15);
9180 g.fillOval(300, 420, 15, 15);
9181 g.fillOval(640, 420, 15, 15);
9182 g.fillOval(272, 540, 15, 15);
9183 g.fillOval(668, 540, 15, 15);
9184
9185
9186JAVA LAB
9187
9188
9189
9190
9191
9192179
9193
9194
9195
9196 }
9197}
9198
9199
9200
9201OUTPUT
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218JAVA LAB
9219
9220
9221
9222
9223
9224180
9225
9226
9227
9228Program #83
9229With parameter passing, check whether the sum of passed numbers is less than,
9230greater than, or equal to zero. Based upon the result, display different images from
9231the folder.
9232
9233
9234
9235SOURCE CODE
9236
9237
9238
9239import java.applet.*;
9240import java.awt.*;
9241import java.awt.event.*;
9242
9243/*<applet code="Img" width="800" height="800"><param name="x" value="-
92441"><param name="y" value="-1"></applet>*/
9245public class Img extends Applet {
9246 Image p1, p2, p3;
9247 String a, b;
9248 int sum;
9249
9250 public void init() {
9251 a = getParameter("x");
9252 b = getParameter("y");
9253 int ia = Integer.parseInt(a);
9254 int ib = Integer.parseInt(b);
9255 sum = ia + ib;
9256 }
9257
9258 public void paint(Graphics g) {
9259 p1 = getImage(getDocumentBase(), "one.jpg");
9260 p2 = getImage(getDocumentBase(), "two.jpg");
9261 p3 = getImage(getDocumentBase(), "three.jpg");
9262 g.setFont(new Font("Times New Roman", Font.BOLD, 20));
9263 g.drawString("A is =" + a, 20, 20);
9264 g.drawString("B is =" + b, 20, 40);
9265 g.drawString("Sum is =" + String.valueOf(sum), 20, 60);
9266 if (sum < 0) {
9267 g.drawImage(p1, 20, 80, this);
9268 } else if (sum > 0) {
9269 g.drawImage(p2, 20, 80, this);
9270 } else {
9271 g.drawImage(p3, 20, 80, this);
9272 }
9273 }
9274}
9275
9276
9277
9278JAVA LAB
9279
9280
9281
9282
9283
9284181
9285
9286
9287
9288OUTPUT
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301JAVA LAB
9302
9303
9304
9305
9306
9307182
9308
9309
9310
9311
9312
9313
9314
9315
9316JAVA LAB
9317
9318
9319
9320
9321
9322183
9323
9324
9325
9326MINI – PROJECT: Project Planner
9327A Kanban Style Project Management Utility
9328
9329
9330
9331DBConnection.java
9332
9333
9334import java.sql.Connection;
9335import java.sql.DriverManager;
9336import java.sql.SQLException;
9337
9338public class DBConnection
9339{
9340 public static Connection connect()
9341 {
9342 Connection conn = null;
9343 try
9344 {
9345 Class.forName("org.sqlite.JDBC");
9346 conn =
9347DriverManager.getConnection("jdbc:sqlite:db.db");
9348 System.out.println("Connection to SQLite has been
9349established.");
9350 }
9351 catch (ClassNotFoundException | SQLException e)
9352 {
9353 System.out.println(e.getMessage());
9354 }
9355 return conn;
9356 }
9357}
9358
9359LandingPage.java
9360
9361
9362import java.awt.EventQueue;
9363
9364import javax.swing.JFrame;
9365import javax.swing.JButton;
9366import java.awt.event.ActionListener;
9367import java.awt.event.ActionEvent;
9368import javax.swing.JList;
9369import javax.swing.JScrollPane;
9370
9371
9372JAVA LAB
9373
9374
9375
9376
9377
9378184
9379
9380
9381
9382import javax.swing.JLabel;
9383import javax.swing.SwingConstants;
9384
9385import javax.swing.DefaultListModel;
9386
9387import java.awt.Font;
9388import javax.swing.ListSelectionModel;
9389import java.awt.event.MouseAdapter;
9390import java.awt.event.MouseEvent;
9391import java.awt.Color;
9392import java.sql.*;
9393
9394public class landingPage
9395{
9396 private JFrame frmProjectPlannera;
9397
9398 /**
9399 * Launch the application.
9400 */
9401 public static void main(String[] args)
9402 {
9403 EventQueue.invokeLater(new Runnable()
9404 {
9405 public void run()
9406 {
9407 try
9408 {
9409 landingPage window = new landingPage();
9410 window.frmProjectPlannera.setVisible(true);
9411 }
9412 catch (Exception e)
9413 {
9414 e.printStackTrace();
9415 }
9416 }
9417 });
9418 }
9419
9420 /**
9421 * Create the application.
9422 */
9423 public landingPage() {
9424 initialize();
9425 }
9426
9427
9428
9429JAVA LAB
9430
9431
9432
9433
9434
9435185
9436
9437
9438
9439 /**
9440 * Initialize the contents of the frame.
9441 */
9442 private void initialize() {
9443 frmProjectPlannera = new JFrame();
9444 frmProjectPlannera.setTitle("Project Planner 0.1a");
9445 frmProjectPlannera.setResizable(false);
9446 frmProjectPlannera.setFont(new Font("Consolas", Font.PLAIN,
944713));
9448 frmProjectPlannera.setBounds(100, 100, 292, 409);
9449
9450 frmProjectPlannera.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9451 frmProjectPlannera.getContentPane().setLayout(null);
9452
9453 JScrollPane scrollPane = new JScrollPane();
9454 scrollPane.setBounds(10, 5, 259, 315);
9455 frmProjectPlannera.getContentPane().add(scrollPane);
9456
9457 JList<ProjectList> project_list = new JList<ProjectList>();
9458 project_list.setBackground(Color.GRAY);
9459 project_list.setBorder(null);
9460
9461 project_list.addMouseListener(new MouseAdapter()
9462 {
9463 @Override
9464 public void mouseClicked(MouseEvent e)
9465 {
9466 if(e.getClickCount() == 2)
9467 {
9468 ProjectList pl =
9469project_list.getSelectedValue();
9470 Planner planner = new Planner(pl.getID());
9471 planner.setVisible(true);
9472 frmProjectPlannera.dispose();
9473 }
9474 }
9475 });
9476
9477
9478
9479 project_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
9480
9481
9482 updateProjectsList(project_list);
9483
9484
9485
9486JAVA LAB
9487
9488
9489
9490
9491
9492186
9493
9494
9495
9496 project_list.setFont(new Font("Consolas", Font.PLAIN, 18));
9497
9498 scrollPane.setViewportView(project_list);
9499
9500 JLabel lblNewLabel = new JLabel("Project List");
9501 lblNewLabel.setLabelFor(frmProjectPlannera);
9502 lblNewLabel.setVerticalAlignment(SwingConstants.BOTTOM);
9503 lblNewLabel.setFont(new Font("Consolas", Font.PLAIN, 15));
9504 lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
9505 scrollPane.setColumnHeaderView(lblNewLabel);
9506
9507 JButton btnNewButton = new JButton("New Board");
9508 btnNewButton.addActionListener(new ActionListener()
9509 {
9510 public void actionPerformed(ActionEvent e)
9511 {
9512 newProject np = new newProject();
9513 np.setVisible(true);
9514 frmProjectPlannera.dispose();
9515 }
9516 });
9517
9518 btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 12));
9519 btnNewButton.setBounds(20, 330, 109, 32);
9520 frmProjectPlannera.getContentPane().add(btnNewButton);
9521
9522 JButton btnDelete = new JButton("Delete");
9523 btnDelete.setToolTipText("Delete Selected Plan");
9524 btnDelete.setFont(new Font("Tahoma", Font.BOLD, 11));
9525 btnDelete.setForeground(Color.RED);
9526 btnDelete.addActionListener(new ActionListener()
9527 {
9528 public void actionPerformed(ActionEvent e)
9529 {
9530 ProjectList pList =
9531project_list.getSelectedValue();
9532 if(pList != null)
9533 {
9534 Connection conn = DBConnection.connect();
9535 try
9536 {
9537 PreparedStatement ps =
9538conn.prepareStatement("delete from projects where pid = ?");
9539 ps.setInt(1, pList.getID());
9540 ps.execute();
9541
9542
9543JAVA LAB
9544
9545
9546
9547
9548
9549187
9550
9551
9552
9553 ps = conn.prepareStatement("delete
9554from tasklist where pid = ?");
9555 ps.setInt(1, pList.getID());
9556 ps.execute();
9557 ps.close();
9558 conn.close();
9559 }
9560 catch (SQLException e1)
9561 {
9562 e1.printStackTrace();
9563 }
9564 updateProjectsList(project_list);
9565 }
9566 }
9567 });
9568 btnDelete.setBounds(161, 331, 84, 32);
9569 frmProjectPlannera.getContentPane().add(btnDelete);
9570 }
9571
9572 private void updateProjectsList(JList<ProjectList> project_list)
9573 {
9574 Connection conn = DBConnection.connect();
9575 try
9576 {
9577 DefaultListModel<ProjectList> model = new
9578DefaultListModel<ProjectList>();
9579 PreparedStatement ps = conn.prepareStatement("select
9580pid,pname from projects");
9581 ResultSet rs = ps.executeQuery();
9582
9583 while(rs.next())
9584 {
9585 ProjectList plist = new
9586ProjectList(rs.getInt("pid"), rs.getString("pname"));
9587 model.addElement(plist);
9588 }
9589 project_list.setModel(model);
9590 ps.close();
9591 rs.close();
9592 conn.close();
9593 }
9594 catch (SQLException e1)
9595 {
9596 e1.printStackTrace();
9597 } } }
9598
9599
9600JAVA LAB
9601
9602
9603
9604
9605
9606188
9607
9608
9609
9610newProject.java
9611
9612
9613
9614import javax.swing.JFrame;
9615import javax.swing.JLabel;
9616import javax.swing.JTextField;
9617import javax.swing.JButton;
9618import java.awt.Font;
9619import javax.swing.SwingConstants;
9620import javax.swing.JTextArea;
9621import javax.swing.JScrollPane;
9622import javax.swing.DropMode;
9623import java.awt.event.ActionListener;
9624import java.sql.Connection;
9625import java.sql.PreparedStatement;
9626import java.sql.ResultSet;
9627import java.sql.SQLException;
9628import java.awt.event.ActionEvent;
9629
9630public class newProject extends JFrame {
9631 private JTextField projectName;
9632 public newProject() {
9633 setType(Type.UTILITY);
9634 setTitle("New Kanban Board");
9635 setResizable(false);
9636 setAlwaysOnTop(true);
9637 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9638 setBounds(100, 100, 393, 267);
9639 getContentPane().setLayout(null);
9640 getContentPane().setLayout(null);
9641 getContentPane().setLayout(null);
9642
9643 JLabel lblNewLabel_1 = new JLabel("Name");
9644 lblNewLabel_1.setFont(new Font("Consolas", Font.PLAIN, 15));
9645
9646 lblNewLabel_1.setHorizontalAlignment(SwingConstants.TRAILING);
9647 lblNewLabel_1.setBounds(10, 10, 92, 27);
9648 getContentPane().add(lblNewLabel_1);
9649
9650 projectName = new JTextField();
9651 projectName.setToolTipText("Enter a project name");
9652 projectName.setFont(new Font("Consolas", Font.PLAIN, 15));
9653 projectName.setBounds(112, 10, 254, 27);
9654 getContentPane().add(projectName);
9655
9656
9657JAVA LAB
9658
9659
9660
9661
9662
9663189
9664
9665
9666
9667 projectName.setColumns(10);
9668
9669 JLabel lblDescription = new JLabel("Description");
9670
9671 lblDescription.setHorizontalAlignment(SwingConstants.TRAILING);
9672 lblDescription.setFont(new Font("Consolas", Font.PLAIN, 15));
9673 lblDescription.setBounds(10, 47, 92, 27);
9674 getContentPane().add(lblDescription);
9675
9676 JScrollPane scrollPane = new JScrollPane();
9677 scrollPane.setBounds(112, 47, 254, 134);
9678 getContentPane().add(scrollPane);
9679
9680 JTextArea projectDescr = new JTextArea();
9681 projectDescr.setLineWrap(true);
9682 projectDescr.setToolTipText("Enter project description");
9683 projectDescr.setDropMode(DropMode.INSERT);
9684 scrollPane.setViewportView(projectDescr);
9685
9686 JButton createProject = new JButton("Create Board");
9687 createProject.addActionListener(new ActionListener() {
9688
9689 public void actionPerformed(ActionEvent e)
9690 {
9691 Connection conn = DBConnection.connect();
9692 String insProjects = "insert into
9693projects(pname,pdescr) values(?,?)";
9694
9695 try
9696 {
9697 PreparedStatement ps =
9698conn.prepareStatement(insProjects);
9699 ps.setString(1,projectName.getText());
9700 ps.setString(2,projectDescr.getText());
9701 ps.execute();
9702
9703 ps = conn.prepareStatement("select MAX(pid)
9704from projects");
9705 ResultSet rs = ps.executeQuery();
9706 rs.next();
9707
9708 Planner plannerWindow = new
9709Planner(rs.getInt(1));
9710 plannerWindow.setVisible(true);
9711
9712
9713
9714JAVA LAB
9715
9716
9717
9718
9719
9720190
9721
9722
9723
9724 ps.close();
9725 conn.close();
9726 dispose();
9727 }
9728 catch (SQLException e1)
9729 {
9730 e1.printStackTrace();
9731 }
9732 }
9733 });
9734 createProject.setToolTipText("Click here to create a new
9735project");
9736 createProject.setFont(new Font("Consolas", Font.PLAIN, 15));
9737 createProject.setBounds(112, 191, 165, 27);
9738 getContentPane().add(createProject);
9739 }
9740}
9741
9742
9743
9744JAVA LAB
9745
9746
9747
9748
9749
9750191
9751
9752
9753
9754Planner.java
9755
9756
9757
9758import javax.swing.JFrame;
9759import javax.swing.JPanel;
9760import javax.swing.border.EmptyBorder;
9761import javax.swing.JTextField;
9762import javax.swing.JScrollPane;
9763import javax.swing.JList;
9764import javax.swing.JLabel;
9765import javax.swing.SwingConstants;
9766import javax.swing.DefaultListModel;
9767
9768import java.awt.Font;
9769import javax.swing.JButton;
9770import javax.swing.ListSelectionModel;
9771import java.sql.Connection;
9772import java.sql.PreparedStatement;
9773import java.sql.ResultSet;
9774import java.sql.SQLException;
9775import java.awt.event.ActionListener;
9776import java.awt.event.ActionEvent;
9777import javax.swing.JTextArea;
9778import java.awt.Color;
9779import java.awt.event.KeyAdapter;
9780import java.awt.event.KeyEvent;
9781
9782public class Planner extends JFrame
9783{
9784
9785 private JPanel contentPane;
9786 private JTextField txtTodo;
9787 private JTextField txtWip;
9788 private JTextField txtDone;
9789
9790 public Planner(int planID)
9791 {
9792 setTitle("Kanban Plan");
9793 int PlanID = planID;
9794
9795 setResizable(false);
9796 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9797 setBounds(100, 100, 810, 572);
9798 contentPane = new JPanel();
9799
9800
9801JAVA LAB
9802
9803
9804
9805
9806
9807192
9808
9809
9810
9811 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
9812 setContentPane(contentPane);
9813 contentPane.setLayout(null);
9814
9815 JScrollPane scrollPane = new JScrollPane();
9816 scrollPane.setBounds(0, 83, 808, 456);
9817 contentPane.add(scrollPane);
9818
9819 JPanel panel = new JPanel();
9820 scrollPane.setViewportView(panel);
9821
9822 JScrollPane todo_scroller = new JScrollPane();
9823 todo_scroller.setBounds(5, 5, 258, 376);
9824
9825 Connection conn = DBConnection.connect();
9826
9827 String lblNametext = null;
9828 String lblDescriptiontext = null;
9829 try
9830 {
9831 PreparedStatement ps = conn.prepareStatement("Select
9832pname,pdescr from projects where pid =" + PlanID);
9833 ResultSet rs = ps.executeQuery();
9834 rs.next();
9835 lblNametext = rs.getString("pname");
9836 lblDescriptiontext = rs.getString("pdescr");
9837 ps.close();
9838 rs.close();
9839 }
9840 catch (SQLException e2)
9841 {
9842 e2.printStackTrace();
9843 }
9844
9845 JLabel lblName = new JLabel(lblNametext);
9846 lblName.setFont(new Font("Tahoma", Font.BOLD, 13));
9847 lblName.setHorizontalAlignment(SwingConstants.CENTER);
9848 lblName.setBounds(10, 10, 229, 63);
9849 contentPane.add(lblName);
9850
9851 JTextArea txtDescription = new JTextArea();
9852 txtDescription.setBackground(Color.GRAY);
9853 txtDescription.setWrapStyleWord(true);
9854 txtDescription.setLineWrap(true);
9855 txtDescription.setText(lblDescriptiontext);
9856
9857
9858JAVA LAB
9859
9860
9861
9862
9863
9864193
9865
9866
9867
9868 txtDescription.setEditable(false);
9869 txtDescription.setBounds(249, 10, 547, 67);
9870 contentPane.add(txtDescription);
9871
9872 JList<TaskList> listTodo = new JList<TaskList>();
9873 listTodo.setBackground(Color.GRAY);
9874 listTodo.setFont(new Font("Tahoma", Font.PLAIN, 13));
9875 updateTodo(PlanID, conn, listTodo);
9876
9877 listTodo.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
9878 todo_scroller.setViewportView(listTodo);
9879
9880 JLabel lblNewLabel = new JLabel("TO-DO");
9881 lblNewLabel.setLabelFor(listTodo);
9882 lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
9883 todo_scroller.setColumnHeaderView(lblNewLabel);
9884
9885 JScrollPane wip_scroller = new JScrollPane();
9886 wip_scroller.setBounds(268, 5, 258, 376);
9887
9888 JList<TaskList> listWip = new JList<TaskList>();
9889 listWip.setBackground(Color.GRAY);
9890 listWip.setFont(new Font("Tahoma", Font.PLAIN, 13));
9891 updateWip(PlanID, conn, listWip);
9892
9893 listWip.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
9894 wip_scroller.setViewportView(listWip);
9895
9896 JLabel lblWorkInProgress = new JLabel("Work In Progress");
9897 lblWorkInProgress.setLabelFor(listWip);
9898
9899 lblWorkInProgress.setHorizontalAlignment(SwingConstants.CENTER);
9900 wip_scroller.setColumnHeaderView(lblWorkInProgress);
9901
9902 JScrollPane done_scroller = new JScrollPane();
9903 done_scroller.setBounds(531, 5, 258, 376);
9904
9905 JList<TaskList> listDone = new JList<TaskList>();
9906 listDone.setBackground(Color.GRAY);
9907 listDone.setFont(new Font("Tahoma", Font.PLAIN, 13));
9908 updateDone(PlanID, conn, listDone);
9909
9910 listDone.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
9911 done_scroller.setViewportView(listDone);
9912
9913
9914
9915JAVA LAB
9916
9917
9918
9919
9920
9921194
9922
9923
9924
9925 JLabel lblNewLabel_1 = new JLabel("Done");
9926 lblNewLabel_1.setLabelFor(listDone);
9927 lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
9928 done_scroller.setColumnHeaderView(lblNewLabel_1);
9929 panel.setLayout(null);
9930 panel.add(todo_scroller);
9931 panel.add(wip_scroller);
9932 panel.add(done_scroller);
9933
9934 JButton btnAddTODO = new JButton("Add TODO");
9935 btnAddTODO.setBounds(5, 415, 199, 21);
9936 panel.add(btnAddTODO);
9937
9938 JButton btnAddWIP = new JButton("Add WIP");
9939 btnAddWIP.setBounds(324, 415, 146, 21);
9940 panel.add(btnAddWIP);
9941
9942 JButton btnAddDONE = new JButton("Add DONE");
9943 btnAddDONE.setBounds(590, 415, 199, 21);
9944 panel.add(btnAddDONE);
9945
9946 txtTodo = new JTextField();
9947 txtTodo.addKeyListener(new KeyAdapter()
9948 {
9949 @Override
9950 public void keyTyped(KeyEvent e)
9951 {
9952 if(e.getKeyCode() == KeyEvent.VK_ENTER)
9953 {
9954 System.out.println("enter pressed");
9955 }
9956 }
9957 });
9958 txtTodo.setBackground(Color.LIGHT_GRAY);
9959 txtTodo.setBounds(5, 386, 258, 21);
9960 panel.add(txtTodo);
9961 txtTodo.setColumns(10);
9962
9963 txtWip = new JTextField();
9964 txtWip.setBackground(Color.LIGHT_GRAY);
9965 txtWip.setBounds(268, 386, 258, 19);
9966 panel.add(txtWip);
9967 txtWip.setColumns(10);
9968
9969 txtDone = new JTextField();
9970
9971
9972JAVA LAB
9973
9974
9975
9976
9977
9978195
9979
9980
9981
9982 txtDone.setBackground(Color.LIGHT_GRAY);
9983 txtDone.setBounds(531, 387, 258, 19);
9984 panel.add(txtDone);
9985 txtDone.setColumns(10);
9986
9987 JButton todoTOwip = new JButton(">");
9988
9989 todoTOwip.setBounds(214, 415, 49, 21);
9990 panel.add(todoTOwip);
9991
9992 JButton wipTOtodo = new JButton("<");
9993 wipTOtodo.setBounds(268, 415, 49, 21);
9994 panel.add(wipTOtodo);
9995
9996 JButton wipTOdone = new JButton(">");
9997 wipTOdone.setBounds(477, 415, 49, 21);
9998 panel.add(wipTOdone);
9999
10000 JButton doneTOwip = new JButton("<");
10001 doneTOwip.setBounds(531, 415, 49, 21);
10002 panel.add(doneTOwip);
10003//=======================================================================
10004======== transfer handlers
10005 todoTOwip.addActionListener(new ActionListener()
10006 {
10007 public void actionPerformed(ActionEvent e)
10008 {
10009 TaskList task = listTodo.getSelectedValue();
10010 if(task != null)
10011 {
10012 try
10013 {
10014 PreparedStatement ps =
10015conn.prepareStatement("update tasklist set state = 'wip' where taskid =
10016?");
10017 ps.setInt(1, task.getTaskID());
10018 ps.execute();
10019 ps.close();
10020 }
10021 catch (SQLException e1)
10022 {
10023 e1.printStackTrace();
10024 }
10025 updateTodo(PlanID, conn, listTodo);
10026 updateWip(PlanID, conn, listWip);
10027
10028
10029JAVA LAB
10030
10031
10032
10033
10034
10035196
10036
10037
10038
10039 }
10040 }
10041 });
10042
10043 wipTOtodo.addActionListener(new ActionListener()
10044 {
10045 public void actionPerformed(ActionEvent e)
10046 {
10047 TaskList task = listWip.getSelectedValue();
10048 if(task != null)
10049 {
10050 try
10051 {
10052 PreparedStatement ps =
10053conn.prepareStatement("update tasklist set state = 'todo' where taskid =
10054?");
10055 ps.setInt(1, task.getTaskID());
10056 ps.execute();
10057 ps.close();
10058 }
10059 catch (SQLException e1)
10060 {
10061 e1.printStackTrace();
10062 }
10063 updateTodo(PlanID, conn, listTodo);
10064 updateWip(PlanID, conn, listWip);
10065 }
10066 }
10067 });
10068
10069 wipTOdone.addActionListener(new ActionListener()
10070 {
10071 public void actionPerformed(ActionEvent e)
10072 {
10073 TaskList task = listWip.getSelectedValue();
10074 if(task != null)
10075 {
10076 try
10077 {
10078 PreparedStatement ps =
10079conn.prepareStatement("update tasklist set state = 'done' where taskid =
10080?");
10081 ps.setInt(1, task.getTaskID());
10082 ps.execute();
10083 ps.close();
10084
10085
10086JAVA LAB
10087
10088
10089
10090
10091
10092197
10093
10094
10095
10096 }
10097 catch (SQLException e1)
10098 {
10099 e1.printStackTrace();
10100 }
10101 updateDone(PlanID, conn, listDone);
10102 updateWip(PlanID, conn, listWip);
10103 }
10104 }
10105 });
10106
10107 doneTOwip.addActionListener(new ActionListener()
10108 {
10109 public void actionPerformed(ActionEvent e)
10110 {
10111 TaskList task = listDone.getSelectedValue();
10112 if(task != null)
10113 {
10114 try
10115 {
10116 PreparedStatement ps =
10117conn.prepareStatement("update tasklist set state = 'wip' where taskid =
10118?");
10119 ps.setInt(1, task.getTaskID());
10120 ps.execute();
10121 ps.close();
10122 }
10123 catch (SQLException e1)
10124 {
10125 e1.printStackTrace();
10126 }
10127 updateDone(PlanID, conn, listDone);
10128 updateWip(PlanID, conn, listWip);
10129 }
10130 }
10131 });
10132//=======================================================================
10133======== add tasks
10134 btnAddTODO.addActionListener(new ActionListener()
10135 {
10136 public void actionPerformed(ActionEvent e)
10137 {
10138 if(txtTodo.getText().isBlank())
10139 {
10140 //do nothing
10141
10142
10143JAVA LAB
10144
10145
10146
10147
10148
10149198
10150
10151
10152
10153 }
10154 else
10155 {
10156 String text = txtTodo.getText();
10157 txtTodo.setText("");
10158 try
10159 {
10160 PreparedStatement ps =
10161conn.prepareStatement("insert into
10162tasklist(pid,task,state)values(?,?,'todo')");
10163 ps.setInt(1, PlanID);
10164 ps.setString(2, text);
10165 ps.execute();
10166 ps.close();
10167 }
10168 catch (SQLException e1)
10169 {
10170 e1.printStackTrace();
10171 }
10172 }
10173 updateTodo(PlanID, conn, listTodo);
10174 }
10175 });
10176
10177 btnAddWIP.addActionListener(new ActionListener()
10178 {
10179 public void actionPerformed(ActionEvent e)
10180 {
10181 if(txtWip.getText().isBlank())
10182 {
10183 //do nothing
10184 }
10185 else
10186 {
10187 String text = txtWip.getText();
10188 txtWip.setText("");
10189 try
10190 {
10191 PreparedStatement ps =
10192conn.prepareStatement("insert into
10193tasklist(pid,task,state)values(?,?,'wip')");
10194 ps.setInt(1, PlanID);
10195 ps.setString(2, text);
10196 ps.execute();
10197 ps.close();
10198
10199
10200JAVA LAB
10201
10202
10203
10204
10205
10206199
10207
10208
10209
10210 }
10211 catch (SQLException e1)
10212 {
10213 e1.printStackTrace();
10214 }
10215 }
10216 updateWip(PlanID, conn, listWip);
10217 }
10218 });
10219
10220 btnAddDONE.addActionListener(new ActionListener()
10221 {
10222 public void actionPerformed(ActionEvent e)
10223 {
10224 if(txtDone.getText().isBlank())
10225 {
10226 //do nothing
10227 }
10228 else
10229 {
10230 String text = txtDone.getText();
10231 txtDone.setText("");
10232 try
10233 {
10234 PreparedStatement ps =
10235conn.prepareStatement("insert into
10236tasklist(pid,task,state)values(?,?,'done')");
10237 ps.setInt(1, PlanID);
10238 ps.setString(2, text);
10239 ps.execute();
10240 ps.close();
10241 }
10242 catch (SQLException e1)
10243 {
10244 e1.printStackTrace();
10245 }
10246 }
10247 updateDone(PlanID, conn, listDone);
10248 }
10249 });
10250//=======================================================================
10251========
10252 }
10253
10254
10255
10256JAVA LAB
10257
10258
10259
10260
10261
10262200
10263
10264
10265
10266 private void updateDone(int PlanID, Connection conn,
10267JList<TaskList> listDone) {
10268 try
10269 {
10270 DefaultListModel<TaskList> model = new
10271DefaultListModel<TaskList>();
10272
10273 PreparedStatement ps1 = conn.prepareStatement("select
10274taskid,pid,task,state from tasklist where pid = ? and state = 'done'");
10275 ps1.setInt(1, PlanID);
10276 ResultSet rs1 = ps1.executeQuery();
10277
10278 while(rs1.next())
10279 {
10280 int pid = rs1.getInt("pid");
10281 int tid = rs1.getInt("taskid");
10282 String task = rs1.getString("task");
10283 String state = rs1.getString("state");
10284
10285 TaskList taskList = new TaskList(pid, tid, task,
10286state);
10287 model.addElement(taskList);
10288 }
10289
10290 listDone.setModel(model);
10291 ps1.close();
10292 rs1.close();
10293 }
10294 catch (SQLException e1)
10295 {
10296 e1.printStackTrace();
10297 }
10298 }
10299
10300 private void updateWip(int PlanID, Connection conn, JList<TaskList>
10301listWip)
10302 {
10303 try
10304 {
10305 DefaultListModel<TaskList> model = new
10306DefaultListModel<TaskList>();
10307
10308 PreparedStatement ps1 = conn.prepareStatement("select
10309taskid,pid,task,state from tasklist where pid = ? and state = 'wip'");
10310 ps1.setInt(1, PlanID);
10311
10312
10313JAVA LAB
10314
10315
10316
10317
10318
10319201
10320
10321
10322
10323 ResultSet rs1 = ps1.executeQuery();
10324
10325 while(rs1.next())
10326 {
10327 int pid = rs1.getInt("pid");
10328 int tid = rs1.getInt("taskid");
10329 String task = rs1.getString("task");
10330 String state = rs1.getString("state");
10331
10332 TaskList taskList = new TaskList(pid, tid, task,
10333state);
10334 model.addElement(taskList);
10335 }
10336
10337 listWip.setModel(model);
10338 ps1.close();
10339 rs1.close();
10340 }
10341 catch (SQLException e1)
10342 {
10343 e1.printStackTrace();
10344 }
10345 }
10346
10347 private void updateTodo(int PlanID, Connection conn,
10348JList<TaskList> listTodo)
10349 {
10350 try
10351 {
10352 DefaultListModel<TaskList> model = new
10353DefaultListModel<TaskList>();
10354
10355 PreparedStatement ps1 = conn.prepareStatement("select
10356taskid,pid,task,state from tasklist where pid = ? and state = 'todo'");
10357 ps1.setInt(1, PlanID);
10358 ResultSet rs1 = ps1.executeQuery();
10359
10360 while(rs1.next())
10361 {
10362 int pid = rs1.getInt("pid");
10363 int tid = rs1.getInt("taskid");
10364 String task = rs1.getString("task");
10365 String state = rs1.getString("state");
10366
10367
10368
10369JAVA LAB
10370
10371
10372
10373
10374
10375202
10376
10377
10378
10379 TaskList taskList = new TaskList(pid, tid, task,
10380state);
10381 model.addElement(taskList);
10382 }
10383
10384 listTodo.setModel(model);
10385 ps1.close();
10386 rs1.close();
10387 }
10388 catch (SQLException e1)
10389 {
10390 e1.printStackTrace();
10391 }
10392 }
10393}
10394
10395ProjectList.java
10396
10397public class ProjectList
10398{
10399 private int pid;
10400 private String listdata;
10401
10402 ProjectList(int i,String name)
10403 {
10404 pid = i;
10405 listdata = name;
10406 }
10407
10408 public int getID()
10409 {
10410 return pid;
10411 }
10412 public String getListData()
10413 {
10414 return listdata;
10415 }
10416
10417 @Override
10418 public String toString()
10419 {
10420 return listdata;
10421 }
10422}
10423
10424
10425
10426JAVA LAB
10427
10428
10429
10430
10431
10432203
10433
10434
10435
10436TaskList.java
10437
10438public class TaskList
10439{
10440 private int pId;
10441 private int taskId;
10442 private String task;
10443 private String state;
10444
10445 TaskList(int pid, int tid, String task, String state)
10446 {
10447 this.pId = pid;
10448 this.taskId = tid;
10449 this.task = task;
10450 this.state = state;
10451 }
10452 @Override
10453 public String toString(){return task;}
10454 public int getPID() {return pId;}
10455 public int getTaskID() {return taskId;}
10456 public String getTask() {return task;}
10457 public String getState() {return state;}
10458}
10459
10460
10461
10462JAVA LAB
10463
10464
10465
10466
10467
10468204
10469
10470
10471
10472SCREENSHOTS
10473
10474
10475
10476