0% found this document useful (0 votes)
12 views21 pages

Java Fullstack Development Lab Programs

The document provides instructions for creating Java programs that demonstrate the use of Generic Classes, Wildcards in Java Generics, and a Loan Calculator using JSP. It includes step-by-step procedures for setting up projects in NetBeans IDE, writing code, and running the applications. Additionally, it outlines the structure and expected output for each program, including the use of JSP elements such as declarations, scriptlets, directives, and expressions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views21 pages

Java Fullstack Development Lab Programs

The document provides instructions for creating Java programs that demonstrate the use of Generic Classes, Wildcards in Java Generics, and a Loan Calculator using JSP. It includes step-by-step procedures for setting up projects in NetBeans IDE, writing code, and running the applications. Additionally, it outlines the structure and expected output for each program, including the use of JSP elements such as declarations, scriptlets, directives, and expressions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1 . Write a Java Program to demonstrate a Generic Class , Gene ric Methods.

Aim

To create and execute a Java program in NetBeans IDE to demonstrate the use of a Generic
Class and Generic Methods.

Procedure

1. Open NetBeans IDE.


2. Click on File → New Project.
3. Select:
o Category: Java
o Project: Java Application
o Click Next.
4. Enter the Project Name (e.g., GenericsDemo).
5. Choose the project location and click Finish.
6. In the Projects window, expand:
o Source Packages
o Open the default package or create a new package.
7. Create a new Java Class:
o Right-click on the package
o Select New → Java Class
o Enter class name (e.g., GenericsDemo)
o Click Finish
8. Define a Generic Class:
o Use angle brackets <T> after the class name.
o Add a variable and method to store and display the value.
9. Define a Generic Method:
o Declare the type parameter <T> before the return type.
o Write a method to print or process the value.
10. In the main() method:
o Create objects of the generic class using different data types (Integer, String,
Double).
o Call the generic method with different data types.
11. Save the file.
12. Click Run → Run Project
(or press F6).
13. Observe the output in the Output Window at the bottom of NetBeans.

Program:

// [Link]
public class GenericDemo {
// ----------- Generic Class -----------
// A simple generic class with one type parameter <T>
static class Box<T> {
private T value;

public void setValue(T value) {


[Link] = value;
}

public T getValue() {
return value;
}

// ----------- Generic Method -----------


// A method that can accept any type <U>
public <U> void display(U item) {
[Link]("Displaying item: " + item);
}
}

public static void main(String[] args) {

// Using generic class with Integer


Box<Integer> intBox = new Box<>();
[Link](100);
[Link]("Integer Value: " + [Link]());

// Using generic class with String


Box<String> strBox = new Box<>();
[Link]("Hello Generics");
[Link]("String Value: " + [Link]());

// Calling generic method


[Link]("This is a generic method");
[Link](55.75);
}
}
Output:
Integer Value: 100
String Value: Hello Generics
Displaying item: This is a generic method
Displaying item: 55.75
2 . Write a Java Program to demonstrate Wildcards in Java Generics.

Aim

To create and execute a Java program in NetBeans IDE to demonstrate the use of Wildcards in
Java Generics.
Procedure

1. Open NetBeans IDE.


2. Click on File → New Project.
3. Select:
o Category: Java
o Project: Java Application
o Click Next.
4. Enter the Project Name (e.g., WildcardsDemo).
5. Click Finish.
6. In the Projects window:
o Expand Source Packages.
o Right-click the package → Select New → Java Class.
o Name the class WildcardsDemo.
7. Import required packages:
8. import [Link].*;
9. Create methods demonstrating:
o Unbounded wildcard (<?>)
o Upper bounded wildcard (<? extends Number>)
o Lower bounded wildcard (<? super Integer>)
10. In the main() method:
o Create lists of Integer, Double, and Number.
o Pass them to the respective wildcard methods.
11. Save the file.
12. Click Run → Run Project (or press F6).
13. Observe the output in the Output Window.

Program:

// [Link]
import [Link].*;

