• No results found

JNTU JAVA Lab Manual Programs

N/A
N/A
Protected

Academic year: 2021

Share "JNTU JAVA Lab Manual Programs"

Copied!
108
0
0

Loading.... (view fulltext now)

Full text

(1)

LAB: Object Oriented Programming IInd CSE

IV YEAR B.Tech

JAVA PROGRAMMING MANUAL

OOP’s Through Java

Prepared

By

R.Venkata Subbaiah

Associate Professor IC RNEC@Development Cell

Department of CSE

(2)

LAB: Object Oriented Programming IInd CSE

(3)

LAB: Object Oriented Programming IInd CSE

Week:1-A

AIM:write a java program to print all real solutions to the qudratic eq ax2+b+c=0 Read a,b,c values and use the formula (–b+sqrt(b2-4ac))/2a.

SOURCE CODE:

//importing io class import java.io.*; //importing Math class import java.lang.Math; class qd_eq

{

public static void main(String[] args) throws IOException {

String s; int a,b,c,d; double r1,r2;

DataInputStream in=new DataInputStream(System.in); System.out.print("Enter a value?");

s=in.readLine(); a=Integer.parseInt(s);

System.out.print("Enter b value?"); s=in.readLine();

(4)

LAB: Object Oriented Programming IInd CSE b=Integer.parseInt(s); System.out.print("Enter c value?"); s=in.readLine(); c=Integer.parseInt(s); d=((b*b)-(4*a*c)); if(d<0) {

System.err.println("No real solution..."); } else { r1=((-b)+Math.sqrt(d))/(2*a); r2=((-b)-Math.sqrt(d))/(2*a); System.out.println("Root 1 = "+r1); System.out.println("Root 2 = "+r2); } } }

(5)

LAB: Object Oriented Programming IInd CSE OUTPUT: COMPILATION: D:\cse>javac qd_eq.java INTERPRETATION: D:\cse>java qd_eq

(6)

LAB: Object Oriented Programming IInd CSE

Week:1-B

AIM: write a java program to print a fibonacci series 1,1,2,3……….. upto the scanned or need value

File Name: fib.java Source code:

//importing io classes import java.io.*;

class Fibonacci {

public static void main(String[] args) throws IOException {

DataInputStream in=new DataInputStream(System.in); String s; int a=1,b=1,c=a+b; int n; System.out.print("Enter nth value?"); s=in.readLine(); n=Integer.parseInt(s); System.out.print(a+","+b);

(7)

LAB: Object Oriented Programming IInd CSE while(c<=n) { System.out.print(c+","); a=b; b=c; c=a+b; } } }

(8)

LAB: Object Oriented Programming IInd CSE Out put: Compilation: D:/cse>javac fibonacci.java Interpretation: D:/cse>java fibanocci

(9)

LAB: Object Oriented Programming IInd CSE

Week:2-A

AIM : write a java program to accept a number and print all the numbers. FileName: prime.java Source code: //import io package import java.io.*; class prime {

public static void main(String[] args) throws IOException {

String s; int no,i,j;

DataInputStream in=new DataInputStream(System.in); System.out.print("Enter a value?");

s=in.readLine();

(10)

LAB: Object Oriented Programming IInd CSE { for(j=2;j<i;j++) { if(i%j==0) break; } if(i==j)

System.out.println(i+" is a prime no.."); }

} }

(11)

LAB: Object Oriented Programming IInd CSE Output: Compilation: D:\cse>javac prime.java Interpretation: D\:cse>java prime

(12)

LAB: Object Oriented Programming IInd CSE

Rollno:09771A0524 WEEK:2-B

AIM: write a java program to display multiplication of two 3*3 matrices. File name: matmul.java

Source code:

//import io package import java.io.*; class matmul {

public static void main(String[] args) throws IOException { int mat1[][]={{1,2,3},{4,5,6},{7,8,9}}; int mat2[][]={{1,2,3},{4,5,6},{7,8,9}}; int mat3[][],i,j,k; mat3=new int[3][3]; for(i=0;i<=2;i++)

(13)

LAB: Object Oriented Programming IInd CSE { for(j=0;j<=2;j++) { for(k=0;k<=2;k++) { mat3[i][j]+=mat1[i][k]*mat2[k][j]; } } }

System.out.println("After Matrix Multiplicatin..."); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { System.out.print(mat3[i][j]+"\t"); } System.out.print("\n"); } }

(14)

