PCS0214 Java programming LAB SEMESTER –II

NAME REG NO

: :

2011-2012

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SRM UNIVERSITY
(UNDER SECTION 3 OF THE UGC ACT, 1956)

VADAPALANI CAMPUS –RAMAPURAM PART, VADAPALANI CHENNAI-600 026

SRM UNIVERSITY
(UNDER SECTION 3 OF THE UGC ACT, 1956)

VADAPALANI CAMPUS – RAMAPURAM PART, VADAPALANI, CHENNAI-600 026 BONAFIDE CERTIFICATE REGISTER NO________________

Certified to Be the Bonafide Record of the work done by ____________________of________________ B.TECH (PT) degree course in the practical ____________________ in SRM UNIVERSITY, Vadapalani campus - Ramapuram part, Vadapalani, Chennai - 26 during the academic year_________

Lab Incharge

Date:

Head of the Department

Submitted

for

University

Examination

held

in

________________________________________SRM UNIVERSITY. at Vadapalani.

Date

Internal Examiner

External Examiner

Name: Class:

Branch:

INDEX
Expt. No Date Description Of title of Experiment Page Date of submission Remarks

Ex: no: 1 Date:

DATATYPES

AIM: To write a program to illustrate the different data types in Java.

ALGORITHM: 1. Java supports multiple data types; Create main class. 2. Declare the Variables of different data types and assign them with values 3. Finally values of different types of variables are displayed.

7 Double Value: 12.478345787644 Short Value: 10245 Char Value: N String Value: Java Program Long Value: 1234567890 Byte Value: 127 .OUTPUT: Integer Value: 25 Float Value: 84.

