+ All Categories
Home > Documents > JNTU JAVA Lab Manual Programs

JNTU JAVA Lab Manual Programs

Date post: 01-Dec-2014
Category:
Upload: subramanyam62
View: 1,803 times
Download: 4 times
Share this document with a friend
108
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE For More solutions : 1 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com IV YEAR B.Tech JAVA PROGRAMMING MANUAL OOPs Through Java Prepared By R.Venkata Subbaiah Associate Professor IC RNEC@Development Cell Department of CSE RAO & NAIDU ENGINEERING COLLEGE
Transcript
Page 1: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 1 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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

RAO & NAIDU ENGINEERING COLLEGE

Page 2: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 2 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

ONGOLE

Page 3: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 3 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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();

Page 4: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 4 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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);

}

}

}

Page 5: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 5 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

OUTPUT:

COMPILATION:

D:\cse>javac qd_eq.java

INTERPRETATION:

D:\cse>java qd_eq

Page 6: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 6 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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);

Page 7: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 7 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

while(c<=n)

{

System.out.print(c+",");

a=b;

b=c;

c=a+b;

}

}

}

Page 8: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 8 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Out put:

Compilation:

D:/cse>javac fibonacci.java

Interpretation:

D:/cse>java fibanocci

Page 9: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 9 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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();

no=Integer.parseInt(s);

for(i=no;i>=1;i--)

Page 10: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 10 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

{

for(j=2;j<i;j++)

{

if(i%j==0)

break;

}

if(i==j)

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

}

}

}

Page 11: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 11 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Output:

Compilation:

D:\cse>javac prime.java

Interpretation:

D\:cse>java prime

Page 12: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 12 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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++)

Page 13: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 13 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

{

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");

}

}

}

Page 14: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 14 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Output:

Compilation:

D:\cse>javac matmul.java

Interpretation:

D:\cse>java matmul

Page 15: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 15 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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.

File Name: stoken.java

Page 16: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 16 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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);

}

}

Page 17: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 17 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

OUT PUT:

COMPILATION:

D:/cse>javac stoken.java

Page 18: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 18 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

INTERPRETATION:

D:/cse>java stoken

Rollno:09771A0524 WEEK:3-A

Aim: write a java program that checks whether the given string is palindrome or not.

Page 19: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 19 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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;

}

Page 20: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 20 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

if(i==j)

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

else

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

}

}

Output:

Compilation:

Page 21: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 21 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

D:/cse>javac pal.java

Interpretation:

D:/cse>java pal

Rollno:09771A0524 WEEK:3-B

Page 22: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 22 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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)

{

Page 23: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 23 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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]);

}

}

Page 24: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 24 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Output:

Compilation:

D:/cse>javac strsort.java

Interpretation:

D:/cse>java strsort

Page 25: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 25 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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 ;

while(token1.hasMoreTokens())

Page 26: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 26 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

{

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");

}

}

}

Page 27: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 27 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Output:

Compilation:

D:\cse>javac wordcount.java

Interpretation:

D\:cse>java wordcount

Page 28: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 28 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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

File f=new File(fname);

Page 29: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 29 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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());

}

}

Page 30: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 30 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

OUTPUT:

Compilation:

D:csejava>javac filetest.java

Interpretataion:

D:\csejava>java filetest

Page 31: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 31 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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);

System.out.print("Enter a file name?");

Page 32: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 32 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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+" ");

}

}

}

}

Page 33: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 33 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

OUTPUT:

Compilation:

D:\csejava>javac fileread.java

Interpretation:

D:\csejava>java fileread

Page 34: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 34 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Rollno:09771A0524 WEEK:4-C

AIM:Program to read a file and print char count,word count,line count.

File name: edit filestat.java

sourcecode:

Page 35: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 35 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

//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++;

//word count

Page 36: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 36 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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:

Page 37: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 37 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

D:\csejava>java filestat

Rollno:09771A0524 WEEK:5-A

AIM: write a java program to implement stack using adt.

File name:

Page 38: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 38 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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

Page 39: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 39 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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++)

System.out.println(no[i]);

}

Page 40: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 40 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

}

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:

Page 41: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 41 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

D:\cse>javac stackadt.java

Interpretation:

D:\cse>java stackadt

Week:5-B

Page 42: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 42 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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;

}

public char pop()

Page 43: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 43 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

{

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=='/')

return 2;

Page 44: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 44 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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 '+':

Page 45: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 45 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

case '-':

check();

push(t);

break;

case '*':

case '/':

check();

push(t);

break;

case '(':

push(t);

break;

case '^':

check();

push(t);

break;

case ')':

do

{

t=pop();

postfix[j++]=t;

Page 46: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 46 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

}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);

}

}

Page 47: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 47 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Output:

Compilation:

D:\cse>javac intopost.java

Interpretation:

D:\cse>java intopost

Page 48: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 48 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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()

{

Page 49: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 49 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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())

{

Page 50: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 50 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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);

Page 51: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 51 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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++;

Page 52: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 52 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

}

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);

}

}