LAB: Object Oriented Programming IInd CSE Output: Compilation: D:\cse>javac matmul.java Interpretation: D:\cse>java matmul

(15)

LAB: Object Oriented Programming IInd CSE

Rollno:09771A0524 WEEK:2-C

AIM:write a java program that reads a line of integers and then display each integer and the sum of all integers.

(16)

LAB: Object Oriented Programming IInd CSE Source code: import java.io.*; import java.util.*; class stoken {

public static void main(String arg[]) throws IOException {

int sm=0,no; String s;

DataInputStream in=new DataInputStream(System.in); System.out.print("Enter numbers ?"); s=in.readLine();

StringTokenizer token =new StringTokenizer(s); while(token.hasMoreTokens()) { no=Integer.parseInt(token.nextToken()); sm+=no System.out.println(no); } System.out.println("Sum : "+sm); } }

(17)

LAB: Object Oriented Programming IInd CSE

OUT PUT:

(18)

LAB: Object Oriented Programming IInd CSE INTERPRETATION:

D:/cse>java stoken

Rollno:09771A0524 WEEK:3-A

(19)

LAB: Object Oriented Programming IInd CSE File Name: pal.java

Source code:

\\import io package\\ import java.io.*; class pal

{

public static void main(String arg[]) throws IOException {

int i,j,len; String s;

DataInputStream in=new DataInputStream(System.in); System.out.print("Enter a string ?"); s=in.readLine(); len=s.length(); for(i=0,j=len-1;i<len/2;i++,j--) { if(s.charAt(i)!=s.charAt(j)) break;

(20)

LAB: Object Oriented Programming IInd CSE if(i==j)

System.out.println("Pal string"); else

System.out.println("Not a Pal string");

} }

Output: Compilation:

(21)

LAB: Object Oriented Programming IInd CSE D:/cse>javac pal.java

Interpretation:

D:/cse>java pal

(22)

LAB: Object Oriented Programming IInd CSE AIM: write a java program for sorting given list of names in ascending or descending. FileName: strsort.java Source code: \\importing io class\\ import java.io.*; class strsort {

public static void main(String arg[]) throws IOException {

int i,j,len;

String s[]=new String[5],swp; DataInputStream in=new DataInputStream(System.in);

System.out.println("Enter 5 strings ?"); for(i=0;i<=4;i++) s[i]=in.readLine(); for(i=0;i<=3;i++) { for(j=i+1;j<=4;j++) if(s[i].compareTo(s[j])>0) {

(23)

LAB: Object Oriented Programming IInd CSE swp=s[i]; s[i]=s[j]; s[j]=swp; } } System.out.println("After sort...."); for(i=0;i<=4;i++) System.out.println(s[i]); } }

(24)

LAB: Object Oriented Programming IInd CSE Output: Compilation: D:/cse>javac strsort.java Interpretation: D:/cse>java strsort

(25)

LAB: Object Oriented Programming IInd CSE

Rollno:09771A0524 WEEK:3-C

Aim: write a java program to count frequency of words File Name: wordcount.java

Source code: \\import io package\\ import java.io.*; import java.util.*; class wordcount {

public static void main(String arg[]) throws IOException {

int ctr=0; String s,s1,s2;

DataInputStream in=new DataInputStream(System.in); System.out.println("Enter a string with words ?");

s=in.readLine();

StringTokenizer token1 =new StringTokenizer(s); StringTokenizer token2 ;

(26)

LAB: Object Oriented Programming IInd CSE { token2=new StringTokenizer(s); s1=token1.nextToken(); ctr=0; while(token2.hasMoreTokens()) { s2=token2.nextToken(); if(s1.equals(s2)) ctr++; } System.out.println("'"+s1+"' Frequency "+ctr+" Times"); } } }

(27)

LAB: Object Oriented Programming IInd CSE Output: Compilation: D:\cse>javac wordcount.java Interpretation: D\:cse>java wordcount

(28)

LAB: Object Oriented Programming IInd CSE

Rollno:09771A0524 WEEK:4-A

AIM:Program to read a file and check whether the file exists,readable,writable and also print the length.

File Name: filetest.java So urce code:

//import io package// import java.io.*; class filetest {

public static void main(String args[]) throws IOException {

String fname;

DataInputStream in=new DataInputStream(System.in);

System.out.print("Enter a file name?"); fname=in.readLine();

//opening file

(29)

LAB: Object Oriented Programming IInd CSE if(f.exists())

System.out.println("File exists.."); else

System.out.println("File not exists..");

if(f.canRead())

System.out.println("File is readable.."); else

System.out.println("File is not readable..");

if(f.canWrite())

System.out.println("File is writable.."); else

System.out.println("File is not Writable.."); System.out.println("File length :"+f.length()); }

(30)

LAB: Object Oriented Programming IInd CSE OUTPUT: Compilation: D:csejava>javac filetest.java Interpretataion: D:\csejava>java filetest

(31)

LAB: Object Oriented Programming IInd CSE

Rollno:09771A0524 WEEK:4-B

AIM:Program to read a file and print the file with line numbers. FileName: fileread.java Source code: //import io package// import java.io.*; class fileread {

public static void main(String args[]) throws IOException {

int ch,ctr=1; String fname;

DataInputStream in=new DataInputStream(System.in);

(32)

LAB: Object Oriented Programming IInd CSE fname=in.readLine();

//opening file

FileInputStream f=new FileInputStream(fname);

System.out.print(ctr+" "); while((ch=f.read())!=-1) { System.out.print((char)ch); if(ch=='\n') { ctr++; System.out.print(ctr+" "); } } } }

