Sunteți pe pagina 1din 57

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCE

(COLLEGE

OF ENGINEERING)

Bogaram(v),Keesara(M),R.R.Dist 501 301

Department Of Computer Science & Engineering

Subject Branch Class

: Dbms Lab Manual : B.Tech IT : IIrd Year

Semester : IInd A.Y. : 2009 2010

Created By M.Monika(Asst.Prof,HITS)

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Introduction to Java:
Java is great programming language for the development of enterprise grade applications. This programming Language is evolved from a language named Oak. Oak was developed in the early nineties at Sun Microsystems as a platformindependent language aimed at allowing entertainment appliances such as video game consoles and VCRs to communicate . Oak was first slated to appear in television set-top boxes designed to provide video-on-demand services. Oak was unsuccessful so in 1995 Sun changed the name to Java and modified the language to take advantage of the burgeoning World Wide Web.Java is an object-oriented language, and this is very similar to C++. Java Programming Language is simplified to eliminate language features that cause common programming errors. Java source code files are compiled into a format called bytecode, which can then be executed by a Java interpreter. What is JDK (Java Development Kit) JDK is a software development program provided by sun Microsystems. Java Development Kit or JDK comes in various version and can be downloaded free from the sun Microsystems. JVM compiler, debugger and other tools are used with JDK for developing java based application & java applets. So make sure that your JVM compiler & JDK versions are same. JDK also known as Java 2 Platform, That comes in three editions J2ME, J2SE & J2EE. If you are beginner or learning Java then start by downloading J2SE.Acronyms: JDK Java Development Kit JVM Java virtual machineDownload JDK You can download JDK from www.javasoft.com/j2seLatest version of JDK

jdk1.6.0_15 http://java.sun.com/j2se/Downloads/index.jsp

Holymary Institute of Technology & Sciences

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Program Strucuture of java in Java Language:


The fundamental structure of any Java programme should look like: [package declarations] [import statements] [class declaration] An example is given below: package abc; import java.lang; class Demo { public static void main(String[] args) { Sytem.out.println("Hello! World"); } } //The file containing this class must be named Demo.java

How to set the path for Java


1.Go to MyComputer properties->Advanced ->Environment Variables. 2.Find the path in SystemVariables and Edit it to your Jdk/bin folder. 3.Now your path is set and you can run the programs from anywhere.

Holymary Institute of Technology & Sciences

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week1 : a) Write a Java program that prints all real solutions to the quadratic equation ax2 + bx + c = 0. Read in a, b, c and use the quadratic formula. If the discriminant b2 -4ac is negative, display a message stating that there are no real solutions. public class Quadratic { public static void main(String args[]) { double a,b,c,root1,root2; a=3.0; b=4.0; c=2.0; if((b*b-4*a*c)==0) { System.out.println("\n ROOTS ARE EQUAL"); root1=-b/(2*a); System.out.println("\n root "+root1); } if((b*b-4*a*c) > 0) { root1=-b+Math.sqrt(b*b-4*a*c)/(2*a); root2=-b-Math.sqrt(b*b-4*a*c)/(2*a); System.out.println("\nROOTS ARE REAL:\n"); System.out.println("\n root1 "+root1+"\n root2 "+root2); } else System.out.println("\n Imaginary Roots."); } } // End of Class Output ROOTS ARE REAL: root1 -5.422649730810374 root2 -6.577350269189626 Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 1: Regulation:R07 Branch: IT

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

public class LabPro2 { public static void main(String args[]) { String n1; int f1=1,f2=1,n,i,f3; n1=5; n=Integer.parseInt(n1); System.out.println("series"+f1+f2); for(i=1;i<=n;i++) { f3=f1+f2; System.out.println(f3); f1=f2; f2=f3; } } } Output Series 1 1 2 3 5

Holymary Institute of Technology & Sciences

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 2 : a) Write a Java program that prompts the user for an integer and then prints out all prime numbers up to that integer. Regulation:R07 Branch: IT

import javax.swing.*; public class LabPro3 { public static void main(String args[]) { String n1; int n,c=0,i,j; n1=JOptionPane.showInputDialog("enter n value"); n=Integer.parseInt(n1); outer: for(i=2;i<=n;i++) { for(j=2;j<=(i/2);j++) { if(i%j==0) continue outer; } JOptionPane.showMessageDialog(null,i+" "); } } }

Output:

Holymary Institute of Technology & Sciences

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Week 2 :

Holymary Institute of Technology & Sciences

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

b) Write a Java program to multiply two given matrices. import javax.swing.*; public class BTechJavaLab6 { public static void main(String args[]) { String n,l,m=" "; int i,j,k; int a[][]=new int[3][3]; int b[][]=new int[3][3]; int c[][]=new int[3][3]; for(i=0;i<3;i++) { for(j=0;j<3;j++) { n=JOptionPane.showInputDialog(null,"enter elements of 1st matrix"); a[i][j]= Integer.parseInt(n); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { l=JOptionPane.showInputDialog(null,"enter elements of 2nd matrix"); b[i][j]= Integer.parseInt(l); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { m=m+c[i][j]; } m=m+"\n"; JOptionPane.showMessageDialog(null,"resultant matrix is"+m); }

Holymary Institute of Technology & Sciences

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. } } Regulation:R07 Branch: IT

Output:

Holymary Institute of Technology & Sciences

10

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

11

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

12

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

13

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Week 2 :

Holymary Institute of Technology & Sciences

14

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

c) Write a Java Program that reads a line of integers, and then displays each integer, and the sum of all the integers (Use StringTokenizer class of java.util) import javax.swing.*; import java.util.*; import java.io.*; public class LabPro7 { public static void main(String args[])throws IOException { int sum=0; String line=JOptionPane.showInputDialog("enter the line"); String k=JOptionPane.showInputDialog("enter the delemeter"); StringTokenizer wd=new StringTokenizer(line,k); while(wd.hasMoreTokens()) sum=sum+Integer.parseInt(wd.nextToken()); JOptionPane.showMessageDialog(null,"sum is"+sum); } }

Option:

Holymary Institute of Technology & Sciences

15

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

16

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 3 : a) Write a Java program that checks whether a given string is a palindrome or not. Ex: MADAM is a palindrome. import java.util.*; public class LabPro4 { public static void main(String args[]) { StringBuffer sb=new StringBuffer(args[0]); String t=sb.reverse().toString(); System.out.println("reverse "+t); System.out.println("boolean results"); System.out.println("check with == "+(t==args[0])); System.out.println("check with equals "+(t.equals(args[0]))); System.out.println("given string "+args[0]); if(t.equals(args[0])) System.out.println("is a pallindrome"); else System.out.println("is not a pallindrome"); } } Regulation:R07 Branch: IT

Output: reverse MADAM boolean results check with == false check with equals true given string MADAM is a palindrome

Holymary Institute of Technology & Sciences

17

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 3 : b) Write a Java program for sorting a given list of names in ascending order. import java.io.*; import javax.swing.JOptionPane; class Ascend { public static void main(String[] args) { String name[]={"","",""},temp; for(int i=0;i<3;i++) { name[i]=JOptionPane.showInputDialog("enter name"); } for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if((name[j].compareTo(name[i]))>0) { temp=name[i]; name[i]=name[j]; name[j]=temp; } } Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

18

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. } for(int i=0;i<3;i++) { Regulation:R07 Branch: IT

JOptionPane.showMessageDialog(null,name[i],"Message",JOptionPane.INFORMAT ION_MESSAGE); } } } Output:

Holymary Institute of Technology & Sciences

19

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

20

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 3 : c) Write a Java program to make frequency count of words in a given text. import java.io.*; import javax.swing.JOptionPane; import java.util.*; class Count { public static void main(String[] args) throws IOException { String s; int i=0,count=0,l=0; s=JOptionPane.showInputDialog("enter a file name"); FileInputStream f=new FileInputStream(s); DataInputStream d=new DataInputStream(f); while(d.readLine()!=null) { i++; StringTokenizer s1=new StringTokenizer(d.readLine()); while(s1.hasMoreTokens()) { String b=s1.nextToken(); count++; l=l+b.length(); } Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

21

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. } Regulation:R07 Branch: IT

JOptionPane.showMessageDialog(null,"words="+count+"\nlines="+i+"\n characters="+l,"Message",JOptionPane.INFORMATION_MESSAGE); } }

Output:

Holymary Institute of Technology & Sciences

22

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

23

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 4 : a) Write a Java program that reads a file name from the user, then displays information about whether the file exists, whether the file is readable, whether the file is writable, the type of file and the length of the file in bytes. import java.io.*; import java.util.*; public class LabPro8 { public static void main(String args[]) { File f=new File(args[0]); System.out.println("Filename " +f.getName()); System.out.println("Filepath " +f.getPath()); System.out.println("Parent " +f.getName()); System.out.println("File size " +f.length()+" bytes"); System.out.println("is readable " +f.canRead()); System.out.println("is Writable " +f.canWrite()); System.out.println("is directory " +f.isDirectory()); System.out.println("is file " +f.isFile()); } } Output: Arguments given: C:\holy.txt Filename holy.txt Filepath C:\holy.txt Parent holy.txt File size 18 bytes is readable true is Writable true is directory false is file true Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

24

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Week 4 : b) Write a Java program that reads a file and displays the file on the screen, with a line number before each line. import java.io.*; import java.util.*; public class LabPro9 { public static void main(String args[]) { try { FileInputStream f=new FileInputStream("C:\\run.txt"); LineNumberReader lr=new LineNumberReader(new InputStreamReader(f)); String data; while((data=lr.readLine())!=null) { System.out.println(lr.getLineNumber()+":"+data); } System.out.println("total lines"+lr.getLineNumber()); f.close(); } catch(Exception e) {System.out.println("err"+e);} } } Output: run: 1:Hello, Welcome to Holy Mary groups 2:hello,java world total lines2

Holymary Institute of Technology & Sciences

25

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Week 4 : c) Write a Java program that displays the number of characters, lines and words in a text file. import java.io.*; import java.util.*; public class LabPro10 { public static void main(String args[]) { try { FileInputStream f=new FileInputStream("C:\\run.txt"); LineNumberReader lr=new LineNumberReader(new InputStreamReader(f)); String data; StringTokenizer st; int words=0,chars=0; while((data=lr.readLine())!=null) { st=new StringTokenizer(data); words+=st.countTokens(); chars+=data.length(); } System.out.println("total words"+words); System.out.println("total chars"+chars); System.out.println("total lines"+lr.getLineNumber()); f.close(); } catch(Exception e) {System.out.println("err"+e);} } } Output: run: total words8 total chars50 total lines2

Holymary Institute of Technology & Sciences

26

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 5 : a) Write a Java program that Implements stack ADT. Regulation:R07 Branch: IT

import java.util.*; import javax.swing.JOptionPane; class Stackdemo { public static void main(String[] args) { Stack st=new Stack(); int j; do { String s=JOptionPane.showInputDialog("enter ur choice\n1>input\n2->delete\n3->exit"); j=Integer.parseInt(s); if(j==1) { s=JOptionPane.showInputDialog("enter an element"); st.push(Integer.parseInt(s)); JOptionPane.showMessageDialog(null,"stack elements:"+st,"stack elements",JOptionPane.INFORMATION_MESSAGE); } try { if(j==2) { Integer a=(Integer)st.pop(); JOptionPane.showMessageDialog(null,"stack elements:"+st,"stack elements",JOptionPane.INFORMATION_MESSAGE); } } catch(EmptyStackException e) { JOptionPane.showMessageDialog(null,"STACK IS EMPTY"+e,"stack elements",JOptionPane.INFORMATION_MESSAGE); } }while(j<3); } }

Holymary Institute of Technology & Sciences

27

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Output: Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

28

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

29

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 5 : b) Converts infix expression into Postfix form iii) Evaluates the postfix expression import java.io.*; import java.util.*; import javax.swing.JOptionPane; class Intopost { public static void main(String[] args) { String s=JOptionPane.showInputDialog("enter infix expression"); Stack st=new Stack(); String output=""; int i=0,len=s.length(); char x; st.push('@'); while(len!=0) { char c=s.charAt(i); if(c=='(') { st.push(c); } else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^'||c=='$') { check(st,c,output); st.push(c); } else if(c==')') { while((x=(Character)st.pop())!='(') { output=output+x; } } else { output+=s.charAt(i); } i=i+1; len--; } while((x=(Character)st.pop())!='@') { output+=x; } Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

30

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

JOptionPane.showMessageDialog(null,output,"postfix expression",JOptionPane.INFORMATION_MESSAGE); } static void check(Stack st,char c,String output) { while(priority(c)<=priority((Character)st.peek())) output=output+st.pop(); } static int priority(char ch) { if(ch=='+'||ch=='-') return(1); else if(ch=='*'||ch=='/') return(2); else if(ch=='$'||ch=='^') return(3); else return(0); } }

Output:

Holymary Institute of Technology & Sciences

31

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 6 : a) Develop an applet that displays a simple message. import java.awt.*; import java.applet.Applet; /*< Applet code=LabPro12 width=300 height=300> < /Applet> */ public class LabPro12 extends Applet { public void paint(Graphics g) { g.drawString ("Hello Worlds",100,100); } } Output: Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

32

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 6 : b) Develop an applet that receives an integer in one text field, and computes its factorial Value and returns it in another text field, when the button named Compute is clicked. import java.applet.*; import java.awt.*; import java.awt.event.*; public class factorial extends Applet implements ActionListener { Button btn,clearbtn; Label lbl1,lbl2; TextField tf1,tf2; public void init() { btn=new Button("COMPUTE"); btn.addActionListener(this); clearbtn=new Button("CLEAR"); clearbtn.addActionListener(this); tf1=new TextField(30); tf2=new TextField(30); lbl1=new Label("NUMBER"); lbl2=new Label("RESULT"); setLayout(new GridLayout(3,2)); add(lbl1); add(tf1); add(lbl2); add(tf2); add(btn); add(clearbtn); } public void actionPerformed(ActionEvent e) { if(e.getSource()==btn) { int a=Integer.parseInt(tf1.getText()); Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

33

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. int fact=1; for(int i=1;i<=a;i++) fact*=i; tf2.setText(""+fact); } else { tf1.setText(""); tf2.setText(""); } } } Output: Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

34

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 7 : Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -,*, % operations. Add a text field to display the result. Regulation:R07 Branch: IT

import java.awt.event.*; import java.applet.Applet; import java.awt.*; public class LabPro14 extends Applet implements ActionListener { TextField t;String a;int p=0,tmp=0; Button bo,b1,b2,b3,b4,b5,b6,b7,b8,b9; Button badd,bsub,bmul,bdiv,bper,beql,bc; public void init() { t = new TextField(50); bo = new Button("0"); b1 = new Button("1"); b2 = new Button("2"); b3 = new Button("3"); b4 = new Button("4"); b5 = new Button("5"); b6 = new Button("6"); b7 = new Button("7"); b8 = new Button("8"); b9 = new Button("9"); badd = new Button("+"); bsub = new Button("-"); bmul = new Button("*"); bdiv = new Button("/"); bper = new Button("%"); bc = new Button("c"); beql = new Button("="); add(t);add(bo);add(b1); add(b2);add(b3);add(b4); add(b5);add(b6);add(b7); add(b8);add(b9);add(badd); add(bsub);add(bmul);add(bdiv); add(bper);add(bc);add(beql); bo.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this);

Holymary Institute of Technology & Sciences

35

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); badd.addActionListener(this); bsub.addActionListener(this); bmul.addActionListener(this); bdiv.addActionListener(this); bper.addActionListener(this); bc.addActionListener(this); beql.addActionListener(this); //setLayout(new GridLayout(4,4)); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==bc) { t.setText("0"); } if(ae.getSource()==bo) { int k=Integer.parseInt(t.getText()); k=k*10+0; t.setText(String.valueOf(k)); } if(ae.getSource()==b1) { int k=Integer.parseInt(t.getText()); k=k*10+1; t.setText(String.valueOf(k)); } if(ae.getSource()==b2) { int k=Integer.parseInt(t.getText()); k=k*10+2; t.setText(String.valueOf(k)); } if(ae.getSource()==b3) { int k=Integer.parseInt(t.getText()); k=k*10+3; t.setText(String.valueOf(k)); } if(ae.getSource()==b4) { int k=Integer.parseInt(t.getText()); Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

36

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. k=k*10+4; t.setText(String.valueOf(k)); } if(ae.getSource()==b5) { int k=Integer.parseInt(t.getText()); k=k*10+5; t.setText(String.valueOf(k)); } if(ae.getSource()==b6) { int k=Integer.parseInt(t.getText()); k=k*10+6; t.setText(String.valueOf(k)); } if(ae.getSource()==b7) { int k=Integer.parseInt(t.getText()); k=k*10+7; t.setText(String.valueOf(k)); } if(ae.getSource()==b8) { int k=Integer.parseInt(t.getText()); k=k*10+8; t.setText(String.valueOf(k)); } if(ae.getSource()==b9) { int k=Integer.parseInt(t.getText()); k=k*10+9; t.setText(String.valueOf(k)); } if(ae.getSource()==badd) { tmp=Integer.parseInt(t.getText()); p=1; t.setText("0"); } if(ae.getSource()==bsub) { tmp=Integer.parseInt(t.getText()); p=2; t.setText("0"); } if(ae.getSource()==bmul) { tmp=Integer.parseInt(t.getText()); Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

37

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. p=3; t.setText("0"); } if(ae.getSource()==bdiv) { tmp=Integer.parseInt(t.getText()); p=4; t.setText("0"); } if(ae.getSource()==bper) { tmp=Integer.parseInt(t.getText()); p=5; t.setText("0"); } if(ae.getSource()==beql) { float newval=Integer.parseInt(t.getText()); float res=0; switch(p) { case 1: res=tmp+newval; break; case 2: res=tmp-newval; break; case 3: res=tmp*newval; break; case 4: res=tmp/newval; break; case 5: res=tmp%newval; break; } t.setText(String.valueOf(res)); } } } Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

