Java Manual
Java Manual
Aim:
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:
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
Aim:
To write a simple JAVA program for checking whether a given number is prime or not.
Algorithm:
Program:
2
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
Input / Output:
Viva 10
Total 25
Result:
3
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
Aim:
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:
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
Aim:
To write a JAVA program for the implementation of Classes and Objects using
Constructors.
Algorithm:
Program:
6
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
Input / Output:
Viva 10
Total 25
Result:
7
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
Aim:
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:
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
Aim:
Algorithm:
Program:
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:
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:
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:
Algorithm:
Program:
15
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
Input / Output:
Viva 10
Total 25
Result:
16
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
Aim:
To write a JAVA program for the implementation of Collection Framework using LinkedList
class.
Algorithm:
Program:
import java.util.*;
class collListmethods1 {
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);
Input / Output:
Entered Elements
Ravi
Vijay
Ravi
Ajay
Result:
Thus the program for the implementation of LinkedList using Collection Framework.
18
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
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:
Viva 10
Total 25
Result:
20
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
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:
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
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:
Viva 10
Total 25
Result:
24
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
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);
25
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
26
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
JOptionPane.showMessageDialog(this, "Error");
}
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
Viva 10
Total 25
Result:
29
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
Aim:
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:
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
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:
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
Aim:
To write a JAVA program for the implementation of FileReader and FileWriter using java.io
package.
Algorithm:
Program:
32
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
Input:
Output:
33
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
Input:
Output:
Viva 10
Total 25
Result:
34
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
Aim:
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:
Input / Output:
35
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
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
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:
Viva 10
Total 25
Result:
39
Programming in JAVA Laboratory
Department of Computer Science and Engineering, SMVEC
Aim:
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:
package BME;
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
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