1

INDEX SL.NO 1. ITEM Program in java formula(Area=Height*Width)to rectangle. Where height and height and width to find width implement the the area of a are the rectangles PAGE NO. 3

2.

Program in java to find the result of following expressions (Assume a=10, b=5) i)(a<<2)+(b>>2) ii)(a)||(b>0) iii)(a+b*100)/10) iv)a&b

3. 4. 5.

Program in Java to explain the use break and continue statements Program in java to find the average of marks you obtained in your 10+2 class Program in Java to find A*B where A is a matrix of 3*3 and B is a matrix of 3*4.Take the values in matrixes A and B from the user. Program to compute the sum of the digits of a given integer. Program with class Rectangle with the data fields width, length, area and colour. The Length, width and area are of double type and colour is of string type. The methods areet_lengh(), set_colour(), and find_area(). Create two objects of Rectangle and compare their colour. If area and colour both are same then display "Matching Rectangles", otherwise display "Non matching Rectangles" Create a class Account with two overloaded constructors. The first constructor is used or initializing the name of account holder, the account number, and the initial amount in the account. The second constructor is used for initialising the name of account holder, the account number, the addresses, the type of account and the balance. This class having methods Deposit(),Withdraw(),and get_balance(). Program in java to create a stack class of variable size with push()and pop() methods. Create two objects of stack with 10 data items in both. Compare the top elements of both stack and print the comparison result Write a program in java to show that private member of a super class cannot be accessed from derived classes. Program in java to create a player class.Inherit classes cricket_player , football_player Hockey_Player from player class. the and

6. 7.

8.

9.

10. 11.

12.

Write a class worker and derive classes Daily Worker and

2
SL.NO ITEM Salaried Worker from it.Every Worker has a name and a salary rate.Write method Company (int hours) to compute the weekpay of every worker.A Daily worker is paid on the basis of the number of days he works.The salaried worker gets paid the wage for 40 hours a week no matter what the actual hours are.Test this program to calculate the pay 13. Consider the trunk calls of a telephone exchange.A trunk call can be ordinary, urgent or lightning.The charges depend on the duration and the type of the call.Write a program using polymorphism to calculate the charges. Program in java to make a package Balance which has Account class with Display_Balance method in it. Import Balance Package in another program to access Display_Balance method of Account class. Write a program in java to show the usefulness of Interfaces as a place to keep constant value of program. Create an Interface and modules. having two methods division PAGE NO.

14.

15. 16.

Create a class which overrides these methods. 17. Program in java which implements interface Student which has two methods Display_Grade and Attendance for PG_students and UG_students. Program in java to display the names and roll numbers of Students. Initialize respective array variables for 10 students. Handle ArrayIndexOutOfBounds Exception, so that any such problem Doesn’t cause illegal termination of program. Program in java to enable the user to handle any chance to Divide by zero exception. 20. 21. An exception class, which throws an exception if operand is Nonnumeric in calculating modules. On a single track two vehicles are running .As vehicles are going in same direction there is no problem. If the vehicles are running in different direction there is a chance of collision. To avoid collision write a program in java using exception handling. Write a program in java to launch 10 threads. Each thread increments a counter variable. Run the program with synchronization. Program for printing even odd numbers. generating 2 threads,one for numbers and other for printing

18.

19.

22.

23.

24. 25.

Program using thread synchronization in multithreading. Program in Java to create a String object. Initialize this object with your name. Find the length of your name

3
SL.NO ITEM using the appropriate String method. Find whether the character ‘a’ is in your name or not; if yes find the number of times ‘a’ appears in your name. Print locations of occurrences of ‘a’. Try the same for different String objects. 26. Program for searching strings for the first occurrence of a character or substring and for the last occurrence of a character or substring. Program in java to read a statement from console, convert it into upper case and again print on console. Write a program in Java, which takes the name of a file from user, read the contents of the file and display it on the console. Program to copy a file into another file. Java Applet program which reads your name and address in different text fields and when a button named find is pressed the sum of the length of characters in name and address is displayed inn another text field. Use appropriate colors, layout to make you applet look good. An applet which displays a rectangle/string with specified color & coordinate passed as parameter from the HTML file. An applet which will display the calendar of a given date. Program to store student’s detail using Card Layout. Java Applet program, which provides a text area with horizontal and vertical scrollbars. Type some lines of text in the text area and use scrollbars for movements in the text area . Read a word in a text field and find whether the word is in the content of the text area or not. Java program to find the numeric address of the following web sites PAGE NO.

27. 28.

29. 30.

31.

32. 33. 34.

35.

i) ii) iii) iv)

www.ignou.ac.in www.indiatimes.com www.rediff.com www.apple.com

In addition to this, find the Internet Address of your local host. 36. Applet which takes name and age as parameters and display the message “<name> is <age> year old”. Print the URL of the class file. Program to connect to a database created in MSACCESS/SQL-SERVER/ORACLE using JDBC concept. Perform basic operations of Selection, Insertion and Deletion on

37.

4
SL.NO ITEM the database. PAGE NO.

