1) Implement a java program to demonstrate creating an ArrayList, adding elements, removing
elements, sorting elements of ArrayList. Also illustrate the use of toArray() method.
import java.util.*;
public class JavaApplication12 {
public static void main(String[] args) {
ArrayList a1 = new ArrayList();
a1.add(12);
a1.add(32);
a1.add(22);
a1.add(1,5);
a1.remove(0);
// a1.remove("5"); // not allowed - remove only takes index not element
Object[] arr = a1.toArray();
System.out.println("Contents of Arraylist is :");
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]); }
Collections.sort(a1);
System.out.println("List after sorting "+ a1);
Collections.sort(a1,Collections.reverseOrder());
Object[] arr2 = a1.toArray();
System.out.println("ArrayList as Array in other form: " + Arrays.toString(arr2));
}}
2) Develop a program to read random numbers between a given range that are multiples of 2 and 5, sort
the numbers according to tens place using comparator.
package javaapplication13;
import java.util.*;
class numcmp implements Comparator<Integer> {
public int compare(Integer n1,Integer n2){
Integer t1=n1%100/10;
Integer t2=n2%100/10;
return t1.compareTo(t2);
}}
public class JavaApplication13 {
public static void main(String[] args) {
ArrayList<Integer>al=new ArrayList<>();
int r1,r2,count,i,rand;
Random r =new Random();
Scanner s=new Scanner(System.in);
System.out.println("Enter the Range");
System.out.println("From:");
r1=s.nextInt();
System.out.println("to:");
r2=s.nextInt();
System.out.println("How many numbers");
count=s.nextInt();
for(i=0;i<count;){
rand=r.nextInt(r2);
if(rand%2==0&&rand%5==0){
al.add(rand);
i++;
}}
System.out.println("Random Numbers are"+al);
Collections.sort(al,new numcmp());
System.out.println("Sorted Numbers are"+al);
s.close();
}}
3) Implement a java program to illustrate storing user defined classes in collection.
package javaapplication11;
import java.util.*;
class Customer{
String name;
int balance,id;
Customer(String a,int b,int c){
name=a;
balance=b;
id=c; }
public String toString(){
return "|Name : "+ name + "|Balance : "+ balance + "|ID : " + id ;
}};
public class JavaApplication11{
public static void main(String[] args) {
LinkedList arr =new LinkedList();
Customer C1 =new Customer("Jay",1000,2);
Customer C2 =new Customer("Shane",8000,4);
Customer C3 =new Customer("Ricky",7000,6);
Customer C4 =new Customer("Tom",4000,8);
Customer C5 =new Customer("Mick",5000,9);
arr.add(C1);
arr.add(C2);
arr.add(C3);
arr.add(C4);
arr.add(C5);
arr.add(C1);
Iterator itr=arr.iterator();
while(itr.hasNext()){
Object ele=itr.next();
System.out.println(ele);
} }}
4) Implement a java program to illustrate the use of different types of string class constructors.
package javaapplication14;
public class JavaApplication14 {
public static void main(String[] args) {
String str1 = "namaskara";
System.out.println("String created using string literal: " + str1);
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str2 = new String(charArray);
System.out.println("String created using character array: " + str2);
String str3 = new String(charArray, 0, 3);
System.out.println("String created using portion of a character array: " + str3);
byte[] byteArray = {72, 101, 108, 108, 111};
String str4 = new String(byteArray);
System.out.println("String created using byte array: " + str4);
String str5 = new String(byteArray,1,3);
System.out.println("String created using byte array and specified character set: " + str5);
StringBuffer h=new StringBuffer("india");
String i=new String(h);
System.out.println("StringBuffer to String="+i);
StringBuilder j=new StringBuilder("welcome");
String k=new String(j);
System.out.println("StringBuilder to Stirng="+k);
int al[ ]={66,67,68,69,70};
String m=new String(al,2,3);
System.out.println("code point to String="+m);
}}
5) Implement a java program to illustrate the use of different types of character extraction, string
comparison, string search and string modification methods.
package javaapplication15;
import java.util.*;
public class JavaApplication15 {
public static void main(String[] args) {
String str = "Hello, World!";
char firstChar = str.charAt(0);
char lastChar = str.charAt(str.length() - 1);
System.out.println("actual string --> " + str + "\nFirst character: " + firstChar);
System.out.println("Last character: " + lastChar);
char b[]=new char[5];
str.getChars(2,7,b,0);
System.out.println("Substring extracted using getchars(): " + b);
String substring = str.substring(7);
System.out.println("Substring from index 7: " + substring);
Scanner s = new Scanner(System.in);
System.out.println("enter 2 strings");
String s1= s.next();
String s2= s.next();
System.out.println("Comparison using equals: " + s1.equals(s2));
System.out.println("Comparison ignoring case: " + s1.equalsIgnoreCase(s2));
String text = "Java is a programming language.";
System.out.println("Sentence is : "+ text);
System.out.println("Contains 'programming': " + text.contains("programming"));
System.out.println("Index of 'programming': " + text.indexOf("programming"));
String modifiedStr = str.replace("World", "Java");
System.out.println("Original string "+str + " Modified as: " + modifiedStr);
String trimmedStr = " Hello , World! ".trim();
System.out.println(" Hello , World! trimmed as :" + trimmedStr);
String uppercaseStr = str.toUpperCase();
System.out.println("Uppercase string: " + uppercaseStr);
String lowercaseStr = str.toLowerCase();
System.out.println("Lowercase string: " + lowercaseStr);
}}
6) Implement a java program to illustrate the use of different types of StringBuffer methods
package javaapplication16;
public class JavaApplication16{
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("Hello");
stringBuffer.append(" World!");
System.out.println("After append: " + stringBuffer);
stringBuffer.insert(6, ", ");
System.out.println("After insert: " + stringBuffer);
stringBuffer.delete(5, 7);
System.out.println("After delete: " + stringBuffer);
stringBuffer.reverse();
System.out.println("After reverse: " + stringBuffer);
stringBuffer.replace(0, 5, "Hi");
System.out.println("After replace: " + stringBuffer);
System.out.println("Capacity: " + stringBuffer.capacity());
System.out.println("Length: " + stringBuffer.length());
stringBuffer.ensureCapacity(50);
System.out.println("Capacity after ensureCapacity: " + stringBuffer.capacity());
stringBuffer.setLength(5);
System.out.println("Length is set to: " + stringBuffer.length());
System.out.println("After set length: " + stringBuffer);
stringBuffer.append("!");
System.out.println("After append: " + stringBuffer);
char charAtIndex = stringBuffer.charAt(0);
System.out.println("Character at index 0: " + charAtIndex);
String substring = stringBuffer.substring(3);
System.out.println("Substring from index 3: " + substring);
String str = stringBuffer.toString();
System.out.println("String representation: " + str);
} }
7) Demonstrate a swing event handling application that creates 2 buttons Alpha and Beta and displays
the text "Alpha pressed" when alpha button is clicked and "Beta pressed" when beta button is clicked.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class EventDemo {
EventDemo() {
JFrame jfrm = new JFrame("An Event Example");
jfrm.setSize(420, 490);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton a = new JButton("Alpha");
JButton b = new JButton("Beta");
JLabel l=new JLabel("Press a button.");
jfrm.add(a);
jfrm.add(b);
jfrm.add(l);
a.setBounds(50,150,90,30);
b.setBounds(150,150,100,30);
l.setBounds(50,500,100,30);
a.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{ l.setText("Alpha was pressed."); } });
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
l.setText("Beta was pressed.");
} });
jfrm.setLocationRelativeTo(null);
jfrm.setVisible(true);
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new EventDemo();
} });
}}
8) A program to display greeting message on the browser "Hello UserName", "How Are You?”, accept
username from the client using servlet.
Index.html
<html>
<head>
<title> Greeting Message</title>
</head>
<body>
<form action ="src" method="post">
Enter your Name:
<br>
<input type ="text" name ="t1">
<input type="Submit" value="Submit">
</form>
</body>
</html>
src.java
import java.io.*;
import jakarta.servlet.*;
public class src extends GenericServlet{
public void service(ServletRequest request, ServletResponse response)
throws IOException {
try(PrintWriter out = response.getWriter(); ){
String msg=request.getParameter("t1");
out.println("Hai "+msg);
out.println("\n");
out.println("How are you??");
}}
9) A servlet program to display the name, USN, and total marks by accepting student detail.
Index.html
<html>
<head>
<title>Student Details</title>
</head>
<body>
<form action ="src.java" method="post">
Enter Username:
<input type ="text" name ="t1"><br>
Enter USN:
<input type ="text" name ="t2"><br>
Enter sub1 marks:
<input type ="text" name ="t3"><br>
Enter sub2 marks:
<input type ="text" name ="t4"><br>
<input type="Submit" value="Submit">
</form>
</body>
</html>
Src.java
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
public class src extends HttpServlet {
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
try (PrintWriter out = response.getWriter()) {
String n=request.getParameter("t1");
String u=request.getParameter("t2");
String m1=request.getParameter("t3");
String m2=request.getParameter("t4");
out.println("Hai " +u);
out.println("Your USN is "+u);
out.println("Total Marks is "+(Integer.parseInt(m1)+Integer.parseInt(m2) ));
10) A Java program to create and read the cookie for the given cookie name as "EMPID” and its value
as "AN2356".
J2.jsp
<html>
<head>
<title>JSP create Cookie Page </title>
</head>
<body>
<%
String id =request.getParameter("empid");
Cookie ck = new Cookie("EMPID",id);
response.addCookie(ck);
%>
<a href="Read.jsp" >Click here to read Cookie</a>
</body>
</html>
Read.jsp
<html>
<head>
<title>JSP read COOKIE Page</title>
</head>
<body>
<%
Cookie []c=request.getCookies();
for(int i=0;i<c.length;i++){
out.println("<br> Cookie name = "+c[i].getName()+"<br>");
out.println("<br> Cookie value = "+c[i].getValue()+"<br>");
%>
</body>
</html>
Index.html
<html>
<head>
<title>Cookies in JSP</title>
</head>
<body>
<form action="J2.jsp" method="post">
<center>
Enter Employee id:
<input type="text" name="empid"><br>
<input type="Submit" name="Submit">
</center>
</form>
</body>
</html>