println("Double Value:" +dValue). long lValue=1234567890.out. System.println("Float Value:" +fValue). System.println("Byte Value:" +bValue). System.478345787644.out. short shValue=10245. . System. System.println("Long Value:" +lValue). } } RESULT: Thus the program for illustrating different datatypes in Java has been executed successfully.out.println("Short Value:" +shValue). char cValue='N'.out. System. String sValue="Java Program".out.println("Char Value:" +cValue).7f. System.println("Integer Value:"+iValue). System. byte bValue=127.println("String Value:" +sValue).out.out. float fValue=84.out. double dValue=12.PROGRAM: class Practical1 { public static void main(String args[]) { int iValue=25.

5. Create main class. Finally calculated respective area and perimeter values are displayed. . 3. Create objects of classes. 2. Square and Rectangle. ALGORITHM: 1. Access the methods of the two classes using object referencing method. Create two classes named Square and Rectangle with methods to calculate its area and perimeter respectively. 4.Ex: no: 2 Date: AIM: CLASS AND OBJECTS To write a program to illustrate the concept of classes and objects in Java.

OUTPUT: .

RePeri(l.out.println("Perimeter Of Second Square :"+sq. System. l=10.out. System. Rectangle rect =new Rectangle().SqPeri(side)).b)).b)).out.SqArea(side)).println("Second Square side is :"+side). System.println("Perimeter Of First Rectangle :"+rect.println("Fist Square side is :"+side).println("Area Of First Rectangle :"+rect.println("Area Of Second Rectangle :"+rect. System. Square sq=new Square().int b) { return l*b. System. System.PROGRAM: class Square { int SqArea(int side) { return side*side.println("Second Rectangle side is :"+l +" "+b).ReArea(l.RePeri(l.out.println("Fist Rectangle value is :"+l +" "+b). System. System.out. System.println("Perimeter Of First Square :"+sq.println("Area Of First Square :"+sq. System. System.out.println("Perimeter Of Second Rectangle :"+rect.SqPeri(side)). System. side=30.out.b=30. } int SqPeri(int side) { return 4*side.SqArea(side)). } } class Rectangle { int ReArea(int l. } int RePeri(int l.out. int l=20.out.out. b=20.b)). .out.println("Area Of Second Square :"+sq.int b) { return 2*(l+b).out. } } class Practical2 { public static void main(String args[]) { int side=20.b)).ReArea(l.

.} } RESULT: Thus the program to illustrate the concept of classes and objects has been executed successfully.

7.Ex: no: 3 Date: SIMPLE INHERITANCE AIM: To write a program to illustrate the concept of simple inheritance in Java. Create objects for parent and child classes. The displayed results show the inherited values of parent class variables and their access from child class. 5. Declare one more variable as well in Class B and methods to display the values of local variable and inherited variables 4. Create main class. . Create a Child class named B by inheriting from class A. 3. Access the variables and methods of classes A and B via the objects created and display the results. Create a Parent class named A with two variables and a method to display their values. 6. 2. ALGORITHM: 1.

OUTPUT: .

} } class Practical3 { public static void main(String args[]) { A superC=new A(). void showij() { System.out.out. subC.println("The sum of i. . } } class B extends A { int k.println("Value of K :"+k).j=8.showij(). superC. subC. B subC=new B().out. void showk() { System.sum().showk(). } void sum() { System.k=9.j.PROGRAM: class A { int i.i=10.showij(). } } RESULT: Thus the program to illustrate the concept of simple inheritance in Java has been executed successfully. subC.i=7.println("Value of i & j :"+i + " "+ j). subC.j=20. subC. subC. superC. superC. j & k :"+ (i+j+k)).

Get the input values of two matrices to be added 4. . ALOGORITHM: 1. Set the row and column size of matrices 3. Add the matrices using for loop and display the resultant matrix.Ex: no: 4 Date: MATRIX ADDITION AIM: To write a program to perform matrix addition using Java. Initialize two 2 dimensional arrays to represent the two input matrices 2.

OUTPUT: .

io.println("Number of Row :"+rows).length. System.j++) System. int array[][]=new int[2][2].i++) { for(int j=0. System.*.println("\nMatrix Addition:"). } } } . for(int i=0.out.out.out. System. int array1[][]=new int[2][2].j++) System.in).i++) { for(int j=0.println(). for(int i=0.i<rows.println("\nGive the Matrix 1:").i<rows.j<cols.println().println("\nMatrix 2:").readLine()).i<rows. System. System. } System.parseInt(din.print((array[i][j]+array1[i][j])+" "). } System.i<rows. System.out.print(array[i][j]+" ").i++) for(int j=0.length. int cols=array[1]. for(int i=0.readLine()).out. int rows=array.j<cols. class Practical4 { public static void main(String args[]) throws Exception { DataInputStream din=new DataInputStream(System. for(int i=0.out. System.out.out.out.out.PROGRAM: import java.println("\nGive the Matrix 2:").j++) System.println("Number of Column:"+cols).i<rows.print(array1[i][j]+" ").parseInt(din.out.j<cols.println().j<cols.out.j<cols.out.i++) { for(int j=0.println("\nMatrix 1:"). for(int i=0.j++) array[i][j]=Integer.j++) array1[i][j]=Integer. System.i++) for(int j=0.

RESULT: Thus the Java program to perform matrix addition has been executed successfully. .

4. Initialize three 2 dimensional arrays to represent the two input matrices and the output matrix. 3. 2. Get the input values of two matrices to be multiplied. . ALGORITHM: 1. Multiply the matrices using for loop and display the resultant matrix. Set the row and column size of matrices.Ex: no: 5 Date: MATRIX MULTIPLICATION AIM: To write a program to perform matrix multiplication using Java.

OUTPUT: .

} System. int cols=array[1].println(). System.j<cols.j++) for(int k=0.i++) { for(int j=0. for(int i=0.length. System.i<rows.j++) array[i][j]=Integer.j++) System. for(int i=0.out.out. } System.println().k<cols. } .PROGRAM: import java.parseInt(din.i++) { for(int j=0. int array1[][]=new int[2][3].println("\nGive the Matrix 1:").j<rows.j<cols.j<cols.k++) array2[i][k]+=(array[i][k]*array1[j][k]).i++) { for(int j=0.i<rows.out.i++) { for(int j=0.parseInt(din.j<cols.print(array[i][j]+" ").i<rows.readLine()). class Practical5 { public static void main(String args[]) throws Exception { DataInputStream din=new DataInputStream(System.out.readLine()).*.i<rows.in).out.println("\nMatrix 1:"). int array2[][]=new int[2][3]. int rows=array. int array[][]=new int[2][3].i++) { for(int j=0.out.j++) array1[i][j]=Integer.length.println("\nGive the Matrix 2:"). for(int i=0.println("\nMatrix 2:").println().j++) System. System. for(int i=0. } System. System.i<rows.out.out.out.out. } System.io.println("\nMatrix Multiplication:").print(array1[i][j]+" "). for(int i=0.

i++) { for(int j=0.out. .out.print(array2[i][j]+" "). System.j++) System.for(int i=0.j<cols.i<rows. } } } RESULT: Thus the Java program to perform matrix multiplication has been executed successfully.println().

ALGORITHM: 1.Ex: no: 6 Date: OVERLOADING AIM: To illustrate the concept of method overloading in Java. Java supports method overloading by having different input parameters. Create a class named OverloadDemo and define the method named test with different input parameters and displaying those values. Create a main class and create an object of the OverloadDemo class. 5. 2. 4. 3. Finally the different input parameter values are displayed. Access the test method by passing different parameters. .

OUTPUT: .

test(). . result = ob.PROGRAM: class OverloadDemo { void test(){ System.test(123.println("a and b: " + a + " " + b).test(10. double result.out. } void test(int a.println("a: " + a). } double test(double a) { System.out.out. System.2).println("Result of ob. return a*a.test(123.2): " + result).test(10). } } RESULT: Thus the Java program for illustrating Method overloading concept has been executed successfully. } void test(int a) { System.println("double a: " + a). 20).out. } } class Practical6 { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(). ob. ob.out.println("No parameters"). ob. int b) { System.

Access the show() method via the object created.Ex: no: 7 Date: OVERRIDDING AIM: To illustrate the concept of Method overriding in Java. . ALGORITHM: 1. Create a main class and child class object 5. It will override the method in super class A and proceeds with the show() method in child class. Java supports method overriding through inheritance 2. 3. Similarly create a child class named B inheriting from class A and with the same method name show() 4. Create a parent class named A and initialize the class constructor and a method to display the values of local variables(show()).

OUTPUT: .

} // display k – this overrides show() in A void show() { System. A(int a.show(). b).println("i and j: " + i + " " + j). j. int b) { i = a. // this calls show() in B } } RESULT: Thus the program to illustrate the method overriding concept in Java has been executed successfully. int b. B(int a.PROGRAM: class A { int i. subOb.println("k: " + k). j = b. 3). } } class B extends A { int k. } void show() { System.out. . 2. k = c.out. int c) { super(a. } } class Practical7{ public static void main(String args[]) { B subOb = new B(1.

Its demonstrated by creating another file input stream to read the contents of same existing file object whereas the existing file object set to null. While the file is not available for operation. 3. . Create a file input stream to get a file and read the contents of it using buffered reader. 5. The below program illustrates exception handling in file management. Hence it throws File not found exception and the catch & finally blocks display the respective status. it throws File not found exception. ALGORITHM: 1. 4. 2.Ex: no: 8 Date: EXCEPTION HANDLING AIM: To write a program to illustrate exception handling in Java.

OUTPUT: .

println("f2: finally block"). while(br. System. class Practical8 { public static FileInputStream f1(String fileName) throws FileNotFoundException.out.println( "main: Starting " + Practical9.*.out.io. try { fis = new FileInputStream(fileName).println("\nf1: File input stream created"). FileNotFoundException caught"). System.out. String fileName = "foo.txt". return fis. FileInputStream fis2 = null. } catch (FileNotFoundException ex) { System. } catch (FileNotFoundException ex) { . // get file input stream 1 try { fis1 = f1(fileName).getName()+ " with file name = " + fileName).readLine()).out. BufferedReader br=new BufferedReader( new FileReader(fileName)). } public static FileInputStream f2(String fileName) { FileInputStream fis = null. } finally { System.out.println("\nfile reading from here"). return fis. System. } public static void main(String args[]) { FileInputStream fis1 = null.IOException { FileInputStream fis = new FileInputStream(fileName).class.out.println("f2: Oops.ready()) System. } System.out.println("File Content : "+br.println("f2: Returning from f2").PROGRAM: import java.

getName() + " ended").out.println("main: " +Practical9. genreal exception caught").println("main: Oops.System. } catch (Exception ex) { System. .out. } } RESULT: Thus the program to illustrate exception handling in Java has been executed successfully.out.println("main: Oops. System. } fis2 = f2(fileName). FileNotFoundException caught").class.

Ex: no: 9 Date: AIM: THREADS To write a program to illustrate Thread Management in Java. ALOGORITHM: 1. 3. The below program illustrates Thread management. 2. Set priority for the both Thread classes 4. . Both are extends of class Thread. Display priority and Thread name respectively. Create two classes MyThread1 and MyThread2.

OUTPUT: .

System.out.out. cur.PROGRAM: class MyThread1 extends Thread { MyThread1(String s) { super(s).out.getPriority(). int p=cur. . start(). System.getName()). System.i++) { Thread cur=Thread. System. start().i++) { Thread cur=Thread. } } } public class Practical9 { public static void main(String args[]) { MyThread1 m1=new MyThread1("My Thread 1"). } public void run() { for(int i=0. MyThread2 m2=new MyThread2("My Thread 2").println("COUNT :"+Thread.i<3.setPriority(Thread.println("1 Thread Priority :"+cur).println("2 Thread Name :"+cur.out.MAX_PRIORITY).currentThread().i<3.out. int p=cur.println("1 Thread Name :"+cur.currentThread().getName()).println("2 Thread Priority :"+cur). cur.getPriority().activeCount()).setPriority(Thread. } public void run() { for(int i=0. } } } class MyThread2 extends Thread { MyThread2(String s) { super(s). System.MIN_PRIORITY).

.} } RESULT: Thus the program to illustrate thread management in Java has been executed successfully.

And Interface B contains a function call – void method3. 2. Interface A contains 2 function calling – void method1 and method2. . The below program illustrates the concept of Interface.Ex: no: 10 Date: AIM: INTERFACE To write a program to illustrate the concept of Interface in Java. 4. ALOGORITHM: 1. Create two Interfaces A and B 3. Call 3 method functions in main function.

OUTPUT: .

out.out.method3(). p11.println("Method 2 Called"). } } RESULT: Thus the program to illustrate implementation of interface in Java has been executed successfully.method1().out. p11.PROGRAM: interface A { void method1().method2(). .println("Method 3 Called"). } public void method3() { System. void method2(). } public static void main(String args[]) { Practical11 p11=new Practical11(). } public void method2() { System. } class Practical10 implements B { public void method1() { System.println("Method 1 Called"). } interface B extends A { void method3(). p11.

Ex: no: 11 Date: AIM: PACKAGES To write a program to illustrate the implementation of packages in Java. 2. Create 3 packages named Circle. ALOGORITHM: 1. Import all three packages and call the public function inside each classes. . Square and Rectangle. 3. With each package calculate their respective area. Display the calculate area of each package. 4. The below program illustrates the implementation of package.

OUTPUT: .

println("Area of Rectangle: "+(l*b)). Square sq=new Square(). } } RESULT: .println("Area of Circle: "+(3. c.out. public class Rectangle { public void area(int l.out. class Practical11 { public static void main(String args[]) { Circle c=new Circle().3). } } Main class import mypack. r. Rectangle r=new Rectangle(). public class Square { public void area1(int s) { System. } } mypack.*. sq.Square.println("Area of Square: "+(s*s)).area1(5).Circle.out.int b) { System.java package mypack. public class Circle { public void area(int r) { System.area(3).PROGRAM: mypack.Rectangle.java package mypack.java package mypack.14*r*r)). } } mypack.area(5.

Create a Text field with different properties. ALOGORITHM: 1.Thus the program to illustrate implementation of package in Java has been executed successfully Ex: no: 12 SWINGS Date: AIM: To write a program to illustrate the implementation of swings in Java. 2. The below program illustrates the implementation of swings. 4. Create a class Practical12 extends JFrame class 3. Get the display of swings .

OUTPUT: .

awt. import java. resultField. BorderLayout.event. public static void main(String[] args) { Practical12 app = new Practical12().SOUTH).format("%.awt. BorderLayout.WEST). resultField. pack().ActionEvent.BorderLayout. n *= 2.ActionListener.CENTER).setEditable(false). n)). import java.PROGRAM: import javax. add(new JLabel("Number to double (n):").setVisible(true).trim(). public class Practical12 extends JFrame { JTextField numberField = new JTextField(15). add(resultField.swing. import java.*. BorderLayout.setText("n * 2 = " + String.EAST).addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String numberStr = numberField. JButton butt = new JButton("Calculate"). app. JTextField resultField = new JTextField(20).event.parseDouble(numberStr).awt. butt. setDefaultCloseOperation(EXIT_ON_CLOSE). } }). numberStr = numberStr. add(numberField. double n = Double.2f". add(butt. } private Practical12() { super("Number doubler").getText(). } } RESULT: Thus the program to illustrate implementation of swings in Java has been executed successfully . BorderLayout.

Get the display from HTML page. ALOGORITHM: 1. .Ex: no: 13 Date: AIM: APPLET To write a program to illustrate the implementation of applet in Java. 2. Create a java applet with Text field boxes for requesting name 3. Create a HTML page implements java applet 4. The below program illustrates the implementation of applet.

OUTPUT: .

bottom.add( new Label("Enter your name:") ).applet. add("South". Panel top = new Panel(). } } HTML <html> <title>Layout. add("Center". No Panel</title> <hr> <applet code="Practical13. /*<applet code="Practical13" width=500 height=500> </applet>*/ public class Practical13 extends java. bottom. bottom ). top.awt.*.add( new TextField("<Your name here>". top ).setLayout( new FlowLayout() ). 30 ) ). top.Applet { public Practical13() { setLayout( new BorderLayout() ).PROGRAM: import java.class" width=530 height=100> </applet> <hr> </html> RESULT: Thus the program to illustrate implementation of applet in Java has been executed successfully .add( new Button("Submit") ). Panel bottom = new Panel().setLayout( new FlowLayout() ). top.