Java - 2520
Java - 2520
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Practical 1
Java Generics
Generics add stability to your code by making more of your bugs detectable at compile time.
Generics enable types (classes and interfaces) to be parameters when defining classes,
interfaces and methods.
class Stack<E>
{
E a[];
int top; Stack()
{
}
class P1A
{
public static void main(String arg[])
{
Stack<Integer> si=new Stack<Integer>(); // creating a Stack that holds a set of Integer
objects
Page 1 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link](10);
[Link](20);
[Link](30);
[Link](1.2);
[Link](2.34);
[Link](56.789);
[Link](0.15);
[Link]("\nintegers...");
while([Link]())
{
[Link]([Link]());
[Link]([Link]);
}
[Link]("\ndoubles...");
while([Link]())
{
[Link]([Link]());
[Link]([Link]);
}
[Link]("\nstudents...");
while([Link]())
{
[Link]([Link]());
[Link]([Link]());
}
}
}
class Student
{
String name; int standard;
Student(String n,int s)
{
name=n; standard=s;
}
Page 2 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output:
Methods
Generic Methods are methods that introduce their own type parameters. This is similar to
declaring a generic type, but the type parameter's scope is limited to the method where it is
declared.
Page 3 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
You can use an upper bounded wildcard to relax the restrictions on a variable. For example,
say you want to write a method that works on List, List, and List; you can achieve this by
using an upper bounded wildcard.
Code:
import [Link].*;
// import [Link];
// import [Link];
/***/
public class UpperBound{
public static void main(String[] args) {
// TODO code application logic here
List<Integer>list1= [Link](3,5,6,8);
[Link]("Total sum is:" + sum(list1));
List<Double> list2 = [Link](3.1,5.1,7.1);
Page 4 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link]("Total sum is:"+sum(list2));
}
private static double sum(List<? extends Number> list)
{
double sum=0.0;
for(Number i : list)
{
sum+=[Link]();
}
return sum;
}}
Output:
[Link] Bounded
Wildcard Lower bounded wildcard restricts the unknown type to be a specific type or a super
type of that type. A lower bounded wildcard is expressed using the wildcard character ('?'),
following by the super keyword, followed by its lower bound:
Code:
import [Link].*;
import [Link];
import [Link];
class LowerBound {
public static void main(String[] args)
{
// Lower Bounded Integer List
List<Integer> list1 = [Link](2, 4, 6, 8);
// Integer list object is being passed
printOnlyIntegerClassorSuperClass(list1);
// Number list
List<Number> list2 = [Link](4, 5, 3, 5,8);
// Integer list object is being passed
printOnlyIntegerClassorSuperClass(list2);
}
public static void printOnlyIntegerClassorSuperClass(
List<? super Integer> list)
Page 5 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
{
[Link](list);
}
}
Output:
[Link] Wildcard
The unbounded wildcard type is specified using the wildcard character (?), for example, List.
This is called a list of unknown type. When the code is using methods in the generic class
that don't depend on the type parameter. For example, [Link] or [Link]. In fact, Class is
so often used because most of the methods in Class do not depend on T.
Code:
import [Link];
import [Link];
import [Link].*;
class UnBounded {
public static void main(String[] args)
{
// Integer List
List<Integer> list1 = [Link](1, 2, 3);
// Double list
List<Double> list2 = [Link](1.1, 2.2, 3.3); printlist(list1);
printlist(list2);
}
private static void printlist(List<?> list)
{
[Link](list);
}
}
Page 6 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output:
Page 7 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Practical 2
The List interface in Java extends the Collection interface and is part of the [Link]
package. It is used to store ordered collections where duplicates are allowed and elements
can be accessed by their index.
Maintains insertion order
Allows duplicate elements
Supports null elements (implementation dependent)
Supports bidirectional traversal using ListIterator
Aim1:Write a Java program to create List containing list of items of type String and use
for-each loop to print the items of the list.
Code:
import [Link].*;
public class p3_a{
public static void main(String args[]){
List<String>a1=new ArrayList<>();
[Link]("sakshi");
[Link]("shweta");
[Link]("sharad");
[Link]("bharti");
for(String item:a1){
[Link](item+" ");
}
}
}
Page 8 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Aim 2: Write a Java program to create List containing list of items and use ListIterator
interface to print items present in the list. Also print the list in reverse / backward
direction.
Code:
import [Link].*;
import [Link];
import [Link];
iterator=[Link]();
[Link]("Travalsal in forward direction");
while([Link]()){
[Link]([Link]());
}
[Link]();
[Link]("Travalsal in backward direction");
while([Link]()){
[Link]([Link]());
}
}
}
Output:
Page 9 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Practical 3
Set Interface:
In Java, the Set interface is a part of the Java Collection Framework, located in the [Link]
package. It represents a collection of unique elements, meaning it does not allow duplicate
values.
The set interface does not allow duplicate elements.
It can contain at most one null value except TreeSet implementation which does
not allow null.
The set interface provides efficient search, insertion, and deletion operations.
Aim 1 : Write a Java program to create a Set containing list of items of type String and
print the items in the list using Iterator interface. Also print the list in reverse /
backward direction.
Code:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class p4_1
{
public static void main(String[] args)
{
HashSet<Integer> evenNumSet = new LinkedHashSet<>(
[Link](4,16,8,102,24,30,46) );
[Link]("Unsorted Set: " + evenNumSet);
Page 10 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output :
Aim 2 : Write a Java program using Set interface containing list of items and perform
the following operations:
a. Add items in the set.
b. Insert items of one set in to other set.
c. Remove items from the set
d. Search the specified item in the set
Code:
import [Link].*;
public class p4_2
{
public static void main(String args[])
{
Set<Integer> numSet = new HashSet<Integer>();
[Link](13);
[Link]([Link](new Integer[] {1,6,4,7,3,9,8,2,12,11,20}));
[Link]("Original Set (numSet):" + numSet);
[Link]("\nNumSet Size:" + [Link]());
Page 11 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output:
Page 12 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Practical 4
Map Interface:
In Java, the Map Interface is part of the [Link] package and represents a collection of key-
value pairs, where Keys should be unique, but values can be duplicated.
It provides efficient retrieval, insertion, and deletion operations based on keys.
Keys should be unique, but values can be duplicated.
HashMap and LinkedHashMap allow one null key, and TreeMap does NOT allow null keys
(if natural ordering is used).
Use ConcurrentHashMap for thread-safe operations, or [Link]() to
make an existing map synchronized.
Aim 1: Write a Java program using Map interface containing list of items having keys
and associated values and perform the following operations:
a. Add items in the map.
b. Remove items from the map
c. Search specific key from the map
d. Get value of the specified key
e. Insert map elements of one map in to other map.
f. Print all keys and values of the map.
Code:
import [Link].*;
// a. ADD ITEMS
[Link](" a. ADDING ITEMS ");
[Link]("101", "Laptop");
[Link]("102", "Mouse");
[Link]("103", "Keyboard");
[Link]("After adding: " + items);
// b. REMOVE ITEM
[Link](" b. REMOVE ITEM ");
[Link]("102");
[Link]("After removing 102: " + items);
// c. SEARCH KEY
[Link](" c. SEARCH KEY ");
if([Link]("101")) {
Page 13 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link]("Key '101' FOUND");
} else {
[Link]("Key '101' NOT FOUND");
}
// d. GET VALUE
[Link]("d. GET VALUE ");
String value = [Link]("101");
[Link]("Value for key 101: " + value);
[Link](map2);
[Link]("After putAll(map2): " + items);
Page 14 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output:
Page 15 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Practical 5
Code:
interface MyLambda {
void display();
}
[Link]();
}
}
Output:
Code:
// Functional Interface
@FunctionalInterface
interface StringConcat {
String concat(String a, String b);
}
Page 16 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
// Lambda Expression
StringConcat sc = (s1, s2) -> s1 + s2;
Output:
Code:
import [Link];
Page 17 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output:
4. Write a Java program using Lambda Expression with multiple parameters to add
two numbers.
Code:
import [Link];
Output:
Page 18 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
5. Write a Java program using Lambda Expression to calculate the following:
Code:
import [Link];
Output:
Code:
import [Link];
Page 19 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Output:
6. Write a Java program using Lambda Expression with or without return keyword.
Code:
import [Link];
// Without return
Square s1 = x -> x * x;
[Link]("Square (without return): " + [Link](n));
// With return
Square s2 = (x) -> {
Page 20 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
return x * x;
};
[Link]("Square (with return): " + [Link](n));
}
}
Output:
Page 21 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Practical 6
Aim 1 : To design a JSP page to display and process a user registration form.
Code:
<form method="post">
Name: <input type="text" name="name"><br><br>
Email: <input type="email" name="email"><br><br>
Password: <input type="password" name="password"><br><br>
Gender:
<input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female<br><br>
Course:
<select name="course">
<option>Java</option>
<option>Web Development</option>
<option>Python</option>
</select><br><br>
<input type="submit" value="Register">
</form>
<%
if([Link]().equalsIgnoreCase("post")) {
%>
<h3>Registration Details</h3>
Name: <%= [Link]("name") %><br>
Email: <%= [Link]("email") %><br>
Gender: <%= [Link]("gender") %><br>
Course: <%= [Link]("course") %>
<%
}
%>
</body>
</html>
Page 22 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output:
Page 23 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Aim 2 : To design a JSP-based Loan Calculator that displays EMI, interest paid, and
remaining balance.
Code:
<body>
<h2>Loan Calculator</h2>
<form method="post">
Principal Amount:
<input type="number" name="principal" required><br><br>
Time (Years):
<input type="number" name="years" min="1" max="30" required><br><br>
<hr>
Page 24 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
<%
String pVal = [Link]("principal");
String yVal = [Link]("years");
[Link]("<h3>Loan Details</h3>");
[Link]("Interest Rate: <b>" + rate + "%</b><br>");
[Link]("Monthly EMI: <b>" + [Link](\"%.2f\", emi) +
"</b><br><br>");
[Link]("<table>");
[Link]("<tr><th>Month</th><th>Interest Paid</th><th>Remaining
Balance</th></tr>");
[Link]("<tr>");
[Link]("<td>" + i + "</td>");
[Link]("<td>" + [Link](\"%.2f\", interest) + "</td>");
[Link]("<td>" + [Link](\"%.2f\", balance) + "</td>");
[Link]("</tr>");
}
Page 25 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link]("</table>");
}
%>
</body>
</html>
Output:
a) 1 to 7 year at 5.35%
b) 8 to 15 year at 5.5%
c) c) 16 to 30 year at 5.75%
Page 26 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Aim 3 : To demonstrate JSP Declaration, Scriptlet, Directive, Expression, Header, and
Footer.
Code:
Header :
<h2>Welcome to JSP Demo</h2>
<hr>
Footer :
<hr>
<p>© 2025 JSP Lab</p>
<%
int a = 5, b = 6;
int sum = a + b;
%> <!-- Scriptlet -->
Page 27 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output:
Page 28 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Code:
<%@ page import="[Link].*" %>
<html>
<head>
<title>Student Database Application</title>
<style>
body {
font-family: Arial;
margin: 40px;
}
h1 {
color: #3b5ba9;
}
table {
border-collapse: collapse;
width: 60%;
}
th, td {
border: 1px solid #999;
padding: 8px;
text-align: center;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<form method="post">
Name:
<input type="text" name="name" required>
<br><br>
Course:
<input type="text" name="course" required>
<br><br>
<input type="submit" value="Save">
</form>
Page 29 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
<hr>
<%
String name = [Link]("name");
String course = [Link]("course");
/* JDBC Connection */
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/college",
"root",
""
);
/* Insert Record */
if(name != null && course != null){
PreparedStatement ps = [Link](
"INSERT INTO student(name, course) VALUES (?, ?)"
);
[Link](1, name);
[Link](2, course);
[Link]();
}
/* Display Records */
Statement st = [Link]();
ResultSet rs = [Link]("SELECT * FROM student");
[Link]("<h3>Student List</h3>");
[Link]("<table>");
[Link]("<tr><th>ID</th><th>Name</th><th>Course</th></tr>");
while([Link]()){
[Link]("<tr>");
[Link]("<td>" + [Link]("id") + "</td>");
[Link]("<td>" + [Link]("name") + "</td>");
[Link]("<td>" + [Link]("course") + "</td>");
[Link]("</tr>");
}
[Link]("</table>");
[Link]();
%>
</body>
</html>
Page 30 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output:
Page 31 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Practical 7
Aim 1 :To write a simple program using Spring Framework to print “Hello World”
by configuring and accessing a Spring bean through the IoC container.
Code:
[Link]
[Link]("Hello World");
[Link]
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
</beans>
Page 32 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link]
import [Link];
import [Link];
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
[Link]();
Output:
Page 33 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Aim 2 :To write a program using Spring Framework to demonstrate Dependency
Injection through Setter method using XML-based configuration.
Code:
[Link]
[Link] = name;
[Link]
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
</beans>
Page 34 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link]
import [Link];
import [Link];
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
Output:
Page 35 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Code :
[Link]
[Link] = name;
[Link]
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
</beans>
Page 36 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link]
import [Link];
import [Link];
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
[Link]();
Output :
Page 37 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Aim 4 :To write a program using Spring Framework to demonstrate Autowiring of
dependent objects using XML configuration.
Code:
[Link]
[Link]
[Link]
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
</beans>
[Link]
import [Link];
import [Link];
Page 38 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
Output:
Page 39 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Practical 8
Code:
public Employee() {
}
Page 40 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link]
<beans xmlns="[Link]
xmlns:xsi="[Link]
xmlns:aop="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]
[Link]
</beans>
[Link]
import [Link];
import [Link];
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
Page 41 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output:
Code:
public Employee() {
}
Page 42 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link]
<beans xmlns="[Link]
xmlns:xsi="[Link]
xmlns:aop="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]
[Link]
</beans>
[Link]
import [Link];
import [Link];
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
Page 43 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output:
Code:
public Employee() {
}
import [Link];
Page 44 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link]
<beans xmlns="[Link]
xmlns:xsi="[Link]
xmlns:aop="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]
[Link]
</aop:aspect>
</aop:config>
</beans>
[Link]
import [Link];
import [Link];
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
Page 45 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output:
Code:
[Link]
<beans xmlns="[Link]
xmlns:xsi="[Link]
xmlns:aop="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]
[Link]
<!-- Enable AOP -->
<aop:aspectj-autoproxy/>
Page 46 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
</beans>
[Link]
import [Link];
import [Link];
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
Output:
Page 47 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Aim 5 : To write a program to demonstrate Spring AOP – After Returning Advice.
Code:
[Link]
<beans xmlns="[Link]
xmlns:xsi="[Link]
xmlns:aop="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]
[Link]
<!-- Enable AOP -->
<aop:aspectj-autoproxy/>
<!-- Beans -->
<bean id="emp" class="Employee"/>
<bean id="aspect" class="LoggingAspect"/>
Page 48 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link]
import [Link];
import [Link];
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
Output:
Page 49 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Aim 5 : To write a program to demonstrate Pointcuts in Spring AOP.
Code:
[Link]
<beans xmlns="[Link]
xmlns:xsi="[Link]
xmlns:aop="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]
[Link]
Page 50 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link]
import [Link];
import [Link];
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
Output:
Page 51 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Practical 9
Aim : To write a program using Spring JDBC to perform database operations (Insert
and Display records).
Code :
[Link]
import [Link];
// Insert Record
public void insert(String name, String course) {
String sql = "INSERT INTO student(name, course) VALUES (?, ?)";
[Link](sql, name, course);
[Link]("Record inserted successfully");
}
// Display Records
public void display() {
[Link](
"SELECT * FROM student",
rs -> {
[Link](
[Link]("id") + " " +
[Link]("name") + " " +
[Link]("course")
);
}
);
}
}
Page 52 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
[Link]
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]
import [Link];
import [Link];
ApplicationContext ctx =
new ClassPathXmlApplicationContext("[Link]");
[Link]("Alice", "BCA");
[Link]("Bob", "BSc");
[Link]("Student Records:");
[Link]();
}
}
Page 53 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Output :
Page 54 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Practical 10
Aim : To write a program to create a simple Spring Boot application that prints a
message using RESTful Web Service.
Code :
[Link]
package [Link].chapter10;
import [Link];
import [Link];
@SpringBootApplication
public class Chapter10Application {
[Link]
package [Link].chapter10;
import [Link];
import [Link];
@RestController
public class HelloController {
@GetMapping("/hello")
public String message() {
return "Hello, Welcome to Spring Boot!";
}
}
Output :
Page 55 of 56
SSCMR
MCA Department MCA L12 Advanced Java Lab Roll No: 2520
Page 56 of 56