0% found this document useful (0 votes)
15 views41 pages

Java Manual

Uploaded by

nandhaclaquiks
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
15 views41 pages

Java Manual

Uploaded by

nandhaclaquiks
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 41

Department of Computer Science and Engineering, SMVEC

Ex. No. 1 (a) Simple Program: Factorial of a given Number Date:

Aim:

To write a simple JAVA program for finding factorial of a given number.

Algorithm:

Step1: Start
Step2: Read input of n value
Step3: f=1
Step4: i=1
Step5: for i=1 to n
Step6: f = f * i
Step7: Next i
Step8: Print output factorial
Step9: Stop

Program:

//Program for finding Factorial of a given number


import java.io.DataInputStream;
import java.io.IOException;
public class forloopex1 {
public static void main(String ar[])throws IOException
{
DataInputStream di = new DataInputStream(System.in);
int i,n;
System.out.println("Enter value of n");
n= Integer.parseInt(di.readLine());
int f=1;
for(i=1;i<=n;i++)
{
f = f * i;
}
System.out.println("The Factorial = "+f);
}
}

Input / Output:

Enter value of n
5
The Factorial = 120

Result:

Thus the program for finding factorial of a given number is successfully executed.

1
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 1 (b) Simple Program: Check prime of not Date:

Aim:

To write a simple JAVA program for checking whether a given number is prime or not.

Algorithm:

Program:

//Program for finding checking prime or not

2
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input / Output:

Particulars Max Mark Mark Secured

Program and Execution 15

Viva 10

Total 25

Result:

3
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 2 (a) Implementation of Classes and Objects Date:

Aim:

To write a JAVA program for the implementation of Classes and Objects.

Algorithm:

Step1: Start
Step2: Create a class
Step3: Create a method getdata()
Step4: Create another method printdata()
Step5: Close the class
Step6: Open the main class and main() method
Step7: Create object for the class
Step8: Call getdata() and printdata() using the object
Step9: Close the main program
Step10: Stop

Program:

//Program for the implementation of Classes and Objects


import java.io.*;
class addproc
{
int a,b;
void getdata()throws IOException
{
DataInputStream di = new DataInputStream(System.in);
System.out.println("Enter a,b values");
a = Integer.parseInt(di.readLine());
b = Integer.parseInt(di.readLine());
}
void printresult()
{
System.out.println("The addition = "+(a+b));
}
}

class classex1
{
public static void main(String ar[])throws IOException
{
addproc a1 = new addproc();
a1.getdata();
a1.printresult();
}
}

4
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input / Output:

Enter a, b values
100
200
The addition = 300

Result:

Thus the program for the implementation of Class and Object is successfully executed.

5
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 2 (b) Implementation of Classes and Objects Date:


using Constructors

Aim:

To write a JAVA program for the implementation of Classes and Objects using
Constructors.

Algorithm:

Program:

//Program for the implementation of Classes and Objects using Constructors

6
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input / Output:

Particulars Max Mark Mark Secured

Program and Execution 15

Viva 10

Total 25

Result:

7
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 3 (a) Implementation of Single Inheritance Date:

Aim:

To write a JAVA program for the implementation of Single Inheritance.

Algorithm:

Step1: Start
Step2: Create B class
Step3: Create a method getA()
Step4: Close the A class
Step5: Create another class B
Step6: Create a method getB()
Step7: Create another method printB()
Step8: Close the B class
Step9: Open the main class and main() method
Step10: Create object for the B class
Step11: Call getA() and printB() using the object
Step12: Close the main program
Step13: Stop

Program:

//Program for the implementation of Single Inheritance