(33)

LAB: Object Oriented Programming IInd CSE OUTPUT: Compilation: D:\csejava>javac fileread.java Interpretation: D:\csejava>java fileread

(34)

LAB: Object Oriented Programming IInd CSE

Rollno:09771A0524 WEEK:4-C

AIM:Program to read a file and print char count,word count,line count. File name: edit filestat.java

(35)

LAB: Object Oriented Programming IInd CSE //import io package//

import java.io.*; class filestat {

public static void main(String args[]) throws IOException {

int pre=' ' , ch , ctr=0 , L=0 , w=1; String fname;

DataInputStream in=new DataInputStream(System.in); System.out.print("Enter a file name?");

fname=in.readLine(); //opening file

FileInputStream f=new FileInputStream(fname); while((ch=f.read())!=-1) { //char count if(ch!=' ' && ch!='\n') ctr++; //line count if(ch=='\n') L++;

(36)

LAB: Object Oriented Programming IInd CSE if(ch==' ' && pre!=' ')

w++; pre=ch; } System.out.println("Char count="+ctr); System.out.println("Word count="+(w+(L-1))); System.out.println("Line count="+L); } } OUTPUT: Compilation: D:\csejava>javac filestat.java Interpretation:

(37)

LAB: Object Oriented Programming IInd CSE D:\csejava>java filestat

Rollno:09771A0524 WEEK:5-A

(38)

LAB: Object Oriented Programming IInd CSE Edit stackadt.java

Source code:

//import io package// import java.io.*;

//stack capable to store 4 elements class Stack { int max,top; int no[]; public Stack() { no=new int[5]; top=-1; max=5; }

public void push(int n) { if(top<max-1) { no[++top]=n; } else

(39)

LAB: Object Oriented Programming IInd CSE System.out.println("Stack full..");

}

public int pop() { int val; if(top==-1) { System.out.println("Stack is Empty..."); return -1; } else { val=no[top--]; } return val; }

public void print() {

System.out.println("Stack elements are.."); for(int i=0;i<top;i++)

(40)

LAB: Object Oriented Programming IInd CSE }

class StackTest {

public static void main(String arg[]) throws IOException {

Stack s=new Stack(); s.push(11); s.push(12); s.push(13); s.push(14); s.push(15); s.push(16); s.print(); System.out.println("Pop ival="+s.pop()); s.print(); System.out.println("Pop ival="+s.pop()); s.print(); } } Output: Compilation:

(41)

LAB: Object Oriented Programming IInd CSE D:\cse>javac stackadt.java Interpretation: D:\cse>java stackadt Week:5-B

(42)

LAB: Object Oriented Programming IInd CSE AIM: write a java program to convert infix expression to postfix expression forms. File name: Edit intopost.java Source code: //import io package// import java.io.*; class intopost { int top,j,i; String str; char t,postfix[],s[]; public intopost() { top=-1;i=0;j=0; postfix=new char[30]; s=new char[30]; }

public void push(char t) top++;

s[top]=t; }

(43)

LAB: Object Oriented Programming IInd CSE { char t; t=s[top]; top--; return t; }

public void check() { while(priority(t)<=priority(s[top])) postfix[j++]=pop(); } int priority(char t) { if(t=='^') return 4; else if(t=='*') return 3; else if(t=='/')

(44)

LAB: Object Oriented Programming IInd CSE else if(t=='+' || t=='-') return 1; else return 0; }