Page 53: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 53 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

output:

Compilation:

D:/cse>javac pfexevl.java

Interpretation:

D:/cse>java pfexevl

Page 54: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 54 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Week:6-A

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

File name:

Page 55: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 55 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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);

g.setColor(Color.green);

Page 56: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 56 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

g.fillRect(150,150,30,50);

}

}

/*

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

</applet>

*/

Output:

Compilation:

D:\cse>javac applt.java

Page 57: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 57 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Interpretation:

D:\cse>appletviewer applt.java

Week:6-B

Page 58: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 58 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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)

{

Page 59: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 59 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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);

add(t1);

Page 60: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 60 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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>

*/

Page 61: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 61 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

OUTPUT:

Compilation:

E:\ADITYA\csejava>javac appltfact.java

Interpretation:

D:\cse java>appletviewer appltfact.java

Page 62: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 62 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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;

Button bt;

Page 63: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 63 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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;

no+=Integer.parseInt(appltcal.t.getText());

Page 64: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 64 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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("*"))

{

Page 65: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 65 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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("/"))

Page 66: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 66 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

{

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;

Page 67: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 67 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

}

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;

Panel p;

public void init()

Page 68: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 68 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

{

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);

Page 69: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 69 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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 //

/*

Page 70: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 70 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

<applet code=appltcal.class height=300 width=400>

</applet>

*/

OUTPUT:

Compilation:

D:\csejava>javac appltcal.java

Interpretation:

D:\csejava>appletviewer appltcal.java

Page 71: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 71 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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]

File name:

Page 72: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 72 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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();

Page 73: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 73 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

}

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///

/*

<applet code=appltmouse1.class height=300 width=400 >

Page 74: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 74 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

</applet>

*/

OUTPUT:

Compilation:

Page 75: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 75 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

D:\csejava>javac appltmouse.java

Interpretation:

D:\csejava>appletviewer appltmouse.java

Week:1-A

Page 76: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 76 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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)

{

Page 77: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 77 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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());

}

Page 78: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 78 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

}

}

}

class threadc extends Thread

{

public void run()

{

for(; ;)

{

try

{

System.out.println("WELCOME");

sleep(3000);

}

catch(Exception e)

{

System.out.println(e.toString());

}

}

}

}

Page 79: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 79 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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();

}

}

Page 80: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 80 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Output:

Compilation:

D:/csejava>javac multhreadss.java

Interpretation:

D:\csejava>java multhreadss

Page 81: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 81 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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;

}

public void run()

Page 82: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 82 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

{

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()

{

for(int i=10;i<20;i++)

Page 83: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 83 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

{

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()

{

Page 84: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 84 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

while(available==false)

{

try

{

wait();

}

Catch(exception e)

{

e.printStackTrace();

}

}

available=false;

notifyAll();

return contents;

}

public synchronized void put(int value)

{

while(available==true)

{

try

Page 85: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 85 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

{

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);

Page 86: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 86 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

p1.start();

c1.start();

}

}

Output:

Compilation:

D:\cse>javac producerconsumer.java

Interpretation:

D:\cse>java producerconsumer

Page 87: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 87 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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.*;

class framediv extends JFrame implements ActionListener

{

Page 88: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 88 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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)

{

msg.setText("Wrong Data Values are enetered...");

Page 89: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 89 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

}

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);

p.add(new JLabel(" = "));

p.add(t3);

Page 90: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 90 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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();

}

}

Page 91: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 91 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Output:

Compilation:

D:\csejava>javac

Interpretation:

D:\csejava>appletviewer framediv.java

Page 92: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 92 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

*

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

Source code:

Page 93: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 93 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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);

}

Page 94: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 94 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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);

}

Page 95: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 95 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

}

}

}

File name: echoser.java

Source code:

import java.net.*;

public class echoser

{

Page 96: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 96 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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..");

InputStream is=s.getInputStream();

Page 97: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 97 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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

Page 98: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 98 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

INTERPRETATON:

D:\cse>java echo

Compilation:

D:\cse>javac echoser.java

Interpretation:

Page 99: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 99 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

D:/cse>java echoser

Week:1-A

Page 100: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 100 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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"))

Page 101: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 101 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

{

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();

b1=new Checkbox("Red",g,false)

Page 102: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 102 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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();

}

}

Page 103: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 103 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Output:

Compilation:

D:/cse>javac framelights.java

Interpretation:

D:/cse>java framelights

Page 104: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 104 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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

g.drawRect(100,100,300,450);

Page 105: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 105 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

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>

*/

Page 106: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 106 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Out put:

Compilation:

E:\ADITYA\csejava>javac appltdraw.java

Interpretation:

E:\ADITYA\csejava>appletviewer appltdraw.java

Page 107: JNTU JAVA Lab Manual Programs

RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming IInd CSE

For More solutions : 107 Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com

Page 108: JNTU JAVA Lab Manual Programs

RAO & NAIDU ENGINEERING COLLEGE

LAB : Object Oriented Programming Class : II nd

C.S.E-A

Page no:


Recommended