Final Assignment Java Akshad
Final Assignment Java Akshad
int i = 0, j = 0;
int k = l;
if (l < r) {
int m = (l + (r - 1)) / 2;
Mergesort(a, l, m);
Mergesort(a, m + 1, r);
merge(a, l, m, r);
}
for (int e : a) {
System.out.print(e + " ");
}
System.out.print("]\n");
for (int e : a) {
System.out.print(e + " ");
}
System.out.print("]\n");
}
Output :
Name : Akshad Jaiswal
Roll no. MC-I-47
}
}
Output :
Name : Akshad Jaiswal
Roll no. MC-I-47
Output :
Name : Akshad Jaiswal
Roll no. MC-I-47
System.out.println("enter a string");
StringBuilder s = new StringBuilder();
s.append(S.next());
Output :
Name : Akshad Jaiswal
Roll no. MC-I-47
5. Create a package mca1 which will have 2 classes as class Mathematics with a
method to add two numbers, add three float numbers and class Maximum with
a method to find maximum of three numbers.
// Mathematics.java class
package mca1;
// Maximum.java class
package mca1;
}
// importing mca1 package ..
import mca1.*;
Output :
Name : Akshad Jaiswal
Roll no. MC-I-47
6. Write a Java program to create Animal interface that contains run() and eat()
method. And implement methods in Dog and Cat class.
interface Animal {
void run();
void eat();
}
d.eat();
d.run();
c.eat();
c.run();
}
}
Output:
7. Write a Java program to create an abstract class Animal that contains non
abstract run() and abstract eat() method.Derive two classes Dog and Cat from it.
abstract class Animal_1 {
void run() {
System.out.println("Animal ran");
};
d.eat();
d.run();
c.eat();
c.run();
}
}
Output:
Name : Akshad Jaiswal
Roll no. MC-I-47
import java.util.Scanner;
while (true) {
System.out.println(
"Choose a number between 1 to 5 : ");
int x = S.nextInt();
switch (x) {
case 1:
try {
int a[] = { 1, 2, 3, 4, 5 };
System.out.println(a[7]);
} catch (Exception e) {
System.out.println("\n" + e + "\n");
}
break;
case 2:
try {
int i = 10 / 0;
System.out.println(x);
} catch (Exception e) {
System.out.println("\n" + e + "\n");
}
break;
case 3:
try {
Class.forName("Test");
} catch (Exception e) {
System.out.println("\n" + e + "\n");
}
break;
case 4:
try {
String w = null;
System.out.println(w.charAt(0));
} catch (Exception e) {
System.out.println("\n" + e + "\n");
}
break;
case 5:
try {
String c = "harsh";
System.out.println(c.charAt(10));
} catch (Exception e) {
System.out.println("\n" + e + "\n");
}
break;
default:
System.out.println("Invalid choice...");
Output:
9 User-defined exception.
import java.util.Scanner;
} catch (Exception e) {
System.out.println("Exception !! Age not above 18 ");
}
}
Output:
10 Write a Java program to create a Thread by extending the Thread class. And
print the name of currently executing thread.
public class qu_10_Thread {
System.out.println(Thread.currentThread().getName());
t1.start();
System.out.println(Thread.currentThread().getName());
}
}
}
}
Output:
Name : Akshad Jaiswal
Roll no. MC-I-47
System.out.println(Thread.currentThread().getName());
th.start();
System.out.println(Thread.currentThread().getName());
}
}
Output:
Name : Akshad Jaiswal
Roll no. MC-I-47
12 Write a multithreaded program to print even and odd numbers. Create two
threads, one thread prints even number and second thread prints odd number.
e1.start();
o1.start();
}
}
a_list.add(1);
a_list.add(2);
a_list.add(3);
a_list.add(4);
a_list.add(1);
a_list.add(5);
a_list.add(2);
a_list.add(3);
set.addAll(a_list);
a_list.clear();
a_list.addAll(set);
14 Write a code to sort a linked list and Reverse a linked list in java.
import java.util.*;
tree.addAll(list);
list.clear();
list.addAll(tree);
}
Output:
Name : Akshad Jaiswal
Roll no. MC-I-47
import java.io.*;
import java.util.*;
try {
int n;
Write.write(n);
} finally {
S.close();
Read.close();
Write.close();
} } }
Output:
Source file…
Execution ..
16. Write Java AWT code to accept Student information and display Student
details on the Screen.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public qu_16_Student_details() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 649, 484);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setName("Student Information");
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
qu_17_String_operations frame = new qu_17_String_operations();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public qu_17_String_operations() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 639, 450);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
String s3 = s1+s2;
textField_2.setText(s3);
}
});
btnNewButton.setBounds(96, 235, 106, 23);
contentPane.add(btnNewButton);
textField_2.setText(s1.substring(0, i)+s2+s1.substring(i+1));
}
});
btnNewButton_1.setBounds(247, 235, 89, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_1_2 = new JButton("Length");
btnNewButton_1_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField_2.setText(String.valueOf(textField.getText().length()));
}
});
btnNewButton_1_2.setBounds(393, 235, 89, 23);
contentPane.add(btnNewButton_1_2);
textField_2.setText(textField.getText().equals(textField_1.getText())?"The Given
strings are equal":"The given Strings are not equal");
}
});
btnNewButton_1_3.setBounds(324, 298, 89, 23);
contentPane.add(btnNewButton_1_3);
}
Output:
Concatenation :
Sub String :
Length :
Reverse :
Equality :
Name : Akshad Jaiswal
Roll no. MC-I-47
18 Write a Java code to Read, Insert, Update and Delete any record from the
database.(Employee table).
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
// Read
case 2:
q = "select id,fname,salary from Employee";
ps = con.prepareStatement(q);
ResultSet rs = ps.executeQuery();
System.out.println("\n");
while (rs.next()) {
System.out.println("Id: " + rs.getInt(1) + " Name: " +
rs.getString(2) + " Salary: " + rs.getInt(3));
}
System.out.println("\n");
break;
// Update
case 3:
System.out.println("id to update = ");
int id1 = S.nextInt();
System.out.println("new salary = ");
int sal1 = S.nextInt();
r = ps.executeUpdate();
System.out.println((r > 0) ? "Update was successfully preformed
changing employee id "+id1+"'s salary to "+sal1: "No Such id was Found");
break;
// Delete
case 4:
System.out.println("id to delete = ");
int id2 = S.nextInt();
case 5: System.exit(0);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
Output :
Database Schema :
Execution :
Name : Akshad Jaiswal
Roll no. MC-I-47
19 Write a Java Servlet Application for login page with proper validations.
Web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
id="WebApp_ID" version="5.0">
<welcome-file-list>
<welcome-file>newLogin.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.reserva.backend.newLoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/userLogin</url-pattern>
</servlet-mapping>
</web-app>
Login.html :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login page</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 97vh;
width: 100%;
}
#body {
display: flex;
flex-direction: column;
}
#submit,
#reset {
width: 100px;
align-self: center;
margin-bottom: 10px;
}
</style>
</head>
<body>
<form action="userLogin" method="post">
<div id="header">
<h1>Welcome To Login Page</h1>
</div>
<div id="body">
</form>
</body>
</html>
Login Servlet :
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/userLogin")
public class newLoginServlet extends HttpServlet {
}
}
Output :
Validations :
Login Fail :
Successful Login :
Result :
Name : Akshad Jaiswal
Roll no. MC-I-47
</body>
</html>
Register.jsp
<%@page import="java.util.*,java.text.*,java.sql.*"%>
<%
try{
Connection con;
Statement st;
ResultSet rs;
Class.forName("org.postgresql.Driver");
con=DriverManager.getConnection("jdbc:postgresql:te512","postgres","");
st=con.createStatement();
%>
Output :