Top Banner
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Prepared by k.Aparanji Gurdeep kour 1
39
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

1

Page 2: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

2

Page 3: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

3

JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY

HYDERABAD

II Year B.Tech.CSE II-Sem T P C

0 3 2

(07A41291) OBJECT ORIENTED PROGRAMMING LAB

OBJECTIVES:

• To make the student learn a object oriented way of solving problems.

• To teach the student to write programs in java to solve problems.

RECOMMENDED SYSTEMS/SOFTWARE REQUIREMENTS:

• Intel based desktop PC with minimum of 166MHZ or faster processor with atleast 64MB RAM and 100MB free disk space.

• JDK kit. Recommended. WEEK 1:

a) Write a java program that prints all real solutions to the quadratic equation ax2 + bx +c =0. Read in a, b ,c and use the quadratic formula. If the discriminant b2 – 4ac is negative display a message stating that there are no real solutions.

b) The Fibonacci sequence is defined by the following rule: The fist two values in the sequence are 1 and 1. Every subsequent value is the Sum of the two values preceding it. Write a java program that uses both recursive and non recursive functions to print the nth value in the Fibonacci Sequence.

WEEK 2:

a) Write a java program that prompts the user for an integer and then prints Out all prime numbers up to that integer.

b) Write a java program to multiply two given matrices. c) Write a java program that reads a line of integer, and then displays

each integer, and the sum of all the integers(use string tokenizer class of java.util)

Page 4: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

4

WEEK 3:

a) Write a java program that checks whether a given string is a palindrome or Not. Ex: MADAM is a palindrome.

b) Write a java program for sorting a given list of names in ascending order.

c) Write a java program to make frequency count of words in a given text.

WEEK 4:

a) Write a java program that reads a file name from the user , then displays The information about whether the file exists, whether the file readable, Whether the file is writable, the type of file and the length of the file in bytes.

b) Write a java program that reads a file and displays the file on the screen, With a line number before each line.

c) Write a java program that displays the number of characters, lines and Words in a text file.

WEEK 5:

a) Write a java program that: i) Implements stack ADT. ii) Converts infix expression into postfix from iii) Evaluates the postfix expression

WEEK 6:

a) Develop an applet that displays a simple message. b) Develop an applet that receives an integer in one text field, and

computers its factorial Value and returns it in another text field, when the button named “Compute” is clicked.

WEEK 7:

Write a java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +,-,*,% operations. Add a text field to display the result. Week 8 a) Write a java program for handling mouse events.

Page 5: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

5

Week 9:

a) Write a java program that creates three threads. First thread displays “Good Morning” every one second, the second thread displays “Hello” every two seconds and the third thread displays “Welcome” every three seconds. b) Write a java program that correctly implements producer consumer problem using the concpect of inter thread communicatin

week 10:

A)write a program that creates a user interface to perform integer divisions. The user enters two numbers in the textfields, num1 and mum2. The division of num1 and num2 is display in the Result field when the divide button is clicked. If num1 and num2 were not an integer, the program would throw a numberformatexception. If num2 were zero the program would throw an arithmeticexception . week 11:

write a program that implements a simple client/server application

Week 12:

a)write a java program that simulates a traffic light. The progam lets the user to select one of three lights. Red yellow, or green. When radio button is selected the light is turned on, and only one light can be on at time. No light is on when program starts b)Write a java program that allow the user to draw lines, rectangles and arcs.

Page 6: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

6

INDEX

PROGRAM PAGE NO.

1.Write a java program that prints all real solutions to the quadratic

equation ax2 + bx +c =0. Read in a, b ,c and use the quadratic formula. If the discriminant b2 – 4ac is negative display a message stating that there are no real

solutions. 8 2.The Fibonacci sequence is defined by the following rule:The fist two values in the

sequence are 1 and 1. Every subsequent value is the Sum of the two values preceding it. Write a java program that uses both recursive and non recursive functions to print the nth value in the Fibonacci

Sequence. 9,10

3.Write a java program that prompts the user for an integer and then prints Out all

prime numbers up to that integer. 11 4.Write a java program to multiply two given matrices. 12 5.Write a java program that reads a line of integer, and then displays each integer, and

the sum of all the integers(use string tokenizer class of java.util) 13

6.Write a java program that checks whether a given string is a palindrome or

Not. Ex: MADAM is a palindrome. 14 7.Write a java program for sorting a given list of names in ascending order. 14

8.Write a java program to make frequency count of words in a given text. 15 9.Write a java program that reads a file name from the user , then displays The

information about whether the file exists, whether the file readable,Whether the file is writable, the type of file and the length of the file in bytes. 16

10.Write a java program that reads a file and displays the file on the screen, With a

line number before each line. 16 11.Write a java program that displays the number of characters, lines and Words in a

text file. 17

12.Write a java program that:

Implements stack ADT. 18

converts infix expression into postfix from 21

Page 7: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

7

13.Develop an applet that displays a simple message. 24

14.Develop an applet that receives an integer in one text field, and computers its factorial Value and returns it in another text field, when the button named “Compute”

is clicked. 25

15. Write a java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +,-,*,% operations. Add a text field to

display the result. 26 16.Write a java program for handling mouse events. 30 17. Write a java program that creates three threads. First thread displays “Good

Morning” every one second, the second thread displays “Hello” every two seconds and the third thread displays “Welcome” every three seconds. 32

18.Write a java program that correctly implements producer consumer problem

using the concpect of inter thread communication 33 19.write a program that creates a user interface to perform integer divisions. The

user enters two numbers in the textfields, num1 and mum2. The division of num1 and num2 is display in the Result field when the divide button is clicked. If num1 and num2 were not an integer, the program would throw a numberformatexception.

If num2 were zero the program would throw an arithmetic exception. 35 .20.write a program that implements a simple client/server application. 36

21.write a java program that simulates a traffic light. The progam lets the user to select one of three lights. Red yellow, or green. When radio button is selected the light is turned on, and only one light can be on at time. No light is on when program

starts 37 22.Write a java program that allow the user to draw lines, rectangles and arcs.

38

Page 8: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

8

Aim :Write a Java Program that prints all real solutions to the

quadratic equation ax2+bx+C=0. Read in a, b, c and use the

quadratic formula. If the discriminate b2 4ac is negative, display a

message stating that there are no real solutions.

class quadratic { public static void main (String ar[]) { int a,b,c; a = Integer.parse Int (ar[0]); b = Integer.parse Int (ar[1]); c = Integer.parse Int (ar[2]); int d = ((b*b) – (4*a*c)); double r1 = (-b+Math.Sqrt(d))/(2xa); double r2 = (-b – Math, Sqrt (d))/(2xa); if (d<0) { System.out.println (“these are no real solutions”); } else { System.out.println (r1 + “/n” + r2 ); } } } Input : Java quad 1-5 6

Output: 3.0 2.0

Page 9: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

9

Aim : write a Java program of Fibonacci series:

Recursive: class a { int fib(int n) { if (n==0) return 0; else if (n==1) return 1; else return (fib (n-1) + fib (n-2)); } } class fib { public static void main (string ar[]) { a a1= new a(); int i=0,c,n; n= Integer.parse Int (ar []); System.out.println(“fib series”+n+”is”); do { c=a1.fib(i); if (c<=n) System.out.println(C); i=t; } while (c<=n) } } Output : Java fib 10

0

1

1

2

3

5

8

Page 10: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

10

//Aim : WAP for Non-Recursive Fibonacci series:

class ab { public static void main (String ar[]) { int f=1, s=1,t=0; int n = Integer.parse Int (ar[0]); System.out.println(f=””+s); for (int i=3.; i<=n, i++) { t= f+s; System.out.println(t); f=s; s = t; } } } Output : Java ab 10

1 1 2 3 5 8 13 21 34 55

Page 11: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

11

Aim : Write a Java program that prompts the user for an integer and

then prints all prime number upto that integer.