class A
{
int a;
A()
{
a=25;
}
void getA()
{
a=50;
}
}
class B extends A
{
int b;
B()
{
b = 100;
}
void getB()
{
b= 150;
}
void printB()
{
b= a+b;
System.out.println(b);
}

8
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

class single1
{
public static void main(String ar[])
{
B b1 = new B();
b1.getA();
b1.printB();
}
}

Input / Output:

150

Result:

Thus the program for the implementation of Single Inheritance is successfully executed.

9
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 3 (b) Implementation of Hierarchical Inheritance Date:

Aim:

To write a JAVA program for the implementation of Hierarchical Inheritance.

Algorithm:

Program:

//Program for the implementation of Hierarchical Inheritance

10
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

11
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input / Output:

Particulars Max Mark Mark Secured

Program and Execution 15

Viva 10

Total 25

Result:

12
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Implementation of Multithreading
Ex. No. 4 (a) Date:
using Thread class

Aim:

To write a JAVA program for the implementation of Multithreading using Thread Class.

Algorithm:

Step1: Start
Step2: Create threadex2 class and inherit from Thread class
Step3: Create a method run() which is the actual class
Step4: Close the threadex2 class
Step5: Open the main class and main() method
Step9: Create 3 objects for the threadex2 class
Step10: Close the main program
Step11: Stop

Program:

//Program for the implementation of Multi Threading using Thread class


class threadex2 extends Thread
{ String s;
threadex2(String s)
{
this.s = s;
start();
}
public void run()
{
for(int i=1;i<=3;i++)
{
System.out.println(s+" - "+i);
try {
Thread.sleep(200);
}catch(InterruptedException ie) {System.out.println("excep in thread");}
} } }

class multithreadclass1
{
public static void main(String ar[])
{
threadex2 t1 = new threadex2("ONE");
threadex2 t2 = new threadex2("TWO");
threadex2 t3 = new threadex2("THREE");
for(int i=1;i<=3;i++)
{
System.out.println("Main - "+i);
try {
Thread.sleep(400);
}catch(InterruptedException ie) {System.out.println("excep in main");}
} } }

13
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input / Output:

Main – 1
ONE – 1
TWO – 1
THREE – 1
Main – 2
TWO – 2
ONE – 2
THREE – 2
Main – 3
ONE – 3
THREE – 3
TWO - 3

Result:

Thus the program for the implementation of Multi Threading using Thread class is successfully
executed.

14
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Implementation of Multithreading
Ex. No. 4 (b) Date:
using Runnable Interface

Aim:

To write a JAVA program for the implementation of Multithreading using Runnable


interface.

Algorithm:

Program:

//Program for the implementation of Multi Threading using Runnable Interface.

15
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input / Output:

Particulars Max Mark Mark Secured

Program and Execution 15

Viva 10

Total 25

Result:

16
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 5 (a) Implementation of Collection Framework using Date:


LinkedList class

Aim:

To write a JAVA program for the implementation of Collection Framework using LinkedList
class.

Algorithm:

Step1: Start of first program


Step2: Create an object for LinkedList class
Step3: Add the elements using add() method
Step4: Print all the elements using Iterator.next() method.
Step5: Stop
Step9: Start of second program
Step10: Create an object for LinkedList class
Step11: Call all the methods supported by LinkedList object
Step12:Stop

Program:

//1. Program for the implementation of LinkedList using Collection Framework


import java.util.*;
public class collLinkedList1
{
public static void main(String args[])
{
LinkedList<String> al=new LinkedList<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator<String> itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}

//2. Program for the implementation of methods in Linked List.

import java.util.*;
class collListmethods1 {

public static void main(String[] args) {


// Creating list using the LinkedList class
List<Integer> numbers = new LinkedList<>();

// Add elements to the list


numbers.add(100);
numbers.add(50);

17
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

numbers.add(25);
numbers.add(7);
numbers.add(22);
System.out.println("List: " + numbers);

// Access element from the list


int number = numbers.get(2);
System.out.println("Accessed Element: " + number);

// Using the indexOf() method


int index = numbers.indexOf(2);
System.out.println("Position of 2 is " + (index+1));

// Remove element from the list


int removedNumber = numbers.remove(1);
System.out.println("Removed Element: " + removedNumber);
numbers.set(2, 10);
System.out.println("List: " + numbers);
System.out.println("Length of the List = "+numbers.size());
boolean res = numbers.contains(50)?true:false;
System.out.println(res);
numbers.sort(null);
System.out.println("Sorted List: " + numbers);
}
}

Input / Output:

1. Output of the first program:

Entered Elements

Ravi
Vijay
Ravi
Ajay

2. Output of the Second program:

List: [100, 50, 25, 7, 22]


Accessed Element: 25
Position of 2 is 0
Removed Element: 50
List: [100, 25, 10, 22]
Length of the List = 4
false
Sorted List: [10, 22, 25, 100]

Result:

Thus the program for the implementation of LinkedList using Collection Framework.

18
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 5 (b) Implementation of Collection Framework using Date:


TreeSet class

Aim:

To write a JAVA program for the implementation of Collection Framework using TreeSet
class.

Algorithm:

Program:

19
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input / Output:

Particulars Max Mark Mark Secured

Program and Execution 15

Viva 10

Total 25

Result:

20
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 6 (a) Implementation of Simple Application using Date:


JDBC for Student Database

Aim:

To write a JAVA program for the implementation of simple Application using MySql-Java
connectivity for Student Database.

Algorithm:

Step1: Start
Step2: Create forname() with Java-MySql Driver
Step3: Create a method getConnection() with name of the Database and password
Step4: Create an object for Statement class using con.createStatement()
Step5: Create an object for ResultSet with executeQuery() method
Step9: Print all the records from the ResultSet using next() method
Step10: Close the main program
Step11: Stop

Program:

//Program for the implementation of MySQL – JAVA connectivity.


import java.sql.*;
public class dbex1{
public static void main(String args[])
{
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ICsecdb","system","manager");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from stud");
System.out.println("Roll Numer\tStud Name");
System.out.println(" -");
while(rs.next())
System.out.println(rs.getInt(1)+" \t\t "+rs.getString(2));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}

21
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input / Output:

Result:

Thus the program for the implementation of MySql-Java connectivity for Student Database is
successfully executed.

22
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 6 (b) Implementation of Simple Application using Date:


JDBC for Employee Database

Aim:

To write a JAVA program for the implementation of simple Application using MySql-Java
connectivity for Employee Database.

Algorithm:

Program:

23
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input / Output:

Particulars Max Mark Mark Secured

Program and Execution 15

Viva 10

Total 25

Result:

24
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 7 Implementation of Student Application with ADD, Date:


EDIT, DELETE using JDBC

Aim:

To write a JAVA program for the implementation of Student Application using MySql-Java
connectivity with ADD, EDIT, DELETE options.

Algorithm:

Step1: Start
Step2: Create forname() with Java-MySql Driver
Step3: Create a method getConnection() with name of the Database and password
Step4: Create an object for Statement class using con.createStatement()
Step5: Create an object for PreparedStatement with prepareStatement() method
Step9: Print all the records from the ResultSet using next() method
Step10: Close the main program
Step11: Stop

Program:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class JDBCexSwing2 extends JFrame implements ActionListener
{
JLabel lblRollno,lblName;
JTextField txtRollno,txtName;
JButton delete1, save,exit, update1;
JDBCexSwing2()
{
super("JDBC and Swing Example");
//JPanel panel = new JPanel(new GridLayout(3,3));
JPanel panel = new JPanel(true);
panel.setLayout(new GridLayout(10,10));
lblRollno= new JLabel("RollNo");
panel.add(lblRollno);
txtRollno= new JTextField();
panel.add(txtRollno);
lblName= new JLabel("Name");
panel.add(lblName);
txtName= new JTextField();
panel.add(txtName);

save = new JButton("Save");


panel.add(save);
save.addActionListener(this);

delete1 = new JButton("Delete");


panel.add(delete1);
delete1.addActionListener(this);

25
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

update1 = new JButton("Update");


panel.add(update1);
update1.addActionListener(this);

exit = new JButton("Exit");


panel.add(exit);
exit.addActionListener(this);
add(panel);
setSize(500, 400);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e)


{
if(e.getSource()==save)
{
AddStudent();
JOptionPane.showMessageDialog(this, "Student information updated
successfully");
}
else if(e.getSource()==delete1)
{
deleteStudent();
JOptionPane.showMessageDialog(this, "Student information deleted successfully");
}
else if(e.getSource()==update1)
{
updateStudent();
JOptionPane.showMessageDialog(this, "Student information updated
successfully");
}
else if(e.getSource()==exit)
{
this.dispose();
}
}

public void deleteStudent()


{
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con1 =
DriverManager.getConnection("jdbc:mysql://localhost:3306/ICsecdb","system","manager");

int rollno = Integer.parseInt(txtRollno.getText());

PreparedStatement ps1=con1.prepareStatement("delete from stud where sid=?");


ps1.setInt(1,rollno);
int x = ps1.executeUpdate();
ps1.close();
con1.close();
}
catch (Exception ex)
{

26
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

JOptionPane.showMessageDialog(this, "Error");
}

public void updateStudent()


{
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con2 =
DriverManager.getConnection("jdbc:mysql://localhost:3306/ICsecdb","system","manager");

int rollno = Integer.parseInt(txtRollno.getText());


String name = txtName.getText();
PreparedStatement ps2=con2.prepareStatement("update stud set sname = ? where
sid=?");
ps2.setString(1,name);
ps2.setInt(2,rollno);
int x = ps2.executeUpdate();
ps2.close();
con2.close();
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(this, "Error");
}
}

public void AddStudent()


{
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/ICsecdb","system","manager");

int rollno = Integer.parseInt(txtRollno.getText());

String name = txtName.getText();

PreparedStatement ps=con.prepareStatement("insert into stud values(?,?)");


ps.setInt(1,rollno);//1 specifies the first parameter in the query
ps.setString(2,name);

int x = ps.executeUpdate();
ps.close();
con.close();
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(this, "Error");
}
}
public static void main(String[] args)

27
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

{
JDBCexSwing2 ss = new JDBCexSwing2();
ss.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Input / Output:

28
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Particulars Max Mark Mark Secured

Program and Execution 15

Viva 10

Total 25

Result:

29
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 8(a) Implementation of FileInputStream and Date:


FileOutputStream using java.io package

Aim:

To write a JAVA program for the implementation of FileInputStream and FileOutputStream


using java.io package.

Algorithm:

Step1: Start
Step2: Create an object for FileInputStream class with the Physical file testout.txt
Step3: Reading the content of the file using read() method
Step4: Close the file using close() method
Step5: Create an object for FileOutputStream class with the Physical file testout.txt
Step9: Writing a content into the file using write() method
Step10: Close the file using close() main program
Step11: Stop

Program:

//a) Program for FileInputStream example

import java.io.*;
public class iofileinputstreamex1
{
public static void main(String args[])
{
try{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=0;
while((i=fin.read())!=-1)
{
System.out.println((char)i);
}
fin.close();
}catch(Exception e){System.out.println(e);}
}
}

Input:

30
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Output:
BME students are very good - yes

// b) Program for FileOutputStream example

import java.io.*;
public class iofileoutputstreamex1 {
public static void main(String args[])throws IOException
{
try{
DataInputStream di = new DataInputStream(System.in);
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
System.out.println("Enter the string to be written");

String s=di.readLine();
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("success...");
}catch(Exception e){System.out.println(e);}
}
}

Input:

Enter the string to be written


Smvec
success...

Output:

Result:

Thus the Java program for the implementation of FileInputStream and FileOutputStream has been
executed successfully.

31
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 8(b) Implementation of FileReader and FileWriter Date:


using java.io package

Aim:

To write a JAVA program for the implementation of FileReader and FileWriter using java.io
package.

Algorithm:

Program:

//a) Program for FileReader example

32
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input:

Output:

// b) Program for FileWriter example

33
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input:

Output:

Particulars Max Mark Mark Secured

Program and Execution 15

Viva 10

Total 25

Result:

34
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 9(a) Implementation of JAVA application using Date:


Exception Handling

Aim:

To write a JAVA program for the implementation of Exception Handling.

Algorithm:

Step1: Start
Step2: Read a and b values
Step3: c = a / b
Step4: if b is 0 then throw the exception and handling using try..catch blocks
Step5: if b is a non zero then print the result of the division
Step9: Stop

Program:

//Exception handling using Multiple catch blocks


import java.io.*;
public class exceptionhandling1 {
public static void main(String ar[])throws IOException
{
DataInputStream di = new DataInputStream(System.in);
int a, b, c;
System.out.println("Enter value for a");
a = Integer.parseInt(di.readLine());
System.out.println("Enter value for b");
b = Integer.parseInt(di.readLine());
try
{
c = (int) a / b;
System.out.println("Division = "+c);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}

Input / Output:

35
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Enter value for a


10
Enter value for b
0
/ by zero

Result:

Thus the Java program for the implementation of Exception Handling has been executed
successfully.

36
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

37
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex. No. 9(b) Implementation of JAVA application using Date:


Exception Handling with User Defined Exception

Aim:

To write a JAVA program for the implementation of Exception Handling with User Defined
Exception.

Algorithm:

Program:

38
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input / Output:

Particulars Max Mark Mark Secured

Program and Execution 15

Viva 10

Total 25

Result:

39
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Ex.No. 10 Implementation of Packages in JAVA Date:

Aim:

To write a JAVA program for the implementation of Packages.

Algorithm:

Step1: Start
Step2: create this bmepack.java in E:/BME/
Step3: Compile this bmepack.java
Step4: Create this packmain.java in E:/
Step5: Compile and execute the packmain.java program
Step9: Stop

Program:

// Step1: create this bmepack.java in E:/BME/

package BME;

public class bmepack


{
public void packex1()
{
System.out.println("Inside the BME class and packex1() method");
}
}

//Step 2: Compile this bmepack.java


//Step 3: Create this packmain.java in E:/
import BME.*;
class packmain
{
public static void main(String ar[])
{
bmepack bc1 = new bmepack();
bc1.packex1();
}
}

// Step 4: Compile and execute the packmain.java program

40
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC

Input / Output:

E:\BME>javac bmepack.java

E:\BME>cd..

E:\>set classpath=e:/

E:\>javac packmain.java

E:\>java packmain
Inside the BME class and packex1() method

Particulars Max Mark Mark Secured

Program and Execution 15

Viva 10

Total 25

Result:

Thus the Java program for the implementation of Creating Packages and utilizing Packages has
been executed successfully.

41
Programming in JAVA Laboratory

You might also like