JNTU JAVA Lab Manual Programs
Short Description
Download JNTU JAVA Lab Manual Programs...
Description
RAO&NAIDU ENGINEERING COLLEGE 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 RAO & NAIDU ENGINEERING COLLEGE For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
1
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming
IInd CSE
ONGOLE
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
2
RAO&NAIDU ENGINEERING COLLEGE 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(); For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
3
RAO&NAIDU ENGINEERING COLLEGE 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(djavac qd_eq.java INTERPRETATION: D:\cse>java qd_eq
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
5
RAO&NAIDU ENGINEERING COLLEGE 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); For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
6
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming while(cjavac fibonacci.java Interpretation: D:/cse>java fibanocci
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
8
RAO&NAIDU ENGINEERING COLLEGE 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(); no=Integer.parseInt(s); for(i=no;i>=1;i--) For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
9
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming {
IInd CSE
for(j=2;jjavac prime.java Interpretation: D\:cse>java prime
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
11
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming
Rollno:09771A0524
IInd CSE
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;ijava pal
Rollno:09771A0524 For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
WEEK:3-B 21
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 { 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;ijavac wordcount.java Interpretation: D\:cse>java wordcount
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
27
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming
Rollno:09771A0524
IInd CSE
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);
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
28
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming if(f.exists())
IInd CSE
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()); } }
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
29
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming
IInd CSE
OUTPUT: Compilation: D:csejava>javac filetest.java Interpretataion: D:\csejava>java filetest
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
30
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming
Rollno:09771A0524
IInd CSE
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?"); For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
31
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming fname=in.readLine();
IInd CSE
//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+" "); } } } }
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
32
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming
IInd CSE
OUTPUT: Compilation: D:\csejava>javac fileread.java Interpretation: D:\csejava>java fileread
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
33
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming
Rollno:09771A0524
IInd CSE
WEEK:4-C
AIM:Program to read a file and print char count,word count,line count. File name: edit filestat.java sourcecode: For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
34
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming //import io package//
IInd CSE
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 For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
35
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming if(ch==' ' && pre!=' ')
IInd CSE
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: For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
36
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming D:\csejava>java filestat
Rollno:09771A0524
IInd CSE
WEEK:5-A
AIM: write a java program to implement stack using adt. File name: For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
37
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming Edit stackadt.java
IInd CSE
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(topjava stackadt
Week:5-B For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
41
RAO&NAIDU ENGINEERING COLLEGE 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; } public char pop() For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
42
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming {
IInd CSE
char t; t=s[top]; top--; return t; } public void check() { while(priority(t)java intopost
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
47
RAO&NAIDU ENGINEERING COLLEGE 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() { For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
48
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming top=-1;i=0;
IInd CSE
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='a' && t='A' && tjavac pfexevl.java Interpretation: D:/cse>java pfexevl
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
53
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming
IInd CSE
Week:6-A Aim: write a java program to develop an applet that display simple message. File name: For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
54
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming Applt.java
IInd CSE
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); For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
55
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming g.fillRect(150,150,30,50);
IInd CSE
} }
/* */
Output: Compilation: D:\cse>javac applt.java For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
56
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming Interpretation:
IInd CSE
D:\cse>appletviewer applt.java
Week:6-B
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
57
RAO&NAIDU ENGINEERING COLLEGE 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(ctrjavac appltfact.java Interpretation: D:\cse java>appletviewer appltfact.java
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
61
RAO&NAIDU ENGINEERING COLLEGE 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; Button bt; For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
62
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming bt=(Button)e.getSource();
IInd CSE
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()); For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
63
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming appltcal.t.setText(String.valueOf(no));
IInd CSE
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("*")) { For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
64
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming if(appltcal.sta==0)
IInd CSE
{ 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("/")) For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
65
RAO&NAIDU ENGINEERING COLLEGE 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; For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
66
RAO&NAIDU ENGINEERING COLLEGE 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;
Panel p; public void init() For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
67
RAO&NAIDU ENGINEERING COLLEGE 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); For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
68
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming p.add(d);
IInd CSE
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 //
/* For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
69
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming
IInd CSE
*/
OUTPUT: Compilation: D:\csejava>javac appltcal.java Interpretation: D:\csejava>appletviewer appltcal.java
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
70
RAO&NAIDU ENGINEERING COLLEGE 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] File name: For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
71
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming edit appltmouse.java
IInd CSE
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(); For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
72
RAO&NAIDU ENGINEERING COLLEGE 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/// /* For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
73
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming
IInd CSE
*/
OUTPUT: Compilation: For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
74
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming D:\csejava>javac appltmouse.java
IInd CSE
Interpretation: D:\csejava>appletviewer appltmouse.java
Week:1-A For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
75
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. 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) { For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
76
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming System.out.println(e.toString());
IInd CSE
} } } }
class threadb extends Thread { public void run() { for(; ;) { try { System.out.println("HELLO"); sleep(2000); } catch(Exception e)
{ System.out.println(e.toString()); } For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
77
RAO&NAIDU ENGINEERING COLLEGE 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()); } } } }
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
78
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming class multhreadss
IInd CSE
{ 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(); } }
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
79
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming
IInd CSE
Output: Compilation: D:/csejava>javac multhreadss.java Interpretation: D:\csejava>java multhreadss
For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
80
RAO&NAIDU ENGINEERING COLLEGE 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;
} public void run() For More solutions : Ravinuthalavs.webs.com Ravinuthalavs.blogspot.com
81
RAO&NAIDU ENGINEERING COLLEGE LAB: Object Oriented Programming {
IInd CSE
int value=0; for(int i=0;i
View more...
Comments