Import java.io.*; class prime { public static void main (String ar[]) throws Exception { BufferedReaderbr=newBufferedReader(new inputreamReader(System.in); Int n=Integer.pareseInt(br.readLine(); For(int i=1;i<=n;i++) int c=0; for (int j=1; j<=is j++) { if (i%j==0) c++; } if (c==2) System.out.println(i); } } Output: Java prime 20

2 3 5 7 11 13 17 19

Page 12: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

12

Aim : Write a Java program to multiply to given matrices.

class matrix { public static void main(String ar[]) { int i,j,k; int a[][]={{1,4,5}, {3,2,1}, {0,6,7}}; int b[][]={{1,2,3}, {7,6,0}, {5,4,1}}; int c[][]=new int [3][3]; for (i=0; i<=2; i++) { for (j=0; j<=2; j++) { c[i][j]=0; for (k=0;k<=2; k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } System.out.println (“multiplication is”); for (i=0; i<a.length; i++) { for (j=0; /<b.length;i++) { System.out.print(c[i][j]+””); } System.out.println(); } } } Output : Java matrix

54 46 8

22 22 10

22 64 7

Page 13: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

13

Aim : WAP that reads a line of integers and then displays each

integers and the sum of all the integers (use string tokenizer class)

import java.io.*; import java.util.*; class ab { public static void main (string ar[]) throcos exception { InputstreamReader isr= new InputStreamReader (system.in); BufferReader br=new BufferReader (isr); String S=br.readLine(); int sum=0; StringTokenizer st=new stringTokenizer (s,””); while (st.hasMoreTokens ()); { String sl= st.nextToken(); int n= integer.parseInt (Sl); System.out.println(n); sum=sum+n; } System.out.println(s); } }

Page 14: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

14

Aim : wap to check a string is palindrome or not

class Poly { public static void main(String ar[]) { String s=ar[0]; String Buffer str =new String Buffer(s); str.reverse(); String s1=str.tostring(); if(s1.Equals(s)); System.out.println(“palindrome”); else System.out.println(“not palindrome”); } } Aim : To sort a given list of names in ascending order

class ab { public static void main(String ar[]) { String s[]={ab”,”ca”,”abc”}; String t; int n=s.length; for(int i=0;i<=n-1;i++) { for (int j=i+1;j<n;j++) { int k=s[i].compareto(s[i]); if (k>=0) { t=s[i]; s[i]=s[j]; s[j]=t; } } } for(i=0;i<=n;i++) System.out.println(s[i]); } }

Page 15: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

15

Aim : wap to make frequency count of words in a given text

import java.util.*; class sdemo { public static void main(String ar[]) { String str=” this is java”; int c=0; StringTokenizer st=new StringTokenizer(str,” “); while(st.hasMoreTokens()) { String s=st.nextToken()) c++; } System.out.println(c); }}

Page 16: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

16

Aim : Wap that reads filename from user then displays information

about whether the file file exits whether the file is readable, whether

the file is writable. The type of file and length of file in bytes

import java.io.*; class ab { public static void main (String ar[]); { File f1 = new File(“a.java”); System.out.println(f1.exists()); System.out.println(f1.canRead()); System.out.println(f1.canwrite()); System.out.println(f1.isfile()); System.out.println(f1.isDirectory()); System.out.println(f1.length()); } } Aim : Wap that reads a file and display the file on the screen with a

line number before each line

import java.io.*; class files { public static void main(String ar[]) throws Exception { int i=1; DataInputstream br=new DataInputStream(System.in); System.out.println(“enter file name”); String s=br.readlnne(); FileInputStream f=new FileInputStream(s); Byte c; System.out.println(i+” “); c=(byte)f.read(); do { System.out.println((char) c); if(c==”\n”) { i++; System.out.println(“\n”+i+” “); }

Page 17: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

17

c=(byte)f..read(); } while(c>0) f.close(); } } Aim: Wap that display the number of characters, lines & words in

text file

import java.io.*; class files { public static void main(String ar[]) { int ch=0,w=0.l=0; DataInputStrean br=new DataInputStream(system.in); System.out.println(“enter filename”); String s=br.readline(); FileInputStream f= new FileInputStream(s); Byte c; c=(byte) f.read(); do { if(c==’ ‘) w++; if(c==”\n”) { w++; if(c==”\n”) { w++; l++; } else ch++; c=(byte)f.read(); } while(c>0) f.close; System.out.println(“no of char”+ch); System.out.println(“no. of words”+w); System.out.println(“no of lines”+l); } }

Page 18: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

18

Aim : write a java program that implements stack ADT

import java.io.*; class S { final int max=20; int top; int s[]=new int[max]; S() { top=-1; } void push(int x) { if(top==max-1) { system.out.println(“stack full”); return; } s[++top]=x; } int pop() { if(top==-1) { System.out.println(“stack empty”); return 0; } int t=s[top--]; return t; } void display() { if(top==-1) System.out.println(“stack empty”); else { for(int i=0;i<=top;i++) { System.out.println(“\t”+s[i]); } } }

Page 19: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

19

} Class Stackadt { public static void main(String args[])throws Exception { DataInputStream br=new DataInputStream(System.in); ss1=new s(); do { System.out.println(“1.push \n 2.pop \n 3.display \n 4.exit”); int n=0,n1=0; System.out.println(“enter ur choice”); n=integer.parseInt(br.readLine()); switch(n) { case 1: system.out.println(“enter no of elements topush”); n1=integer.parseInt(br.readLine()); System.out.println(“enter elements”); for(int i=0;i<n;i++) s1.push(Integer.parseInt(br.readLine()); break; case 2: System.out.println(“enter the no of elements to pop”); int n2=Integer.parseInt(br.readLine()); System.out.println(“poped elements”); for(int i=0;i<n;i++) System.out.println(“\t”+s1.pop()); break; case 3: System.out.println(“elements of stack are”); s1.display(); break; case 4: system.exit(0); break; }} while(true) } } o/p:

Page 20: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

20

java stkadt 1.push 2.pop 3.display 4.exit enter ur choice 1 enter no of elements to push 3 enter the elements 2 3 4 enter ur choice 2 enter the elements to pop 2 poped elements 4 enter ur choice 4

Page 21: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

21

Aim:Write a java program that converts infix expression into postfix

form

import java.io.*; class Stk; { final static int max=50; int top=-1; char s[]=new char[max]; char s2[]=new char[max]; string 1=” “; string t=” “; void push(char x) { if(top==max-1) { System.out.println(“stack is full”); return; top++; s2[top]=x; } char pop(); { if(top==-1) { System.out.println(“stack is empty”); return 0; } char t=s2[top]; top=-1; return t; } int pri(char c) { if(c==’$’) return 3; if(c==’*’||c==’/’||c==’%’) return 1; else return 0; }

Page 22: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

22

void infix() { char opr; for(int i=0;i<s.length;i++) { if(s[i]==’ ‘) continue; if(character.is letter.digits(s[]i])) k+=s[i]; if(s[i]==’(‘) push(s[i]); if(s[i]==’*’||s[i]==’+’||s[i]==’-‘||s[i]==’/’||s[i]==’5’||s[i]==’$’) { if(top!=1) { opr=pop(); while(pri(opr)>=pri(s[i])) { k+=opr; opr=pop(); } push(opr); push(s[i]); } else push(s[i]); } if(s[i]==’)’); { opr=pop(); while(opr!=’(‘) { k+=opr; opr=pop(); } } } while(top!=-1) { opr=pop(); k+=opr; } }

Page 23: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

23

void display() { System.out.println(“postfix notation is /t”+t); } } class Inpost { public static void main(String args[])throws Exception { stk d=new stk(); DataInputStream br=new DataInputStream(System.in); System.out.println(“enter infix notation”); d.a=br.readLine(); for(int i=0;i<d.a.length();i++) d.s[i]=d.a.charAt(i); d.infix(); d.display(); } } o/p: java inpost enter infix expression a+b-c*d/e stack is empty postfix notation is ab+cd*e/-

Page 24: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

24

Aim: write an applet that displays a simple message

import java.awt.*; import java.applet.*; /*<applet code=”colapp.class” width=200 height=200> </applet>*/ public class colapp extends applet { public void paint(Graphics g) { g.setColor(color,red) g.drawLine(10,10,100,10); g.setColor(color.blue); g.drawLine(100,10,100,100); f.setColor(color.green); g.drawLine(10,10,100,100); } }

Page 25: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

25

Aim :develop an applet that receives an integer in one text field, and

compute its factorial value and returns it in another text field, when

the button named compute is clicked

import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code="fact.class" width=300 height=300></applet>*/ public class fact extends Applet implements ActionListener { int n; TextField t1, t2; Label l1; Button b; public void init() { l1=new Label("enter n value",Label.LEFT); t1=new TextField(20); b=new Button("compute"); t2=new TextField(20); add(l1); add(t1); add(b); add(t2); b.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String s1=t1.getText(); int f=1; int n=Integer.parseInt(s1); for(int i=1;i<=n;i++) f=f*i; String s="fact="+f; t2.setText(s); } }

Page 26: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

26

Aim: wap that works as a simple calculator

import java.io.*; import java.lang.*; import java.awt.*; import java.awt.event.*; class exal extends Frame implements ActionListener { String Temp=” ”; GridBagLayout gb; TextField t; Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16; GridBagconstraints gc; exal() { setSize(150,150); gb=new GridBagLayout(); gc=new GridBagconstraints(); t=new TextField(8); b0=new Button(“0”); b0=new Button(“0”); b0=new Button(“0”); b1=new Button(“1”); b2=new Button(“2”); b3=new Button(“3”); b4=new Button(“4”); b5=new Button(“5”); b6=new Button(“6”); b7=new Button(“7”); b8=new Button(“8”); b9=new Button(“9”); b10=new Button(“=”); b11=new Button(“clr”); b12=new Button(“+”); b13=new Button(“-”); b14=new Button(“*”); b15=new Button(“/”); b16=new Button(“%”); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this);

Page 27: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

27

b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); b10.addActionListener(this); b11.addActionListener(this); b12.addActionListener(this); b13.addActionListener(this); b14.addActionListener(this); b15.addActionListener(this); b16.addActionListener(this); t.addActionListener(this); setLayout(gb); set(0,0,4,1,t,gb,gc); set(0,2,1,1,b1,gb,gc); set(1,2,1,1,b2,gb,gc); set(2,2,1,1,b3,gb,gc); set(0,3,1,1,b4,gb,gc); set(1,3,1,1,b7,gb,gc); set(2,3,1,1,b8,gb,gc); set(0,4,1,1,b9,gb,gc); set(1,4,1,1,b10,gb,gc); set(2,4,1,1,b11,gb,gc); set(1,5,1,1,b12,gb,gc); set(2,5,1,1,b1,gb,gc); set(0,5,1,1,b13,gb,gc); set(3,2,1,1,b14,gb,gc); set(3,3,1,1,b15,gb,gc); set(3,4,1,1,b16,gb,gc); set(3,5,1,1,b17,gb,gc); set(9,0,1,1,b1,gb,gc); show(); } void set (int x, int y, int w, int h, Button b, GridBagLayout g1, GridBagConstraints g2) { g2.gridx=x; g2.gridy=y; g2.gridWidth=w; g2.gridheight=h; g1.setConstraints(b,g2); add(b); }

Page 28: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

28

void set(int x, int y, int w, int h, TextField t, GridBagLayout g1, GridBagConstraints g2) { g2.gridx=x; g2.gridy=y; g2.gridWidth=w; g2.gridHeight=h; g1.setConstraints(t,g2); add(t); } public void actionPerformed(ActionEvent ae) { String s=ae.getActionCommand(); If(s==”=”) { temp=” “+(k.getText()); for(int i=0;i<temp.lenth();i++) { if(temp.charAt(i)==’+’) { String num1=temp.subString(0,i); String num2=temp.subString(i+1,temp.length()); int a=Integer.parseInt(num1); int b=Integer.parseInt(num2); int c=a+b; t.setText(“ “+c); } if(temp.charAt(i)==’-’) { String num1=temp.subString(0,i); String num2=temp.subString(i+1,temp.length()); int a=Integer.parseInt(num1); int b=Integer.parseInt(num2); int c=a-b; t.setText(“ “+c); } if(temp.charAt(i)==’*’) { String num1=temp.subString(0,i); String num2=temp.subString(i+1,temp.length()); int a=Integer.parseInt(num1); int b=Integer.parseInt(num2); int c=a*b;

Page 29: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

29

t.setText(“ “+c); } if(temp.charAt(i)==’/’) { String num1=temp.subString(0,i); String num2=temp.subString(i+1,temp.length()); int a=Integer.parseInt(num1); int b=Integer.parseInt(num2); int c=a/b; t.setText(“ “+c); } if(temp.charAt(i)==’%’) { String num1=temp.subString(0,i); String num2=temp.subString(i+1,temp.length()); int a=Integer.parseInt(num1); int b=Integer.parseInt(num2); int c=a%b; t.setText(“ “+c); } } } else if(s==”clr”) { t.setText(“ “); temp=” ”; } else { temp+=s; t.setText(“ “+temp); } } } class calc { Public static void main(String ar[]) { exal e=new exal(); } }

Page 30: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

30

Aim :wap for handling mouse events

import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code=”mouseevents.class” width=300 height=300></applet>*/ public class mouseevents extends Applet implements MouseListener, MouseMotionListener { String msg=” “; int mx=0;my=0; public void init() { add.MouseListener(this); add.MouseMotionListener(this); } public void mouseClicked(MouseEvent me) { mx=0; my=10; msg=”mouse clicked”; repaint(); } public void mouseEntered(MouseEvent me) { mx=0; my=10; msg=”mouse entered”; reapaint(); } public void mouseExited(MouseEvent me) { mx=0; my=10; msg=”mouse exited”; reapaint(); } public void mousePressed(MouseEvent me) { mx=me.getX(); my=me.getY();

Page 31: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

31

msg=”mouse down”; reapaint(); } public void mouseReleased(MouseEvent me) { mx=me.getX(); my=me.getY(); msg=”*”; showStatus(“dragging mouse at”+me.getX()+”,”+me.getY()); reapaint(); } public void mouseMoved(MouseEvent me) { showStatus(“moving mouse at”+me.getX()+”,”+me.getY()); } Public void paint(Graphics g) { g.drawString(msg,mx,my); } }

Page 32: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

32

Aim :wap that creates three theads. First thread displays “good

morning” every one second. The second thread display” hello” every

two seconds and third thread displays “welcome” every three seconds

class mt implants Runnable { Thread t; String s; int t; mt(String ss, int tt) { t=new Thread(this, ss); s=ss; t=tt t.start(); public void run() { for(;;) { System.out.println(s); try { Thread.sleep(t); } Catch(Exception e) { } } } class ab { public static void main(String ar[]) { mt t1=new mt(“good morning”,1000); mt t2=new mt(“hello”,2000); mt t1=new mt(“welcome”,3000); }}

Page 33: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

33

Aim :Wap that correctly implements producer customer problem

using the concept of interthread communication

class Q { int n; Boolean valueset=false; Synchronized int get() { if(! valueset) try { wait(); } catch(InterruptedException e) { System.out.println(“interrupted Exeception caught”); } System.out.println(“GDk:”+n); valueSet=false; notify(); return n; } Synchronized void put(int n) { if (valueset) try { wait(); } Catch(InterruptedException e) { System.out.println(“interrupted Exception Caught”); } this.n=n; valueset=true; System.out.println(“put:”+n); notify(); } } class producer implements Ryunnable { Q q;

Page 34: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

34

Producer (Q q) { this.q=q; new thread(this,”producer”).strat(); } public void run() { int i=0; while(true) { q.put(i++); } } } class consumer implements Runnable { Q q; Consumer(q q) { this.q=q; new Thread(this,”consumer”).strat(); } public void run() { while(true) { q.get(); } } } class pcfixed { public Static void main(String ar[]) { Q q=new Q(); new producer(q); new consumer(q); System.out.println(“press ctrl c to stop”); } }

Page 35: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

35

Aim : write a program that creates a user interface to perform

integer divisions. The user enters two numbers in the textfields, num1

and num2. the division of num1 and num2 is displayed in the result

field when the divide button is clicked. If num1 or num2 were not an

integer, the program would throw a NumberFormatException. If

num2 were zero, the program would trow an ArithmeticException.

Display the exception in message dialog box

import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code="ap.class" width=300 height=300></applet>*/ public class ap extends Applet implements ActionListener { Button b1; TextField t1, t2,r ; Label l1,l2; String s,e; public void init() { l1=new Label("enter x"); l2=new Label("enter y"); t1=new TextField(10); t2=new TextField(10); r=new TextField(10); b1=new Button("division"); add(l1); add(t1); add(l2); add(t2); add(b1); add(r); b1.addActionListener(this); } public void actionPerformed(ActionEvent ae) { int x=Integer.parseInt(t1.getText()); int y=Integer.parseInt(t2.getText()); s="division="+(x/y); r.setText(s); }

Page 36: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

36

Aim: wap program implements simple Client/Server program

Import javax.servlet.*’ Import javax.servlet.http.*; Import java.io.*; Public class GetPostServlet extends HttpServlet { Public void doGet(HttpServletRequest req, HttpServletResponse res) Throws ServletException, IOException { Res.setContentType(“text/html”); Printwriter out=res.getwrite(); Out.println(“<html><head>”); Out.println(“<title>handling get request</title>”); Out.println(“<body><h2>get request received</h2>”); Out.println(“<h3>type ur name and press send</h3>”); Out.println(“<form method=\”post\>”); Out.println(“<input type=\”text”\name=\”user\”>”); Out.pritnln(“<input type=\”submit\” value=\”send\”/>; Out.println(“</form>”); Out.println(“</body></html>”)’ } Public void doPost(httpservletrequest req, httpservletresponse res)throws servletexecption, ioexception { Res.setcontentType(“text/html”); Printwriter out=res.getwriter(); String user = req.getParameter(“user”); Out.println(“<html><head>”); Out.println(“<title>handling post request</title>”); Out.println(“<body><h2>post request received</h2>”); Out.println(“hello”<em>mr.”+user+”</em>”); Out.println(“</body></html>”); } }

Page 37: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

37

Aim: Write a java program that simulates a traffic light. The

program lets the user select one of three lights. Red,yello,or green.

When a radio button is selected the light is turned on, and only one

light can be on at a time. No light is on when the program starts.

import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code="abc.class" width=300 height=300></applet>*/ public class abc extends Applet implements ItemListener { CheckBox c1,c2,c3; CheckBoxGroup cbg=new CheckBoxGroup(); public void init() { c1=new CheckBox(“red”,cbg,true); c2=new CheckBox(“yellow”,cbg,true); c3=new CheckBox(“green”,cbg,true); add(c1); add(c2); add(c3); c1.addItemListener(this); c2.addItemListener(this); c3.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { String s=(cbg.getSelectCheckBox()).getLable()); if(s.equals(“red”)) setBackGround(Color.red); elseif(s.equals(“yellow”)) setBackGround(Color.yellow); elseif(s.equals(“green”)) setBackGround(Color.green); } }

Page 38: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

38

Aim: wap that allows the user to draw lines, rectangles and ovals

import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code=”draw.class” width=1500 height=1500></applet>*/ public class draw extends Applet implements ItemListener, MouseListener,MouseMotionListener { Choice ch; String msg=” “; int x1=0;x2=0;y1=0;y2=0; public void init() { ch=new choice(); ch.add(“select anyone”); ch.add(“Line); ch.add(“Rectangle”); ch.add(“oval”); add(ch); setBackground(color.yellow); setForeground(color.red); addMouseListener(this); ch.additemListener(this); addMouseMotionListener(this); } public void itemStatechanged(item Event ie) { repaint(); } public void mouseclicked(Mouse Event me) { repaint(); } public void mouseEntered(MouseEvent me) { repaint() } public void mousePressed(MouseEvent me) { x1=me.getx(); y1=me.gety(); repaint(); }

Page 39: java

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Prepared by k.Aparanji Gurdeep kour

39

public void MouseReleased(Mouse Event me) { x2=me.getx(); y2=me.getY(); repaint(); } public void mouseDragged(mouse Event me) { repaint(); } public void mouseMoved(mouseEvent me) { showStatus(“morning at”+me.getx()+”, “+me.gety()); } public void paint(Graphics g) { msg=ch.getselectedItem(); if(msg.Equals(“line”) { g.drawLine(x1,y1,x2,y2); } if(msg.Equals(“rectangle”) { g.fillRect(x1,y1,x2-x1,y2-y1); } if(msg.Equals(“oval”)); { g.filloval(x1,y1,x-x1,y2-y1); } } }