Upcoming SlideShare
×

# Java PRACTICAL file

7,965

Published on

COMPLETE PRACTICAL FILE OF BASIC JAVA LANGUAGE

5 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
7,965
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
1,102
0
Likes
5
Embeds 0
No embeds

No notes for slide

### Java PRACTICAL file

1. 1. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 1 /**WAP TO CALCULATE SQUARE ROOT OF A NUMBER.*/ import java.lang.Math; import java.util.Scanner; class Rachit { Scanner input=new Scanner(System.in); public static void main(String[] args) { int a; double b; System.out.println(“ENTER A NUMBER”); A=input.nextInt(); b=Math.sqrt(a); System.out.println("the square root of a is"+b); } } OUTPUT: ENTER A NUMBER 25 the square root of a is 5.0
2. 2. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 2 /**WAP TO FIND PERIMETER OF A RECTANGLE.*/ import java.util.Scanner; int len,brd; void getdata(int l,int b) { len=l; brd=b; } int CIRCUMFERENCE() { int a; a=2*(len * brd); return a; } } class display { public static void main(String args[]) { Scanner input=new Scanner(System.in); int l,b,area; System.out.println("ENTER THE LENGTH :"); l=input.nextInt(); System.out.println("ENTER THE BREADTH :"); b=input.nextInt(); rectangle r=new rectangle(); r.getdata(l,b); area=r.CIRCUMFERENCE();
3. 3. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 3 System.out.println("THE CIRCUMFERENCE OF RECTANGLE IS :"+area); } } OUTPUT: ENTER THE LENGTH : 10 ENTER THE BREADTH : 5 THE CIRCUMFERENCE OF RECTANGLE IS :100
4. 4. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 4 /**WAP TO CALCULATE AREA OF A CIRCLE.*/ import java.util.Scanner; public class NewClass { public static void main(String[] args) { Scanner input=new Scanner(System.in); int a; double area; final double pi=3.14578; System.out.println(“ENTER THE RADIUS OF THE CIRCLE); a=input.nextInt(); area=pi*a*a; System.out.println("Area of circle with radius "+a); System.out.print(" is given as :"+area); } } OUTPUT: ENTER THE RADIUS OF THE CIRCLE 12 Area of circle with radius 12 is given as :452.9923199999999
5. 5. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 5 /**WAP TO CALCULATE EXPONENT (M RAISED TO POWER N).*/ package rachit; import java.lang.Math; import java.util.Scanner; class EXPONENTATION { Scanner input=new Scanner(System.in); public static void main(String args[]) { int a,b; double result; System.out.println("ENTER IST INTEGER”); a=input.nextInt(); System.out.println("ENTER 2nd INTEGER”); b=input.nextInt(); result=Math.pow(a,b); System.out.println("The result of exponentation is :"+result); } } OUTPUT: ENTER IST INTEGER 5 ENTER 2ND INTEGER 4 The result of exponentation is : 625.0
6. 6. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 6 /**WAP TO FIND GREATER OF THREE NUMBERS USING IF ELSE.*/ class GRT_3_NOS { public static void main(String args[]) { Scanner input=new Scanner(System.in); int a,b,c; a=input.nextInt(); b=input.nextInt(); c=input.nextInt(); if((a==b)&&(a==c)) System.out.println("All numbers are equal"); else if(a>b) { if(a>c) System.err.println("Greater number is :"+a); else System.out.println("Greater number is :"+c); } else { if(b>c) System.out.println("Greater number is :"+b); else System.out.println("Greater number is :"+c); } } }
7. 7. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 7 OUTPUT: 45 99 88 Greater number is : 99
8. 8. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 8 /**Wap to find greater of two numbers using ternary operator.*/ Package rachit; import java.util.Scanner; class GRTER _3_NOS { public static void main(String args[]) { Scanner input=new Scanner(System.in); int a,b,c,d; a=input.nextInt(); b=input.nextInt(); c=input.nextInt(); d=(a>b)?((a>c)?a:c):((b>c)?b:c); System.out.println("Greater number is :"+d); } } OUTPUT: 55 77 11 Greater number is :77
9. 9. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 9 /**wap to check whether number is even or odd.*/ class EVEN_ODD { public static void main(String args[]) { Scanner input=new Scanner(System.in); int a; a=input.nextInt(); if(a%2==0) { System.out.println("The Entered Number is Even"); } else { System.out.println("The Entered Number is ODD"); } } } OUTPUT: 55 The Entered Number is ODD 10000 The Entered Number is Even
10. 10. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 10 /**wap to calculate percentage(%) of a student in three subjects.*/ import java.util.Scanner; public class percentage { public static void main(String args[]) { Scanner input=new Scanner(System.in); double a,b,c,percent; a=input.nextDouble(); b=input.nextDouble(); c=input.nextDouble(); percent=((a+b+c)/300)*100; if(percent>=75) System.out.println("Distinction scored"); else if((percent>=65)&&(percent<75)) System.out.println("IST DIVISION SCORED"); else if((percent>=55)&&(percent<65)) System.out.println("2ND DIVISION SCORED"); else if((percent>=35)&&(percent<55)) System.out.println("3RD DIVISION SCORED"); else System.out.println("student failed"); } }
11. 11. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 11 OUTPUT: 99 88 77 Distinction scored 55 44 66 2ND DIVISION SCORED
12. 12. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 12 /**wap to find sum of first n natural numbers using do while loop.*/ import java.util.Scanner; public class sum_natural_nos { public static void main(String args[]) { Scanner input=new Scanner(System.in); int lt,sum=0,i=1; System.out.println("ENTER THE LIMIT UPTO WHICH SUM IS DESIRED"); lt=input.nextInt(); do{ sum=sum+i; i++; }while(i<=lt); System.out.print("THE SUM OF NATURAL NUMBERS UPTO "+lt); System.out.print(" IS :"+sum); System.out.println(""); } } OUTPUT: ENTER THE LIMIT UPTO WHICH SUM IS DESIRED 100 THE SUM OF NATURAL NUMBERS UPTO 100 IS :5050
13. 13. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 13 /**wap to find average of first n natural number using for loop.*/ import java.util.Scanner; public class avg_natural_nos { public static void main(String args[]) { Scanner input=new Scanner(System.in); int lt,sum=0,i=1; double avg; System.out.println("ENTER THE LIMIT UPTO WHICH AVERAGE IS DESIRED"); lt=input.nextInt(); for(i=0; i<=lt; i++) { sum=sum+i; } avg=(double)sum/lt; System.out.print("THE AVERAGE OF NATURAL NUMBERS UPTO "+lt); System.out.print(" IS :"+avg); System.out.println(""); } } OUTPUT: ENTER THE LIMIT UPTO WHICH AVERAGE IS DESIRED 5 THE AVERAGE OF NATURAL NUMBERS UPTO 5 IS :3.0 ENTER THE LIMIT UPTO WHICH AVERAGE IS DESIRED 100 THE AVERAGE OF NATURAL NUMBERS UPTO 100 IS :50.5
14. 14. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 14 /**wap to find area of a rectangle using two classes.*/ package rachit; import java.util.Scanner; public class rectangle_area { public static void main(String args[]) { rect r=new rect(); r.getdata(); } } class rect { Scanner input=new Scanner(System.in); int l,b,peri,area; void getdata() { System.out.println("Enter the length of rectangle :"); l=input.nextInt(); System.out.println("Enter the breadth of rectangle :"); b=input.nextInt(); area=l*b; System.out.println("The area of rectangle is : "+area); } }
15. 15. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 15 OUTPUT: Enter the length of rectangle : 55 Enter the breadth of rectangle : 44 The area of rectangle is : 2420
16. 16. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 16 /**wap to find area and circumference of a circle.*/ import java.util.Scanner; public class area_circum_circle { final double pi=3.14; double radius; void getdata() { Scanner input=new Scanner(System.in); System.out.println("Enter the radius :"); radius=input.nextDouble(); } double area() { double ar; ar=pi*radius*radius; return ar; } double circumference() { double circum; circum=2*pi*radius; return circum; } } class Display
17. 17. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 17 { public static void main(String args[]) { area_circum_circle ac=new area_circum_circle(); double area,circum; ac.getdata(); area=ac.area(); circum=ac.circumference(); System.out.println("THE AREA IS :"+area); System.out.println("THE CIRCUMFERENCE IS :"+circum); } } OUTPUT: Enter the radius : 55 THE AREA IS :9498.500000000002 THE CIRCUMFERENCE IS :345.40000000000003
18. 18. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 18 /**wap to generate electric bill of a person under the following rules a. a)for first 100 units rs. 1/unit. b. b)for next 200 units rs.2/unit . c. c)for further units rs.3/unit.*/ import java.util.Scanner; public class electric_bill { int units,code; int r; String name; void getdata() { Scanner input=new Scanner(System.in); System.out.println("Enter the name"); name=input.next(); System.out.println("Enter the code"); code=input.nextInt(); System.out.println("Enter the units"); units=input.nextInt(); } int calculate() { int u; if(units<=100) { r=units*1; } else if((units<=300)&&(units>100))
19. 19. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 19 { u=units-100; r=100+u*2; } else if(units>300) { u=units-300; r=500+u*3; } else System.out.println("Enter correct units"); System.out.println("tttELECTRIC BILL"); System.out.println("--------------------------------------------------"); System.out.println("tNAME :"+name); System.out.println("tCODE :"+code); System.out.println("tUNITS CONSUMED :"+units); System.out.println("--------------------------------------------------"); System.out.println("tTOTAL AMOUNT TO BE PAID :"+r); return 0; } } class Show { public static void main(String args[]) { int rs; electric_bill e=new electric_bill();
20. 20. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 20 e.getdata(); rs=e.calculate(); } } OUTPUT: Enter the name RACHIT Enter the code 66 Enter the units 2041 ELECTRIC BILL -------------------------------------------------- NAME :RACHIT CODE :66 UNITS CONSUMED :2041 -------------------------------------------------- TOTAL AMOUNT TO BE PAID :5723
21. 21. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 21 /**wap showing concept of switch statement.*/ import java.util.Scanner; public class switch_case { public static void main(String args[]) { Scanner input=new Scanner(System.in); int a,b,ch; do { System.out.println("******************************************************************"); System.out.println("ttEnter 1 for ADDITION"); System.out.println("ttEnter 2 for MULTIPLICATION"); System.out.println("ttEnter 3 for SUBTRACTION"); System.out.println("ttEnter 4 for DIVISION"); System.out.println("ttEnter 0 for EXIT"); System.out.println("******************************************************************"); System.out.println("ntt Enter your choice :"); ch=input.nextInt(); int add,sub,mul; double div; switch(ch) { case 1:System.out.println("Enter two integers :"); a=input.nextInt(); b=input.nextInt(); add=a+b; System.out.println("ADDITION YIELDS :"+add);break;
22. 22. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 22 case 2:System.out.println("Enter two integers :"); a=input.nextInt(); b=input.nextInt(); mul=a*b; System.out.println("MULTIPLICATION YIELDS :"+mul);break; case 3:System.out.println("Enter two integers :"); a=input.nextInt(); b=input.nextInt(); sub=a-b; System.out.println("SUBTRACTION YIELDS :"+sub);break; case 4:System.out.println("Enter two integers :"); a=input.nextInt(); b=input.nextInt(); div=(double)a/b; System.out.println("DIVISION YIELDS :"+div);break; case 0: return; default :{ System.out.println("INVALID CHOICE"); return; } } } while(ch!=0); } }
23. 23. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 23 OUTPUT: ****************************************************************** Enter 1 for ADDITION Enter 2 for MULTIPLICATION Enter 3 for SUBTRACTION Enter 4 for DIVISION Enter 0 for EXIT ****************************************************************** Enter your choice : 1 Enter two integers : 88 101010 ADDITION YIELDS :101098 ****************************************************************** Enter 1 for ADDITION Enter 2 for MULTIPLICATION Enter 3 for SUBTRACTION Enter 4 for DIVISION Enter 0 for EXIT ****************************************************************** Enter your choice : 4 Enter two integers : 164895 5 DIVISION YIELDS :32979.0
24. 24. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 24 /**wap using break and continue statement.*/ public class break_continue { public static void main(String args[]) { int i; for(i=0;i<=10;i++) { System.out.println("i="+i); if(i==5) break; } System.out.println("======================================================"); for(i=0;i<=10;i++) { if(i==5) continue; System.out.println("i="+i); } } }
25. 25. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 25 OUTPUT: BREAK STATEMENT i=0 i=1 i=2 i=3 i=4 i=5 ====================================================== CONTINUE STATEMENT i=0 i=1 i=2 i=3 i=4 i=6 i=7 i=8 i=9 i=10
26. 26. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 26 /**wap to generate fibonacii series.*/ package rachit; import java.util.Scanner; public class fibonaci { public static void main(String args[]) { int a=0,b=1,sum=0,limit,i=1; System.out.println("Enter the limit upto which series is to be printed :"); Scanner input=new Scanner(System.in); limit=input.nextInt(); System.out.print("tTHE SERIES IS AS FOLLOW :"); System.out.print("0"); System.out.print("t1"); while(i!=limit-1) { sum=a+b; a=b; b=sum; System.out.print("t"+sum); i++; } System.out.println(""); } } OUTPUT: Enter the limit upto which series is to be printed : 10 THE SERIES IS AS FOLLOW :0 1 1 2 3 5 8 13 21 34
27. 27. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 27 /**wap to convert binary no. into decimal no.*/ import java.util.Scanner; import java.lang.Math; public class bin_to_dec { public static void main(String args[]) { int bnum,dnum=0,rem,i=0; Scanner input=new Scanner(System.in); System.out.println("ENTER ANY BINARY NUMBER"); bnum=input.nextInt(); int num=bnum; while(bnum!=0) { rem=bnum%10; dnum=dnum+rem*(int)Math.pow(2,i); bnum=bnum/10; i++; } System.out.print("DECIMAL NUMBER EQUIVQLENT TO "+num); System.out.print(" IS "+dnum); System.out.println(); } } OUTPUT: ENTER ANY BINARY NUMBER 101010111 DECIMAL NUMBER EQUIVQLENT TO 101010111 IS 343
28. 28. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 28 /**wap to find lcm and hcf of two numbers.*/ import java.util.Scanner; public class hcf_lcm { public static void main(String args[]) { Scanner input=new Scanner(System.in); int num1,num2,rem=0; System.out.println("ENter two +ve nos :"); num1=input.nextInt(); int a=num1; num2=input.nextInt(); int b=num2; while(num2!=0) { rem=num1%num2; num1=num2; num2=rem; } int lcm; lcm=(a*b)/num1; System.out.println(num1+" is hcf"); System.out.println(lcm+" is LCM"); } } OUTPUT: ENter two +ve nos : 88 24 8 is hcf 264 is LCM
29. 29. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 29 /**wap to find armstrong number in between 1-1000.*/ import java.lang.Math; public class armstrong_num { public static void main(String args[]) { int i,num; System.out.println("ARMSTRONG NUMBERS LESS THAN 1000 ARE AS FOLLOW"); for(i=100;i<=1000;i++) { num=i; int sum=0,rem; while(num!=0) { rem=num%10; sum=sum+(int)Math.pow(rem,3); num=num/10; } if(i==sum) System.out.print("t"+sum); } } } OUTPUT: ARMSTRONG NUMBERS LESS THAN 1000 ARE AS FOLLOW 153 370 366 407
30. 30. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 30 /**wap to find first n prime numbers.*/ import java.util.Scanner; public class prime_nos { public static void main(String args[]) { Scanner input=new Scanner(System.in); int lim,num,i,j; System.out.println("ENTER THE LIMIT UPTO WHICH PRIME NUMBERS ARE TO BE PRINTED"); lim=input.nextInt(); System.out.print("2t"); for(i=3;i<lim;i++) { num=i; for(j=2;j<=num;j++) { if((num%j==0)&&(num!=j)) break; }if(j>num/2) System.out.print(num+"t"); } System.out.println(); } } OUTPUT: ENTER THE LIMIT UPTO WHICH PRIME NUMBERS ARE TO BE PRINTED 15 2 3 5 7 11 13
31. 31. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 31 /**wap to check whether character is vowel or not.*/ import java.util.Scanner; public class vowels_switch { public static void main(String args[]) { Scanner input=new Scanner(System.in); String ch; int i=1; do { System.out.println("Enter an Alphabet"); ch=input.next(); switch(ch) { case "A": case "a": case "E": case "e": case "I": case "i": case "O": case "o": case "U": case "u": System.out.println("its an vowel"); break; default: System.out.println("its not an vowel");
32. 32. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 32 i++; } }while(i!=0); } } OUTPUT: Enter an Alphabet A its an vowel Enter an Alphabet B its not an vowel Enter an Alphabet E its an vowel Enter an Alphabet O its an vowel Enter an Alphabet Z its not an vowel
33. 33. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 33 /**wap to overlad a method with same number of arguments.*/ public class func_ovrload { void add(int a,int b) { int sum; sum=a+b; System.out.println("THE SUM OF INTEGERS IS : "+sum); } void add(double c,double d) { double sum; sum=c+d; System.out.println("THE SUM OF INTEGERS IS : "+sum); } void add(char c,char d) { char sum; sum='c'+'d'; System.out.println("THE SUM OF INTEGERS IS : "+sum); } } class calling { public static void main(String args[]) { func_ovrload f=new func_ovrload(); f.add(100,200);
34. 34. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 34 f.add('r', 'd'); f.add(100.5, 99.5); } } OUTPUT: THE SUM OF INTEGERS IS : 300 THE SUM OF INTEGERS IS : Ç THE SUM OF INTEGERS IS : 200.0
35. 35. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 35 /**wap to overlad a method with different number of arguments.*/ package rachit; import java.util.Scanner; import java.lang.Math; public class area_overload { int area(int l,int b) { int area=l*b; return area; } int area(int s) { int area=(int)Math.pow(s,2); return area; } double area(int a,int b,int c) { double s=(a+b+c)/2; int s1=(int) (s*((s-a)*(s-b)*(s-c))); double area=(double)Math.sqrt(s1); return area; } } class fun_overload { public static void main(String args[]) {
36. 36. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 36 Scanner input=new Scanner(System.in); area_overload a=new area_overload(); System.out.println("ENTER THE LENGTH AND BREADTH OF RECTANGLE :"); int l=input.nextInt(); int b=input.nextInt(); System.out.println("ENTER THE SIDE OF SQUARE :"); int s=input.nextInt(); System.out.println("ENTER THE THREE SIDES OF TRIANGLE :"); int A=input.nextInt(); int B=input.nextInt(); int C=input.nextInt(); int a1=a.area(l,b); int a2=a.area(s); double a3=a.area(A,B,C); System.out.println("AREA OF RECTANGLE IS CALCULATED AS :"+a1); System.out.println("AREA OF SQUARE IS CALCULATED AS :"+a2); System.out.println("AREA OF TRIANGLE IS CALCULATED AS :"+a3); } }
37. 37. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 37 OUTPUT: ENTER THE LENGTH AND BREADTH OF RECTANGLE : 10 5 ENTER THE SIDE OF SQUARE : 15 ENTER THE THREE SIDES OF TRIANGLE : 10 15 20 AREA OF RECTANGLE IS CALCULATED AS :50 AREA OF SQUARE IS CALCULATED AS :225 AREA OF TRIANGLE IS CALCULATED AS :60.794736614282655
38. 38. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 38 /**wap showing concept of static variable and static method.*/ public class static_method { static double radius=100,area; final static double pi=3.14578; static void display() { static_method.area=(double)(pi*static_method.radius*static_method.radius); System.out.println("THE AREA OF CIRCLE IS : "+static_method.area); } } class circle { public static void main(String args[]) { static_method s=new static_method(); static_method.display(); } } OUTPUT: THE AREA OF CIRCLE IS : 31457.799999999996
39. 39. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 39 /**wap to calculate simple interest using keyword final.*/ import java.util.Scanner; public class final_static_intrest { Scanner input=new Scanner(System.in); final static double rate=10; double p,t,si; void getdata() { System.out.println("Enter the principal : "); p=input.nextDouble(); System.out.println("Enter the time period : "); t=input.nextDouble(); } void display() { si=p*rate*t/100; System.out.println("THE INTREST IS : "+si); System.out.println("FINAL AMOUNT IS : "+(si+p)); } } class intrest { public static void main(String args[]) { final_static_intrest f=new final_static_intrest(); f.getdata();
40. 40. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 40 f.display(); } } OUTPUT: Enter the principal : 3246 Enter the time period : 5 THE INTREST IS : 1623.0 FINAL AMOUNT IS : 4869.0
41. 41. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 41 /**wap for default constructor.*/ public class def_constructor { int a,b,c,area; final double pi=3.14; def_constructor() { a=10; b=20; } void area() { area=a*b; System.out.println("AREA = "+area); } } class Showing{ public static void main(String args[]) { def_constructor dc=new def_constructor(); dc.area(); }} OUTPUT: AREA = 200
42. 42. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 42 /**wap for parameterised constructor.*/ public class paramet_const { int a,b,area; paramet_const(int l,int br) { a=l; b=br; } void area() { area=a*b; System.out.println("Area = "+area); } } class displaying { public static void main(String args[]) { paramet_const pc=new paramet_const(100,50); pc.area(); } } OUTPUT: Area = 5000
43. 43. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 43 /**wap for copy cnstructor.*/ public class copy_constructor { int a,b,area; copy_constructor(copy_constructor cc1) { a=cc1.a; b=cc1.b; } copy_constructor(int x,int y) { a=x; b=y; } void area() { area=a*b; System.out.println("AREA = "+area); } } class showing { public static void main(String args[]) { copy_constructor cc=new copy_constructor(25,15); copy_constructor cc2=new copy_constructor(cc); cc2.area();
44. 44. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 44 } } OUTPUT: AREA = 375
45. 45. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 45 /**wap showing constructor overloading.*/ public class const_ovrload { int a,b,area; const_ovrload() { a=500; b=250; area=a*b; System.out.println("AREA OF RECTANGLE 1 = "+area); } const_ovrload(int x,int y) { a=x; b=y; area=a*b; System.out.println("AREA OF RECTANGLE 2 = "+area); } const_ovrload(int s) { area=s*s; System.out.println("AREA OF SQUARE = "+area); } } class play { public static void main(String args[])
46. 46. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 46 { const_ovrload co2=new const_ovrload(); const_ovrload co=new const_ovrload(150,120); const_ovrload co1=new const_ovrload(150); } } OUTPUT: AREA OF RECTANGLE 1 = 125000 AREA OF RECTANGLE 2 = 18000 AREA OF SQUARE = 22500
47. 47. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 47 /**wap that produces the OUTPUT: as:- *********************************** * mca fourth semester * * java:412 * * toc:413 * *********************************** */ package strings; public class design_print { void print() { System.out.println("**************************************************"); System.out.println("*ttM C A 4TH SEMESTER *"); System.out.println("*ttJAVA : 412 *"); System.out.println("*ttT O C : 413 *"); System.out.println("**************************************************"); } } class MAIN { public static void main(String args[]) { design_print dp=new design_print(); dp.print(); } }
48. 48. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 48 OUTPUT: ************************************************** * M C A 4TH SEMESTER * * JAVA : 412 * * T O C : 413 * **************************************************
49. 49. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 49 /**wap to genetrate tic tac toe board game.*/ import java.util.Scanner; public class TICTACTOE1 { String square[] = {"o","1","2","3","4","5","6","7","8","9"}; int check() { if (square[1].equals(square[2]) && square[2].equals(square[3])) return 1; else if (square[4].equals(square[5]) && square[5].equals(square[6])) return 1; else if (square[7].equals(square[8]) && square[8].equals(square[9])) return 1; else if (square[1].equals(square[4]) && square[4].equals(square[7])) return 1; else if (square[2].equals(square[5]) && square[5].equals(square[8])) return 1; else if (square[3].equals(square[6]) && square[6].equals(square[9])) return 1; else if (!"1".equals(square[1]) && !"2".equals(square[2]) && !"3".equals(square[3]) && !"4".equals(square[4]) && !"5".equals(square[5]) && !"6".equals(square[6]) && !"7".equals(square[7]) && !"8".equals(square[8]) && !"9".equals(square[9])) return 0; else return -1; } void board() {
50. 50. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 50 System.out.println( "n TIC TAC TOE "); System.out.println("Player 1 (0) - Player 2 (x)"); System.out.println(""); System.out.println(" +....+.....+.....+ "); System.out.println(" : "+square[1]+" : "+square[2]+" : "+square[3]+" :"); System.out.println(" +....+.....+.....+ "); System.out.println(" : "+square[4]+" : " +square[5]+" : " +square[6]+" :"); System.out.println(" +....+.....+.....+ "); System.out.println(" : "+square[7]+" : " +square[8]+ " : "+square[9]+" :"); System.out.println(" +....+.....+.....+ " ); System.out.println(""); } } class disp { public static void main(String args[]) { int player =1,i,choice; String fill; TICTACTOE1 t1=new TICTACTOE1(); do { t1.board(); if(player%2==0) { player=2;
51. 51. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 51 } else { player=1; } System.out.print ("Player "+player+", enter a number: "); Scanner in=new Scanner(System.in); choice=in.nextInt(); if (player==1) { fill="0"; } else { fill="x"; } if (choice == 1 && "1".equals(t1.square[1])) { t1.square[1]=fill; } else if (choice == 2 && "2".equals(t1.square[2])) { t1.square[2]=fill; } else if (choice == 3 && "3".equals(t1.square[3])) { t1.square[3]=fill; }
52. 52. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 52 else if (choice == 4 && "4".equals(t1.square[4])) { t1.square[4]=fill; } else if (choice == 5 && "5".equals(t1.square[5])) { t1.square[5]=fill; } else if (choice == 6 && "6".equals(t1.square[6])) { t1.square[6]=fill; } else if (choice == 7 && "7".equals(t1.square[7])) { t1.square[7]=fill; } else if (choice == 8 && "8".equals(t1.square[8])) { t1.square[8]=fill; } else if (choice == 9 && "9".equals(t1.square[9])) { t1.square[9]=fill; } else { System.out.print("Invalid move "); player--;
53. 53. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 53 } i=t1.check(); player++; }while(i==-1); t1.board(); if(i==1) { player--; System.out.println(" !!!!!!!!!!!!!!!!!!CONGRATULATION!!!!!!!!!!!!!!!!!!!"); System.out.println(""); System.out.println(" Player " +player+ " WIN!!!!!!! "); } else { System.out.println(" BETTER LUCK NEXT TIME"); System.out.println(" GAME DRAW!!!!!!!"); } } } OUTPUT: TIC TAC TOE Player 1 (0) - Player 2 (x) +....+.....+.....+ : 1 : 2 : 3 : +....+.....+.....+ : 4 : 5 : 6 :
54. 54. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 54 +....+.....+.....+ : 7 : 8 : 9 : +....+.....+.....+ Player 1, enter a number: 2 TIC TAC TOE Player 1 (0) - Player 2 (x) +....+.....+.....+ : 1 : 0 : 3 : +....+.....+.....+ : 4 : 5 : 6 : +....+.....+.....+ : 7 : 8 : 9 : +....+.....+.....+ Player 2, enter a number: 5 TIC TAC TOE Player 1 (0) - Player 2 (x) +....+.....+.....+ : 1 : 0 : 3 : +....+.....+.....+ : 4 : x : 6 : +....+.....+.....+ : 7 : 8 : 9 : +....+.....+.....+ Player 1, enter a number: 1 TIC TAC TOE Player 1 (0) - Player 2 (x) +....+.....+.....+
55. 55. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 55 : 0 : 0 : 3 : +....+.....+.....+ : 4 : x : 6 : +....+.....+.....+ : 7 : 8 : 9 : +....+.....+.....+ Player 2, enter a number: 4 TIC TAC TOE Player 1 (0) - Player 2 (x) +....+.....+.....+ : 0 : 0 : 3 : +....+.....+.....+ : x : x : 6 : +....+.....+.....+ : 7 : 8 : 9 : +....+.....+.....+ Player 1, enter a number: 3 TIC TAC TOE Player 1 (0) - Player 2 (x) +....+.....+.....+ : 0 : 0 : 0 : +....+.....+.....+ : x : x : 6 : +....+.....+.....+ : 7 : 8 : 9 : +....+.....+.....+ !!!!!!!!!!!!!!!!!!CONGRATULATION!!!!!!!!!!!!!!!!!!! Player 1 WIN!!!!!!!
56. 56. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 56 /**wap that prompts user to enter time for a certain event in hours, minutes and second the program then OUTPUT:s the time in seconds.*/ public class time_converter { private int hr,min,sec,se; Scanner input=new Scanner(System.in); void getdata() { System.out.println("ENTER THE TIME PLEASE :"); System.out.println("ENTER THE HOURS : "); hr=input.nextInt(); System.out.println("ENTER THE MINUTES : "); min=input.nextInt(); System.out.println("ENTER THE SECONDS : "); sec=input.nextInt(); se=sec; } void calculate() { sec=sec+min*60+hr*3600; } void display() { System.out.print("THE TOTAL SECONDS IN "+hr); System.out.print(" : "+min); System.out.print(" : "+se); System.out.print(" IS "+sec); System.out.println("");
57. 57. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 57 } } class converter { public static void main(String args[]) { time_converter tc=new time_converter(); tc.getdata(); tc.calculate(); tc.display(); } } OUTPUT: ENTER THE TIME PLEASE : ENTER THE HOURS : 11 ENTER THE MINUTES : 12 ENTER THE SECONDS : 13 THE TOTAL SECONDS IN 11 : 12 : 13 IS 40333
58. 58. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 58 /**wap to calculate the roots of the quadratic equation .*/ import java.util.*; import java.lang.*; public class quad\$equation { public static void main(String args[]) { equation e=new equation(); e.solution(); } } class equation //CLASS CREATED { Scanner input=new Scanner(System.in); double a,b,c,d; //DATA MEMBERS DECLARED double r,r1,r2; void solution() { //INPUT THE VALUES OF CO-EFFICIENTS System.out.println("Enter the values of a, b and c"); a=input.nextDouble(); b=input.nextDouble(); c=input.nextDouble(); d=b*b-4*a*c; //FIND DISCRIMINANT if(d>0) { r1=(-b+Math.sqrt(d))/(2*a); //CALCULATING ROOTS
59. 59. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 59 r2=(-b-Math.sqrt(d))/(2*a); System.out.println(" Roots are real and are t"+r1+"t"+r2); } //PRINTING ROOTS else if(d==0) { r=-b/(2*a); //CALCULATING ROOTS System.out.println("Equation has unique roots i.e "+r); } //PRINTING ROOTS else System.out.println("No real solution exists"); } } OUTPUT: Enter the values of a, b and c 1 4 -12 Roots are real and are 2.0 -6.0 Enter the values of a, b and c 1 3 7 No real solution exists
62. 62. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 62 ad.display(); } } OUTPUT: ENTER BELOW THE DATE 1 ENTER THE DAY : 26 ENTER THE MONTH : 8 ENTER THE YEAR : 2013 ENTER BELOW THE DATE 2 ENTER THE DAY : 24 ENTER THE MONTH : 9 ENTER THE YEAR : 2012 THE SUM OF TWO DATES IS : 20:6:4026
63. 63. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 63 /**wap to design and implement the class book that stores information about a book suchastitle,author,publisher ,isbn& date it is published. use appropriate constructor to initialize the data and methods such as ‘get title’ and ‘get author’ that return the itle of the book and the authors name of the book.*/ package strings; import java.util.Date; import java.util.Random; import java.util.Scanner; public class book\$database { public static void main(String args[]) { book b=new book(); b.getdetails(); b.display(); } } class book { Scanner input=new Scanner(System.in); String title,author,publisher; long isbn; Random r=new Random(23423423); Date date=new Date(); void getdetails() { System.out.println("ENTER THE PUBLISHER OF THE BOOK : ");
64. 64. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 64 publisher=input.next(); } String getauthor() { System.out.println("ENTER THE AUTHOR OF THE BOOK : "); return (author=input.next()); } String getitle() { System.out.println("ENTER THE TITLE OF THE BOOK : "); return (title=input.next()); } void display() { isbn=r.nextLong(); String t=getitle(),a=getauthor(); System.out.println("TITLE OF BOOK : "+t); System.out.println("AUTHOR OF BOOK : "+a); System.out.println("PUBLISHER OF BOOK : "+publisher); System.out.println("PUBLISHING DATE : "+date); System.out.println("ISBN OF BOOK : "+isbn); } }
65. 65. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 65 OUTPUT: ENTER THE PUBLISHER OF THE BOOK : MCGRAWHILL ENTER THE TITLE OF THE BOOK : JAVA ENTER THE AUTHOR OF THE BOOK : BALAGURUSAMY TITLE OF BOOK : JAVA AUTHOR OF BOOK : BALAGURUSAMY PUBLISHER OF BOOK : MCGRAWHILL PUBLISHING DATE : Tue Apr 23 23:30:36 IST 2013 ISBN OF BOOK : 8500674452061305336
66. 66. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 66 /**wap that prompts user to input a 6 digit +ve number .the program then OUTPUT the digits of the number one digit per line.*/ import java.util.Scanner; public class DIGIT\$PER\$LINE { Scanner input=new Scanner(System.in); int num,rem,i; byte[] ar=new byte[6]; void getnum() { System.out.println("ENTER A 6 DIGIT NUMBER : "); num=input.nextInt(); } void calculation() { for(i=0;i<=5;i++) { rem=num%10; num=num/10; ar[i]=(byte)rem; } for(i=5;i>=0;i--) { System.out.println(ar[i]); } } }
67. 67. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 67 class number { public static void main(String []args) { DIGIT\$PER\$LINE dpl=new DIGIT\$PER\$LINE(); dpl.getnum(); dpl.calculation(); } } OUTPUT: ENTER A 6 DIGIT NUMBER : 618973 6 1 8 9 7 3
68. 68. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 68 /**wap to add,subtract,multiply and divide two complex numbers using objects and classes.*/ class complexno { int real=0; int imagine=0; complexno(int a, int b) { real = a; imagine = b; } void add(complexno o1,complexno o2) { real=o1.real+o2.real; imagine=o1.imagine+o2.imagine; System.out.print("Sum of two complex no is "+real); System.out.println(" + i"+imagine); } void substract(complexno o1,complexno o2) { real=o1.real-o2.real; imagine=o1.imagine-o2.imagine; System.out.print("Subtraction of two complex no is "+real); System.out.println(" + i"+imagine);} void multiply(complexno o1,complexno o2) { real=(o1.real)*(o2.real); imagine=(o1.imagine)*(o2.imagine);
69. 69. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 69 System.out.print("Multiplication of two complex no is "+real); System.out.println(" + i"+imagine);} void divide(complexno o1,complexno o2) { real=(o1.real)/o2.real; imagine=(o1.imagine)/(o2.imagine); System.out.print("Division of two complex no is "+real); System.out.println(" + i"+imagine);} } class complexx { public static void main(String args[]) { complexno o1=new complexno(22,40); complexno o2=new complexno(11,20); complexno o3=new complexno(1,1); o3.add(o1,o2); o3.substract(o1,o2); o3.multiply(o1,o2); o3.divide(o1,o2); } } OUTPUT: Sum of two complex no is 33 + i60 Subtraction of two complex no is 11 + i20 Multiplication of two complex no is 242 + i800 Division of two complex no is 2 + i2
70. 70. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 70 /**wap to transfer money from one account to another using objects.*/ import java.util.Scanner; public class ACCOUNT { double withdraw(int draw,double bal) { double d,b,a=0; d=draw; b=bal; if(draw<bal) { System.out.println("YOU CANT WITHDRAW THE AMOUNT... LESS BALANCE"); a=bal; } else { a=b-d; } return a; } double deposit(int depo,double bal) { double x; x=bal+depo; return x; } double interest(double bal,double roi,int t) {
71. 71. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 71 double s_int; s_int=(bal*roi*t)/100; return s_int; } } class detail { public static void main(String args[]) { Scanner obj=new Scanner(System.in); int id; final double roi; double bal,date,am; System.out.print("Enter the Acc Id "); id=obj.nextInt(); System.out.println("Enter the Balance "); bal=obj.nextInt(); System.out.println("Enter the Rate of Interest "); roi=obj.nextInt(); ACCOUNT o1=new ACCOUNT(); int draw; System.out.println("Enter amount to withdraw. "); draw=obj.nextInt(); System.out.println("BALANCE AFTER WITHDRAWAL "+bal); bal=o1.deposit(draw,bal); int depo; System.out.println("Enter amount to deposit."); depo=obj.nextInt();
72. 72. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 72 bal=o1.withdraw(depo,bal); System.out.println("BALANCE AFTER DEPOSIT "+bal); am=o1.interest(bal,roi,2); System.out.println("INTEREST IS "+am); } } OUTPUT: Enter the Acc Id 159 Enter the Balance 54654 Enter the Rate of Interest 9 Enter amount to withdraw. 7895 BALANCE AFTER WITHDRAWAL 54654.0 Enter amount to deposit. 55555 YOU CANT WITHDRAW THE AMOUNT... LESS BALANCE BALANCE AFTER DEPOSIT 62549.0 INTEREST IS 11258.82
73. 73. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 73 /**wap to implement basic operations of stack using classes and objects.*/ import java.util.Scanner; public class STACK { int size,top; static int max=100; int stack[]=new int[max]; void push(int num,int size) { int num1=num; int size1=size; if(top==size1-1) { System.out.println("nStack Overflow"); } else { top++; stack[top]=num1; System.out.println("nOne number inserted successfully!!n"); } } void pop() { if(top==-1) { System.out.println("n stack is empty!!");
74. 74. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 74 } else { System.out.println(stack[top]); System.out.println("deleted successfully from top of the stack"); top--; } } void display() { int i; if(top==-1) { System.out.println("n Stack is empty!!"); return; } { System.out.println("Stack elements are!!n"); for(i=top;i>0;i--) { System.out.println("n" +stack[i]); } } }
75. 75. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 75 } class stack1 { static int max=100; public static void main(String args[]) { STACK s=new STACK(); int size,num,top=-1,choice; Scanner input=new Scanner(System.in); System.out.println("nEnter size of the stack:"); size=input.nextInt(); while(size<=0||size>max) { System.out.println("invalid size"); System.out.println("enter size of stack again : "); size=input.nextInt(); } do { System.out.println("ntttPRESS 1.PUSH OPERATIONn"); System.out.println("tttPRESS 2.POP OPERATIONn"); System.out.println("tttPRESS 3.DISPLAY OPERATIONn"); System.out.println("tttPRESS 0.EXITn"); System.out.println("nntEnter your choice:"); choice=input.nextInt();
76. 76. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 76 switch(choice) { case 1: System.out.println("Enter the number you want to insert at the top of the stack:"); num=input.nextInt(); s.push(num,size); break; case 2: s.pop(); break; case 3:s.display(); break; default: System.out.println("Enter correct choice:n"); } } while(choice!=0); } } OUTPUT: Enter size of the stack: 5 MAIN MENU PRESS 1.PUSH OPERATION PRESS 2.POP OPERATION PRESS 3.DISPLAY OPERATION PRESS 0.EXIT
77. 77. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 77 Enter your choice:1 Enter the number you want to insert at the top of the stack:444 One number inserted successfully!! MAIN MENU PRESS 1.PUSH OPERATION PRESS 2.POP OPERATION PRESS 3.DISPLAY OPERATION Enter your choice:1 Enter the number you want to insert at the top of the stack:555 One number inserted successfully!! MAIN MENU PRESS 1.PUSH OPERATION PRESS 2.POP OPERATION PRESS 3.DISPLAY OPERATION PRESS 0.EXIT Enter your choice:1 Enter the number you want to insert at the top of the stack:666 One number inserted successfully!! MAIN MENU PRESS 1.PUSH OPERATION PRESS 2.POP OPERATION PRESS 3.DISPLAY OPERATION PRESS 0.EXIT Enter your choice:3 Stack elements are!! 666 555 444
78. 78. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 78 MAIN MENU PRESS 1.PUSH OPERATION PRESS 2.POP OPERATION PRESS 3.DISPLAY OPERATION PRESS 0.EXIT Enter your choice:2 666 deleted successfully from top of the stack MAIN MENU PRESS 1.PUSH OPERATION PRESS 2.POP OPERATION PRESS 3.DISPLAY OPERATION PRESS 0.EXIT Enter your choice:3 Stack elements are!! 555 444
79. 79. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 79 /**wap to compute the tax to be paid by a person using objects.*/ import java.util.Scanner; public class tax { double salary, tax,annincome,taxcalculated; Scanner input=new Scanner (System.in); void getdata() { System.out.println("salary of employee = "); salary=input.nextDouble(); } void calculatetax() { annincome=salary*12; System.out.println("annual income of employee= " +annincome ); if((annincome>=200000)&&(annincome<=350000)) { annincome=annincome-200000; tax=(annincome*(.2)); System.out.println(" income tax to be paid by an employee="+tax); } else if(annincome>=350000) { annincome=annincome-300000; tax=(annincome*(.25)); System.out.println("income tax ="+tax); taxcalculated=annincome-tax; System.out.println("tax to be paid by employee=" +taxcalculated);
80. 80. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 80 } else { System.out.println("tax not paid" ); } }} class result1 { public static void main(String args[]) { tax t=new tax(); t.getdata(); t.calculatetax(); } } OUTPUT: salary of employee = 18000 annual income of employee= 216000.0 income tax to be paid by an employee=3200.0
81. 81. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 81 /**Employees in an office are required to enter their attendence at the time of reporting for the duty.manager at any time can check whether the employee has reported for duty or not. wap to simulate the problem.*/ import java.util.Scanner; public class employee { public static void main(String args[]) { Scanner input=new Scanner(System.in); String [] name =new String[5]; String[] attendence=new String[5]; name[0]="RACHIT"; name[1]="KALI"; name[2]="ARUN"; name[3]="BHARAT"; name[4]="DINESH"; System.out.println("Enter attendence:"); System.out.println("Employees RACHIT:"); attendence[0]=input.next(); System.out.println("Employees KALI:"); attendence[1]=input.next(); System.out.println("Employees ARUN:"); attendence[2]=input.next(); System.out.println("Employees BHARAT:"); attendence[3]=input.next(); System.out.println("Employees DINESH:"); attendence[4]=input.next(); System.out.println("/t/t/tEmployees Attendence");
82. 82. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 82 System.out.println(name[0]+" " +attendence[0]); System.out.println(name[1]+" "+attendence[1]); System.out.println(name[2]+" "+attendence[2]); System.out.println(name[3]+" "+attendence[3]); System.out.println(name[4]+" "+attendence[4]); } } OUTPUT: Enter attendence: Employees RACHIT: P Employees KALI: P Employees ARUN: P Employees BHARAT: A Employees DINESH: A Employees Attendence RACHIT P KALI P ARUN P BHARAT A DINESH A
83. 83. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 83 /**Develop a class personal details.it should have fields name,height (in inches) and weight(in pounds).convert the height to cenimeters and weight to kilograms and display the result. (1 inch=2.54cm) and (1 pound=0.4546kg).*/ import java .util.Scanner; class aaaa { Scanner input=new Scanner(System.in); double h1; double w1; int h; String n; int w; void getdata() { System.out.println("enter the name"); n=input.next(); System.out.println("enter the height in inches"); h=input.nextInt(); System.out.println("enter the weight in pound"); w=input.nextInt(); } void display() { h1=h*(2.54); w1=w*(0.4546); System.out.println("name="+n); System.out.println("height in centimeter ="+h1);
84. 84. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 84 System.out.println("weight in kg="+w1); } } class pppp { public static void main (String args[]) { aaaa o=new aaaa(); o.getdata(); o.display(); } } OUTPUT: enter the name : rachit enter the height in inches : 60 enter the weight in pound : 160 Name=rachit Height in centimeter =152.4 Weight in kg=72.736
85. 85. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 85 /**Design a class account that has the following member, data fields: account id(int),balance(double), annual rate of interest(double),date created(double). methods: withdraw(),deposit(),getmonthlyinterestrate() and other constructurs, use the withdraw() method to withdraw rs.2000 and then deposit rs.1000 using deposit() method.now print the balance,monthly rate of interest and date when the account was created.*/ import java.util.Scanner; public class account { double withdraw(int draw,double bal) { double d,b,a=0; d=draw; b=bal; if(draw>bal) { System.out.println("YOU CANT WITHDRAW THE AMOUNT... LESS BALANCE"); a=bal; } else { a=b-d; } return a; } double deposit(int depo,double bal)
86. 86. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 86 { double x; x=bal+depo; return x; } double interest(double bal,double roi,int t) { double s_int; s_int=(bal*roi*t)/100; return s_int; } } class detail { public static void main(String args[]) { Scanner obj=new Scanner(System.in); int id; final double roi; double bal,date,am; System.out.print("Enter the Acc Id "); id=obj.nextInt(); System.out.println("Enter the Balance "); bal=obj.nextInt(); System.out.println("Enter the Rate of Interest "); roi=obj.nextInt(); account o1=new account(); int draw=2000;
87. 87. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 87 bal=o1.withdraw(draw,bal); System.out.println("BALANCE AFTER WITHDRAWAL "+bal); int depo=1000; bal=o1.deposit(depo,bal); System.out.println("BALANCE AFTER DEPOSIT "+bal); am=o1.interest(bal,roi,2); System.out.println("INTEREST IS "+am); } } OUTPUT: Enter the Acc Id 66 Enter the Balance 10000 Enter the Rate of Interest 9 BALANCE AFTER WITHDRAWAL 8000.0 BALANCE AFTER DEPOSIT 9000.0 INTEREST IS 1620.0
88. 88. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 88 /**wap to perform linear search in an array.*/ package rachit; import java.util.Scanner; public class arrays { public static void main(String args[]) { Scanner input=new Scanner(System.in); int array[]; array=new int [10]; System.out.println("ENTER THE ELEMENTS OF ARRAY :"); for(int i=0;i<10;i++) array[i]=input.nextInt(); System.out.println("THE ELEMENTS OF ARRAY ARE :"); for(int i=0;i<10;i++) System.out.print("t"+array[i]); System.out.println(); System.out.println("ENTER THE ELEMENT TO BE SEARCHED :"); int key=input.nextInt(),count=0; for(int i=0;i<array.length;i++) { if(array[i]==key) count++; } if(count>0) { System.out.println(key+" FOUND "+count+" TIMES "); } else System.out.println(key+" NOT FOUND ");
89. 89. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 89 } } OUTPUT: ENTER THE ELEMENTS OF ARRAY : 4 5 6 7 8 9 1 2 3 8 THE ELEMENTS OF ARRAY ARE : 4 5 6 7 8 9 1 2 3 8 ENTER THE ELEMENT TO BE SEARCHED : 8 8 FOUND 2 TIMES
90. 90. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 90 /**wap to perform BINARY search in an array.*/ import java.util.Scanner; public class binsrch { public static void main(String args[]) { data d=new data(); d.getdata(); d.search(); } } class data { Scanner input=new Scanner(System.in); int len; int[] ar=new int[100]; void getdata() { System.out.println("ENTER THE NIMBER OF ELEMENTS : "); len=input.nextInt(); System.out.println("ENTER THE ELEMENTS : "); for(int i=0;i<len;i++) { ar[i]=input.nextInt(); }
91. 91. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 91 } void search() { int mid,key,i=0,count=0; System.out.println("ENTER THE ELEMENT TO BE SEARCHED : "); key=input.nextInt(); int lb=0,ub=len-1; mid=(lb+ub)/2; while((lb<=ub) && (count!=1)) { if(ar[mid]==key) count++; else if(key < ar[mid]) ub=mid-1; else if(key > ar[mid]) lb=mid+1; mid=(lb+ub)/2; } if(count>0) System.out.println("nnttELEMENT "+key+" FOUND "+count+" TIMES"); else System.out.println("nnELEMENT NOT FOUND"); } }
92. 92. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 92 OUTPUT: ENTER THE NIMBER OF ELEMENTS : 5 ENTER THE ELEMENTS : 11 55 77 88 99 ENTER THE ELEMENT TO BE SEARCHED : 55 ELEMENT 55 FOUND 1 TIMES
93. 93. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 93 /**wap to check whether the string is pallindrome or not.*/ import java.util.Scanner; class palindrome { String str,rev=""; Scanner input=new Scanner(System.in); void getdata() { System.out.println("ENTER THE STRING TO BE CHECKED WHETEHER PALINDROME OR NOT :"); str=input.nextLine(); } void check() { int length=str.length(); for(int i=length-1;i>=0;i--){ rev=(rev+str.charAt(i));} System.out.println(rev+" IS reversed string"); if(str.equals(rev)) System.out.println(str+" IS PALINDROME"); else System.out.println(str+" IS NOT PALINDROME"); } } class string { public static void main(String[] args) { palindrome p=new palindrome();
94. 94. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 94 p.getdata(); p.check(); } } OUTPUT: ENTER THE STRING TO BE CHECKED WHETEHER PALINDROME OR NOT : rachit tihcar IS reversed string rachit IS NOT PALINDROME ENTER THE STRING TO BE CHECKED WHETEHER PALINDROME OR NOT : katak katak IS reversed string katak IS PALINDROME
95. 95. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 95 /**wap to count the number of each character in a word.*/ import java.util.Scanner; public class everycharfreq { public static void main(String args[]) { String name; int i,j; int [] cnt=new int[100]; Scanner in=new Scanner (System.in); System.out.println("Enetr the string: "); name=in.nextLine(); int d=name.length(); cnt[0]=1; for(i=0;i<d;i++) { for(j=1;j<d;j++) { if (name.charAt(i)==name.charAt(j)) { cnt[i]++; } } System.out.println("Frequency of "+name.charAt(i)+" in String "+name+" is "+cnt[i]); } } }
96. 96. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 96 OUTPUT: Enetr the string : rachit_is_here Frequency of r in String rachit_is_here is 2 Frequency of a in String rachit_is_here is 1 Frequency of c in String rachit_is_here is 1 Frequency of h in String rachit_is_here is 2 Frequency of i in String rachit_is_here is 2 Frequency of t in String rachit_is_here is 1 Frequency of _ in String rachit_is_here is 2 Frequency of i in String rachit_is_here is 2 Frequency of s in String rachit_is_here is 1 Frequency of _ in String rachit_is_here is 2 Frequency of h in String rachit_is_here is 2 Frequency of e in String rachit_is_here is 2 Frequency of r in String rachit_is_here is 1 Frequency of e in String rachit_is_here is 2
97. 97. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 97 /**wap to sort the elements of the array.*/ import java.util.Scanner; public class sorted { int n,i,j,temp; int a[]=new int[100]; Scanner in=new Scanner(System.in); void getdata() { System.out.println("ENTER THE LIMIT OF THE ARRAY:"); n=in.nextInt(); System.out.println("n Enter the elementsn"); for(i=1;i<=n;i++) { a[i]=in.nextInt();; } } void sort() { for(i=1;i<=n-1;i++) { for(j=1;j<=n-i;j++) { if (a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp;}
98. 98. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 98 } } System.out.println("nSorted array: "); for(i=1;i<=n;i++) { System.out.println("t "+a[i]); } } } class sorting { public static void main(String args[]) { sorted s=new sorted(); s.getdata(); s.sort(); } }
99. 99. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 99 OUTPUT: ENTER THE LIMIT OF THE ARRAY: 5 Enter the elements 15 46 97 578 454 Sorted array: 15 46 97 454 578
100. 100. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 100 /**wap to read 50 no.s and display only distinct numbers.*/ import java.util.Scanner; public class diff_50_nos { public static void main(String args[]) { byte[] name=new byte[50]; int i,j; int [] cnt=new int[50]; Scanner in=new Scanner (System.in); System.out.println("Enter 50 digits: "); for(i=0;i<name.length;i++) { name[i]=in.nextByte(); } cnt[0]=1; for(i=0;i<name.length;i++) { for(j=1;j<name.length;j++) { if (name[i]==name[j]) { cnt[i]++; } } } for(i=0;i<name.length;i++) {
101. 101. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 101 if(cnt[i]==1) { System.out.println(name[i]); } } } } OUTPUT: Enter 50 digits: 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 11 22 33 44 55 66 77 88 99 4 5 6 1 2 3 17 18 19 45 46 47 48 49 50 51 52 53 54 55 56 57 58 DISTINCT ELEMENTS ARE : 7 8 9 1 17 18 19 45 46 47 48 49 50 51 52 53 54 56 57 58
102. 102. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 102 /**wap to multiply two matrices.*/ import java.util.Scanner; public class matrix_mul { public static void main(String args[]) { int m,n,p,q,sum=0,c,d,k; Scanner in=new Scanner(System.in); System.out.println("enter the no. of rows and column of first matrix"); m=in.nextInt(); n=in.nextInt(); int first[][]=new int [m][n]; System.out.println("enter the element of the matrix"); for(c=0;c<m;c++) for(d=0;d<n;d++) first[c][d]=in.nextInt(); System.out.println("enter the no. of rows and column of second matrix"); p=in.nextInt(); q=in.nextInt(); if(n!=p) System.out.println("multiplication is not possible"); else { int second[][]=new int [p][q]; int multiply[][]=new int [m][q]; System.out.println("enter the elements of second matrix "); for(c=0;c<p;c++) for(d=0;d<q;d++) second[c][d]=in.nextInt();
103. 103. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 103 for(c=0;c<m;c++) { for(d=0;d<q;d++) { for(k=0;k<p;k++) { sum=sum+first[c][k]*second[k][d]; } multiply[c][d]=sum; sum=0; } } System.out.println("product of two matrix is"); for(c=0;c<m;c++) { for(d=0;d<q;d++) { System.out.print(multiply[c][d]+"t"); } System.out.print("n"); } } } }
104. 104. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 104 OUTPUT: enter the no. of rows and column of first matrix 2 2 enter the element of the matrix 1 2 3 4 enter the no. of rows and column of second matrix 2 1 enter the elements of second matrix 5 6 product of two matrix is 17 39
105. 105. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 105 /**wap to print the difference between largest and smallest element in an array.*/ import java.util.Scanner; public class arraydifference { Scanner in=new Scanner(System.in); int array1[][]=new int[3][3]; int row,col,largest,smallest,diff=0; void getdata() { System.out.println("ENTER THE NUMBER OF ROWS IN MATRIX :"); row=in.nextInt(); System.out.println("ENTER THE NUMBER OF COLS IN MATRIX :"); col=in.nextInt(); System.out.println("ENTER THE ELEMENTS IN MATRIX :"); for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { array1[i][j]=in.nextInt(); } } } void show() { System.out.println("THE ELEMENTS OF MATRIX ARE :n"); for(int i=0;i<row;i++)
106. 106. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 106 { for(int j=0;j<col;j++) { System.out.print("t"+array1[i][j]); } System.out.println(); } } void difference() { smallest=array1[0][0]; largest=array1[0][0]; for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { if(array1[i][j]<smallest) smallest=array1[i][j]; if(array1[i][j]>largest) largest=array1[i][j]; } } System.out.println("THE SMALLEST ELEMENT IN ARRAY IS :"+smallest); System.out.println("THE LARGEST ELEMENT IN ARRAY IS :"+largest); System.out.println("THE DIFFERENCE OF SMALLEST AND LARGEST ELEMENT IN ARRAY IS :"+(largest- smallest)); } } class show
107. 107. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 107 { public static void main(String []args) { arraydifference ad=new arraydifference(); ad.getdata(); ad.show(); ad.difference(); } } OUTPUT: ENTER THE NUMBER OF ROWS IN MATRIX :2 ENTER THE NUMBER OF COLS IN MATRIX :2 ENTER THE ELEMENTS IN MATRIX : 4 5 6 9 THE ELEMENTS OF MATRIX ARE : 4 5 6 9 THE SMALLEST ELEMENT IN ARRAY IS :4 THE LARGEST ELEMENT IN ARRAY IS :9 THE DIFFERENCE OF SMALLEST AND LARGEST ELEMENT IN ARRAY IS :5
108. 108. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 108 /**wap that reads a string and prints its first and last character.*/ import java.util.Scanner; public class showchar { public static void main(String args[]) { String name; char f,l; int a; int i=0; Scanner in=new Scanner(System.in); System.out.println("Enter the string"); name=in.nextLine(); a=name.length(); System.out.println(a); f=name.charAt(i); l=name.charAt(a-1); System.out.println("first and last character of the entered string is: ".toUpperCase()); System.out.println("First character= ".toUpperCase()+f+" and Last character= ".toUpperCase()+l) } } OUTPUT: Enter the string : rachitguptaishere FIRST AND LAST CHARACTER OF THE ENTERED STRING IS: FIRST CHARACTER= r AND LAST CHARACTER= e
109. 109. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 109 /**wap that switches the letter a and s.*/ import java.util.Scanner; public class replace_a_s { public static void main(String args[]) { Scanner input=new Scanner(System.in); String s; System.out.println("ENTER THE STRING : "); s=input.next(); String s1=s.replace('a','s'); System.out.println("THE STRING AFTER REPLACING 'a' WITH 's' IS : "+s1); } } OUTPUT: ENTER THE STRING : rachit THE STRING AFTER REPLACING 'a' WITH 's' IS : rschit
110. 110. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 110 /**wap that checks whether two words are anagrams or not.*/ import java.util.Scanner; import java.lang.String; public class nagrams { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.println("Enter your first string "); String string1=input.nextLine(); System.out.println("Enter your second string "); String string2=input.nextLine(); if(isanagram(sort(string1),sort(string2))==true) { System.out.println("Anagrams "); } else { System.out.println("Not anagrams"); } System.out.println(string1); System.out.println(string2); } public static String sort(String s) { char[] s1=new char[s.length()]; s1=s.toCharArray(); java.util.Arrays.sort(s1);
111. 111. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 111 s=new String(s1); return s; } public static boolean isanagram(String s1,String s2) { return s1.equals(s2); } } OUTPUT: Enter your first string rachit Enter your second string hitrac Anagrams rachit hitrac
112. 112. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 112 /**wap showing inheritance concept.*/ import java.util.Scanner; class baseclass { int a; Scanner input=new Scanner(System.in); public void getdata() { System.out.println("ENTER IST INTEGER :"); a=input.nextInt(); } } class derieved extends baseclass { int b;int sum=0; Scanner input=new Scanner(System.in); public void getdatad() { System.out.println("ENTER 2ND INTEGER :"); b=input.nextInt(); } public void add() { sum=a+b; System.out.println("sum of numbers is :"+sum); } }
113. 113. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 113 public class inheritence { public static void main(String args[]) { derieved d=new derieved(); d.getdata(); d.getdatad(); d.add(); } } OUTPUT: ENTER IST INTEGER : 45 ENTER 2ND INTEGER : 55 sum of numbers is :100
114. 114. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 114 /**wap for inheriting private members of base class.*/ public class privatefield_access { private int a; public void setdata(int x) { a=x; } public int getdata() { return a; } } class derieved1 extends privatefield_access { int b; void add() { int c=getdata(); System.out.println("ADDITION YIELDS :"+(c+b)); } void show() { int D=getdata(); System.out.println("a = "+D+"tb = "+b); } }
115. 115. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 115 class inheritence1 { public static void main(String args[]) { derieved1 d=new derieved1(); d.b=100; d.setdata(200); d.show(); d.add(); } } OUTPUT: a = 200 b = 100 ADDITION YIELDS :300
116. 116. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 116 /**wap for inheriting constructor using super keyword*/ public class super_keyword { int a,b,c; super_keyword(int x,int y,int z) { a=x; b=y; c=z; } public int returna() { return a; } public int returnb() { return b; } public int returnc() { return c; } } class derieved2 extends super_keyword { int r; derieved2(int x,int y,int z,int d) { super(x,y,z); r=d; }
117. 117. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 117 public int returnr() { return r; } } class inheritence3 { public static void main(String args[]) { derieved2 d=new derieved2(10,20,30,40); System.out.println(d.returna()); System.out.println(d.returnb()); System.out.println(d.returnc()); System.out.println(d.returnr()); } } OUTPUT: 10 20 30 40
118. 118. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 118 /**wap showing method overriding.*/ import java.util.Scanner; public class overriding { String name; double height; Scanner input=new Scanner(System.in); public void getdata() { System.out.println("ENTER NAME : "); name=input.next(); System.out.println("ENTER HEIGHT : "); height=input.nextDouble(); } public void show() { System.out.println("NAME : "+name); System.out.println("HEIGHT : "+height); } } class derieved12 extends overriding { String city,state ; double weight; public void getdata() { super.getdata(); System.out.println("ENTER WEIGHT : ");
119. 119. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 119 weight=input.nextDouble(); System.out.println("ENTER CITY : "); city=input.next(); System.out.println("ENTER STATE : "); state=input.next(); } public void show() { super.show(); System.out.println("WEIGHT : "+weight); System.out.println("CITY : "+city); System.out.println("STATE : "+state); } } class m { public static void main(String args[]) { derieved12 d12=new derieved12(); d12.getdata(); d12.show(); } }
120. 120. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 120 OUTPUT: ENTER NAME : RACHIT ENTER HEIGHT : 5.11 ENTER WEIGHT : 65.25 ENTER CITY : BHADERWAH ENTER STATE : J&K NAME : RACHIT HEIGHT : 5.11 WEIGHT : 65.25 CITY : BHADERWAH STATE : J&K
121. 121. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 121 /**wap for inheriting protected data of base class.*/ public class protected_data { protected String a,b; protected_data(String x, String y) { a=x; b=y; } public void show() { System.out.println("a= " +a+ " b= "+b); } } class data_derieved extends protected_data { private String c; data_derieved(String x, String y) { super(x,y); } public void add() { c=a.concat(b); System.out.println("AFTER CONCATINATION THE STRINGS BECOME : "+c); } } class d
122. 122. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 122 { public static void main(String args[]) { data_derieved b1=new data_derieved("RACHIT "," GUPTA"); b1.show(); b1.add(); } } OUTPUT: a= RACHIT b= GUPTA AFTER CONCATINATION THE STRINGS BECOME : RACHIT GUPTA
123. 123. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 123 /**wap showing concept of multilevel inheritance.*/ import java.util.Scanner; public class multilevel { Scanner input=new Scanner(System.in); private String name; multilevel(String s) { name=s; } public void display() { System.out.println("NAME : " +name); } } class derieved11 extends multilevel { private int id; derieved11(int i,String s) { super(s); id=i; } public void display() { super.display(); System.out.println("ROLL NO : " +id);
124. 124. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 124 } } class derieved22 extends derieved11 { private int marks; double per; derieved22(int a,double p,int i,String s) { super(i,s); marks=a; per=p; } public void display() { super.display(); System.out.println("MARKS IN LAST SEM :" +marks); System.out.println("PERCENTAGE :" +per); } } class MAIN_CLASS { public static void main(String args[]) { derieved22 m=new derieved22(460,66.45,66,"RACHIT GUPTA"); m.display(); }
125. 125. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 125 } OUTPUT: NAME : RACHIT GUPTA ROLL NO : 66 MARKS IN LAST SEM :460 PERCENTAGE :66.45
126. 126. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 126 /**wap showing concept of composition*/ public class composition { public static void main(String args[]) { date d=new date(9,04,2013); details dd=new details(66,"RACHIT",d); dd.display(); } } class date { private int day,month,year; date(int dd,int mm,int yy) { day=dd; month=mm; year=yy; } public String returndate() { return(day+"/"+month+"/"+year); } } class details { private int id; private String name;
127. 127. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 127 date joindate; details(int i,String n,date dt) { id=i; name=n; joindate=dt; } void display() { System.out.println("NAME :"+name); System.out.println("EMP_ID:"+id); System.out.println("JOIN DATE :"+joindate.returndate()); } } OUTPUT: NAME :RACHIT EMP_ID:66 JOIN DATE :9/4/2013
128. 128. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 128 /**wap showing concept of polymorphism*/ class polymorphism { public static void main(String []args) { shape s; rectangle1 r=new rectangle1(12.75,10.25); s=r; s.show(); circle1 c=new circle1(6.25); s=c; s.show(); } } class shape { public void show() { System.out.println("SHOWS SHAPE CLASS"); } } class rectangle1 extends shape { double len,brd; rectangle1(double l, double b) { len=l; brd=b; }
129. 129. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 129 public void show() { System.out.println("AREA OPF RECTANGLE IS :"+(len*brd)); } } class circle1 extends shape { double radius; final double pi=3.143; circle1(double r) { radius=r; } public void show() { System.out.println("AREA OF CIRCLE IS :"+(pi*radius*radius)); } } OUTPUT: AREA OPF RECTANGLE IS :130.6875 AREA OF CIRCLE IS :122.77343749999999
130. 130. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 130 /**wap showing concept of abstract class*/ public class abstract_class { public static void main(String []args) { shape1 s; rectangle2 r=new rectangle2(12.75,10.25); s=r; s.show(); circle2 c=new circle2(6.25); s=c; s.show(); } } abstract class shape1 { public void show() { System.out.println("SHOWS SHAPE CLASS"); } } class rectangle2 extends shape1 { double len,brd; rectangle2(double l, double b) { len=l; brd=b;
131. 131. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 131 } public void show() { System.out.println("AREA OF RECTANGLE IS :"+(len*brd)); System.out.println("CIRCUMFERENCE OF RECTANGLE IS :"+(2*(len+brd))); } } class circle2 extends shape1 { double radius; final double pi=3.143; circle2(double r) { radius=r; } public void show() { System.out.println("AREA OF CIRCLE IS :"+(pi*radius*radius)); System.out.println("CIRCUMFERENCE OF CIRCLE IS :"+(pi*radius*2)); } } OUTPUT: AREA OF RECTANGLE IS :130.6875 CIRCUMFERENCE OF RECTANGLE IS :46.0 AREA OF CIRCLE IS :122.77343749999999 CIRCUMFERENCE OF CIRCLE IS :39.287499999999994
132. 132. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 132 /**wap showing concept of interface*/ interface shape { void area(); void circumference(); } class rectangle implements shape { double len,brd; rectangle(double l,double b) { len=l; brd=b; } public void area() { System.out.println("AREA OF RECTANGLE IS : "+(len*brd)); } public void circumference() { System.out.println("circumference OF RECTANGLE IS : "+(2*(len+brd))); } } class circle implements shape { double radius; final double pi=3.14; circle(double r)
133. 133. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 133 { radius=r; } public void area() { System.out.println("AREA OF CIRCLE IS : "+(pi*radius*radius)); } public void circumference() { System.out.println("CIRCUMFERENCE OF CIRCLE IS : "+(2*pi*radius)); } } public class interfaces { public static void main(String []args) { shape[] s=new shape[2]; s[0]=new rectangle(10.0,15.0); s[1]=new circle(12.00); for(int i=0;i<s.length;i++) { s[i].area(); s[i].circumference(); } } }
134. 134. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 134 OUTPUT: AREA OF RECTANGLE IS : 150.0 circumference OF RECTANGLE IS : 50.0 AREA OF CIRCLE IS : 452.15999999999997 CIRCUMFERENCE OF CIRCLE IS : 75.36
135. 135. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 135 /**wap showing concept of multiple interfaces*/ public class multiple_interfaces { public static void main(String []args) { rachit real; real=new racx(); real.function1(); rex nick; nick=new racx(); nick.function2(); racx nick2=new racx(); nick2.function3(); } } interface rachit { void function1(); } interface rex { void function2(); } class racx implements rachit,rex
136. 136. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 136 { public void function1() { System.out.println("THIS IS MY REAL NAME"); } public void function2() { System.out.println("THIS IS MY NICK NAME"); } void function3() { System.out.println("THIS IS AGAIN MY NICK NAME"); } } OUTPUT: THIS IS MY REAL NAME THIS IS MY NICK NAME THIS IS AGAIN MY NICK NAME
137. 137. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 137 /**wap showing concept of exception handling*/ import java.util.Scanner; public class etend_interfaces { public static void main(String []args) { dispaly rn; rn=new show(); rn.getname(); rn.getrollno(); rn.display(); } } interface name { void getname(); } interface rollno extends name { void getrollno(); } interface dispaly extends rollno { void display(); } class show implements dispaly
138. 138. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 138 { Scanner input=new Scanner(System.in); String name; int rollno; public void getname() { System.out.println("ENTER THE NAME PLEASE :"); name=input.next(); } public void getrollno() { System.out.println("ENTER THE ROLL NO PLEASE :"); rollno=input.nextInt(); } public void display() { System.out.println("NAME : "+name); System.out.println("ROLL NO : "+rollno); } } OUTPUT: ENTER THE NAME PLEASE : RACHIT ENTER THE ROLL NO PLEASE : 66 NAME : RACHIT ROLL NO : 66
139. 139. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 139 /**wap to illustrate the concept of exception handling*/ public class exception_2catch { public static void main(String args[]) { int[] nume={10,20,30,40,50,60}; int[] dene={10,10,10,0}; for(int i=0;i<nume.length;i++) { try { System.out.println(nume[i]+" / "+dene[i]+" = "+nume[i]/dene[i]); } catch(ArithmeticException e) { System.out.println("ERROR ! division by zero encountered"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("ERROR ! Array is indexed out of bounds"); } } } }
140. 140. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 140 OUTPUT: 10 / 10 = 1 20 / 10 = 2 30 / 10 = 3 ERROR ! division by zero encountered ERROR ! Array is indexed out of bounds ERROR ! Array is indexed out of bounds
141. 141. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 141 /**Design an application for hospital management system for this create the following classes and members. 1)person(name.d.o.b,sex/gender) 2)doctor(specialisation) 3)patient(case no,disease,date of admission,date of discharge,bill no) write code to input data from user and display data to the user.*/ package strings; import java.util.Scanner; public class hospital_mngmt { public static void main(String []args) { person p=new person(); doctor d=new doctor(); patient p1=new patient(); p.getdata(); p.display(); d.getdata(); d.display(); p1.getdata(); p1.display(); } } class person { Scanner input=new Scanner(System.in); String name,dob,sex; void getdata()
142. 142. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 142 { System.out.println("ENTER THE NAME OF PERSON :"); name=input.next(); System.out.println("ENTER THE D O B OF PERSON :"); dob=input.next(); System.out.println("ENTER THE SEX OF PERSON :"); sex=input.next(); } void display() { System.out.println("ttt DETAILS OF PERSON ARE ARE :"); System.out.println("---------------------------------------------------------------------n"); System.out.println("NAME : "+name); System.out.println("DATE OF BIRTH : "+dob); System.out.println("SEX OF PERSON : "+sex); System.out.println("---------------------------------------------------------------------n"); } } class doctor { Scanner input=new Scanner(System.in); String spec; void getdata() { System.out.println("ENTER THE SPECILIZATION OF DOCTOR :"); spec=input.next();
143. 143. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 143 } void display() { System.out.println("ttt DETAILS OF DOCTOR ARE :"); System.out.println("---------------------------------------------------------------------n"); System.out.println("SPECILIZATION : "+spec); System.out.println("---------------------------------------------------------------------n"); } } class patient { Scanner input=new Scanner(System.in); String caseno,disease,doa,dod,billno; void getdata() { System.out.println("ENTER THE CASE NO OF THE PATIENT :"); caseno=input.next(); System.out.println("ENTER THE DISEASE OF THE PATIENT :"); disease=input.next(); System.out.println("ENTER THE DATE OF ADMISSION OF THE PATIENT :"); doa=input.next(); System.out.println("ENTER THE DATE OF DISCHARGE OF THE PATIENT :"); dod=input.next(); System.out.println("ENTER THE BILL NO OF THE PATIENT :"); billno=input.next(); }
144. 144. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 144 void display() { System.out.println("ttt DETAILS OF PATIENT ARE :"); System.out.println("---------------------------------------------------------------------n"); System.out.println("CASE NUMBER : "+caseno); System.out.println("DISEASE : "+disease); System.out.println("DATE OF ADMISSION : "+doa); System.out.println("DATE OF DISCHARGE : "+dod); System.out.println("BILL NUMBER : "+billno); System.out.println("---------------------------------------------------------------------n"); } } OUTPUT: ENTER THE NAME OF PERSON : rachit ENTER THE D O B OF PERSON : 18/9/1990 ENTER THE SEX OF PERSON : male DETAILS OF PERSON ARE ARE : --------------------------------------------------------------------- NAME : rachit DATE OF BIRTH : 18/9/1990
145. 145. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 145 SEX OF PERSON : male --------------------------------------------------------------------- ENTER THE SPECILIZATION OF DOCTOR : heart DETAILS OF DOCTOR ARE : --------------------------------------------------------------------- SPECILIZATION : heart --------------------------------------------------------------------- ENTER THE CASE NO OF THE PATIENT : 123 ENTER THE DISEASE OF THE PATIENT : fever ENTER THE DATE OF ADMISSION OF THE PATIENT : 12/3/13 ENTER THE DATE OF DISCHARGE OF THE PATIENT : 17/3/13 ENTER THE BILL NO OF THE PATIENT : 43566 DETAILS OF PATIENT ARE : --------------------------------------------------------------------- CASE NUMBER : 123 DISEASE : fever DATE OF ADMISSION : 12/3/13 DATE OF DISCHARGE : 17/3/13 BILL NUMBER : 43566
148. 148. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 148 /**wap to create a package and then to import this package in some class outside this package*/ package shapes_3d; R A C X public class cuboid { double len,brd,hgt; public cuboid(double l,double b,double h) { len=l;brd=b;hgt=h; } public double volume() { return(len*brd*hgt); } public double surfacearea() { return(2*((len*brd)+(len*hgt)+(brd*hgt))); } }
149. 149. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 149 package shapes_3d; public class sphere { double radius,pi=3.14; public sphere(double r) { radius=r; } public double volume() { return((4.0*pi*radius*radius*radius)/3); } public double surfacearea() { return(4.0*pi*radius*radius); } }
150. 150. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 150 import shapes_3d.cuboid; import shapes_3d.sphere; public class shapedemo { public static void main(String args[]) { sphere s=new sphere(15.02); System.out.println("volume of sphere : "+s.volume()); System.out.println("surface area of sphere : "+s.surfacearea()); cuboid c=new cuboid(5.77,8.54,9.11); System.out.println("volume of sphere : "+s.volume()); System.out.println("surface area of sphere : "+s.surfacearea()); } } OUTPUT: volume of sphere : 14186.59539349333 surface area of sphere : 2833.5410239999997 volume of sphere : 14186.59539349333 surface area of sphere : 2833.5410239999997
152. 152. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 152 System.out.println("Main thread ends..."); } } OUTPUT: Main thread starts: Main thread ends... Thread number 1 is running Thread number 3 is running Thread number 2 is running
153. 153. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 153 /** Wap showing concept of threads by implementing runnable interface.*/ import java.lang.Thread; public class mythread1 implements Runnable { private int threadid; private int delay; mythread1(int id,int d) { threadid=id; delay=d; } public void run() { for(int i=0;i<5;i++) { System.out.println("Thread "+threadid+" is running"); try { Thread.sleep(delay); } catch(InterruptedException e) { e.printStackTrace(); } }
156. 156. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 156 /**Wap implementig multiple threads without syncronisation.*/ public class withoutsyncr { private double balance=5000; public double getbalance() { return balance; } public void deposit(double amount) { System.out.println(Thread.currentThread().getName()+"read balance--> "+balance); double newbalance=balance+amount; try { Thread.sleep(1000); } catch(InterruptedException e) { } balance=newbalance; } } class addam implements Runnable
157. 157. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 157 { withoutsyncr act; addam(withoutsyncr ac) { act=ac; } public void run() { act.deposit(100); } } class account1 { public static void main(String args[]) { withoutsyncr p=new withoutsyncr(); addam m=new addam(p); Thread t1=new Thread(m); Thread t2=new Thread(m); Thread t3=new Thread(m); t1.start(); t2.start(); t3.start(); try
158. 158. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 158 { t1.join(); t2.join(); t3.join(); } catch(InterruptedException e) { } System.out.println("Total balance in account is ---> "+p.getbalance()); } } OUTPUT: Thread-0read balance--> 5000.0 Thread-1read balance--> 5000.0 Thread-2read balance--> 5000.0 Total balance in account is ---> 5100.0
159. 159. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 159 /**wap implementig multiple threads with method syncronisation.*/ public class methodsyncr { private double balance=5000; public double getbalance() { return balance; } public synchronized void deposit(double amount) { System.out.println(Thread.currentThread().getName()+"read balance--> "+balance); double newbalance=balance+amount; try { Thread.sleep(1000); } catch(InterruptedException e) { } balance=newbalance; } } class addam1 implements Runnable { methodsyncr act;
160. 160. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 160 addam1(methodsyncr ac) { act=ac; } public void run() { act.deposit(100); } } class account11 { public static void main(String args[]) { methodsyncr p=new methodsyncr(); addam1 m=new addam1(p); Thread t1=new Thread(m); Thread t2=new Thread(m); Thread t3=new Thread(m); t1.start(); t2.start(); t3.start(); try { t1.join();
161. 161. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 161 t2.join(); t3.join(); } catch(InterruptedException e) { } System.out.println("Total balance in account is ---> "+p.getbalance()); } } OUTPUT: Thread-0read balance--> 5000.0 Thread-2read balance--> 5100.0 Thread-1read balance--> 5200.0 Total balance in account is ---> 5300.0
162. 162. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 162 /**Wap implementig multiple threads with block syncronisation.*/ public class blocksyncr { private double balance=5000; public double getbalance() { return balance; } public void deposit(double amount) { synchronized (this) { System.out.println(Thread.currentThread().getName()+"read balance--> "+balance); double newbalance=balance+amount; try { Thread.sleep(1000); } catch(InterruptedException e) { } balance=newbalance; } } } class addam2 implements Runnable {
163. 163. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 163 blocksyncr act; addam2(blocksyncr ac) { act=ac; } public void run() { act.deposit(100); } } class account2 { public static void main(String args[]) { blocksyncr p=new blocksyncr(); addam2 m=new addam2(p); Thread t1=new Thread(m); Thread t2=new Thread(m); Thread t3=new Thread(m); t1.start(); t2.start(); t3.start(); try {
164. 164. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 164 t1.join(); t2.join(); t3.join(); } catch(InterruptedException e) { } System.out.println("Total balance in account is ---> "+p.getbalance()); } } OUTPUT: Thread-0read balance--> 5000.0 Thread-2read balance--> 5100.0 Thread-1read balance--> 5200.0 Total balance in account is ---> 5300.0
165. 165. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 165 /**Wap implementig multiple threads with use of yield(),stop() and sleep() methods.*/ class useofyss extends Thread { public void run() { for(int i=1;i<=5;i++) { if(i==1) yield(); System.out.println("From thread A: i= "+i); } System.out.println("Exit from A "); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { System.out.println("From thread B: j= "+j); if(j==3) stop();
166. 166. RACHIT GUPTA MCA 4TH SEM 66-MCA-2011 JAVA PRACTICAL FILE BHADERWAH CAMPUS, UNIVERSITY OF JAMMU. Page 166 } System.out.println("Exit from B "); } } class c extends Thread { public void run() { for(int k=1;k<=5;k++) { System.out.println("From thread C: k= "+k); if(k==1) try { sleep(1000); } catch(Exception e) { } } System.out.println(" Exit from C "); } } class threadmethod