Lab Manual - AJ
Lab Manual - AJ
DATE: 22-2-23
Aim:
To write a Java Program for creating multithreads
Algorithm:
Step 1: Start the program with header files.
Step 2: Create two classes for calculating Fibonacci number and prime number
Step 3: Create two threads for two classes
Step 4: Assign two classes to two threads
Step 5: Execute both thread with sleep method
Step 6: Analysis the results
Coding:
import java.io.*;
class fibthread implements Runnable
{int n;
public void run()
{ n=1;
long f1=0;
long f2=1;
long f3;
System.out.println("*************************");
System.out.println("FIBONACCI NUMBER");
System.out.println("*************************");
System.out.println(f1);
System.out.println(f2);
while (n!=50)
{
f3=f1+f2;
f1=f2;
f2=f3;
System.out.println(f3);
1
n++;
try{
if ((n==10))
{ Thread.sleep(50); System.out.println("FIBINACCICS NUMBER");}
if (n==20)
{ Thread.sleep(250); System.out.println("FIBINACCICS NUMBER");}
if (n==30)
{ Thread.sleep(450); System.out.println("FIBINACCICS NUMBER");}
}
catch(InterruptedException e){;}
}
}
}
do{
if(n==1)
{ System.out.println(n);}
for( i=2;i<n;i++)
2
{ if(n%i==0)
{f=1;break;}
else{f=0;}
try{
if(m==10||m==20)
{ Thread.sleep(5);
}
}
catch(InterruptedException e){;}
}
if(f!=1){System.out.println(n);m++;}
n++;
}while(m!=25);
}
}
class threadcal
{
public static void main (String args[])//throws IOException
{
fibthread r=new fibthread();
Thread t1=new Thread(r,"thread1");
t1.setPriority(Thread.NORM_PRIORITY+3);
t1.start();
primethread s=new primethread();
Thread t2=new Thread(s,"thread2");
t2.setPriority(Thread.NORM_PRIORITY);
t2.start();
}
}
3
OUTPUT:
Results:
Program results are verified for multithread concept
4
EX NO: 2 Create a program to implement the interface concept
DATE: 01-3-23
DATE:
Aim:
To write a Java Program for creating interface
Algorithm:
Step 1: Start the program with header files.
Step 2: Create two classes for calculating Fibonacci number and prime number
Step 3: Create two interface named as process
Step 4: Execute both class with interface
Step 5: Analysis the results
Coding:
import java.io.*;
public interface number
{int process(int x,int y);}
import java.io.*;
import java.util.*;
class avg implements number
{
int average;
public int process(int x,int y)
{average=x/y;
return average;}
}
import java.io.*;
import java.util.*;
class sum implements number
{
int tot;
public int process(int x,int y)
{ tot=x+y;
return tot;}
}
import java.io.*;
import java.util.*;
class interfacemain
{
public static void main(String args[])throws IOException
{Scanner s=new Scanner(System.in);
5
sum s1=new sum();
avg a1=new avg();
System.out.println("enter the first number:");
int n1=s.nextInt();
System.out.println("enter the second number:");
int n2=s.nextInt();
int tot=s1.process(n1,n2);
System.out.println("sum of two number:"+tot);
int avg=a1.process(tot,2);
System.out.println("avg of two number:"+avg);
}
}
OUTPUT:
Results:
Program results are verified for interface concept
6
EX NO: 3 Create a program to implement the package concept
DATE: 08-03-23
DATE:
Aim:
To write a Java Program for creating package
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a class for mathematical formulas – sin, cos and exponential
Step 3: Create a class created under the package
Step 4: import the packages into main class
Step 5: Execute the program
Step 6: Analysis the results
Coding:
package mathfun;
import java.io.*;
import java.util.*;
import java.lang.*;
public class math
{
public void sin1(double x1)
{
int i=1,j=3;
double sum1=0.0,sum2=0.0;
while(i<=20)
{
double d=0.0,f=0.0;
d=fact(i);
f=Math.pow(x1,i);
f=f/d;
sum1=sum1+f;
i=i+4;
}
while(j<=20)
{
double d1=0.0;
d1=fact(j);
double f=0.0;
f=Math.pow(x1,j);
f=f/d1;
sum2=sum2+f;
j=j+4;
}
7
System.out.println("MATH SIN X :"+Math.sin(x1));
System.out.println();
System.out.println("SIN X :"+(sum1-sum2));
System.out.println();
System.out.println("RELATIVE ERROR:"+(Math.sin(x1)-(sum1-sum2)));
System.out.println();
}
8
i=i+1;
}
double f1=Math.pow(Math.E,x1);
double f2=(1+sum1);
System.out.println("MATH E POWER X :"+f1);
System.out.println();
System.out.println("MY E POWER X :"+f2);
System.out.println();
System.out.println("RELATIVE ERROR :"+(f1-f2));
System.out.println();
}
import mathfun.math;
import java.io.*;
class mfunmain
{ public static void main(String args[])throws IOException
{
math m=new math();
double x=0.0,x2=0.0;
DataInputStream in=new DataInputStream(System.in);
System.out.println("ENTER THE VALUE IN DEGREE:");
x=Double.parseDouble(in.readLine());
x2=(double)(x*22)/(180*7);
System.out.println("******************************");
System.out.println(" SIN X:");
System.out.println("******************************");
m.sin1(x2);
System.out.println("******************************");
System.out.println(" COS X:");
System.out.println("******************************");
m.cos1(x2);
System.out.println("******************************");
System.out.println(" E POWER X:");
System.out.println("******************************");
m.ex1(x2);
}
}
OUTPUT:
9
Results:
Program results are verified for package concept
10
EX NO: 4 Create a program to implement the RMI concept
DATE: 15-03-23
DATE:
Aim:
To write a Java Program for RMI concept
Algorithm:
Step 1: Start the program with RMI header files.
Step 2: Create an interface for establish a remote connection
Step 3: Create a class for implementing the interface method
Step 4: Test server status
Step 5: Execute the program
Step 6: Analysis the results
Coding:
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
11
import java.rmi.server.UnicastRemoteObject;
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
12
// Looking up the registry for the remote object
Hello stub = (Hello) registry.lookup("Hello");
Output:
13
Results:
Program results are verified for RMI concept
14
EX NO: 5 Write a program to design a calculator using Java components
and handle various events related activities
DATE: 29-03-23
Aim:
To write a Java Program for Calculator
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for calculator
Step 3: Create action listener on Jframe
Step 4: Design the calculator with buttons
Step 5: Execute the code
Step 6: Analysis the results
Coding:
// Java program to create a simple calculator
// with basic +, -, /, * using java swing elements
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class calculator extends JFrame implements ActionListener
{
// create a frame
static JFrame f;
// create a textfield
static JTextField l;
15
// default constrcutor
calculator()
{
s0 = s1 = s2 = "";
}
// main function
public static void main(String args[])
{
// create a frame
f = new JFrame("calculator");
try {
// set look and feel
UIManager.setLookAndFeel
(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
System.err.println(e.getMessage
());
}
// create a textfield
l = new JTextField(16);
16
// create number buttons and some
operators
JButton b0, b1, b2, b3, b4, b5, b6, b7,
// equals button
beq1 = new JButton("=");
// create . button
be = new JButton(".");
// create a panel
JPanel p = new JPanel();
17
// add action listeners
bm.addActionListener(c);
bd.addActionListener(c);
bs.addActionListener(c);
ba.addActionListener(c);
b9.addActionListener(c);
b8.addActionListener(c);
b7.addActionListener(c);
b6.addActionListener(c);
b5.addActionListener(c);
b4.addActionListener(c);
b3.addActionListener(c);
b2.addActionListener(c);
b1.addActionListener(c);
b0.addActionListener(c);
be.addActionListener(c);
beq.addActionListener(c);
beq1.addActionListener(c);
f.setSize(200, 220);
f.show();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
19
// set the value of text
l.setText(s0 + s1 + s2);
}
else if (s.charAt(0) == '=') {
double te;
// convert it to string
s0 = Double.toString(te);
s1 = s2 = "";
}
else {
// if there was no operand
if (s1.equals("") || s2.equals
(""))
s1 = s;
// else evaluate
else {
double te;
20
// store the value in 1st
if (s1.equals("+"))
te = (Double.parseDouble(s0) + Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
else if (s1.equals("/"))
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
else
te = (Double.parseDouble(s0) * Double.parseDouble(s2));
// convert it to string
s0 = Double.toString(te);
21
Results:
Program results are verified for Swing concept with calculator
22
EX NO: 6 Write a program to demonstrate use of Grid Layout
DATE: 01-04-23
DATE:
Aim:
To write a Java Program for creating Grid Layout
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for Grid Layout
Step 3: Create frame for window
Step 4: Design the grid layout on frame with buttons
Step 5: Execute the code
Step 6: Analysis the results
Coding:
import java.awt.*;
import javax.swing.*;
f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
23
f.add(b6);f.add(b7);f.add(b8);f.add(b9);
f.setLayout(new GridLayout(3,3));
//setting grid layout of 3 rows and 3 columns
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyGridLayout();
}
}
Output:
Results:
Program results are verified for grid layout concept
24
EX NO: 7 Write a program to demonstrate use of Flow Layout
DATE: 05-04-23
Aim:
To write a Java Program for creating Flow Layout
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for Flow Layout
Step 3: Create frame for window
Step 4: Design the Flow layout on frame with buttons
Step 5: Execute the code
Step 6: Analysis the results
Coding:
import java.awt.*;
import javax.swing.*;
f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//setting flow layout of right alignment
25
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyFlowLayout();
}
}
Output:
Results:
Program results are verified for Flow Layout
26
EX NO: 8 Write a program to demonstrate use of Card Layout
DATE: 19-04-23
Aim:
To write a Java Program for creating Card Layout
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for Card Layout
Step 3: Create frame for window
Step 4: Design the Card layout on frame with buttons
Step 5: Execute the code
Step 6: Analysis the results
Coding:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
ActionListener{
CardLayout card;
JButton b1,b2,b3;
Container c;
CardLayoutExample(){
c=getContentPane();
card=new CardLayout(40,30);
//create CardLayout object with 40 hor space and 30 ver
space
c.setLayout(card);
27
b1=new JButton("Apple");
b2=new JButton("Boy");
b3=new JButton("Cat");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add("a",b1);c.add("b",b2);c.add("c",b3);
}
public void actionPerformed(ActionEvent e) {
card.next(c);
}
Results:
Program results are verified for Card concept
28
EX NO: 9 Write a program to demonstrate use of Border Layout
DATE: 26-04-23
Aim:
To write a Java Program for creating Border Layout
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for Border Layout
Step 3: Create frame for window
Step 4: Design the Border layout on frame with buttons
Step 5: Execute the code
Step 6: Analysis the results
Coding:
import java.awt.*;
import javax.swing.*;
f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
f.add(b4,BorderLayout.WEST);
29
f.add(b5,BorderLayout.CENTER);
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new Border();
}
}
Output:
Results:
Program results are verified for Border Layout concept
30
EX NO: 10 Write a program to design a form using basic swing components
DATE: 03-05-23
Aim:
To write a Java Program for Form on Swing
Algorithm:
Step 1: Start the program with header files.
Step 2: Create two classes for design a login form
Step 3: Design the form with swing components
Step 4: Add the action listener on the components
Step 5: Execute the code
Step 6: Analysis the results
Coding:
import java.awt.Color;
import java.awt.Font;
import java.util.*;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
31
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Username");
l3 = new JLabel("Password");
tf1 = new JTextField();
p1 = new JPasswordField();
btn1 = new JButton("Login");
frame.add(l1);
frame.add(l2);
frame.add(tf1);
frame.add(l3);
frame.add(p1);
frame.add(btn1);
frame.setSize(400, 400);
frame.setLayout(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String uname = t1.getText();
String pass = p1.getText();
if(uname.equals("sudhir@oodlesTech") && pass.equals("abc@123"))
{
Welcome wel = new Welcome();
wel.setVisible(true);
32
JLabel label = new JLabel("Welcome:"+uname);
wel.getContentPane().add(label);
}
else
{
JOptionPane.showMessageDialog(this,"Incorrect login or password",
"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
new LoginForm();
}
}
Welcome.java
//package com.sudhir.oodlesTech;
import javax.swing.*;
import java.awt.*;
class Welcome extends JFrame
{
Welcome()
{
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Welcome");
setSize(400, 200);
}
}
Output:
33
Results:
Program results are verified for Swing Form concept
34
EX NO: 11 Write a program to demonstrate the use of scroll panes in Swing
DATE: 10-05-23
Aim:
To write a Java Program for Scroll panel
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for adding horizontal and vertical scroll panel
Step 3: Design the GUI with swing components
Step 4: Execute the program
Step 5: Analysis the results
Coding:
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.*;
scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBA
R_ALWAYS);
scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AL
WAYS);
frame.getContentPane().add(scrollableTextArea);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
36
Results:
Program results are verified for Scroll panel
37
EX NO: 12 Write a servlet to demonstrate the Http Servlet class using do Get ()
DATE: 16-05-23
Aim:
To write a Java Program for Servlet using doGet()
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for calculating http servlet using doGet()
Step 3: Establish the connection between server and client
Step 4: Execute the program
Step 5: Analysis the results
Coding:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
38
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
Output:
Results:
Program results are verified for Http Servlet concept
39
EX NO: 13 Write a servlet to demonstrate the Http Servlet class using do Post ()
DATE: 23-05-23
Aim:
To write a Java Program for Servlet using doPost()
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a classes for calculating http servlet using doPost()
Step 3: Establish the connection between server and client
Step 4: Execute the program
Step 5: Analysis the results
Coding:
package com.edu4java.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
40
private void response(HttpServletResponse resp, String msg)
throws IOException {
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<t1>" + msg + "</t1>");
out.println("</body>");
out.println("</html>");
}
}
Output:
Results:
Program results are verified for doPost () Servlet concept
41
EX NO: 14 Write a servlet to demonstrate the cookie
DATE: 30-05-23
Aim:
To write a Java Program for cookie
Algorithm:
Step 1: Start the program with header files.
Step 2: Create html and xml for cookie program
Step 3: Create two programs for httprequest and response
Step 4: Execute the program
Step 5: Analysis the results
Coding:
index.html
<form action="servlet1" method="post">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>
FirstServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);
42
Cookie ck=new Cookie("uname",n);//creating cookie object
response.addCookie(ck);//adding cookie in the response
out.close();
}catch(Exception e){System.out.println(e);}
}
}
SecondServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie ck[]=request.getCookies();
out.print("Hello "+ck[0].getValue());
out.close();
}catch(Exception e){System.out.println(e);}
43
}
}
web.xml
<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
Output:
44
Results:
Program results are verified for Cookie concept
45
EX NO: 15 Write an Application program/Applet to send queries
through JDBC bridge & handle result.
DATE: 06-06-23
Aim:
To write a Java Program for JDBC SQL connection
Algorithm:
Step 1: Start the program with header files.
Step 2: Create a class for accessing SQL through JDBC – ODBC Bridge
Step 3: Create the connection and statement
Step 4: Execute the program
Step 5: Analysis the results
Coding:
import java.sql.*;
import java.io.*;
class DBQuery6
IOException
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
catch(ClassNotFoundException e){};
Connection cnn;
CallableStatement cs;
ResultSet rs;
String open="jdbc:odbc:JavaTest";
46
cnn=DriverManager.getConnection(open,"Admin","");
int inp;
InputStreamReader(System.in));
inp=Integer.parseInt(d.readLine());
cs.setInt(1,inp);
rs= cs.executeQuery();
System.out.println("--------------------------------------------------------------------------------");
int code;
while (rs.next())
code= rs.getInt("SupplierCode");
name= rs.getString("SupplierName");
add= rs.getString("SupplierAddress");
Output:
47
Sup Code Sup Name Sup Address
-----------------------------------------------------------------------
Results:
Program results are verified for JDBC Bride concept
48