public void convert(String str) {

Push(‘#’) while(i<str.length()) {

t=str.charAt(i);

if( (t>='a' && t<='z') || (t>='A' && t<='Z')) postfix[j++]=t; else { if(t=='+' || t=='-' || t=='*' || t=='/' || t=='(' || t==')' || t=='^') { switch(t) { case '+':

(45)

LAB: Object Oriented Programming IInd CSE case '-': check(); push(t); break; case '*': case '/': check(); push(t); break; case '(': push(t); break; case '^': check(); push(t); break; case ')': do { t=pop();

(46)

LAB: Object Oriented Programming IInd CSE }while(t!='('); j--; break; } } } i++; } while(s[top]!='#') postfix[j++]=pop(); postfix[j]='\0'; System.out.println(postfix); }

public static void main(String args[]) throws IOException {

String str;

DataInputStream in=new DataInputStream(System.in); System.out.println("Enter a string?");

str=in.readLine();

(new intopost()).convert(str); }

(47)

LAB: Object Oriented Programming IInd CSE Output: Compilation: D:\cse>javac intopost.java Interpretation: D:\cse>java intopost

(48)

LAB: Object Oriented Programming IInd CSE

Week:5-C

AIM: write a java program to evaluate a postfix expression. File name:

Edit pfexevl.java Source code:

//import io package// import java.io.*;

/import lang package// import java.lang.Math; class pfixevl { int top,j,i; String str; char t,postfix[]; int s[]; DataInputStream in; public pfixevl() {

(49)

LAB: Object Oriented Programming IInd CSE top=-1;i=0; postfix=new char[30]; s=new int[30]; in=new DataInputStream(System.in); }

public void push(int val) {

top++; s[top]=val; }

public int pop() { int val; val=s[top]; top--; return val; }

public void evaluate(String str) throws IOException {

int op1,op2,value=0; while(i<str.length())

(50)

LAB: Object Oriented Programming IInd CSE t=str.charAt(i);

if( (t>='a' && t<='z') || (t>='A' && t<='Z')) {

System.out.print("Enter value for '"+t+"' ?"); value=Integer.parseInt(in.readLine()); push(value); } else { switch(t) { case '+': op2=pop(); op1=pop(); value=op1+op2; push(value); break; case '-': op2=pop(); op1=pop(); value=op1-op2; push(value);

(51)

LAB: Object Oriented Programming IInd CSE break; case '*': op2=pop(); op1=pop(); value=op1*op2; push(value); break; case '/': op2=pop(); op1=pop(); value=op1*op2; push(value); break; case '^': op2=pop(); op1=pop(); value=(int)Math.pow((float)op1,(float)op2); push(value); break; } } i++;

(52)

LAB: Object Oriented Programming IInd CSE }

System.out.println("postfix evaluated val = "+value); }

public static void main(String args[]) throws IOException {

String str;

DataInputStream in=new DataInputStream(System.in); System.out.print("Enter a string?");

str=in.readLine();

(new pfixevl()).evaluate(str); }

(53)

LAB: Object Oriented Programming IInd CSE output: Compilation: D:/cse>javac pfexevl.java Interpretation: D:/cse>java pfexevl

(54)

LAB: Object Oriented Programming IInd CSE

Week:6-A

Aim: write a java program to develop an applet that display simple message. File name:

(55)

LAB: Object Oriented Programming IInd CSE Applt.java

Source code:

import java.awt.*; import java.applet.*;

public class applt extends Applet {

public void init() {

resize(250,250); }

public void paint(Graphics g) {

Font myfont=new Font("Times new roman",Font.BOLD + Font.ITALIC,25); g.setFont(myfont); g.drawRect(100,100,300,450); g.setColor(Color.orange); g.fillRect(100,100,30,50); g.setColor(Color.red); g.drawString("hello world",120,120); g.drawRect(100,100,300,450);

(56)

LAB: Object Oriented Programming IInd CSE g.fillRect(150,150,30,50); } } /*

<applet code=applt.class height=300 width=400> </applet>

*/

Output: Compilation:

(57)

LAB: Object Oriented Programming IInd CSE Interpretation:

D:\cse>appletviewer applt.java

Week:6-B

(58)

LAB: Object Oriented Programming IInd CSE AIM:Develop an Applet that receives an integer in one text field and computes its factorial value and returans it in another text field,when the button named

