Professional Documents
Culture Documents
JNTU JAVA Lab Manual Programs PDF
JNTU JAVA Lab Manual Programs PDF
Prepared
By
R.Venkata Subbaiah
Associate Professor
IC RNEC@Development Cell
Department of CSE
RAO & NAIDU ENGINEERING COLLEGE
For More solutions : 1
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
LAB: Object Oriented Programming IInd CSE
ONGOLE
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.*;
import java.lang.Math;
class qd_eq
String s;
int a,b,c,d;
double r1,r2;
System.out.print("Enter a value?");
s=in.readLine();
a=Integer.parseInt(s);
System.out.print("Enter b value?");
s=in.readLine();
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)
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);
OUTPUT:
COMPILATION:
D:\cse>javac qd_eq.java
INTERPRETATION:
D:\cse>java qd_eq
Week:1-B
AIM: write a java program to print a fibonacci series 1,1,2,3…………….. upto the
scanned or need value
Source code:
//importing io classes
import java.io.*;
class Fibonacci
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);
System.out.print(c+",");
a=b;
b=c;
c=a+b;
Out put:
Compilation:
D:/cse>javac fibonacci.java
Interpretation:
D:/cse>java fibanocci
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
String s;
int no,i,j;
System.out.print("Enter a value?");
s=in.readLine();
no=Integer.parseInt(s);
for(i=no;i>=1;i--)
For More solutions : 9
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
LAB: Object Oriented Programming IInd CSE
{
for(j=2;j<i;j++)
if(i%j==0)
break;
if(i==j)
Output:
Compilation:
D:\cse>javac prime.java
Interpretation:
D\:cse>java prime
Rollno:09771A0524 WEEK:2-B
Source code:
//import io package
import java.io.*;
class matmul
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++)
for(j=0;j<=2;j++)
for(k=0;k<=2;k++)
mat3[i][j]+=mat1[i][k]*mat2[k][j];
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
System.out.print(mat3[i][j]+"\t");
System.out.print("\n");
Output:
Compilation:
D:\cse>javac matmul.java
Interpretation:
D:\cse>java matmul
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.
import java.io.*;
import java.util.*;
class stoken
int sm=0,no;
String s;
s=in.readLine();
while(token.hasMoreTokens())
no=Integer.parseInt(token.nextToken());
sm+=no
System.out.println(no);
System.out.println("Sum : "+sm);
OUT PUT:
COMPILATION:
D:/cse>javac stoken.java
D:/cse>java stoken
Rollno:09771A0524 WEEK:3-A
Aim: write a java program that checks whether the given string is palindrome or not.
For More solutions : 18
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
LAB: Object Oriented Programming IInd CSE
File Name: pal.java
Source code:
\\import io package\\
import java.io.*;
class pal
int i,j,len;
String s;
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;
System.out.println("Pal string");
else
Output:
Compilation:
Interpretation:
D:/cse>java pal
Rollno:09771A0524 WEEK:3-B
For More solutions : 21
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
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
int i,j,len;
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)
{
For More solutions : 22
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
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]);
Compilation:
D:/cse>javac strsort.java
Interpretation:
D:/cse>java strsort
Rollno:09771A0524 WEEK:3-C
Source code:
\\import io package\\
import java.io.*;
import java.util.*;
class wordcount
int ctr=0;
String s,s1,s2;
s=in.readLine();
StringTokenizer token2 ;
while(token1.hasMoreTokens())
For More solutions : 25
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
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++;
Times");
Output:
Compilation:
D:\cse>javac wordcount.java
Interpretation:
D\:cse>java wordcount
Rollno:09771A0524 WEEK:4-A
So urce code:
//import io package//
import java.io.*;
class filetest
String fname;
fname=in.readLine();
//opening file
System.out.println("File exists..");
else
if(f.canRead())
System.out.println("File is readable..");
else
if(f.canWrite())
System.out.println("File is writable..");
else
OUTPUT:
Compilation:
D:csejava>javac filetest.java
Interpretataion:
D:\csejava>java filetest
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
int ch,ctr=1;
String fname;
//opening file
System.out.print(ctr+" ");
while((ch=f.read())!=-1)
System.out.print((char)ch);
if(ch=='\n')
ctr++;
System.out.print(ctr+" ");
OUTPUT:
Compilation:
D:\csejava>javac fileread.java
Interpretation:
D:\csejava>java fileread
Rollno:09771A0524 WEEK:4-C
sourcecode:
For More solutions : 34
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
LAB: Object Oriented Programming IInd CSE
//import io package//
import java.io.*;
class filestat
String fname;
fname=in.readLine();
//opening file
while((ch=f.read())!=-1)
//char count
ctr++;
//line count
if(ch=='\n')
L++;
//word count
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:
Rollno:09771A0524 WEEK:5-A
File name:
For More solutions : 37
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
LAB: Object Oriented Programming IInd CSE
Edit stackadt.java
Source code:
//import io package//
import java.io.*;
class Stack
int max,top;
int no[];
public Stack()
no=new int[5];
top=-1;
max=5;
if(top<max-1)
no[++top]=n;
else
int val;
if(top==-1)
System.out.println("Stack is Empty...");
return -1;
else
val=no[top--];
return val;
for(int i=0;i<top;i++)
System.out.println(no[i]);
class StackTest
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:
Interpretation:
D:\cse>java stackadt
Week:5-B
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];
top++;
s[top]=t;
char t;
t=s[top];
top--;
return t;
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;
if(t=='+' || t=='-')
return 1;
else
return 0;
Push(‘#’)
while(i<str.length())
t=str.charAt(i);
postfix[j++]=t;
else
switch(t)
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;
j--;
break;
i++;
while(s[top]!='#')
postfix[j++]=pop();
postfix[j]='\0';
System.out.println(postfix);
String str;
System.out.println("Enter a string?");
str=in.readLine();
(new intopost()).convert(str);
Output:
Compilation:
D:\cse>javac intopost.java
Interpretation:
D:\cse>java intopost
Week:5-C
File name:
Edit pfexevl.java
Source code:
//import io package//
import java.io.*;
import java.lang.Math;
class pfixevl
int top,j,i;
String str;
char t,postfix[];
int s[];
DataInputStream in;
public pfixevl()
postfix=new char[30];
s=new int[30];
in=new DataInputStream(System.in);
top++;
s[top]=val;
int val;
val=s[top];
top--;
return val;
int op1,op2,value=0;
while(i<str.length())
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);
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++;
String str;
System.out.print("Enter a string?");
str=in.readLine();
(new pfixevl()).evaluate(str);
output:
Compilation:
D:/cse>javac pfexevl.java
Interpretation:
D:/cse>java pfexevl
Week:6-A
Aim: write a java program to develop an applet that display simple message.
File name:
Source code:
import java.awt.*;
import java.applet.*;
resize(250,250);
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);
/*
</applet>
*/
Output:
Compilation:
D:\cse>javac applt.java
D:\cse>appletviewer applt.java
Week:6-B
Source code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
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)
{
For More solutions : 58
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
LAB: Object Oriented Programming IInd CSE
fact*=ctr;
ctr++;
appltfact.t2.setText(String.valueOf(fact));
System.out.println("....");
Label l1,l2;
Button b;
t1=new TextField();
b=new Button("compute");
add(l1);
add(t1);
add(t2);
b.addActionListener(new actlsn());
add(b);
setSize(300,400);
setVisible(true);
\\applet code\\
/*
</applet>
*/
Compilation:
E:\ADITYA\csejava>javac appltfact.java
Interpretation:
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:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
int no=0,val,prev;
String txt;
Button bt;
For More solutions : 62
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
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;
no+=Integer.parseInt(appltcal.t.getText());
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("*"))
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;
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;
else
no=appltcal.pval;
no%=Integer.parseInt(appltcal.t.getText());
appltcal.t.setText(String.valueOf(no));
appltcal.sta=0;
static TextField t;
Button a,m,d,s,r,b;
Panel p;
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
a.addActionListener(lstn);
s.addActionListener(lstn);
d.addActionListener(lstn);
m.addActionListener(lstn);
r.addActionListener(lstn);
b.addActionListener(lstn);
p=new Panel();
p.setLayout(new GridLayout(3,2));
p.add(t);
p.add(a);
p.add(s);
p.add(m);
p.add(r);
p.add(b);
//adding pane
add(p);
setSize(300,400);
setVisible(true);
showStatus("Calculator...");
//applet code //
/*
</applet>
*/
OUTPUT:
Compilation:
D:\csejava>javac appltcal.java
Interpretation:
D:\csejava>appletviewer appltcal.java
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:
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;
static TextField t;
int x,y;
x=m.getX();
y=m.getY();
appltmouse.t.setText(String.valueOf(x)+" ,
"+String.valueOf(y));
repaint();
t=new TextField("........");
p.add(t);
add(p);
addMouseMotionListener(this);
setSize(300,400);
setVisible(true);
g.drawRect(20,20,100,200);
g.drawString(t.getText(),x,y);
//applet code///
/*
*/
OUTPUT:
Compilation:
Interpretation:
D:\csejava>appletviewer appltmouse.java
Week:1-A
For More solutions : 75
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
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.
Source code:
import java.io.*;
import java.lang.*;
for(; ;)
try
System.out.println("GOOD MORNING");
sleep(1000);
catch(Exception e)
{
For More solutions : 76
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
LAB: Object Oriented Programming IInd CSE
System.out.println(e.toString());
for(; ;)
try
System.out.println("HELLO");
sleep(2000);
catch(Exception e)
System.out.println(e.toString());
for(; ;)
try
System.out.println("WELCOME");
sleep(3000);
catch(Exception e)
System.out.println(e.toString());
//staring threads
t1.start();
t2.start();
t3.start();
Output:
Compilation:
D:/csejava>javac multhreadss.java
Interpretation:
D:\csejava>java multhreadss
Week:1-A
File name:
Edit producerconsumer.java
Source code:
\\import io package\\
import java.io.*;
cubbyhole=c;
this.number=number;
int value=0;
for(int i=0;i<10;i++)
value=cubbyhole.get();
System.out.println("Consumer’ #’this.number+got+value);
cubbyhole=c;
this.number=number;
for(int i=10;i<20;i++)
cubbyhole.put(i);
try
sleep(100);
catch(InterruptedException e)
e.printStackTrace();
class CubbyHole
try
wait();
Catch(exception e)
e.printStackTrace();
available=false;
notifyAll();
return contents;
while(available==true)
try
wait();
catch (InterruptedException e)
e.printStackTrace();
available=true;
contents=value;
notifyAll();
c1.start();
Output:
Compilation:
D:\cse>javac producerconsumer.java
Interpretation:
D:\cse>java producerconsumer
Week:1-A
Source code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
JTextField t1,t2,t3;
JButton b;
JLabel msg;
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)
catch(ArithmeticException ex)
public framediv()
p=new JPanel();
t1=new JTextField("10000");
t2=new JTextField("1000");
b=new JButton("Cal");
b.addActionListener(this);
p.add(t1);
p.add(t2);
p.add(t3);
p.add(msg);
add(p);
setSize(300,400);
setVisible(true);
new framediv();
Output:
Compilation:
D:\csejava>javac
Interpretation:
D:\csejava>appletviewer framediv.java
Week:1-A
Source code:
For More solutions : 92
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
LAB: Object Oriented Programming IInd CSE
import java.net.*;
import java.io.*;
Socket es=null;
PrintStream os=null;
DataInputStream is=null;
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);
try
String userip;
while((userip=stdin.readLine())!=null)
os.println(userip);
os.close();
is.close();
es.close();
catch(Exception e)
System.err.println(e);
Source code:
import java.net.*;
ServerSocket ss=null;
try
ss=new ServerSocket(17);
System.out.println("Done");
catch(Exception e)
System.out.println(e);
while(true)
try
Socket s=ss.accept();
InputStream is=s.getInputStream();
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
D:\cse>java echo
Compilation:
D:\cse>javac echoser.java
Interpretation:
Week:1-A
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.
So urce code:
import java.awt.*;
import java.awt.event.*;
Frame f;
Label l;
Checkbox b1,b2,b3;
CheckboxGroup g;
Panel p;
Checkbox c=(Checkbox)e.getSource();
if(c.getLabel().equals("Red"))
For More solutions : 100
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
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()
g=new CheckboxGroup();
b1=new Checkbox("Red",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);
new framelights();
Output:
Compilation:
D:/cse>javac framelights.java
Interpretation:
D:/cse>java framelights
Week:1-A
Filename:edit appltdraw.java
Source code:
import java.awt.*;
import java.applet.*;
resize(250,250);
//orange rect
g.drawRect(100,100,300,450);
For More solutions : 104
Ravinuthalavs.webs.com
Ravinuthalavs.blogspot.com
RAO&NAIDU ENGINEERING COLLEGE
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>
*/
Out put:
Compilation:
E:\ADITYA\csejava>javac appltdraw.java
Interpretation:
E:\ADITYA\csejava>appletviewer appltdraw.java
Page no: