Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
TAMILNADU COLLEGE OF ENGINEERING PALANISAME RAVI NAGAR, KARUMATHAMPATTI, COIMBATORE -641659 LABORATORY RECORD NAME _______________________ CLASS _______________________ BRANCH _______________________ SUBJECT _______________________ UNIVERSITY REGISTER No. ROLL No. CERTIFIED THAT THIS IS BONAFIDE RECORD OF WORK DONE BY THE ABOVE STUDENT LABORATORY DURING THE YEAR / SEMESTER SIGNATURE OF LAB - IN – CHARGE SIGNATURE OF HEAD OF DEPT SUBMITTED FOR THE PRACTICAL EXAMINATION HELD ON _________________________ EXTERNAL EXAMINER INTERNAL EXAMINER INDEX S.No. DATE Name of Experiment Page No. Marks Signature of Staff INDEX S.No. DATE Name of Experiment Page No. Marks Signature of Staff Ex.no: 1-a Date: GREATEST OF THREE NUMBERS AIM: To write a simple java program to find greatest of three numbers. ALGORITHM: Start the Program. Declare the necessary variables. Define the values to the variables. Find the greatest number using “if…else…” condition. Print the Greatest Number. Stop the Program. PROGRAM CODING: class Myclass { public static void main(String args[]) { int a=5,b=3,c=7; System.out.println("a="+a); System.out.println("b="+b); System.out.println("c="+c); if(a>b && a>c) { System.out.println("a is greater"); } else if(b>c && b>a) { System.out.println("b is greater"); } else { System.out.println("c is greater"); }}} OUTPUT: RESULT: Thus the above program is executed and the required output is obtained. Ex.no:1-b Date: EVEN NUMBERS BETWEEN 1 AND INPUT NUMBER AIM: To write a simple java program to list all even numbers between 1 and the input number. ALGORITHM: Start the Program. Import the necessary packages and add the Exception Create the objects for BufferedReader along with InputStreamReader. Get the number from the user Print the even number up to the input number using “for” loop and “if” condition. Stop the program. PROGRAM CODING: import java.io.*; class Even { public static void main(String args[])throws IOException { BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter number:"); int num=Integer.parseInt(b.readLine()); System.out.println("Even numbers:"); for(int i=1;i<=num;i++) { if(i%2==0) { System.out.println(i+" "); } } } } OUTPUT: RESULT: Thus the above program is executed and the required output is obtained. Ex.no:2-a Date: CONSTRUCTOR OVERLOADING AIM: To write a simple java program to find the area of rectangle using constructor overloading. ALGORITHM: Start the program. Define the class. Define one or more Constructor for the class. Define the Main class. Create one or more objects to the class to invoke the constructors defined. Stop the program. PROGRAM CODING: class Rectangle { int width,length; Rectangle(int x) { width=length=x; } Rectangle(int x,int y) { length=x; width=y; } int rarea() { return width*length; } } class Myclass1 { public static void main(String args[]) { Rectangle obj1=new Rectangle(10); Rectangle obj2=new Rectangle(10,20); int result1=obj1.rarea(); int result2=obj2.rarea(); System.out.println("result1="+result1); System.out.println("result2="+result2); }} OUTPUT: RESULT: Thus the above program is executed and the required output is obtained Ex.no:2-b Date: ABSTRACT CLASS AIM: To write a simple java program to implement an abstract class. ALGORITHM: Start the program. Define the abstract class using the keyword “abstract”. Declare the abstract methods. Define the class and invoke the abstract class using “extends” concepts. Define the Abstract methods inside the respective class. In the main class, create the respective objects for the class and call the abstract methods. Stop the program. PROGRAM CODING: import java.lang.*; abstract class Shape { abstract void numberofSides(); } class Triangle extends Shape { public void numberofSides() { System.out.println("Three"); }} class Trapezoid extends Shape { public void numberofSides() { System.out.println("Four"); } } class Hexagon extends Shape { public void numberofSides() { System.out.println("six"); } } public class Sides { public static void main(String args[]) { Triangle T=new Triangle(); Trapezoid Ta=new Trapezoid(); Hexagon H=new Hexagon(); T.numberofSides(); Ta.numberofSides(); H.numberofSides(); } } OUTPUT: RESULT: Thus the above program is executed and the required output is obtained. Ex.no:2-c Date: COMMANDLINE ARGUMENTS AIM: To write a simple java program to swap two numbers using command line arguments. ALGORITHM: Start the program. Define the main class. Declare the required variables for swapping of two numbers. Do the swap operation using third variable. Execute program and pass the command line arguments to the program. Stop the program. PROGRAM CODING: class Swap { public static void main(String args[]) { int x=Integer.parseInt(args[0]); int y=Integer.parseInt(args[1]); System.out.println("Befote exchange:a="+x+"b="+y); int temp=x; x=y; y=temp; System.out.println("After exchange:a="+x+"b="+y); } } OUTPUT: RESULT: Thus the above program is executed and the required output is obtained. Ex.no:3-a Date: SINGLE INHERITANCE AIM: To write a simple java program to implement single inheritance ALGORITHM: Start the program. Define the Base class. Define the Sub – Class and inherit the Base Class using “extends” keyword. In the Main Class, create objects for the sub – class and access both the sub-class and base class. Stop the Program. PROGRAM CODING: class A { int rollno; String name; void show() { System.out.println("rollno:"+rollno); System.out.println("name:"+name); } } class B extends A { int m1,m2,m3,tot; float avg; void show1() { System.out.println("mark1:"+m1); System.out.println("mark2:"+m2); System.out.println("mark3:"+m3); tot=m1+m2+m3; avg=tot/3; System.out.println("total:"+tot); System.out.println("average:"+avg); } } class Single { public static void main(String args[]) { B ob=new B(); ob.rollno=123; ob.name="abc"; ob.show(); ob.m1=100; ob.m2=99; ob.m3=98; ob.show1(); } } OUTPUT: RESULT: Thus the above program is executed and the required output is obtained. Ex.no:3-b Date: MULTILEVEL INHERITANCE AIM: To write a simple java program to implement multilevel inheritance ALGORITHM: Start the program. Define the Base class and a sub class to it. Define another sub class and inherit the previous class using “extends” keyword. In the main class, create the objects for the sub class and access the base class properties. Stop the program. PROGRAM CODING: class A { int rollno; String name; void show() { System.out.println("rollno:"+rollno); System.out.println("name:"+name); } } class B extends A { int m1,m2,m3; void show1() { System.out.println("mark1:"+m1); System.out.println("mark2:"+m2); System.out.println("mark3:"+m3); } } class C extends B { int tot; float avg; void show2() { tot=m1+m2+m3; avg=tot/3; System.out.println("total:"+tot); System.out.println("average:"+avg); } } class Multilevel { public static void main(String args[]) { C ob=new C(); ob.rollno=123; ob.name="abc"; ob.show(); ob.m1=100; ob.m2=99; ob.m3=98; ob.show1(); ob.show2(); } } OUTPUT: RESULT: Thus the above program is executed and the required output is obtained. Ex. No: 4 Date : INTERFACE AIM: To write a Java Program for implementing the Stack operations using Interface. ALGORITHM: Start the Program. Import the necessary packages. Define an interface and declare the required methods. Implement the interface into the class to compute the stack operations such as push and pop. In the main class, create the appropriate objects for the class and invoke the methods . Stop the program. PROGRAM CODING: import java.io.*; import java.lang.*; interface IntStack { void push(int Item); int pop(); } class FixedStack implements IntStack { private int stck[]; private int tos; FixedStack(int size) { stck=new int[size]; tos=-1; } public void push(int item) { if(tos==stck.length-1) System.out.println("Stack is full"); else stck[++tos]=item; } public int pop() { if(tos<0) { System.out.println("Stack Underflow"); return 0; } else return stck[tos--]; } } class IfTest { public static void main(String args[]) { FixedStack mystack1=new FixedStack(5); FixedStack mystack2=new FixedStack(8); for(int i=0;i<5;i++) mystack1.push(i); for(int i=0;i<8;i++) mystack2.push(i); System.out.println("Stack in mystack1: "); for(int i=0;i<5;i++) System.out.println(mystack1.pop()); System.out.println("Stack in mystack2: "); for(int i=0;i<5;i++) System.out.println(mystack2.pop()); } } OUTPUT: ** RESULT: Thus the above java program for implementing stack operations using interface was executed successfully and the output is verified. Ex. No: 5-a Date: SIMPLE THREADING AIM: To write a java program for implementing simple threading concept. ALGORITHM: Start the program. Import the necessary packages. Extends the thread into the class and create the method for the class. Define the run() of thread inside the class. In the main class, execute the start() of thread for the corresponding method. Stop the Program. PROGRAM CODING: import java.io.*; import java.lang.*; import java.util.*; class SimpleThread1 { public static void main(String args[]) { new SimpleThread("Hello").start(); new SimpleThread("Hello").start(); } } class SimpleThread extends Thread { public SimpleThread(String str) { super(str); } public void run() { for(int i=0;i<10;i++) { System.out.println(" "+getName()); } System.out.println("Done ! "+getName()); } } OUTPUT: RESULT: Thus the above java program for implementing simple thread is done successfully and the output is verified. Ex. No: 5-b Date: MULTITHREADING AIM: To write a java program for implementing the multi threading concept. ALGORITHM: Start the program. Import the necessary packages. Extends the thread into the class and create the method for the class. Define the run() of thread inside the class. In the main class, execute the start() of thread for the corresponding method. Stop the Program. PROGRAM CODING: import java.io.*; import java.lang.*; import java.util.*; class ThreadTest { public static void main(String args[]) { new A().start(); new B().start(); new C().start(); } } class A extends Thread { public void run() { for(int i=1;i<=5;i++) { System.out.println("\t From Thread A:"+i); } System.out.println("Exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { System.out.println("\t From Thread B"+j); } System.out.println("Exit from B"); } } class C extends Thread { public void run() { for(int k=1;k<=5;k++) { System.out.println("\t From Thread C:"+k); } System.out.println("Exit from C"); } } OUTPUT: RESULT: Thus the above java program for implementing Multi threading is done successfully and the output is verified. Ex. No: 5-c Date : MULTI THREADING USING yield(), stop() AND sleep() AIM: To write a java program for implementing the multi threading concept and implement yield(), stop() and sleep(). ALGORITHM: Start the program. Import the necessary packages. Define one or two class. Extends the thread into the class and create the method for the class. Define the run() of thread inside the class. Invoke the yield(), sleep() and stop() in each class. In the main class, execute the start() of thread for the corresponding method. Stop the Program. PROGRAM CODING: import java.io.*; import java.lang.*; import java.util.*; class A extends Thread { public void run() { for(int i=1;i<=5;i++) { if(i==1) { yield(); } System.out.println("\tFrom Thread A: i="+i); } System.out.println("Exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { System.out.println("\t From Thread B: j="+j); if(j==3) stop(); } System.out.println("Exit from B"); } } class C extends Thread { public void run() { for(int k=1;k<=5;k++) { System.out.println("\tFrom Thread C:"+k); if(k==1) try { sleep(1000); } catch(Exception e) { } } System.out.println("Exit from C"); } } class Tm { public static void main(String args[]) { A ThreadA=new A(); B ThreadB=new B(); C ThreadC=new C(); System.out.println("Start Thread A"); ThreadA.start(); System.out.println("Start Thread B"); ThreadB.start(); System.out.println("Start Thread C"); ThreadC.start(); System.out.println("End of Main Thread"); } } OUTPUT: RESULT: Thus the above java program for implementing Multi threading using yield(), sleep() and stop() is done successfully and the output is verified. EX.NO:6 DATE: MOUSE EVENTS HANDLING AIM: To write a java program to display the function of keyboard ALGORITHM: 1. Start the program 2. Import java applet and applet window kit 3. Declare a class mouse events which extends applet implements, mouse and mouse motion listener 4. Declare the required variables and strings 5. Mouse listener is used to define five method namely mouse clicked, entered , exited, pressed and released. 6. Mouse motion listener is used to define two methods namely mouse dragged and mouse moved. 7. In mouse dragged method give the x-axis value and y-axis value 8. Execute the program 9. Stop the program. PROGRAM CODING: import java.awt.*; import java.awt.event.*; import java.applet.*; public class MouseEvents extends Applet implements MouseListener,MouseMotionListener { String msg=""; int mouseX=0,mouseY=0; public void init() { addMouseListener(this); addMouseMotionListener(this); setBackground(Color.red); setForeground(Color.green); } public void mouseClicked(MouseEvent me){ mouseX=0; mouseY=10; msg="Mouse clicked."; repaint(); } public void mouseEntered(MouseEvent me) { mouseX=0; mouseY=10; msg="Mouse entered."; repaint(); } public void mouseExited(MouseEvent me) { mouseX=0; mouseY=10; msg="Mouse exited."; repaint(); } public void mousePressed(MouseEvent me) { mouseX=me.getX(); mouseY=me.getY(); msg="Mouse Pressed"; repaint(); } public void mouseReleased(MouseEvent me) { mouseX=me.getX(); mouseY=me.getY(); msg="Mouse Realeased"; repaint(); } public void mouseDragged(MouseEvent me) { mouseX=me.getX(); mouseY=me.getY(); msg="*"; showStatus("dragging mouse at"+mouseX+","+mouseY); repaint(); } public void mouseMoved(MouseEvent me) { showStatus("moving mouse at"+me.getX()+","+me.getY()); } public void paint(Graphics g){ g.drawString(msg,mouseX,mouseY); } } APPLET CODING <applet code=MouseEvents.class width=100 height=300> </applet> OUTPUT: RESULT: Thus the java program has been implemented and the output is verified successfully. EX.NO:7 Date: KEYBOARD EVENTS HANDLING AIM: To write a java program to display the function of keyboard ALGORITHM: 1. Start the program 2. Import a class keyboard events which extents applet implements keyboard and Key listener tester 3. Declare the required variables and string 4. Keyboard events used to define the methods released, pressed, typed. 5. At last define the void paint the method. 6. Execute the program 7. stop the program PROGRAM CODING: import java.awt.*; import java.awt.event.*; public class KeyListenerTester extends Frame implements KeyListener { TextField t1; Label l1; public KeyListenerTester(String s) { super(s); Panel p =new Panel(); l1 = new Label ("Key Listener!") ; p.add(l1); add(p); addKeyListener(this) ; setSize (200,100); setVisible(true); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void keyTyped ( KeyEvent e ) { l1.setText("Key Typed"); } public void keyPressed ( KeyEvent e) { l1.setText ("Key Pressed"); } public void keyReleased (KeyEvent e) { l1.setText("Key Released"); } public static void main(String[]args) { new KeyListenerTester ("Key Listener Tester"); } } OUTPUT: RESULT: Thus the java program to implement key events is executed successfully and the output is verified. EX:NO:8 Date: RADIO BUTTON USING SWING AIM: To write a java program to implement RadioButton. ALGORITHM: 1. Start the program 2. Import the packages 3. Declare the class JRadioButtonDemo which extends JApplet which implements ActionListener 4. Declare the packages JTextField with package name if 5. Create objects b1, b2, b3 for the class JRadioButton 6. Using these object bg for Button group and using bg object access add () method. Thus the objects b1, b2, b3 are added to the Button Group 8. Give the applet code and save it as JRadioButton.html 9. Stop the program PROGRAM CODING: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JRadioButtonDemo extends JApplet implements ActionListener { JTextField tf; public void init(){ Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); JRadioButton b1=new JRadioButton("A"); b1.addActionListener(this); contentPane.add(b1); JRadioButton b2=new JRadioButton("b"); b2.addActionListener(this); contentPane.add(b2); JRadioButton b3=new JRadioButton("c"); b3.addActionListener(this); contentPane.add(b3); ButtonGroup bg=new ButtonGroup(); bg.add(b1); bg.add(b2); bg.add(b3); tf=new JTextField(5); contentPane.add(tf); } public void actionPerformed(ActionEvent ae){ tf.setText(ae.getActionCommand()); } } APPLET CODING: <applet code=JRadioButtonDemo.class width=100 height=300> </applet> OUTPUT: RESULT: Thus the java program for RadioButton is executed and the output is verified successfully. Ex.no:9 Date: FILE HANDLING AIM: To write a simple java program to perform file handling operation ALGORITHM: 1. Start the program 2. The class file1 and file2 are used to call the class method such as file copy and file search 3. In class file copy create run method which has one object for file reader using this object read the input content 4. Create output file, using file writer, read the data from input file & copied into output file 5. In class file search, create run method, which has one object for scanner to search the string 6. Stop the program PROGRAM CODING: FILE1: import java.io.*; class file1 { public static void main(String a[]) { FileCopy fc=new FileCopy(); fc.run(); } } FILE2: import java.io.*; class file2 { public static void main(String a[]) { FileSearch fs=new FileSearch(); fs.run(); } } FILECOPY: import java.io.*; public class FileCopy { public void run() { FileReader src; try { String srcName="testfile.txt"; src=new FileReader(srcName); } catch(FileNotFoundException e) { System.out.println("Cannot open source file"); return; } FileWriter dst; try { String dstName="fileout.txt"; dst=new FileWriter(dstName); } catch(IOException e) { System.out.println("Cannot open destination file"); return; } try { char[]buf=new char[128]; while(true) { int n=src.read(buf); if(n<0) break; dst.write(buf,0,n); } src.close(); dst.close(); } catch(IOException e) { System.out.println("I/O error:Copy may be incomplete"); } } } FILE SEARCH: import java.util.Scanner; import java.io.*; public class FileSearch { public void run() { Scanner src; try { String srcName="testfile.txt"; src=new Scanner(new FileReader(srcName)); } catch(FileNotFoundException e) { System.out.println("cannot ope source file"); return; } PrintWriter dst; try { String dstName="testfileout.txt"; dst=new PrintWriter(new FileWriter(dstName)); } catch(IOException e) { System.out.println("cannot open destination file"); return; } String toFind="java"; while(src.hasNextLine()) { String line=src.nextLine(); if(line.indexOf(toFind)>=0)dst.println(line); } src.close(); dst.close(); } } OUTPUT: RESULT: Thus the java program to perform file handling is executed and the output is verified successfully. Ex .no : 10 Date : AIM: DATABASE APPLICATION(JDBC) To write a java program to implement JDBC. ALGORITHM: 1. Type the program and save it in separate folder in one of the disk drives. 2. Create data source folder. 3. Establish an ODBC Connection with Ms-Access . 4. Compiler and run the program for every sql statement. 5. Open the corresponding database to check it all the statement. 6.Stop the program. PROGRAM CODING: import java.io.*; import java.sql.*; class conn { public static void main(String args[]) { Connection c=null; Statement st; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); c=DriverManager.getConnection("jdbc:odbc:db"); System.out.println("CONNECTION MADE...."); } catch(Exception e) { } try { st=c.createStatement(); //st.executeUpdate("create table emp(ID number,Name text)"); st.executeUpdate("insert into emp values(2,'Chennai')"); st.executeUpdate("update emp set Name='Mumbai'where ID=1"); st.close(); c.close(); System.out.println("CREATED"); } catch(Exception e) { } } } OUTPUT: RESULT: Thus above java program was done to implement JDBC and output is verified.