0% found this document useful (0 votes)
17 views

Final_AJP_LAB

The document contains multiple Java programs, including matrix multiplication, employee database management, arithmetic operations using applets, mouse event handling, user detail submission via servlets, cookie handling, and database operations. Each program is structured with appropriate classes and methods, demonstrating various Java concepts such as object-oriented programming, event handling, and servlet functionality. The document also includes sample outputs for each program, illustrating their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Final_AJP_LAB

The document contains multiple Java programs, including matrix multiplication, employee database management, arithmetic operations using applets, mouse event handling, user detail submission via servlets, cookie handling, and database operations. Each program is structured with appropriate classes and methods, demonstrating various Java concepts such as object-oriented programming, event handling, and servlet functionality. The document also includes sample outputs for each program, illustrating their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

1.

Write a Java Program to read two matrices A(M x N) and B(X x Y)


and find the product matrix C(M x Y).

import java.util.*;

public class MatrixMultiplication {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Input dimensions of matrix A

System.out.print("Enter the number of rows for matrix A (M): ");

int m = sc.nextInt();

System.out.print("Enter the number of columns for matrix A (N): ");

int n = sc.nextInt();

// Input dimensions of matrix B

System.out.print("Enter the number of rows for matrix B (X): ");

int x = sc.nextInt();

System.out.print("Enter the number of columns for matrix B (Y): ");

int y = sc.nextInt();

// Check if multiplication is possible

if (n != x) {

System.out.println("Matrix multiplication not possible. Number of columns of A


must equal number of rows of B.");

return;

int[][] A = new int[m][n];

int[][] B = new int[x][y];

// Input matrix A

System.out.println("Enter the elements of matrix A:");


for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

A[i][j] = sc.nextInt();

// Input matrix B

System.out.println("Enter the elements of matrix B:");

for (int i = 0; i < x; i++) {

for (int j = 0; j < y; j++) {

B[i][j] = sc.nextInt();

// Print matrix A

System.out.println("Matrix A:");

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

System.out.print(A[i][j] + " ");

System.out.println();

System.out.println("Matrix B:");

for (int i = 0; i < x; i++) {

for (int j = 0; j < y; j++) {

System.out.print(B[i][j] + " ");

System.out.println();
}

int[][] C = new int[m][y];

// Compute matrix multiplication

for (int i = 0; i < m; i++) {

for (int j = 0; j < y; j++) {

C[i][j] = 0; // Initialize the element

for (int k = 0; k < n; k++) {

C[i][j] += A[i][k] * B[k][j];

System.out.println("The product matrix C is:");

for (int i = 0; i < m; i++) {

for (int j = 0; j < y; j++) {

System.out.print(C[i][j] + " ");

System.out.println();

}
OUTPUT:

Enter the number of rows for matrix A (M): 2

Enter the number of columns for matrix A (N): 3

Enter the number of rows for matrix B (X): 3

Enter the number of columns for matrix B (Y): 2

Enter the elements of matrix A:

10 20 30 40 50 60

Enter the elements of matrix B:

11 22 33 44 55 66

Matrix A:

10 20 30

40 50 60

Matrix B:

11 22

33 44

55 66

The product matrix C is:

2420 3080

5390 7040
2. An educational institution wishes to maintain a database of its
employees. The database is divided into no of classes whose
hierarchical relationships are shown in fig. Write a Java program
to specify all the classes and define methods to read and display
data of all the classes
Department
dname

Staff
sname, experience,salary

Teaching Non-Teaching
subject position

import java.util.Scanner;

class Department {

String dname;

public void readDepartment(Scanner sc) {

System.out.print("Enter department name: ");

dname = sc.nextLine();

public void displayDepartment() {

System.out.print(dname + "\t\t");

class Staff extends Department {

String sname;
int experience;

double salary;

public void readStaff(Scanner sc) {

super.readDepartment(sc);

System.out.print("Enter staff name: ");

sname = sc.nextLine();

System.out.print("Enter experience (in years): ");

experience = sc.nextInt();

System.out.print("Enter salary: ");

salary = sc.nextDouble();

sc.nextLine();

public void displayStaff() {

super.displayDepartment();

System.out.print(sname + "\t\t" + experience + "\t\t" + salary + "\t\t");

class Teaching extends Staff {

String subject;

public void readTeaching(Scanner sc) {

super.readStaff(sc);

System.out.print("Enter subject: ");

subject = sc.nextLine();

public void displayTeaching() {

super.displayStaff();
System.out.println(subject);

class NonTeaching extends Staff {

String position;

public void readNonTeaching(Scanner sc) {

super.readStaff(sc);

System.out.print("Enter position: ");

position = sc.nextLine();

public void displayNonTeaching() {

super.displayStaff();

System.out.println(position);

public class EducationalInstitution {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of teaching staff: ");

int numTeaching = sc.nextInt();

sc.nextLine();

Teaching[] teachingStaffArray = new Teaching[numTeaching];

for (int i = 0; i < numTeaching; i++) {

System.out.println("\nEnter details for Teaching Staff " + (i + 1) + ":");

teachingStaffArray[i] = new Teaching();


teachingStaffArray[i].readTeaching(sc);

System.out.print("\nEnter the number of non-teaching staff: ");

int numNonTeaching = sc.nextInt();

sc.nextLine();

NonTeaching[] nonTeachingStaffArray = new NonTeaching[numNonTeaching];

for (int i = 0; i < numNonTeaching; i++) {

System.out.println("\nEnter details for Non-Teaching Staff " + (i + 1) + ":");

nonTeachingStaffArray[i] = new NonTeaching();

nonTeachingStaffArray[i].readNonTeaching(sc);

System.out.println("\nDetails of Teaching Staff:");

System.out.println("Department\tStaff Name\tExperience\tSalary\t\tSubject");

System.out.println("--------------------------------------------------------------------------");

for (Teaching teachingStaff : teachingStaffArray) {

teachingStaff.displayTeaching();

// Displaying all non-teaching staff details

System.out.println("\nDetails of Non-Teaching Staff:");

System.out.println("Department\tStaff Name\tExperience\tSalary\t\tPosition");

System.out.println("--------------------------------------------------------------------------");

for (NonTeaching nonTeachingStaff : nonTeachingStaffArray) {

nonTeachingStaff.displayNonTeaching();
}

OUTPUT

Enter the number of teaching staff: 2

Enter details for Teaching Staff 1

Enter department name: ISE

Enter staff name: Abc

Enter experience (in years): 2

Enter salary: 50000

Enter subject: Java

Enter details for Teaching Staff 2:

Enter department name: ISE

Enter staff name: Mno

Enter experience (in years): 5

Enter salary: 50000

Enter subject: AJP

Enter the number of non-teaching staff: 2

Enter details for Non-Teaching Staff 1:

Enter department name: Xyz

Enter staff name: Xyz

Enter experience (in years): 3

Enter salary: 20000

Enter position: Assistant

Enter details for Non-Teaching Staff 2:

Enter department name: ISE

Enter staff name: Pqr


Enter experience (in years): 4

Enter salary: 20000

Enter position: LabAsst

Details of Teaching Staff:

Department Staff Name Experience Salary Subject

--------------------------------------------------------------------------

ISE Abc 2 50000.0 Java

ISE Mno 5 50000.0 AJP

Details of Non-Teaching Staff:

Department Staff Name Experience Salary Position

--------------------------------------------------------------------------

Xyz Xyz 3 20000.0 Assistant

ISE Pqr 4 20000.0 LabAsst


3. Develop a Java applet which performs arithmetic operations on
two numbers entered by the user and present the results to the
user. (by handling action events on buttons)

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class action extends Applet implements ActionListener{

TextField t1,t2,t3;

Label l1,l2,l3;

Button b1,b2,b3,b4;

public void init()

t1=new TextField(10);

t2=new TextField(10);

t3=new TextField(20);

l1=new Label("1st Number:");

l2=new Label("2nd Number:");

l3=new Label("Result:");

b1=new Button("Add");

b2=new Button("Subtract");

b3=new Button("Multiply");

b4=new Button("Divide");

add(l1);

add(t1);

add(l2);

add(t2);
add(b1);

add(b2);

add(b3);

add(b4);

add(l3);

add(t3);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

public void actionPerformed(ActionEvent ae)

float a,b,res=0;

a=Float.parseFloat(t1.getText());

b=Float.parseFloat(t2.getText());

String cmd=ae.getActionCommand();

if(cmd.equals("Add"))

res=a+b;

else if(cmd.equals("Subtract"))

res=a-b;

else if(cmd.equals("Multiply"))

{
res=a*b;

else

res=a/b;

String msg=String.valueOf(res);

t3.setText(msg);

repaint();

public void paint(Graphics g)

}
OUTPUT:
4. Develop a Java applet which handles mouse events by displaying
the respective event names and co-ordinates.

import java.awt.event.*;

import java.applet.*;

import java.awt.*;

public class mouseevent extends Applet implements


MouseListener,MouseMotionListener

String msg=" ";

int mouseX=0, mouseY=0;

public void init()

addMouseListener(this);

addMouseMotionListener(this);

public void mouseClicked(MouseEvent me)

mouseX=10;

mouseY=20;

msg="Mouse is clicked";

repaint();

public void mouseEntered(MouseEvent me)

mouseX=10;

mouseY=20;
msg="Mouse entered";

repaint();

public void mouseExited(MouseEvent me)

mouseX=10;

mouseY=20;

msg="Mouse exited";

repaint();

public void mousePressed(MouseEvent me)

mouseX=me.getX();

mouseY=me.getY();

msg="Down";

repaint();

public void mouseReleased(MouseEvent me)

mouseX=me.getX();

mouseY=me.getY();

msg="Up";

repaint();

public void mouseMoved(MouseEvent me)

mouseX=me.getX();
mouseY=me.getY();

showStatus("Moving mouse at"+mouseX+" ,"+mouseY);

repaint();

public void mouseDragged(MouseEvent me)

mouseX=me.getX();

mouseY=me.getY();

msg="*";

showStatus("Dragging mouse at"+mouseX+" ,"+mouseY);

repaint();

public void paint(Graphics g)

g.drawString(msg,mouseX,mouseY);

}
OUTPUT:
5. Develop a Java servlet to accept user details entered through a
web form such as name, gender, password, feedback (through a
text area) and job category (through drop down list). Display the
same with appropriate messages.
User.html

<html>

<head>

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>

<form action="./UserDetails" method="get">

Name: <input type="text" name="name"><br><br>

Password: <input type="password" name="pwd"><br><br>

Gender:

<input type="radio" name="gender" value="male" checked>Male<br>

<input type="radio" name="gender" value="female" checked>Female<br><br>

Feedback: <textarea name="feedback" rows="8" cols="30"></textarea><br><br>

Job Category:

<select name="jobcat" multiple>

<option value="Science">Science</option>

<option value="Maths">Maths</option>

<option value="English">English</option>

<option value="Social">Social</option>

</select><br><br>

<input type="submit" value="submit">

</form>
</body>

</html>

UserDetails.java

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;

public class UserDetails extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

String n = request.getParameter("name");

String p = request.getParameter("pwd");

String g = request.getParameter("gender");

String f = request.getParameter("feedback");

String[] j = request.getParameterValues("jobcat");

try {

out.println("<html>");

out.println("<body>");

out.println("<p>Your name is " + n + "<br>");

out.println("Your Password is " + p + "<br>");


out.println("Your Gender is " + g + "<br>");

out.println("Your Feedback is " + f + "<br>");

out.println("Your Job Category is ");

for (int i = 0; i < j.length; i++) {

out.println(j[i]);

out.println("</body>");

out.println("</html>");

} finally {

out.close();

}
OUTPUT:
6. Develop a Java servlet to read and write cookies.

Cookies.html

<html>

<head><title>Cookies Handling</title></head>

<body>

<form action="./Cookie1" method="get">

UserName:- <input type="text" name="name"><br><br>

Password:- <input type="password" name="pwd"><br><br>

<input type="submit" value="submit">

</form>

</body>

</html>

Cookie1.java

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;

public class Cookie1 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

PrintWriter out = response.getWriter();


response.setContentType("text/html");

String S = request.getParameter("name");

String V = request.getParameter("pwd");

Cookie myCookie = new Cookie(S, V);

response.addCookie(myCookie);

out.println("<html><body>");

Cookie[] myCookie1 = request.getCookies();

if (myCookie1 == null) {

out.println("No Cookies");

} else {

int i;

for (i = 0; i < myCookie1.length; i++) {

String name = myCookie1[i].getName();

String value = myCookie1[i].getValue();

out.println(name + "=" + value + "<br>");

out.println("</body></html>");

}
OUTPUT:
7. Write a Java program to create a database table, insert records in it, and display
them.

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Database extends HttpServlet {

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

String url = "jdbc:mysql://localhost:3306/customerdb";

String uid = "root";

String pwd = "root";

Connection db = null;
try {

Class.forName("com.mysql.jdbc.Driver");

db = DriverManager.getConnection(url, uid, pwd);

} catch (ClassNotFoundException e) {

out.println("Class not found " + e);

System.exit(0);

} catch (SQLException e) {

out.println("SQL Exception " + e);

System.exit(0);

try {

// Create table

String query = "create table Emp(Name varchar(15), id int)";

Statement st = db.createStatement();

st.executeUpdate(query);

out.println("Table created<br>");

// Insert data

String query1 = "insert into pooja values('xyz', 1234567891)";

st.executeUpdate(query1);

out.println("Data inserted into table<br>");

// Select and display data

String query2 = "select * from Emp";

ResultSet rs = st.executeQuery(query2);

if (!rs.next()) {
out.println("No Records found");

} else {

out.println("Name\tID<br>");

do {

String name = rs.getString("Name");

int id = rs.getInt("ID");

out.println(name + "\t" + id + "<br>");

} while (rs.next());

} catch (SQLException e) {

out.println("SQL Exception " + e);

OUTPUT:

Table created

Data inserted into table

Name ID

xyz 1234567891
8. Write a JSP program to print in table format multiplication table
of a number entered by user.

mult.jsp:

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>multiplication table</title>

</head>

<table border="1">

<body>

<% String s=request.getParameter("no");

int n=Integer.parseInt(s);%>

<% for(int i=1;i<=10;i++)

{%>

<tr>

<th>

<%out.println(n+"x"+i);%>

</th>

<th>

<%out.println("=");%>

</th>

<th>

<%out.println(n*i);%>

</th>

</tr>
<%}%>

</table>

</body>

</html>

mult.html

<html>

<head>

<title>TODO supply a title</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<form action="mult.jsp" method="get">

<input type="text" name="no">

<input type="submit">

</form>

</body>

</html>
OUTPUT:
9. Write a JSP program to print Fibonacci series for a number
entered by the user.

Fibonacci.jsp

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<% String s=request.getParameter("no");

int n=Integer.parseInt(s);

int i=1,f1=0,f2=1,f3;

while(i<=n)

out.println(f1);

f3=f1+f2;

f1=f2;

f2=f3;

i++;

}%>

</body>

</html>
FiboSeries.html

<html>

<head>

<title>Fibo Series</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<form action="fibo.jsp" method="get">

number:<input type="text" name="no">

<input type="submit">

</form>

</body>

</html>
OUTPUT:
10.Write a JSP program to:

a) Demonstrate error page in JSP.

ErrorDemo.html

<html>

<head>

<title>error handling</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<form action="Calculation.jsp" method="get">

1st number:<input type="number" name="no1">

2nd number:<input type="number" name="no2">

<input type="submit" value="calculate">

</form>

</body>

</html>

Calculation.jsp
<!DOCTYPE html>

<html>

<head>

<title>Calculation Page</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">


</head>

<body>

<%@page errorPage=”ErrorHandling.jsp” %>

<% int num1 = Integer.parseInt(request.getParameter("no1"));

int num2 = Integer.parseInt(request.getParameter("no2"));

int result = num1 / num2;

%>

<p>Result: <%= result %> </p>

</body>

</html>

ErrorHandling.jsp
<html>

<head> <title>Error Handling Page</title>

</head>

<body>

<h1> Exception </h1>

<%@page isErrorPage="true" %>

<%=exception%>

</body>

</html>

</html>

OUTPUT:
b) Display HTTP request headers in table form on a web page.
<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>request header</title>

</head>

<body>

<table border="1">

<%@page import="java.util.*"%>

<%Enumeration e=request.getHeaderNames();

<tr>

<th>Header Name</th>

<th>Header Value</th>

</tr>

while(e.hasMoreElements())

String Name=(String)e.nextElement();

String value=request.getHeader(Name);%>

<tr>

<td>

<%out.println(Name);%>

</td>

<td>

<%out.println(value);%>

</td>

</tr>
<%}%>

</table>

</body>

</html>

OUTPUT:

You might also like