compute is clicked.

File name: edit appltfact.java Source code:

\\import java packages\\ import java.applet.*; import java.awt.*; import java.awt.event.*;

class actlstn implements ActionListener {

public void actionPerformed(ActionEvent e) { int ctr=1,no=0,fact=1; Button bt; bt=(Button)e.getSource(); if((bt.getLabel()).equals("compute")) { no=Integer.parseInt(appltfact.t1.getText()); while(ctr<=no) {

(59)

LAB: Object Oriented Programming IInd CSE fact*=ctr; ctr++; } appltfact.t2.setText(String.valueOf(fact)); } System.out.println("...."); } }

public class appltfact extends Applet {

static TextField t1,t2; Label l1,l2;

Button b;

public void init() {

l1=new Label("enter an integer."); l2=new Label("fatorial val:") t1=new TextField();

t2=new TextField(" "); b=new Button("compute"); add(l1);

(60)

LAB: Object Oriented Programming IInd CSE add(l2); add(t2); b.addActionListener(new actlsn()); add(b); setSize(300,400); setVisible(true); }

public void paint(Graphics g) {

showStatus("computing Factorial value..."); }

}

\\applet code\\ /*

<applet code=appltfact.class height=300 width=500 > </applet>

(61)

LAB: Object Oriented Programming IInd CSE OUTPUT:

Compilation:

E:\ADITYA\csejava>javac appltfact.java Interpretation:

(62)

LAB: Object Oriented Programming IInd CSE

Week:7

AIM:Write a java program that works as a simple calculator use a grid layput to arrange for the digits and for +,-,*,/,% operations.Adda text field to display the resut.

File name:

Edit appltcal.java Source code:

\\importing java packages\\ import java.applet.*;

import java.awt.*; import java.awt.event.*;

//event listener interface for event handling

class actlstn implements ActionListener {

public void actionPerformed(ActionEvent e) {

int no=0,val,prev; String txt;

(63)

LAB: Object Oriented Programming IInd CSE bt=(Button)e.getSource(); txt=bt.getLabel(); if(txt.equals("C")) { appltcal.t.setText(""); } else { if(txt.equals("+")) { if(appltcal.sta==0) { appltcal.pval=Integer.parseInt(appltcal.t.getText()); appltcal.t.setText(""); appltcal.sta=1; } else { no=appltcal.pval;

(64)

LAB: Object Oriented Programming IInd CSE appltcal.t.setText(String.valueOf(no)); appltcal.sta=0; } } if(txt.equals("-")) { if(appltcal.sta==0) { appltcal.pval=Integer.parseInt(appltcal.t.getText()); appltcal.t.setText(""); appltcal.sta=1; } else { no=appltcal.pval; no-=Integer.parseInt(appltcal.t.getText()); appltcal.t.setText(String.valueOf(no)); appltcal.sta=0; } } if(txt.equals("*")) {

(65)

LAB: Object Oriented Programming IInd CSE if(appltcal.sta==0) { appltcal.pval=Integer.parseInt(appltcal.t.getText()); appltcal.t.setText(""); appltcal.sta=1; } else { no=appltcal.pval; no*=Integer.parseInt(appltcal.t.getText()); appltcal.t.setText(String.valueOf(no)); appltcal.sta=0; } }

(66)

LAB: Object Oriented Programming IInd CSE { if(appltcal.sta==0) { appltcal.pval=Integer.parseInt(appltcal.t.getText()); appltcal.t.setText(""); appltcal.sta=1; } else { no=appltcal.pval; no/=Integer.parseInt(appltcal.t.getText()); appltcal.t.setText(String.valueOf(no)); appltcal.sta=0; } } if(txt.equals("%")) { if(appltcal.sta==0) { appltcal.pval=Integer.parseInt(appltcal.t.getText()); appltcal.t.setText(""); appltcal.sta=1;

(67)

LAB: Object Oriented Programming IInd CSE } else { no=appltcal.pval; no%=Integer.parseInt(appltcal.t.getText()); appltcal.t.setText(String.valueOf(no)); appltcal.sta=0; } } } } }

public class appltcal extends Applet {

static int sta,pval; static TextField t; Button a,m,d,s,r,b;

(68)

LAB: Object Oriented Programming IInd CSE { t=new TextField("000000"); a=new Button("+"); s=new Button("-"); d=new Button("/"); m=new Button("*"); r=new Button("%"); b=new Button("C"); //adding listener

actlstn lstn=new actlstn(); a.addActionListener(lstn); s.addActionListener(lstn); d.addActionListener(lstn); m.addActionListener(lstn); r.addActionListener(lstn); b.addActionListener(lstn); //setting panel and layout

p=new Panel();

p.setLayout(new GridLayout(3,2)); p.add(t);

p.add(a); p.add(s);

(69)

LAB: Object Oriented Programming IInd CSE p.add(d); p.add(m); p.add(r); p.add(b); //adding pane add(p); setSize(300,400); setVisible(true); }

public void paint(Graphics g) { showStatus("Calculator..."); } } //applet code //

(70)

LAB: Object Oriented Programming IInd CSE <applet code=appltcal.class height=300 width=400>

</applet> */ OUTPUT: Compilation: D:\csejava>javac appltcal.java Interpretation: D:\csejava>appletviewer appltcal.java

