Sie sind auf Seite 1von 47

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.

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

PROGRAM: class Practical1 { public static void main(String args[]) { int iValue=25; float fValue=84.7f; double dValue=12.478345787644; long lValue=1234567890; short shValue=10245; char cValue='N'; byte bValue=127; String sValue="Java Program"; System.out.println("Integer Value:"+iValue); System.out.println("Float Value:" +fValue); System.out.println("Double Value:" +dValue); System.out.println("Short Value:" +shValue); System.out.println("Char Value:" +cValue); System.out.println("String Value:" +sValue); System.out.println("Long Value:" +lValue); System.out.println("Byte Value:" +bValue); } }

RESULT: Thus the program for illustrating different datatypes in Java has been executed successfully.

Ex: no: 2 Date: AIM:

CLASS AND OBJECTS

To write a program to illustrate the concept of classes and objects in Java.

ALGORITHM: 1. Create two classes named Square and Rectangle with methods to calculate its area and perimeter respectively. 2. Create main class. 3. Create objects of classes, Square and Rectangle. 4. Access the methods of the two classes using object referencing method. 5. Finally calculated respective area and perimeter values are displayed.

OUTPUT:

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

} }

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

Ex: no: 3 Date:

SIMPLE INHERITANCE

AIM: To write a program to illustrate the concept of simple inheritance in Java.

ALGORITHM: 1. Create a Parent class named A with two variables and a method to display their values. 2. Create a Child class named B by inheriting from class A. 3. 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. 5. Create objects for parent and child classes. 6. Access the variables and methods of classes A and B via the objects created and display the results. 7. The displayed results show the inherited values of parent class variables and their access from child class.

OUTPUT:

PROGRAM: class A { int i,j; void showij() { System.out.println("Value of i & j :"+i + " "+ j); } } class B extends A { int k; void showk() { System.out.println("Value of K :"+k); } void sum() { System.out.println("The sum of i, j & k :"+ (i+j+k)); } } class Practical3 { public static void main(String args[]) { A superC=new A(); B subC=new B(); superC.i=10; superC.j=20; superC.showij(); subC.i=7; subC.j=8; subC.k=9; subC.showij(); subC.showk(); subC.sum(); } }

RESULT: Thus the program to illustrate the concept of simple inheritance in Java has been executed successfully.

Ex: no: 4 Date:

MATRIX ADDITION

AIM: To write a program to perform matrix addition using Java.

ALOGORITHM: 1. Initialize two 2 dimensional arrays to represent the two input matrices 2. Set the row and column size of matrices 3. Get the input values of two matrices to be added 4. Add the matrices using for loop and display the resultant matrix.

OUTPUT:

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

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

Ex: no: 5 Date:

MATRIX MULTIPLICATION

AIM: To write a program to perform matrix multiplication using Java.

ALGORITHM: 1. Initialize three 2 dimensional arrays to represent the two input matrices and the output matrix. 2. Set the row and column size of matrices. 3. Get the input values of two matrices to be multiplied. 4. Multiply the matrices using for loop and display the resultant matrix.

OUTPUT:

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

for(int i=0;i<rows;i++) { for(int j=0;j<cols;j++) System.out.print(array2[i][j]+" "); System.out.println(); } } }

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

Ex: no: 6 Date:

OVERLOADING

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

OUTPUT:

PROGRAM: class OverloadDemo { void test(){ System.out.println("No parameters"); } void test(int a) { System.out.println("a: " + a); } void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } double test(double a) { System.out.println("double a: " + a); return a*a; } } class Practical6 { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; ob.test(); ob.test(10); ob.test(10, 20); result = ob.test(123.2); System.out.println("Result of ob.test(123.2): " + result); } }

RESULT: Thus the Java program for illustrating Method overloading concept has been executed successfully.

Ex: no: 7 Date:

OVERRIDDING

AIM: To illustrate the concept of Method overriding in Java.

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

OUTPUT:

PROGRAM: class A { int i, j; A(int a, int b) { i = a; j = b; } void show() { System.out.println("i and j: " + i + " " + j); } } class B extends A { int k; B(int a, int b, int c) { super(a, b); k = c; } // display k this overrides show() in A void show() { System.out.println("k: " + k); } } class Practical7{ public static void main(String args[]) { B subOb = new B(1, 2, 3); subOb.show(); // this calls show() in B } }

RESULT: Thus the program to illustrate the method overriding concept in Java has been executed successfully.

Ex: no: 8 Date:

EXCEPTION HANDLING

AIM: To write a program to illustrate exception handling in Java. ALGORITHM: 1. The below program illustrates exception handling in file management. 2. Create a file input stream to get a file and read the contents of it using buffered reader. 3. While the file is not available for operation, it throws File not found exception. 4. 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. 5. Hence it throws File not found exception and the catch & finally blocks display the respective status.

OUTPUT:

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

System.out.println("main: Oops, FileNotFoundException caught"); } catch (Exception ex) { System.out.println("main: Oops, genreal exception caught"); } fis2 = f2(fileName); System.out.println("main: " +Practical9.class.getName() + " ended"); } }

RESULT:

Thus the program to illustrate exception handling in Java has been executed successfully.

Ex: no: 9 Date: AIM:

THREADS

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

OUTPUT:

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

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

Ex: no: 10 Date: AIM:

INTERFACE

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

OUTPUT:

PROGRAM: interface A { void method1(); void method2(); } interface B extends A { void method3(); } class Practical10 implements B { public void method1() { System.out.println("Method 1 Called"); } public void method2() { System.out.println("Method 2 Called"); } public void method3() { System.out.println("Method 3 Called"); } public static void main(String args[]) { Practical11 p11=new Practical11(); p11.method1(); p11.method2(); p11.method3(); } } RESULT:

Thus the program to illustrate implementation of interface in Java has been executed successfully.

Ex: no: 11 Date: AIM:

PACKAGES

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

OUTPUT:

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

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.

ALOGORITHM: 1. The below program illustrates the implementation of swings. 2. Create a class Practical12 extends JFrame class 3. Create a Text field with different properties. 4. Get the display of swings

OUTPUT:

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

Ex: no: 13 Date: AIM:

APPLET

To write a program to illustrate the implementation of applet in Java.

ALOGORITHM: 1. The below program illustrates the implementation of applet. 2. Create a java applet with Text field boxes for requesting name 3. Create a HTML page implements java applet 4. Get the display from HTML page.

OUTPUT:

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

Das könnte Ihnen auch gefallen