public class WildcardDemo {

// Method using Unbounded Wildcard (?)


public static void printList(List<?> list) {
[Link]("Printing List (Unbounded Wildcard):");
for (Object obj : list) {
[Link](obj);
}
}

// Method using Upper Bounded Wildcard (? extends Number)


public static void sumNumbers(List<? extends Number> list) {
double sum = 0;
for (Number n : list) {
sum += [Link]();
}
[Link]("Sum (Upper Bound ? extends Number): " + sum);
}

// Method using Lower Bounded Wildcard (? super Integer)


public static void addIntegers(List<? super Integer> list) {
[Link](100);
[Link](200);
[Link]("List after adding integers (Lower Bound ? super Integer):");
[Link](list);
}

public static void main(String[] args) {

List<String> stringList = [Link]("Apple", "Banana", "Cherry");


List<Integer> intList = [Link](10, 20, 30);
List<Double> doubleList = [Link](2.5, 3.5, 4.5);
List<Object> objectList = new ArrayList<>();

// Demonstrate Unbounded Wildcard


printList(stringList);

// Demonstrate Upper Bounded Wildcard


sumNumbers(intList);
sumNumbers(doubleList);

// Demonstrate Lower Bounded Wildcard


addIntegers(objectList);
}
}

Output:
Printing List (Unbounded Wildcard):
Apple
Banana
Cherry
Sum (Upper Bound ? extends Number): 60.0
Sum (Upper Bound ? extends Number): 10.5
List after adding integers (Lower Bound ? super Integer):
[100, 200]

3 . Design loan calculator using JSP which accepts Period of Time (in years) and Principal Loan

Amount. Display the payment amount for each loan and then list the loan balance and interest

paid for each payment over the term of the loan for the following time period and interest rate:

a. 1 to 7 year at 5.35%
b. 8 to 15 year at 5.5%

c. 16 to 30 year at 5.75%

Aim

To design and develop a Loan Calculator using JSP in NetBeans that:

 Accepts:
o Principal Loan Amount
o Period of Time (in years)
 Applies interest rate based on the loan period:
o 1–7 years → 5.35%
o 8–15 years → 5.5%
o 16–30 years → 5.75%
 Displays:
o Monthly payment amount (EMI)
o Loan balance after each payment
o Interest paid for each payment over the loan term

Procedure

Step 1: Create Web Application

1. Open NetBeans IDE.


2. Click File → New Project.
3. Select:
o Category: Java Web
o Project: Web Application
4. Click Next.
5. Enter Project Name: LoanCalculatorSlab
6. Select Apache Tomcat Server.
7. Click Finish.

Step 2: Create JSP Page

1. Expand Web Pages.


2. Right-click → New → JSP.
3. Name the file: [Link].
4. Click Finish.

Step 3: Design Input Form

Add form fields to accept:


 Principal Amount
 Loan Period (Years)

Step 4: Write JSP Logic

1. Read user inputs using [Link]().


2. Convert values to numeric types.
3. Apply interest rate based on years using if-else.
4. Calculate:
o Monthly interest rate
o Total months
o EMI
5. Use a loop to calculate:
o Monthly interest
o Principal paid
o Remaining balance
6. Display results in tabular form.

Step 5: Run the Project

1. Right-click Project → Run.