38

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Output: Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

39

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 8 : a) Write a Java program for handling mouse events. import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class LabPro15 extends Applet implements MouseListener { String msg="welcome";int x=100,y=100; public void init() { addMouseListener(this); } public void paint(Graphics g) { g.drawString(msg,x,y); } public void update(Graphics g) { paint(g); } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { msg="to";x=150;y=150; repaint(); } public void mouseReleased(MouseEvent e) { msg="gmrit";x=200;y=200; repaint(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } Regulation:R07 Branch: IT

Output:

Holymary Institute of Technology & Sciences

40

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

41

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 9 : a) Write a Java program that creates three threads. First thread displays Good Morning every one second, the second thread displays Hello every two seconds and the third thread displays Welcome every three seconds. class tst implements Runnable { String name; Thread t; tst(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("newThread : " +t); t.start(); } public void run() { try { for(int i=5;i > 0;i--) { System.out.println(name + ": " + i); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println(name + "Interrupted"); } System.out.println(name + "exiting"); } } class LabPro16 { public static void main(String args[]) { new tst("Good Morning"); new tst("Hello"); new tst("Welcome"); try { Thread.sleep(10000); } catch(InterruptedException e) { Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

42

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. System.out.println("Main thread Interrupted"); } System.out.println("Main thread exiting"); } } Regulation:R07 Branch: IT

Output: run: newThread : Thread[Good Morning,5,main] newThread : Thread[Hello,5,main] newThread : Thread[Welcome,5,main] Good Morning: 5 Hello: 5 Welcome: 5 Good Morning: 4 Hello: 4 Welcome: 4 Good Morning: 3 Welcome: 3 Hello: 3 Good Morning: 2 Hello: 2 Welcome: 2 Good Morning: 1 Welcome: 1 Hello: 1 Good Morningexiting Welcomeexiting Helloexiting

Holymary Institute of Technology & Sciences

43

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 9 : b) Write a Java program that correctly implements producer consumer problem using the concept of inter thread communication. class LabPro17 { int n; boolean vs = false; synchronized int get() { if(!vs) try{wait();} catch(InterruptedException e) { System.out.println("InterruptedException caught");} System.out.println("got:" + n); vs = false; notify(); return n; } synchronized int put(int n) { if(vs) try{wait();} catch(InterruptedException e) { System.out.println("InterruptedException caught");} this.n = n; vs = true; System.out.println("put:" + n); notify(); return n; } } class Producer implements Runnable { LabPro17 k; Producer(LabPro17 k) { this.k = k; new Thread(this, "Producer").start(); } public void run() { int i = 0; while(true) {k.put(i++);} } Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

44

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. } class Consumer implements Runnable { LabPro17 k; Consumer(LabPro17 k) { this.k = k; new Thread(this, "Consumer").start(); } public void run() { while(true) {k.get();} } } class PCFixed { public static void main(String args[]) { LabPro17 k = new LabPro17(); new Producer(k); new Consumer(k); System.out.println("press control - c to stop. "); } } Output: run: put:0 press control - c to stop. got:0 put:1 got:1 put:2 got:2 put:3 got:3 Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

45

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. put:4 got:4 put:5 got:5 put:6 got:6 put:7 got:7 put:8 got:8 put:9 got:9 put:10 got:10 put:11 got:11 put:12 got:12 put:13 got:13 put:14 got:14 put:15 got:15 Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

46

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. put:16 got:16 put:17 got:17 put:18 got:18 put:19 got:19 Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

47

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 10 : Write a program that creates a user interface to perform integer divisions. The user enters two numbers in the textfields, Num1 and Num2. The division of Num1 and Num2 is displayed in the Result field when the Divide button is clicked. If Num1 or Num2 were not an integer, the program would throw a NumberFormatException. If Num2 were Zero, the program would throw an ArithmeticException Display the exception in a message dialog box. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Division extends JFrame implements ActionListener { Container c; JButton btn; JLabel lbl1,lbl2,lbl3; JTextField tf1,tf2,tf3; JPanel p; SuhritDivision() { super("Exception Handler"); c=getContentPane(); c.setBackground(Color.red); btn=new JButton("DIVIDE"); btn.addActionListener(this); tf1=new JTextField(30); tf2=new JTextField(30); tf3=new JTextField(30); lbl1=new JLabel("NUM 1"); lbl2=new JLabel("NUM 2"); lbl3=new JLabel("RESULT"); p=new JPanel(); p.setLayout(new GridLayout(3,2)); p.add(lbl1); p.add(tf1); p.add(lbl2); p.add(tf2); p.add(lbl3); p.add(tf3); c.add(new JLabel("Division"),"North"); c.add(p,"Center"); c.add(btn,"South"); } Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

48

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

public void actionPerformed(ActionEvent e) { if(e.getSource()==btn) { try { int a=Integer.parseInt(tf1.getText()); int b=Integer.parseInt(tf2.getText()); int c=a/b; tf3.setText(""+c); } catch(NumberFormatException ex) { tf3.setText("--"); JOptionPane.showMessageDialog(this,"Only Integer Division"); } catch(ArithmeticException ex) { tf3.setText("--"); JOptionPane.showMessageDialog(this,"Division by zero"); } catch(Exception ex) { tf3.setText("--"); JOptionPane.showMessageDialog(this,"Other Err "+ex.getMessage()); } } } public static void main(String args[]) { Division b=new Division(); b.setSize(300,300); b.setVisible(true); }

Holymary Institute of Technology & Sciences

49

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Output:

Holymary Institute of Technology & Sciences

50

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week 11 : Write a Java program that allows the user to draw lines, rectangles and ovals. import java.awt.*; import java.awt.event.*; import java.applet.*; public class LabPro19 extends Applet implements MouseListener,ActionListener,MouseMotionListener { Button lines,ovals,rect,free; int ch; int x,y,x2,y2,k; public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { x=e.getX(); y=e.getY(); } public void mouseReleased(MouseEvent e) { x2=e.getX(); y2=e.getY(); repaint(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) {if(k==1){ x2=e.getX(); y2=e.getY(); Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

51

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. repaint();} } public void init() { lines=new Button("line"); ovals=new Button("oval"); rect =new Button("rect"); free=new Button("free hand"); add(lines);add(ovals);add(rect);add(free); lines.addActionListener(this); ovals.addActionListener(this); rect.addActionListener(this); free.addActionListener(this); addMouseListener(this); addMouseMotionListener(this); } public void actionPerformed(ActionEvent ki) { if(ki.getSource()==lines) { ch=1; k=0;} else if(ki.getSource()==ovals) { ch=2; k=0;} else if(ki.getSource()==rect) {ch=3; k=0;} else if(ki.getSource()==free) { ch=4; k=1;} } public void paint(Graphics g) { switch (ch) { case 1:g.drawLine(x,y,x2,y2); break; case 2:g.drawOval(x,y,(x2-x),(y2-y)); break; case 3:g.drawRect(x,y,(x2-x),(y2-y)); break; case 4:repaint(); break; } } } Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

52

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Output:

Holymary Institute of Technology & Sciences

53

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

54

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Week:12 a) Write a Java program to create Multiple Threads Regulation:R07 Branch: IT

class MyThread extends Thread{ MyThread(String s){ super(s); start(); } public void run(){ for(int i=0;i<5;i++){ System.out.println("Thread Name :" +Thread.currentThread().getName()); try{ Thread.sleep(1000); }catch(Exception e){} } } } public class MultiThread1{ public static void main(String args[]){ System.out.println("Thread Name :" +Thread.currentThread().getName()); MyThread m1=new MyThread("My Thread 1"); MyThread m2=new MyThread("My Thread 2"); } }

Holymary Institute of Technology & Sciences

55

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Holymary Institute of Technology & Sciences

56

HOLY MARY INSTITUTE OF TECHNOLOGY & SCIENCES (college of Engineering) Subject:Java Lab Manual Course: II B.Tech. II Sem. Regulation:R07 Branch: IT

Week 12:
b) Write a java program to find the your Host IP Address import java.net.*; public class Ipaddress{ public static void main(String args[]){ try{ InetAddress local= InetAddress.getLocalHost(); System.out.println ("Local IP Address is : " + local); } catch (UnknownHostException e){ System.err.println ("Can't detect IP Address : " + e); } } }

Holymary Institute of Technology & Sciences

57

S-ar putea să vă placă și