List Ops PROGRAM 1 Create a List of Strings and Print Using For-Each Loop import java.util.*; public class ListForEach { public static void main(String[] args) {
} PROGRAM 2 List Using Iterator + Reverse Using ListIterator import java.util.*; public class ListIteratorExample { public static void main(String[] args) {
} PROGRAM 3 List of Grocery/Beauty Items Using ListIterator Forward & Backward import java.util.*; public class GroceryListIterator { public static void main(String[] args) {
} SET Ops PROGRAM 1 Set Operations — Add, Remove, Search, Insert One Set into Another Question: Write a Java program using Set interface containing list of items and perform operations: Add items, Remove items, Search item, Insert one set into another set. Code: package practical; import java.util.*; public class SetOperations { public static void main(String[] args) {
} ________________________________________ PROGRAM 2 Set of Strings using Iterator + Reverse Direction Question: Write a Java program to create a Set containing list of items of type String and print the items using Iterator. Also print the list in reverse/backward direction. Code: package practical; import java.util.*; public class SetIteratorReverse { public static void main(String[] args) {
} PROGRAM 3 TreeSet — Forward & Reverse Traversal Question: Write a Java program to demonstrate TreeSet interface and iterate data in forward and reverse direction (grocery items / covid patient records). Code (Grocery Items Example): package practical; import java.util.*; public class TreeSetExample { public static void main(String[] args) {
} MAP Ops PROGRAM — Map Interface Operations Question: Write a Java program using Map interface containing list of items having keys and associated values and perform the following operations: • Add items in the map • Remove items from map • Search specific key • Get value of specified key • Insert map elements into another map • Print all keys and values of the map Java Code import java.util.*; public class MapOperations { public static void main(String[] args) {
} GENERIC OPS PROGRAM 1 Generic Class for Integer and Double Calculator Question: Write a Java program to demonstrate a Generic Class for Integer and Double calculator. Code: class Calculator { T num1, num2;
} public class GenericCalculator { public static void main(String[] args) {
} PROGRAM 2 Generic Method Demonstration Question: Write a Java program to demonstrate Generic Methods. Code: public class GenericMethodExample {
} PROGRAM 3 Upper Bound Wildcards in Java Generics Question: Write a Java program to demonstrate Upper Bound Wildcards in Java Generics. Code: import java.util.*; public class UpperBoundWildcard {
} PROGRAM 4 Lower Bound Wildcards in Java Generics Question: Write a Java program to demonstrate Lower Bound Wildcards in Java Generics. Code: import java.util.*; public class LowerBoundWildcard {
} LAMBDA OPS PROGRAM 1 Lambda Expression to Concatenate Two Strings Question: Write a Java program using Lambda Expression to concatenate two strings. Code: interface StringConcat { String concat(String s1, String s2); } public class LambdaConcat { public static void main(String[] args) {
} PROGRAM 2 Lambda Expression with Single Parameter Question: Write a Java program using Lambda Expression with a single parameter. Code: interface SingleParam { void display(String name); } public class LambdaSingleParameter { public static void main(String[] args) {
} PROGRAM 3 Lambda Expression with Multiple Parameters (Add Two Numbers) Question: Write a Java program using Lambda Expression with multiple parameters to add two numbers. Code: interface AddNumbers { int add(int a, int b); } public class LambdaMultipleParameters { public static void main(String[] args) {
} PROGRAM 4 Lambda Expression With and Without Return Keyword Question: Write a Java program using Lambda Expression with or without return keyword. Code: package JavaCo; interface MyCalculator { int calculate(int a, int b); } public class LambdaReturnExample { public static void main(String[] args) {
} Fahrenheit → Celsius import java.util.function.Function; public class FtoC { public static void main(String[] args) { Function<Double, Double> convert = f -> (f - 32) * 5 / 9; System.out.println("Celsius: " + convert.apply(98.6)); } } Celsius → Fahrenheit import java.util.function.Function; public class CtoF { public static void main(String[] args) { Function<Double, Double> convert = c -> (c * 9 / 5) + 32; System.out.println("Fahrenheit: " + convert.apply(37.0)); } } Kilometers → Miles import java.util.function.Function; public class KmToMiles { public static void main(String[] args) { Function<Double, Double> convert = km -> km * 0.621371; System.out.println("Miles: " + convert.apply(10.0)); } } Lambda Calculator (Add, Sub, Mul, Div) interface Calculator { double operate(double a, double b); } public class LambdaCalc { public static void main(String[] args) {
} Largest of Three Numbers interface MaxThree { int findMax(int a, int b, int c); } public class LargestLambda { public static void main(String[] args) { MaxThree max = (a, b, c) -> Math.max(a, Math.max(b, c)); System.out.println("Largest: " + max.findMax(10, 25, 15)); } } Sum 1 to 100 + Reverse Display import java.util.stream.IntStream; public class SumReverse { public static void main(String[] args) {
} Area of Triangle interface Triangle { double area(double base, double height); } public class TriangleArea { public static void main(String[] args) { Triangle t = (b, h) -> 0.5 * b * h; System.out.println("Area: " + t.area(10, 5)); } } Circumference of Circle interface Circle { double circumference(double r); } public class CircleCircumference { public static void main(String[] args) { Circle c = r -> 2 * Math.PI * r; System.out.println("Circumference: " + c.circumference(7)); } } Perimeter of Rectangle interface Rectangle { double perimeter(double l, double w); } public class RectPerimeter { public static void main(String[] args) { Rectangle r = (l, w) -> 2 * (l + w); System.out.println("Perimeter: " + r.perimeter(10, 5)); } } Volume of Cylinder interface Cylinder { double volume(double r, double h); } public class CylinderVolume { public static void main(String[] args) { Cylinder c = (r, h) -> Math.PI * r * r * h; System.out.println("Volume: " + c.volume(7, 10)); } } Volume of Sphere interface Sphere { double volume(double r); } public class SphereVolume { public static void main(String[] args) { Sphere s = r -> (4.0 / 3) * Math.PI * r * r * r; System.out.println("Volume: " + s.volume(7)); } } Write a program to demonstrate Spring AOP before advice. HelloService.java – public class HelloService { public void sayHello() { System.out.println("Hello from target method"); } } LoggingAspect.java – import org.aspectj.lang.JoinPoint; public class LoggingAspect { public void beforeAdvice(JoinPoint jp) { System.out.println("Before advice executed before method: " + jp.getSignature().getName()); } } MainApp.java – import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService hs = (HelloService) ctx.getBean("helloService"); hs.sayHello(); } } applicationContext.xml – aop:config <aop:aspect ref="loggingAspect"> <aop:pointcut id="pc" expression="execution(* HelloService.sayHello(..))"/> <aop:before method="beforeAdvice" pointcut-ref="pc"/> </aop:aspect> </aop:config> Write a program to demonstrate Spring AOP after advice. HelloService.java – public class HelloService { public void sayHello() { System.out.println("Hello from target method"); } } LoggingAspect.java – import org.aspectj.lang.JoinPoint; public class LoggingAspect { public void afterAdvice(JoinPoint jp) { System.out.println("After advice executed after method: " + jp.getSignature().getName()); } } MainApp.java – import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService hs = (HelloService) ctx.getBean("helloService"); hs.sayHello(); } } applicationContext.xml – aop:config <aop:aspect ref="loggingAspect"> <aop:pointcut id="pc" expression="execution(* HelloService.sayHello(..))"/> <aop:after method="afterAdvice" pointcut-ref="pc"/> </aop:aspect> </aop:config> ________________________________________Write a program to demonstrate Spring AOP around advice. HelloService.java – public class HelloService { public void sayHello() { System.out.println("Hello from target method"); } } LoggingAspect.java – import org.aspectj.lang.ProceedingJoinPoint; public class LoggingAspect { public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable { System.out.println("Before method (Around)"); Object obj = pjp.proceed(); System.out.println("After method (Around)"); return obj; } } MainApp.java – import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService hs = (HelloService) ctx.getBean("helloService"); hs.sayHello(); } } applicationContext.xml – aop:config <aop:aspect ref="loggingAspect"> <aop:pointcut id="pc" expression="execution(* HelloService.sayHello(..))"/> <aop:around method="aroundAdvice" pointcut-ref="pc"/> </aop:aspect> </aop:config> Write a program to demonstrate Spring AOP after returning advice. Code: HelloService.java – public class HelloService { public void sayHello() { System.out.println("Hello from target method"); } } LoggingAspect.java – import org.aspectj.lang.JoinPoint; public class LoggingAspect { public void afterReturningAdvice(Object result) { System.out.println("After Returning advice executed"); } } MainApp.java – import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService hs = (HelloService) ctx.getBean("helloService"); hs.sayHello(); } } applicationContext.xml – aop:config <aop:aspect ref="loggingAspect"> <aop:pointcut id="pc" expression="execution(* HelloService.sayHello(..))"/> <aop:after-returning method="afterReturningAdvice" pointcut-ref="pc" returning="result"/> </aop:aspect> </aop:config> Write a program to demonstrate Spring AOP after throwing advice. HelloService.java – public class HelloService { public void sayHello() { int a = 10 / 0; } } LoggingAspect.java – import org.aspectj.lang.JoinPoint; public class LoggingAspect { public void afterThrowingAdvice(Exception ex) { System.out.println("After Throwing advice executed: " + ex.getMessage()); } } MainApp.java – import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService hs = (HelloService) ctx.getBean("helloService"); hs.sayHello(); } } applicationContext.xml – aop:config <aop:aspect ref="loggingAspect"> <aop:pointcut id="pc" expression="execution(* HelloService.sayHello(..))"/> <aop:after-throwing method="afterThrowingAdvice" pointcut-ref="pc" throwing="ex"/> </aop:aspect> </aop:config> Spring Framework to demonstrate the working of dependency injection via setter method..
Address.java –
package com.example;
public class Address {
private String city;
private String state;
private String zipcode;
public Address() {}
public String getCity() { return city; }
public void setCity(String city) { this.city = city; }
public String getState() { return state; }
public void setState(String state) { this.state = state; }
public String getZipcode() { return zipcode; }
public void setZipcode(String zipcode) { this.zipcode = zipcode; }
@Override
public String toString() {
return city + ", " + state + " - " + zipcode;
}
}
App.java –
package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
Student s = (Student) ctx.getBean("studentBean");
s.display();
((ClassPathXmlApplicationContext) ctx).close();
}
}
Course.java –
package com.example;
public class Course {
private String courseName;
private String duration;
public Course() {}
public String getCourseName() { return courseName; }
public void setCourseName(String courseName) { this.courseName = courseName; }
public String getDuration() { return duration; }
public void setDuration(String duration) { this.duration = duration; }
@Override
public String toString() {
return courseName + " (" + duration + ")";
}
}
Student.java –
package com.example;
public class Student {
private int rollNo;
private String name;
private Course course;
Spring Framework to demonstrate dependency injection via constructor Author.java – package com.example; public class Author { private String name; private String country; public Author(String name, String country) { this.name = name; this.country = country; } public String getName() { return name; } public String getCountry() { return country; } @Override public String toString() { return name + " from " + country; } } Book.java –
package com.example;
public class Book {
private String title;
private double price;
private Author author; App.java – package com.example; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); Book book = (Book) ctx.getBean("bookBean"); book.display(); ((ClassPathXmlApplicationContext) ctx).close(); } } spring.xml –
|