2. Enter principal and years.
3. Click Calculate.
4. Observe EMI and amortization table in browser.
Program:
<%@page import="[Link]"%>
<%@page import="[Link].*"%>
<!DOCTYPE html>
<html>
<head>
<title>Loan Calculator</title>
<style>
body { font-family: Arial; background:#f2f2f2; padding:20px; }
.box { background:white; padding:20px; border-radius:8px; width:70%; margin:auto; }
table { border-collapse: collapse; width:100%; margin-top:20px; }
th, td { border:1px solid #555; padding:8px; text-align:center; }
th { background:#ddd; }
</style>
</head>
<body>

<div class="box">
<h2>Loan Calculator</h2>

<form method="post">
<label>Principal Loan Amount:</label><br>
<input type="text" name="principal" required><br><br>
<label>Period of Time (Years):</label><br>
<input type="text" name="years" required><br><br>

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


</form>
</div>

<%
if ([Link]().equalsIgnoreCase("POST")) {

double principal = [Link]([Link]("principal"));


int years = [Link]([Link]("years"));
double rate = 0;

// Determine interest rate


if (years >= 1 && years <= 7) rate = 5.35;
else if (years >= 8 && years <= 15) rate = 5.50;
else if (years >= 16 && years <= 30) rate = 5.75;

double monthlyRate = rate / 1200; // Convert % to monthly


int months = years * 12;

// Monthly Payment Formula


double monthlyPayment = (principal * monthlyRate) /
(1 - [Link](1 + monthlyRate, -months));

DecimalFormat df = new DecimalFormat("0.00");


%>

<div class="box">
<h2>Loan Summary</h2>
<p><strong>Principal:</strong> ₹<%= [Link](principal) %></p>
<p><strong>Years:</strong> <%= years %></p>
<p><strong>Interest Rate:</strong> <%= rate %> %</p>
<p><strong>Monthly Payment:</strong> ₹<%= [Link](monthlyPayment) %></p>

<h3>Payment Schedule</h3>
<table>
<tr>
<th>Payment #</th>
<th>Interest Paid</th>
<th>Principal Paid</th>
<th>Remaining Balance</th>
</tr>

<%
double balance = principal;

for (int i = 1; i <= months; i++) {


double interest = balance * monthlyRate;
double principalPaid = monthlyPayment - interest;
balance -= principalPaid;

if (balance < 0) balance = 0;


%>
<tr>
<td><%= i %></td>
<td>₹<%= [Link](interest) %></td>
<td>₹<%= [Link](principalPaid) %></td>
<td>₹<%= [Link](balance) %></td>
</tr>
<%
}
%>
</table>
</div>

<%
}
%>

</body>
</html>

Output:

Loan Calculator
Principal Loan Amount:

Period of Time (Years):

Calculate Loan

Loan Summary
Principal: ?2000.00

Years: 2

Interest Rate: 5.35 %

Monthly Payment: ?88.06


Payment Schedule

Payment # Interest Paid Principal Paid Remaining Balance

1 ?8.92 ?79.14 ?1920.86

2 ?8.56 ?79.49 ?1841.37

3 ?8.21 ?79.85 ?1761.52

4 ?7.85 ?80.20 ?1681.32

5 ?7.50 ?80.56 ?1600.76

6 ?7.14 ?80.92 ?1519.84

7 ?6.78 ?81.28 ?1438.56

8 ?6.41 ?81.64 ?1356.91

9 ?6.05 ?82.01 ?1274.91

10 ?5.68 ?82.37 ?1192.53

11 ?5.32 ?82.74 ?1109.79

12 ?4.95 ?83.11 ?1026.68

13 ?4.58 ?83.48 ?943.20


14 ?4.21 ?83.85 ?859.35

15 ?3.83 ?84.23 ?775.13

16 ?3.46 ?84.60 ?690.53

17 ?3.08 ?84.98 ?605.55

18 ?2.70 ?85.36 ?520.19

19 ?2.32 ?85.74 ?434.45

20 ?1.94 ?86.12 ?348.34

21 ?1.55 ?86.50 ?261.83

22 ?1.17 ?86.89 ?174.94

23 ?0.78 ?87.28 ?87.67

24 ?0.39 ?87.67 ?0.00

4 . Write a JSP program that demonst rates the use of JSP declaration, scriptlet,
directives,expression, header and footer.

Aim

To write and execute a JSP program in NetBeans that demonstrates the use of:

 JSP Declaration
 JSP Scriptlet
 JSP Directives
 JSP Expression
 JSP Header and Footer using include directive

Procedure

Step 1: Create Web Application

1. Open NetBeans IDE.


2. Click File → New Project.
3. Select:
o Category: Java Web
o Project: Web Application
4. Click Next.
5. Enter Project Name: JSPElementsDemo.
6. Select Apache Tomcat Server.
7. Click Finish.

Step 2: Create Header JSP File

1. Right-click Web Pages → New → JSP.


2. Name it: [Link].
3. Add the following content:

<h2 style="color:blue;">Welcome to JSP Demonstration</h2>


<hr>

Step 3: Create Footer JSP File

1. Right-click Web Pages → New → JSP.


2. Name it: [Link].
3. Add the following content:

<hr>
<h4 style="color:gray;">© 2026 JSP Demo Program</h4>

Step 4: Create Main JSP File

1. Right-click Web Pages → New → JSP.


2. Name it: [Link].
Step 5: Write JSP Code in [Link]

Step 6: Run the Project

1. Right-click Project → Run.


2. Browser opens automatically.
3. Output displays:
o Header content
o Scriptlet values
o Expression output
o Footer content

Program1:

Step 1: Create [Link]


<!-- [Link] -->
<div style="background:#2d89ef; padding:15px; color:white; text-
align:center;">
<h2>My JSP Demonstration Page</h2>
</div>

Program2:

Step 2: Create [Link]


<!-- [Link] -->
<div style="background:#333; padding:10px; color:white; text-
align:center; margin-top:20px;">
<p>JSP Footer Section © 2025</p>
</div>

Program3:

Step 3: Create [Link] (Main JSP File)


<%@page import="[Link]"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<title>JSP Elements Demo</title>
</head>
<body>
<!-- Header using include directive -->
<%@ include file="[Link]" %>

<%
// JSP SCRIPTLET: Executes Java code within the JSP
int a = 10, b = 20;
int sum = a + b;
%>

<%!
// JSP DECLARATION: Declares methods or variables at servlet
class level
public String greetUser(String name) {
return "Welcome " + name + "! This is a JSP Declaration
Method.";
}
%>

<div style="padding:20px;">

<h3>JSP Elements Demonstration</h3>

<p>
<strong>JSP Expression:</strong><br>
The sum of 10 and 20 is:
<b><%= sum %></b>
</p>

<p>
<strong>Current Date (JSP Expression):</strong><br>
<%= new Date() %>
</p>

<p>
<strong>Using JSP Declaration Method:</strong><br>
<%= greetUser("Student") %>
</p>

</div>

<!-- Footer using include directive -->


<%@ include file="[Link]" %>

</body>
</html>

Output:
JSP Elements Demonstration

JSP Expression:
The sum of 10 and 20 is: 30

Current Date (JSP Expression):


Tue Nov 18 15:01:10 IST 2025

Using JSP Declaration Method:


Welcome Student! This is a JSP Declaration Method.

5 . Write a program to demonstrate dependency injection via setter method.

Aim

To write and execute a Java program in NetBeans that demonstrates Dependency Injection (DI)
using Setter Method.

Procedure

Step 1: Create Java Project

1. Open NetBeans IDE.


2. Click File → New Project.
3. Select:
o Category: Java
o Project: Java Application
4. Click Next.
5. Enter Project Name: SetterInjectionDemo.
6. Click Finish.

Step 2: Create Dependency Class

1. Right-click Source Packages → New → Java Class.


2. Name the class Engine.
3. Write a method inside it.

Step 3: Create Dependent Class

1. Right-click Source Packages → New → Java Class.


2. Name the class Car.
3. Create:
o A private Engine reference variable.
o A setter method to inject the dependency.
o A method to use the injected object.

Step 4: Write Main Class

1. Open [Link].
2. In the main() method:
o Create objects of Car and Engine.
o Inject the dependency using setter method.
o Call the method to display result.

Step 5: Run the Program

1. Right-click Project → Run (or press F6).


2. Observe output in Output window.

Program1:

Create [Link] (Interface)


package dependency;

public interface Service {


void sendMessage(String message);
}
Program2:

Create [Link] (Implementation)


package dependency;

public class EmailService implements Service {

@Override
public void sendMessage(String message) {
[Link]("Email Service: " + message);
}
}

Program3:
Create [Link] (Class Using Setter Injection)
package dependency;

public class Customer {

private Service service; // Dependency

// Setter Method for Dependency Injection


public void setService(Service service) {
[Link] = service;
}

// Method that uses the injected service


public void processOrder() {
[Link]("Order processing started!");
}
}
Program4:

Create [Link] (Test Program)


package dependency;

public class MainApp {

public static void main(String[] args) {

// Create objects
Customer customer = new Customer();
Service emailService = new EmailService();

// Inject dependency using setter method


[Link](emailService);

// Use the injected service


[Link]();
}
}

Output:

Email Service: Order processing started!

BUILD SUCCESSFUL (total time: 0 seconds)

6 . Write a program to demonstrate dependency injection via Constructor.


Aim

To write and execute a Java program in NetBeans that demonstrates Dependency Injection (DI)
using Constructor Injection.

Procedure

Step 1: Create Java Project

1. Open NetBeans IDE.


2. Click File → New Project.
3. Select:
o Category: Java
o Project: Java Application
4. Click Next.
5. Enter Project Name: ConstructorInjectionDemo.
6. Click Finish.

Step 2: Create Dependency Class

1. Right-click Source Packages → New → Java Class.


2. Name the class Engine.
3. Add a method inside it.

Step 3: Create Dependent Class

1. Right-click Source Packages → New → Java Class.


2. Name the class Car.
3. Create:
o A private Engine reference variable.
o A constructor that accepts Engine as a parameter.
o A method to use the injected object.

Step 4: Write Main Class

1. Open [Link].
2. In the main() method:
o Create an object of Engine.
o Pass it to the Car constructor.
o Call the method to display output.

Step 5: Run the Program

1. Right-click Project → Run (or press F6).


2. Observe output in Output window.
Program1:

[Link] (Interface)
package aopdemo;

public interface Service {


void sendMessage(String message);
}
Program2:

[Link] (Concrete Service)


package aopdemo;

public class EmailService implements Service {

@Override
public void sendMessage(String message) {
[Link]("EmailService: " + message);
}
}
Program3:

[Link] (Aspect Class)


package aopdemo;

public class LoggingAspect {

public void before(String method) {


[Link]("[AOP Before] Calling method: " + method);
}

public void after(String method) {


[Link]("[AOP After] Finished method: " + method);
}
}
Program4:

[Link] (Dynamic Proxy Handler)


package aopdemo;

import [Link];
import [Link];
public class ServiceInvocationHandler implements InvocationHandler {

private Object target;


private LoggingAspect aspect;

public ServiceInvocationHandler(Object target, LoggingAspect aspect) {


[Link] = target;
[Link] = aspect;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

[Link]([Link]()); // AOP before advice


Object result = [Link](target, args); // Execute original method
[Link]([Link]()); // AOP after advice

return result;
}
}

Program5:

[Link] (Uses Constructor Injection)


package aopdemo;

public class Customer {

private Service service;

// Constructor Injection
public Customer(Service service) {
[Link] = service;
}

public void placeOrder() {


[Link]("Order placed successfully!");
}
}
Program6:

[Link] (Creates Proxy + Runs DI & AOP)


package aopdemo;

import [Link];
public class MainApp {

public static void main(String[] args) {

// Original service
EmailService emailService = new EmailService();

// Aspect
LoggingAspect aspect = new LoggingAspect();

// Create dynamic proxy for AOP


Service serviceProxy = (Service) [Link](
[Link](),
new Class[]{[Link]},
new ServiceInvocationHandler(emailService, aspect)
);

// Constructor Injection of proxy service


Customer customer = new Customer(serviceProxy);

// Perform business operation


[Link]();
}
}

OUTPUT:
[AOP Before] Calling method: sendMessage
EmailService: Order placed successfully!
[AOP After] Finished method: sendMessage

7. i) Write a program to demonstrate Spring AOP – before advice.

ii) Write a program to demonstrate Spring AOP – after advice.

Java Generics iii) Write a program to demonstrate Spring AOP – around advice. iv)Write a

program to demonstrate Spring AOP – after returning advice. vi) Write a program to

demonstrate Spring AOP – after throwing advice.

vii) Write a program to demonstrate Spring AOP – pointcuts

Spring JDBC

You might also like