(71)

LAB: Object Oriented Programming IInd CSE

Week:1-A

AIM:Write a java program for handling mouse events [whenever user moves the mouse it had to display the xy coordinates on the canvas(graphics]

(72)

LAB: Object Oriented Programming IInd CSE edit appltmouse.java Source code: import java.applet.; import java.awt.Graphics; import java.awt.TextField; import java.awt.event.MouseMotionListener; import java.awt.event.MouseEvent; import java.awt.Panel;

public class appltmouse1 extends Applet implements MouseMotionListener {

static TextField t; int x,y;

public void mouseDragged(MouseEvent m) {

}

public void mouseMoved(MouseEvent m) { x=m.getX(); y=m.getY(); appltmouse.t.setText(String.valueOf(x)+" , "+String.valueOf(y)); repaint();

(73)

LAB: Object Oriented Programming IInd CSE }

public void init() {

t=new TextField("..."); Panel p=new Panel(); p.add(t);

add(p);

//mouse event deligation

addMouseMotionListener(this); setSize(300,400);

setVisible(true); }

public void paint(Graphics g) { g.drawRect(20,20,100,200); g.drawString(t.getText(),x,y); } } //applet code/// /*

(74)

LAB: Object Oriented Programming IInd CSE </applet> */ OUTPUT: Compilation:

(75)

LAB: Object Oriented Programming IInd CSE D:\csejava>javac appltmouse.java

Interpretation:

D:\csejava>appletviewer appltmouse.java

(76)

LAB: Object Oriented Programming IInd CSE

AIM:Write a java program that create 3 threads that the 1st thread to display GOOD MORNING for every 1second,2nd thread to display HELLO for every 2seconds and the 3rd thread to display WELCOME for every 3 seconds.

File name:edit multhread.java Source code:

import java.io.*; import java.lang.*;

class threada extends Thread {

public void run() { for(; ;) { try { System.out.println("GOOD MORNING"); sleep(1000); } catch(Exception e) {

(77)

LAB: Object Oriented Programming IInd CSE System.out.println(e.toString()); } } } }

class threadb extends Thread {

public void run() { for(; ;) { try { System.out.println("HELLO"); sleep(2000); } catch(Exception e) { System.out.println(e.toString());

(78)

LAB: Object Oriented Programming IInd CSE }

} }

class threadc extends Thread {

public void run() { for(; ;) { try { System.out.println("WELCOME"); sleep(3000); } catch(Exception e) { System.out.println(e.toString()); } } } }

(79)

LAB: Object Oriented Programming IInd CSE class multhreadss

{

public static void main(String arg[]) {

//creating new thread threada t1=new threada(); threadb t2=new threadb(); threadc t3=new threadc();

//staring threads t1.start(); t2.start(); t3.start(); } }

(80)

LAB: Object Oriented Programming IInd CSE Output: Compilation: D:/csejava>javac multhreadss.java Interpretation: D:\csejava>java multhreadss

(81)

LAB: Object Oriented Programming IInd CSE

Week:1-A

AIM: write a java program that implements producer,consumer problem using the concept of inter threacommunication. File name:

Edit producerconsumer.java Source code:

\\import io package\\ import java.io.*;

class Consumer extends Thread {

private CubbyHole cubbyhole; private int number;

public Consumer(CubbyHole c,int number) {

cubbyhole=c;

this.number=number;

(82)

LAB: Object Oriented Programming IInd CSE { int value=0; for(int i=0;i<10;i++) { value=cubbyhole.get(); System.out.println("Consumer’ #’this.number+got+value); } } }

class Producer extends Thread {

private CubbyHole cubbyhole; private int number;

public Producer(CubbyHole c, int number) {

cubbyhole=c;

this.number=number; }

public void run() {

(83)

LAB: Object Oriented Programming IInd CSE { cubbyhole.put(i); try { sleep(100); } catch(InterruptedException e) { e.printStackTrace(); }

System.out.println("Consumer #"+this.number+"put:" +i); }

} }

class CubbyHole {

private int contents;

private boolean available=false; public synchronized int get() {

(84)

LAB: Object Oriented Programming IInd CSE while(available==false) { try { wait(); } Catch(exception e) { e.printStackTrace(); } } available=false; notifyAll(); return contents; }

public synchronized void put(int value) {

while(available==true) {

(85)

LAB: Object Oriented Programming IInd CSE { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } available=true; contents=value; notifyAll(); } }

public class ProducerConsumer {

public static void main(String arg[]) {

CubbyHole c=new CubbyHole(); Producer p1= new Producer(c,2); Consumer c1=new Consumer(c,1);

(86)

LAB: Object Oriented Programming IInd CSE p1.start(); c1.start(); } } Output: Compilation: D:\cse>javac producerconsumer.java Interpretation: D:\cse>java producerconsumer

(87)

LAB: Object Oriented Programming IInd CSE

Week:1-A

AIM:Program to create an interface to perform division operations when user enters the two integers.The division of two values must displayed in tth ethird text box when user clicks the CAL button.if num1 & num2 are not integers then the program to throw NumberFormatException,when num1 divide with 0 it has to throw ArithematicException in exception Message label.

File name: framediv.java Source code:

import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*;

(88)

LAB: Object Oriented Programming IInd CSE JPanel p;

JTextField t1,t2,t3; JButton b;

JLabel msg;

public void actionPerformed(ActionEvent e) { int n1,n2,n3; JButton bt; bt=(JButton)e.getSource(); if(bt.getLabel().equals("Cal")) { try { n1=Integer.parseInt(t1.getText()); n2=Integer.parseInt(t2.getText()); n3=n1/n2; t3.setText(String.valueOf(n3)); msg.setText("Calculation performed..."); } catch(NumberFormatException ex) {

(89)

LAB: Object Oriented Programming IInd CSE }

catch(ArithmeticException ex) {

msg.setText("Zero divide canot possible"); }

} }

public framediv() {

setTitle("Frame for division.."); p=new JPanel();

t1=new JTextField("10000"); t2=new JTextField("1000");

t3=new JTextField("Result Here"); b=new JButton("Cal");

//adding action listener b.addActionListener(this); p.add(t1);

p.add(new JLabel(" / ")); p.add(t2);

(90)

LAB: Object Oriented Programming IInd CSE p.add(b);

msg=new JLabel("Click Cal Button to perform cal..."); p.add(msg);

add(p);

setSize(300,400); setVisible(true); }

public static void main(String args[]) {

new framediv(); }

}

(91)

LAB: Object Oriented Programming IInd CSE Output: Compilation: D:\csejava>javac Interpretation: D:\csejava>appletviewer framediv.java

(92)

LAB: Object Oriented Programming IInd CSE

*

Week:1-A

AIM:Write a program that implements client/server application.The client sends data to the server.the server receives the data and result echo back to the client. File name: echo.java

(93)

LAB: Object Oriented Programming IInd CSE import java.net.*;

import java.io.*; public class echo {

public static void main(String argv[]) {

Socket es=null; PrintStream os=null; DataInputStream is=null;

DataInputStream stdin=new DataInputStream(System.in);

try { es=new Socket("127.0.0.1",17); is=new DataInputStream(es.getInputStream()); os=new PrintStream(es.getOutputStream()); } catch (Exception e) { System.err.println(e);

(94)

LAB: Object Oriented Programming IInd CSE

if(es!=null && os!=null && is!=null) { try { String userip; while((userip=stdin.readLine())!=null) { os.println(userip);

String serverip =is.readLine();

System.out.println("echo: " + serverip); } os.close(); is.close(); es.close(); } catch(Exception e) { System.err.println(e); }

(95)

LAB: Object Oriented Programming IInd CSE }

} }

File name: echoser.java Source code:

import java.net.*; public class echoser {

(96)

LAB: Object Oriented Programming IInd CSE public static void main(String args[])

{

ServerSocket ss=null; try

{

System.out.println("Creating server socket"); ss=new ServerSocket(17); System.out.println("Done"); } catch(Exception e) { System.out.println(e); } while(true) { try {

System.out.println("Waiting for a connection"); Socket s=ss.accept();

PrintStream ps=new PrintStream(s.getOutputStream()); System.out.println("Passing the quote..");

(97)

LAB: Object Oriented Programming IInd CSE int ch; while((ch=is.read())!=-1) { ps.print((char)ch); System.out.print((char)ch); } ps.close(); is.close(); { catch(Exception e) { System.out.println(e); System.exit(0); } } } Out put: COMPILATION: D:\cse>javac echo.java

(98)

LAB: Object Oriented Programming IInd CSE INTERPRETATON: D:\cse>java echo Compilation: D:\cse>javac echoser.java Interpretation:

(99)

LAB: Object Oriented Programming IInd CSE D:/cse>java echoser

(100)

LAB: Object Oriented Programming IInd CSE

AIM: write a java program that simulates trafficlight the program let user select one of three lights ,thread yellowor green .when a radio button is select the light is turn ed one light can be on at atime.

No lights is on when the program starts. File name: Edit framelights.java

So urce code:

\\import java packages\\ import java.awt.*; import java.awt.event.*;

class framelights implements ItemListener { Frame f; Label l; Checkbox b1,b2,b3; CheckboxGroup g; Panel p;

public void itemStateChanged(ItemEvent e) {

Checkbox c=(Checkbox)e.getSource(); if(c.getLabel().equals("Red"))

(101)

LAB: Object Oriented Programming IInd CSE { l.setText("Red color..."); } else if(c.getLabel().equals("Yellow")) { l.setText("Yellow color..."); } else if(c.getLabel().equals("Green")) { l.setText("Green color..."); } } public framelights() {

f=new Frame("Frame with Lights"); l=new Label("COLOR BOX..."); g=new CheckboxGroup();

(102)

LAB: Object Oriented Programming IInd CSE b2=new Checkbox("Yellow",g,false); b3=new Checkbox("Green",g,false); b1.addItemListener(this); b2.addItemListener(this); b3.addItemListener(this); p=new Panel(); p.add(l); p.add(b1); p.add(b2); p.add(b3); f.add(p); f.setSize(300,400); f.setVisible(true); }

public static void main(String[] args) {

new framelights(); }

(103)

LAB: Object Oriented Programming IInd CSE Output: Compilation: D:/cse>javac framelights.java Interpretation: D:/cse>java framelights

(104)

LAB: Object Oriented Programming IInd CSE

Week:1-A

AIM:Write a java program to draw a line,ellipse and rectangle. Filename:edit appltdraw.java

Source code:

\\import java packages\\ import java.awt.*;

import java.applet.*;

public class appltdraw extends java.applet.Applet {

public void init() {

resize(250,250); }

public void paint(Graphics g) {

//orange rect

(105)

LAB: Object Oriented Programming IInd CSE g.setColor(Color.orange); g.fillRect(100,100,30,50); //red line g.setColor(Color.red); g.drawLine(0,0,120,120); //blue rect g.setColor(Color.blue); g.drawRect(100,100,30,50); g.setColor(Color.green); g.fillRect(150,150,30,50); //gray oval g.setColor(Color.gray); g.drawOval(0,0,50,100); } } \\applet code\\ /*

<applet code=appltdraw.class height=300 width=400> </applet>

(106)

LAB: Object Oriented Programming IInd CSE Out put: Compilation: E:\ADITYA\csejava>javac appltdraw.java Interpretation: E:\ADITYA\csejava>appletviewer appltdraw.java

(107)
(108)

LAB Class :

References

Related documents

Content Providers allowing applications to access data such as contacts and media files. • The Content Providers currently available on

There are many augmented reality (AR) applications avail- able that can be used to create educational contents for these mobile devices. This paper surveys the most popular

The point at which the resultant of lift forces acts is: A) the hub. B) the center of gravity. D) the center of pressure.. A) airflow velocity increasing downward having been

Given the restricted use of e-signatures to include internal use only and the requirement that all users must be authenticated to the Authority domain for an e-signature to be

Since the data presented in this report were collected using the Tobii Accuracy and Precision Test method version 3, which is a method where several variables are less controlled, the

Código de programa Controlador (Java) package control; import java.lang.Exception; import java.lang.InterruptedException; import java.net.SocketException;

In addition, the total root yield has highly positive significant association with the characters of storage root number per plant, average root weight, marketable and