area().println(" i) (a<<2)+(b>>2): System. System.out. int area=r. w=y.println(" iv) (a&b) : } } "+(a<<2)+(b>>2) ).int y) { h=x.w. } } class s01_01 {public static void main(String args[]) { rectangle r=new rectangle(10.println("Area of Rectangle="+area). "+(a&b) ). Where height and width are the rectangles height and width PROGRAMME: class rectangle { int h. . "+(a)||(b>0) ).out.println("iii) (a+b*100)/10 : System. } int area() { return(h*w). } } AIM: Program in java to find the result of following expressions (Assume a=10.out. "+(a+b*100)/10 ).out.b=5.println(" ii) (a)||(b>0) : System.20).out.5 AIM: Program in java to implement the formula(Area=Height*Width)to find the area of a rectangle. rectangle(int x. b=5) i)(a<<2)+(b>>2) ii)(a)||(b>0) iii)(a+b*100)/10) iv)a&b PROGRAM: class s01_02 { public static void main(String args[]) { int a=10. System.

out.println("NUMBER OF VALID ARGUMENTS :" + i). System. .println("Mark1 : "+m1). System.out. int l. System.m3. System.println("------------------------").out. System.println("VALID ARGUMENT :" + args[k]).out.println("INVALID ARGUMENT :" + args[k]). System.m1.println("Average : "+(float)(m1+m2+m3)/3).println("Mark2 : "+m2).parseInt(args[k]).out. System.out.println("------------------------"). } i++.println("Total : "+(m1+m2+m3)). System. String name.println("Register No : "+reg).println(" MARK LIST "). m3=Integer. m1=Integer.parseInt(args[0]).out. System. name=args[1].println("NUMBER OF INVALID ARGUMENT:" + j).6 AIM: Program in Java to explain the use break and continue statements PROGRAM: class s01_03 { public static void main(String args[]) { int i = 0.out.out. int j = 0. System.println("Mark3 : "+m3).length) break.println("Name : "+name).k++) { if(k>=args.out. System. reg=Integer. m2=Integer. } } AIM: program in java to find the average of marks you obtained in your 10+2 class PROGRAM: class s01_04 { public static void main(String args[]) { int reg. System.m2.out.parseInt(args[3]).out.out. System.parseInt(args[4]). } catch(NumberFormatException e) { j++.out. for(int k=0.out. continue. } System. System.parseInt(args[2]). try { l = Integer. .println("------------------------").

7 }} .

j<3.j++) System.parseLong(arg[0]).j<3.j<3.j++) b[i][j]=Integer.out. } for(int i=0. while(n>0) { sum+=n%10.i++) { for(int j=0.parseInt(arg[n++]). int b[][]=new int[3][3]. n/=10.Take the values in matrixes A and B from the user.k++) c[i][j]+=(a[i][k]*b[k][j]).i++) for(int j=0. } }} AIM: Program to compute the sum of the digits of a given integer.i<3.i<3. } System. for(int i=0. } } .i<3.out. for(int i=0. System. long n=Long.println("Sum="+sum). int n=0. //multipying the two matrix for(int i=0. PROGRAM: class s02_02 { public static void main(String arg[]) { int sum=0.i++) for(int j=0.parseInt(arg[n++]).out.i<3.j<3.print(c[i][j]+" ").i++) for(int j=0.j++) a[i][j]=Integer.println(). PROGRAM: class s02_01 {public static void main(String arg[]) { int a[][]=new int[3][3]. for(int k=0.j++) { c[i][j]=0.k<3.8 AIM: Program in Java to find A*B where A is a matrix of 3*3 and B is a matrix of 3*4. int c[][]=new int[3][3].

area and colour.set_width(Integer. If area and colour both are same then display "Matching Rectangles". } String getcolor() { return(color).readLine().parseInt(s)). s=in. s=in.println("Non Matching Rectangle ").out. void set_length(int a) { length=a.println("Enter the length for second rectangle").println("Enter the length for first rectangle").io.println("Matching Rectangle ").set_color(in. else System. DataInputStream in=new DataInputStream(System.parseInt(s)).parseInt(s)).out. set_colour(). length. rect b=new rect(). System. The Length. class rect { int width. s=in. String color.readLine(). } } class s03_01 { public static void main(String arg[])throws Exception { String s=null. } int area() { return(width*length).parseInt(s)). and find_area().out.readLine()). a.out. a.println("Enter the width for second rectangle").set_length(Integer. b.length. rect a=new rect(). width and area are of double type and colour is of string type. otherwise display "Non matching Rectangles" PROGRAM: import java.*. b. if(a. s=in.9 AIM: Program with class Rectangle with the data fields width. } void set_color(String a) { color=a.getcolor().out.println("Enter the Color for second rectangle").set_length(Integer. a. Create two objects of Rectangle and compare their colour.area()==b.out.out.area() && a.set_color(in.equals(b. System.println("Enter the width for first rectangle").set_width(Integer.in).readLine(). System.getcolor())) System.readLine(). } void set_width(int a) { width=a. } } . b.readLine()). System. The methods areet_lengh(). System. System.println("Enter the Color for first rectangle").out.

int no. } void show() { System. This class having methods Deposit()."Tirur".String t.out.println("Name : "+name).address="Calicut". } void deposite(int a) { bal+=a. the addresses. } account(String n.bal.10 AIM: Create a class Account with two overloaded constructors. accno=no. a1.out. PROGRAM: class account { String name. a1. } void withdraw(int a) { bal-=a. the account number. System. System.println("Balance : "+bal).out.show().out.out. System.and get_balance(). } } .5000). and the initial amount in the account.out. The second constructor is used for initialising the name of account holder.println("------------------------"). bal=b.println(" ACCOUNT DETAILS"). a2.String addr.type="fixed deposite". a1. the type of account and the balance.555.address. } } class s03_02 { public static void main(String arg[])throws Exception { account a1=new account("Anil".int b) { name=n.out.1000). System. System.out. System. System. account a2=new account("Anil". the account number.println("________________________"). a2.println("Address : "+address).println("Type : "+type).println("------------------------").deposite(a2. int accno. account(String n."Current account". bal=b. address=addr.deposite(5000). } int getbalance() { return(bal).666. The first constructor is used or initializing the name of account holder.int no. System.show().int b) { name=n. type=t.Withdraw().println("Account No : "+accno).getbalance()). a2.type.withdraw(350).out. a1. accno=no.

push(d).in).io.out.i<10. } if(s1. s2.println(1+i+") Enter data for the second stack"). else data[sp++]=a. else System. class stack { int data[]=new int[50].out.out.println("The top of the stacks are same").out. int sp=0. } void push(int a) {if(sp>=50) System.push(d). } for(int i=0.11 AIM: Program in java to create a stack class of variable size with push()and pop() methods. } . stack s1=new stack(). } else return(data[sp--]). int d. String s.i<10.pop()) System.parseInt(s). int pop() { if(sp<=0) { System.out.readLine().println(1+i+") Enter data for the first stack"). Create two objects of stack with 10 data items in both. stack s2=new stack().i++) { System.println("The top of the stacks are same").i++) { System. s1.println("Stack is empty"). d=Integer. for(int i=0. try { in=new DataInputStream(System.println("Stack overflow"). return(0).out. Compare the top elements of both stack and print the comparison result PROGRAM: import java.readLine().pop()==s2. s=in.*.parseInt(s). s=in. d=Integer. } } class s03_03 { public static void main(String arg[]) { DataInputStream in=null.

out.println(e).12 catch(Exception e) { System. } }} .

out. }} AIM. System. criket_player(String n. PROGRAM: class room { private int l.20.println("Area of Room : "+a1).int a) { . int v1=cr. } int volume() { return(area()*h). class_room(int x.area().int z) { super(x.out.} int area() {return(l*b). System.int y) { l=x.int a) { name=n.} } class class_room extends room { int h.volume().15). player(String n.int y. int a1=cr.String t.out. h=z.out.Inherit the classes cricket_player . } void show() { System.println("Volume of Room : "+v1). PROGRAM: class player { String name.y).out. age=a. room(int x.b. System. int age. System.println("Age : "+age).println("\n"). football_player and Hockey_Player from player class. b=y. } } class criket_player extends player { String type. Program in java to create a player class.println("Player name : "+name). } } class s04_01 {public static void main(String args[]) { class_room cr=new class_room(10.13 AIM: Write a program in java to show that private member of a super class cannot be accessed from derived classes.

a).show().show(). type=t."foot ball".out. football_player f=new football_player("arun".a).show(). } } class hockey_player extends player { String type.String t. f.a). } public void show() { super.show(). football_player(String n.println("Player type : "+type). } } .String t.println("Player type : "+type). System.14 super(n.out.show().println("Player type : "+type). } } //--------main ----------class s04_02 { public static void main(String args[]) { criket_player c=new criket_player("Ameer". System."hockey". type=t.int a) { super(n.25).25). System.25). type=t."criket". } } class football_player extends player { String type. h.int a) { super(n. c.out. hockey_player h=new hockey_player("Ram".show(). hockey_player(String n. } public void show() { super. } public void show() { super.

n). System. rate=r.15 AIM: Write a class worker and derive classes Daily Worker and Salaried Worker from it.100).int r) { super(no. salariedworker(int no.n)."Unni".println("Employee number : "+empno). salariedworker s=new salariedworker(666.The salaried worker gets paid the wage for 40 hours a week no matter what the actual hours are.String n. name=n.compay(45). System.Test this program to calculate the pay PROGRAM: class worker { String name. dailyworker(int no.out. int empno.println("Salary : "+rate*hour). } int hour=40. } } //--------main ----------class s04_03 { public static void main(String args[]) { dailyworker d=new dailyworker(254. } void compay(int h) { show(). rate=r.int r) { super(no. } void show() { System.out.A Daily worker is paid on the basis of the number of days he works.75).println("Employee name : "+name).println("Salary : "+rate*h).Every Worker has a name and a salary rate.println("\n--------------------------"). } } class dailyworker extends worker { int rate.String n.String n) { empno=no.out. d. System.out. void compay() { show(). } } class salariedworker extends worker { int rate."Arjun". . System.out. worker(int no.Write method Company (int hours) to compute the weekpay of every worker.

16 s.compay(). }} .

out.readLine(). } } class bill extends call { float amount. else if(dur<=3) amount=rate()*dur+2. else return 3f.The charges depend on the duration and the type of the call.out. float rate() { if(type.Write a program using polymorphism to calculate the charges.out.17 AIM: Consider the trunk calls of a telephone exchange. } catch(Exception e) { System.println("**********************"). bill() { try { in=new DataInputStream(System. System.println("enter call type(urgent. DataInputStream in=null. dur=Float. urgent or lightning. else amount=rate()*dur+5f.lightning.5f.in).5f.5) amount=rate()*dur+1.out.equals("urgent")) return 4. else if(type=="lightning") return 3.println("enter call duration:").println(e).io.readLine().floatValue(). else if(dur<=5) amount=rate()*dur+4. class call { float dur.5f. s=in.5f. } } void read()throws Exception { String s.valueOf(s).ordinary):"). type=in. PROGRAM: import java.5f. . } void print() { System. System.A trunk call can be ordinary. String type.*. } void calculate() { if(dur<=1.

System. } catch(Exception e) { System. public class account { long acc.out.account().println(" PHONE BILL ").println("Enter the account number :").parseLong(in.out.readLine()).bal. public void read()throws Exception { DataInputStream in=new DataInputStream(System. b. b. System. System.out.println(e).out.disp().read().out.*. bal=Long. System. acc=Long.println("**********************").read().println("Enter the name :"). Import Balance Package in another program to access Display_Balance method of Account class.in).calculate().out.readLine()).account a=new balance. System. }} AIM: Program in java to make a package Balance which has Account class with Display_Balance method in it. import java.out. System. } }} package balance.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"). . System.println(" CHARGE : "+amount). name=in.parseLong(in.println(" Call type : "+type).println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~").out.out.out.18 System.io.out.print().out. PROGRAM: class s05_01 { public static void main(String ar[]) { try { balance.println("Enter the account balance :"). String name.println("**********************"). System. } } class s04_04 { public static void main(String arg[])throws Exception { bill b=new bill().println(" Duration : "+dur).out. System.println("--Account Details ---").readLine(). } public void disp() { System. b. a. System. a.

19 System.println("Balance :"+bal).out. System.out.println("Account number :"+acc). System.out.println("Name :"+name). } } .

out. PROGRAM: interface area { static final float pi=3. System. void modules(int b). ar=rect.println("Modules :"+mod).out. Create a class which overrides these methods.disp(). } public void modules(int b) { mod=b.division(5).println("Division :"+div).compute(10. PROGRAM: interface course { void division(int a). } public void division(int a) { div=a. area ar. s.out. }} AIM: Create an Interface having two methods division and modules. System. . } class rectangle implements area { public float compute(float x. } void disp() { System.float y).out.} } class circle implements area { public float compute(float x. int div. float compute(float x.} } class s05_02 {public static void main(String args[]) { rectangle rect=new rectangle(). circle cr=new circle().142f.modules(15).compute(10.0)).float y) {return(pi*x*x). s.out. ar=cr.println("Name :"+name).name("Arun"). System. s.println("Area of the circle= "+ar.20 AIM: Write a program in java to show the usefulness of Interfaces as a place to keep constant value of program.println("Area of the rectangle= "+ar. s. }} //--------main--------------class s05_03 {public static void main(String args[]) { stud s=new stud(). } class stud implements course { String name.float y) {return(x*y). void name(String n) { name=n. System.mod.20)).

21 }} .

void read()throws Exception { DataInputStream in= new DataInputStream(System.io. } void disp() { System.in). System. else if(tt>=150) grade="C".out.println("enter the name : ").parseInt(in. System.out. else if(tt>=100) grade="D".out. has and .grade. else grade="E". m2=Integer.readLine()). interface student { void disp_grade(). PROGRAM: import java.out.println("Grade :"+grade).println("enter attendance : "). System.out.parseInt(in.out. System. System.println("Mark3 :"+m3).println("Register No :"+reg).println("enter mark3 : ").println("Attendance :"+att).println(" MARK LIST OF PG STUDENTS "). System.println("Name :"+name). reg=Integer. System. System. } public void disp_attendance() { System.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~").parseInt(in.println("enter mark2 : "). m1=Integer.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~").parseInt(in.out. att=Integer. System.att.out.println("enter mark1 : ").readLine()).out. } public void disp_grade() { int tt=m1+m2+m3. System. int reg.parseInt(in.m1. m3=Integer.readLine(). System. disp_grade(). name=in.m2.out. else if(tt>=200) grade="B".readLine()).out.out.println("enter the register no : ").println("Mark1 :"+m1). } class pg_stud implements student { String name.out. if(tt>=250) grade="A".m3.*. System.readLine()).println("Mark2 :"+m2). void disp_attendance().22 AIM: Program in java which implements interface Student which two methods Display_Grade and Attendance for PG_students UG_students.out.out. System.out.readLine()). disp_attendance(). System.

out. System. .readLine()).println("enter mark3 : ").m1. if(tt>=250) grade="A".println("Name :"+name).out. System.println("enter mark1 : ").in).23 } } class ug_stud implements student { String name.println("Mark3 :"+m3). System.println("Mark2 :"+m2).println("Register No :"+reg). else if(tt>=100) grade="D".out.out. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"). } void disp() { System. disp_grade().println("enter attendance : "). System. System. void read()throws Exception { DataInputStream in= new DataInputStream(System.m2. else grade="E".out.out.parseInt(in.readLine()). m1=Integer. else if(tt>=200) grade="B". System.println("Attendance :"+att). System. att=Integer.println("enter mark2 : ").readLine()).disp(). System. disp_attendance(). m2=Integer.grade.readLine().out.out. } } class s05_04 { public static void main(String ar[])throws Exception { pg_stud pg=new pg_stud().out. m3=Integer.parseInt(in.readLine()).println("enter the register no : "). } public void disp_attendance() { System. System.out.println("Grade :"+grade).println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~").println("Mark1 :"+m1). name=in. pg. int reg.println("enter the name : "). System.parseInt(in.m3.read(). pg. } public void disp_grade() { int tt=m1+m2+m3.readLine()). reg=Integer. System.out.att.out.parseInt(in.out.out.parseInt(in.println(" MARK LIST OF UG STUDENTS "). System. else if(tt>=150) grade="C". System.out.

read(). }} . ug. ug.24 ug_stud ug=new ug_stud().disp().

println("enter the name : ").out. System.io.grade. System. System.in). else grade="E". class student { String name.println("Name :"+name). if(tt>=250) grade="A".out.out. System.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"). disp_grade(). } } class s06_01 { public static void main(String ar[]) { int no=0.readLine()).parseInt(in. Initialize respective array variables for 10 students.m1.out. student s=new student().parseInt(in.25 AIM: Program in java to display the names and roll numbers of Students. int reg. else if(tt>=100) grade="D".println("enter mark3 : "). m3=Integer.readLine().println("Mark3 :"+m3).out.println("Register No :"+reg). System.out. System. PROGRAM: import java. reg=Integer. System.println(" MARK LIST OF STUDENTS ").out.println("Mark2 :"+m2). Handle ArrayIndexOutOfBounds Exception. m2=Integer.out. System.out. else if(tt>=200) grade="B". name=in.*.println("Grade :"+grade). System.readLine()).println("enter mark1 : ").m3.println("Mark1 :"+m1). void read()throws Exception { DataInputStream in= new DataInputStream(System. System. System.m2. try . else if(tt>=150) grade="C". } void disp() { System. m1=Integer. so that any such problem Doesn’t cause illegal termination of program. System.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~").readLine()). } public void disp_grade() { int tt=m1+m2+m3.parseInt(in.out.readLine()).out.println("enter mark2 : ").parseInt(in.println("enter the register no : "). System.out.out.out.

println("the maximum students should be ten\n"). System.println("enter the number of students : ").i++). } System. result=1.out.result=0. } for(int i=0.parseInt(in.read(). s. } } AIM: Program in java to enable the user to handle any chance to Divide by zero exception. } catch(ArrayIndexOutOfBoundsException e) { System.26 { DataInputStream in= new DataInputStream(System. PROGRAM: class s06_02 { public static void main(String ar[]) { int no=0. System.i<no. } catch(Exception e) { System. } catch(Exception e) { System.out.out.out.println(e).i<no. try{ result=m/no.out.println("Result :"+result).in).readLine()).println(" division by zero "). no=Integer. no=10.i++).println(" value of result has been set as one").out.println(e).disp(). } catch(ArithmeticException e) { System. } } . s. for(int i=0.m=10.out.

t2=in. if(!t1.*. try { a=Integer. which throws an exception if operand is Nonnumeric in calculating modules.println(e). If the vehicles are running in different direction there is a chance of collision. } } class s06_04 { public static void main(String ar[]) { String t1=null. t2=t1. } } } AIM: On a single track two vehicles are running .out. } catch(NumberFormatException e) { System. try { DataInputStream in= new DataInputStream(System.println("enter the direction of vehicle2:(left/right):").t2=null.println(e).out.out.(Use command line arguments) PROGRAM: class NonNum extends Exception { NonNum() { super("the value is non numeric \n").readLine(). } } class s06_03 { public static void main(String ar[]) { int a.b. class collision extends Exception { collision(String s) { super(s). PROGRAM: import java. t1=in.out.println(e).27 AIM: An exception class.parseInt(ar[0]). } . System. } catch(NonNum e) { System.out.equals(t2)) throw new collision("truck2 has to go on "+ t1 +" direction").println(e).out. System. To avoid collision write a program in java using exception handling.As vehicles are going in same direction there is no problem.c=0. } catch(collision e) { System. System. } catch(Exception e){ System.in).readLine().println("the collision has been avoided by redirection truck2").out.println("enter the direction of vehicle1:(left/right):").io. throw new NonNum().

28 System.out.println("direction of truck1 :"+t1).println("direction of truck2 :"+t2).out. } } . System.

} public void run() { d=syn. System. data d8=new data(). t. PROGRAM: class s07_02 { public static void main(String arg[])throws Exception { data d1=new data(). Thread t. return(i). Each thread Run the program with . } } //============================== class syn { synchronized static item increment(item i) { i.29 AIM: Write a program in java to launch increments a counter variable. data d4=new data(). data d10=new data(). synchronization. data d7=new data().println(d10. } class data extends item implements Runnable { item d=this.count). } } 10 threads. data d6=new data(). data d9=new data(). } } //--------------------------class item { static int count=0.increment(d).start(). data() { t=new Thread(this). data d2=new data().count++. data d3=new data().out. data d5=new data().

println("thread interepted").i+=2) System.i<50.one and other for printing odd numbers. } catch(Exception e) {System.} } } class odd extends Thread { Thread t=null. start(). odd o=new odd().println("thread interepted"). } public void run() { try { for(int i=1. } catch(Exception e) {System.out.out. Thread.} } } class s07_03 { public static void main(String arg[]) { even e=new even(). } } . for printing even numbers PROGRAM: class even extends Thread { Thread t=null.sleep(100).i+=2) System. } public void run() { try { for(int i=2. even() { t=new Thread(this).i<50. Thread.30 AIM: Program for generating 2 threads.print(i+" ").out. start().print(i+" "). odd() { t=new Thread(this).out.sleep(100).

float avg. } } class s07_04 { public static void main(String arg[]) { try{ stud s1=new stud().println(x). class read { static String get(String x) { String n=null.m3.sum(data).start(). Thread t.in).out.println("i/o stream error"). } return(n). t. System. t.tt. stud s2=new stud().m2. data=sy.out. } public void run() { try { data=sy.31 AIM: Program using thread synchronization in multithreading. PROGRAM.} } } class st { String name. } catch(Exception e){System. int code.println("i/o stream error"). } catch(Exception e) { System. n=in. stud() { t=new Thread(this).io.out.out.readLine(). import java.flush(). .*.m1. System. } class stud extends st implements Runnable { st data=this. try { DataInputStream in = new DataInputStream(System.r(data).sleep(100).

out.println("mark3 :"+x.out.get("enter mark1:")). e. } catch(Exception e) { System.println("no :"+x. System.m3).println("mark2 :"+x.out. System.println("name :"+x.32 t. System.m2+z. z.parseInt(read.println("total :"+x.avg). return(z).tt)/3.m1+z.parseInt(read. e.sleep(100).println("sychronized fountion read() ").m2=Integer. } synchronized static st sum(st z) { System. t.get("enter the name:").sleep(100).println("synchronized sum() ").get("enter the regno:")).tt=z.code=Integer.tt). e. sy. e.parseInt(read.name=read. } } .name).parseInt(read.p(data).get("enter mark2:")).m1=Integer.m2).out.out.m3=Integer.out.avg=(z.println("avg :"+x. System.out.get("enter mark3:")). return(e).println("mark1 :"+x.m1). } synchronized static void p(st x) { System. System. z.code). } } } class sy { synchronized static st r(st e) { System.out.println("interrupted").out. e. System.out.m3.

PROGRAM: class data { String name. System. } } class s08_01 { public static void main(String ar[]) { data d1=new data("anil kumar").out.println("there is no 'A' available in the string"). int len=name.disp(). Find whether the character ‘a’ is in your name or not.println("number of occurance :"+c).disp(). Initialize this object with your name. int c=0. Print locations of occurrences of ‘a’. Try the same for different String objects. System. d1. } } .i++) if(name. if yes find the number of times ‘a’ appears in your name. } if(c==0) System.33 AIM: Program in Java to create a String object.out.charAt(i)=='A'||name.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"). d2.charAt(i)=='a') { c++.length().i<len. } void disp() { System. System. data(String n){ name=n.println("Possition :"+(i+1)). data d2=new data("biju"). for(int i=0.out. Find the length of your name using the appropriate String method.println("Name :"+name).out.out.

String s1=in.io. String s2=in. class s08_03 {public static void main(String[]args) throws Exception { int len1. } } .in). System.substring(i. last=i+1.length().println("first occurance is at possition :"+(i+1)).readLine().out.readLine(). class s08_04 {public static void main(String a[]) throws IOException { DataInputStream in=new DataInputStream(System. len2=s2.out.io.last=0.len2. } } if(last!=0) System.readLine(). DataInputStream in=new DataInputStream(System. System.toUpperCase()).34 AIM: Program for searching strings for the first occurrence of a character or substring and for the last occurrence of a character or substring.println("Enter file Statement:").i<=(len1-len2).len2+i). PROGRAM: import java. len1=s1.out.*.println("Enter the string:"). PROGRAM: import java. convert it into upper case and again print on console.*. for(int i=0.out.i++) { if(s1. String s1=in.println("the string is not found").out. }} AIM: Program in java to read a statement from console.equals(s2)) { if(last==0) System.println(s1. else System.out.in).out.println("last occurance is at possition :"+last). System. System.length().println("Enter searching string:").

io. class s08_06 {public static void main(String a[]) throws IOException { int d=0.} } }} AIM: Program to copy a file into another file. FileInputStream fi=null.print((char)ch). PROGRAM: import java.println("interrupted"). } } catch(IOException e) { System. fo=new FileOutputStream(s2).out. which takes the name of a file from user. class s08_05 {public static void main(String args[]) { FileInputStream fis=null.out.read())!=-1) fo. PROGRAM: import java.out. try { fi=new FileInputStream(s1). read the contents of the file and display it on the console. String s1=in.*.readLine().35 AIM: Write a program in Java.out. System. System.out.println("Enter new file name:").println("File copyied"). System. while((d=fi.println("error in closing").read())!=-1) {System. while((ch=(byte)fis.in).io.println("Enter file name:"). System. } .println("Copying file").} finally { try {fis. String s2=in. FileOutputStream fo=null. byte ch.} catch(IOException e) {System.close().readLine(). try{ fis=new FileInputStream(args[0]).out.*.out.write((byte)d). DataInputStream in=new DataInputStream(System.

out.println(e).} }} .36 catch(IOException e){System.

setBackground(new Color(255. 60.event.*. import java. 20). 153. b1. 100. add(t2).awt. label2 = new Label(). b1. t2. label2.setBounds(130. t1. add(b1). label1. setLayout(null).*. t2 = new TextField(). import java. 20). 120. label2. t1 = new TextField().*. label2.setAlignment(Label.awt. 180. label3.RIGHT).setBounds(150.setText("Name"). t3 = new TextField(). label3 = new Label(). PROGRAM: import java. 20). t3. 102)). add(label3). add(t3). 24). label1. add(label2).applet. 50.setBounds(140. label1. 20).setBounds(200.setAlignment(Label. b1.setText("Address"). 20). 60. } public void actionPerformed(ActionEvent ae) { . 80.setBounds(200.RIGHT). add(t1). 153)). add(label1).setLabel("Total"). 100.addActionListener(this). 90. 100.setBounds(200. Use appropriate colors.setText("Total length"). public class s09_01 extends Applet implements ActionListener { public void init() { label1 = new Label(). 60.RIGHT). 50. label3. 90. 204.37 AIM: Java Applet program which reads your name and address in different text fields and when a button named find is pressed the sum of the length of characters in name and address is displayed inn another text field. label3. setBackground(new Color(0. b1 = new Button().setAlignment(Label. 20). layout to make you applet look good. b1. 120.setBounds(140.

getText(). } <html> <body> <applet code="s09_01. a+=t2.getText(). TextField t3. TextField t2. Label label3. Button b1. t3.38 int a=t1.toString(a)).setText(Integer. } Label label1. TextField t1.length().length().class" width=400 height=400> </applet> </body> </html> . Label label2.

import java.100)). setBackground(new Color(0. t=getParameter("hh"). y=Integer.b)).*.parseInt(t). w=Integer.*.10.parseInt(t).y.parseInt(t).39 AIM: An applet which displays a rectangle/string with coordinate passed as parameter from the HTML file. x=Integer. t=getParameter("bb"). h=Integer.awt.g. t=getParameter("yy").y. import java.event.fillRect(x. } } <html> <body> <applet code="s09_02. t=getParameter("rr"). p.h. public class s09_02 extends Applet implements ActionListener { public void init() { setLayout(null). } public void paint(Graphics p) { String t=null.class" width=400 height=400> <param name="xx" value="25"> <param name="yy" value="25"> <param name="ww" value="150"> <param name="hh" value="150"> <param name="rr" value="0"> <param name="gg" value="150"> <param name="bb" value="100"> </applet> </body> </html> .r.applet. t=getParameter("ww").parseInt(t).g.h). b=Integer. int x.b. t=getParameter("xx"). t=getParameter("gg").awt.parseInt(t).parseInt(t).parseInt(t). p. specified color & PROGRAM: import java.w. g=Integer.*.setColor(new Color(r. r=Integer.w.

b.parseInt(s1). s3=getParameter("as2").36).s3.*. s1=getParameter("as").34. d=Integer. PROGRAM: import java. import java.*.parseInt(s2). } public void init() { } public void paint(Graphics g) { if(s.b=0.equals("red")) g.parseInt(s3).util.YEAR.c.parseInt(s4).d). s2=getParameter("as1"). int a=0.drawString("Calendar is"+cal.DATE+"/"+cal.MONTH+"/"+cal.*. import java. g. a=Integer.awt.25).applet. public class s9e3 extends Applet { GregorianCalendar cal=new GregorianCalendar(). g. g.c=0. s4=getParameter("as3"). String s.s2.s1. b=Integer.25.s4.drawRect(a.drawString("Color = "+"". c=Integer. public void start() { s=getParameter("fg").setColor(Color.40 AIM: An applet which will display the calendar of a given date. } } .red).d=0.

TextField t4. Label l1. public void start() { } public void init() { c1=new CardLayout(). PROGRAM: import java. public class s9e4 extends Applet { CardLayout c1. p. import java.awt. p=new Panel(). TextField t3.*.*. add(l3).41 AIM: Program to store student’s detail using Card Layout. // add(t2). add(l1).*. import java.*. add(l2). // add(t3).setLayout(c1). add(t1). l1=new Label("Enter Name :"). l3=new Label("Address :mo(ho)").math. Label l2. Panel p. t1=new TextField(20). Label l3.event. Label l4. add(l4). l4=new Label("Pin :670571 "). TextField t2. // add(t4).awt.applet. TextField t1. } public void paint(Graphics g) { } } . import java. l2=new Label("Enter Place :").

24). 80.setBackground(new Color(0. 160. add(b1). Button b1.applet. 102.setBounds(200. 204. add(ta). label1.setLabel("Search").len2+i). b1. add(label1). b1. 153)). label1.getText(). 110).setBounds(150.awt. setLayout(null). msg="The string is in the text area".addActionListener(this). break.substring(i.setText("Searching string").setFont(new Font("Dialog". label1. 100. 0.awt.getText().length(). } . String s1=ta.length(). b1 = new Button().equals(s2)) { f=true.*. } public void actionPerformed(ActionEvent ae) { boolean f=false. String s2=t1.setAlignment(Label. public void init() { label1 = new Label(). import java. Type some lines of text in the text area and use scrollbars for movements in the text area . t1 = new TextField().setForeground(new Color(255. 280. Read a word in a text field and find whether the word is in the content of the text area or not. setBackground(new Color(0. ta = new TextArea(). 255.i++) { if(s1. label1. which provides a text area with horizontal and vertical scrollbars. b1.42 AIM: Java Applet program. for(int i=0. label1. 23). 120. b1. TextField t1.red).RIGHT). 153)).setBounds(70. setForeground(Color. 20). Label label1.event. 160. import java. PROGRAM: import java. int len1=s1. t1.setBounds(50.*. int len2=s2. TextArea ta. 30. ta.*. 14)). add(t1). 51)). public class s09_05 extends Applet implements ActionListener { String msg=null.i<=(len1-len2). 210.

250). } } <html> <body> <applet code="s09_05. repaint().43 } if(!f) msg="The string is not in the text area". } public void paint(Graphics g) { g.100.class" width=400 height=400> </applet> </body> </html> .drawString(msg.

println("The IP of www.out.out. a=getParameter("age").com").getByName("www.25). class s10_01 { public static void main(String ar[])throws Exception { InetAddress a=InetAddress.com viii) www.event. setFont(f). find the Internet Address of your local host. PROGRAM: import java.44 AIM: Java program to find the numeric address of the following web sites v) www. System.indiatimes.i<s.*.drawString(msg.com :"+s[i]). System. setForeground(Color.msg.com In addition to this. } } AIM: Applet which takes name and age as parameters and display the “<name> is <age> year old”.*.com").indiatimes.com :"+a).indiatimes.PLAIN.out.a.i++) System.com").ac.*.apple.ignou.length.println("The IP of www.in").com :"+a). import java. a=InetAddress. msg=n+" is "+a+" year old. a=InetAddress. PROGRAM: import java.apple.ac.rediff.ac.awt.getByName("www.in vi) www.200)). a=InetAddress. for(int i=0. Print the URL of the class file.0.getLocalHost(). System.out.println("The IP of www.awt.class" width=400 height=400> <param name="name" value="Anil kumar"> message .*.getByName("www.25.com vii) www.apple.Font. } } <html> <body> <applet code="s10_02.out.applet.getAllByName("www.ignou.rediff. setBackground(new Color(200. } public void paint(Graphics g) { String n.println("The Local Host IP:"+a). import java.ignou.in :"+a). n=getParameter("name").rediff. System.net.println("The IP of www. setLayout(null). g.18).green). InetAddress s[]=InetAddress. public class s10_02 extends Applet { public void init() { Font f=new Font("adobe-courier". ".

45 <param name="age" value="25"> </applet> </body></html> .

odbc.createStatement().st2.exit(0). switch(n) {case 1: System.print("Enter number of fields:").println("\t\t 6. while(n!=7) { String st1.getConnection("jdbc:odbc:table").readLine()).forName("sun. . strb=strb. System. } try {in=new DataInputStream(System. System. System.jdbc. int i1. } try {con=DriverManager.println("Enter table name:").println("\t\t 7.odbc. } catch(Exception e) { System.out.io.i3.getMessage()).To Insert values").*.println(e.append("CREATE TABLE "+s2+"(").out. n=Integer.st3. int n=0. System.out. System. s=con. System.exit(0).To Drop a table"). import sun.out.println("WHAT IS YOUR NEEDS").To Delete rows"). } catch(Exception e) { System.*.46 AIM: Program to connect to a database created in MS-ACCESS/SQL-SERVER/ORACLE using JDBC concept. System.out.println("\t\t 2.*.To Create a New table"). Perform basic operations of Selection.println(e.out.readLine()).println("\t\t 1.parseInt(in.out.To Update "). StringBuffer strb=new StringBuffer(). ResultSet rs=null. String s2=new String(in.sql.print("ENTER YOUR CHOICE:").println("\t\t 5.out. System.n1=Integer. System.println("\t\t 3. try {Class. Insertion and Deletion on the database. long l1.in). System.jdbc.JdbcOdbcDriver").out.Exit"). Connection con=null. int i.To Select all records ").l3. System. PROGRAM: import java.getMessage()).out. import java.parseInt(in.i2.out.println("\t\t 4.readLine()). Statement s=null.out. public class s10_04 {public static void main(String a[]) throws Exception {DataInputStream in=null.l2. System.out.

out. switch(i1) {case 1: System. System.parseLong(in. case 2: System.println(eno+" "+name+" "+sal).readLine()). st2=in.out. } strb=strb. st1=in.readLine()).executeUpdate(st1).append(" )").println("2.println("1. eno=Integer. System. System.out.out.out.append(st1+" "+st2). i1=Integer.out. break.readLine()). s.DATE/TIME]:"). break. l1=Long.out. s.parseLong(in."+sal+")". System.out.out. case 3: System.out.println("Enter new salary:"). case 2: System.out.println("Employee Name:").print("Enter Field Name:").executeUpdate(st3).println("Enter employee name:").println("Table created"). System.'"+name+"'.readLine()).executeUpdate("UPDATE EMP SET sal="+l2+" WHERE eno="+l1).readLine()). System.print("Enter Data Type[NUMBER. l1=Long. System.out.out.EMPLOYEE SALARY").parseLong(in."). case 3: System. if(i==n1-1) strb=strb.println("Enter Choice:").out.47 for(i=0. st1="INSERT INTO EMP VALUES("+eno+".readLine().println("Enter employee number:").append(st1+" "+st2+". System. System.readLine()).readLine().readLine()).println("3.println("Enter employee number:").readLine(). int eno=Integer.println("Enter Employee number:").println("Enter Employee number:"). s.toString(). System. .out.TEXT.out.print("Enter new name:").executeUpdate("UPDATE EMP SET ename='"+st3+"' WHERE eno="+l1). System.executeUpdate("DELETE FROM EMP WHERE eno="+eno).println("Enter salary:"). break. l1=Long.parseInt(in.out.out.parseInt(in.parseInt(in. System.parseLong(in.println("Enter new salary:").out. long sal=Long.out.out.println("UPDATE WHAT?").++i) {System. case 4: System. st1=in. System. s.println("Table updated"). String name=in. break. break. System.println("Table updated").EMPLOYEE NAME"). else strb=strb.readLine()). st3=in. System.readLine(). st3=strb.readLine().print("Enter employee number:").i<n1.BOTH").out. l2=Long.parseLong(in. s.

readLine()).jdbc. ResultSet rs=null. System.getString("eno")+"\t").sql.getString("ename")+"\t").out.} catch(Exception e) { System. System.name='"+st1+"' WHERE eno="+l1). } System.out.print(rs.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"). while(rs.exit(0).println().out.println("WRONG CHOICE"). try {Class.driver.out.print(rs.readLine().println("driver failure"). break. } }} catch(Exception e) { System.println("Table updated").exit(0). public class oracle {public static void main(String a[]) throws Exception { Statement s=null. st1=in. break. break.getMessage()).println(e.println("Enter table name:").println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~").executeUpdate("UPDATE EMP SET sal="+l2+".out.out.odbc.out.forName("oracle. System.executeUpdate("DROP TABLE "+st1).out. case 6:System.out.*. System. break.48 l2=Long. default:System. } rs. System.out.out. default : System. .jdbc. case 5:System.*.parseLong(in.println("EMPNUMPER"+" "+"EMPNAME"+" "+"SAL"). System.print(rs.close().next()) { System.println("Table Dropped"). s. } break.OracleDriver").println("EMPNUMPER EMPNAME SAL "). case 7:break. import sun. rs=s. s.println("Wrong Choice"). System. Connection con=null.out. } }} import java. System. break.getString("sal")+"\t").out.out.executeQuery("SELECT * FROM EMP"). System.

out.executeQuery("SELECT * FROM EMP").getString("sal")+"\t"). System. } }}   . System. System.49 try {con=DriverManager. while(rs.println("problem on selection"). } try { s=con.print(rs.println().getString("ename")+"\t").getString("empno")+"\t").print(rs.createStatement(). rs=s.out.getConnection("jdbc:odbc:emp".out.out. } } catch(Exception e) { System. System.out.exit(0).println("connection problem").exit(0).next()) { System.print(rs."scott".out. System."tiger"). } catch(Exception e) { System.

Sign up to vote on this title
UsefulNot useful