Java Lab
Java Lab
elements, sorting elements of ArrayList. Also illustrate the use of toArray() method.
import java.util.*;
a1.add(12);
a1.add(32);
a1.add(22);
a1.add(1,5);
a1.remove(0);
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]); }
Collections.sort(a1);
Collections.sort(a1,Collections.reverseOrder());
}}
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.*;
Integer t1=n1%100/10;
Integer t2=n2%100/10;
return t1.compareTo(t2);
}}
ArrayList<Integer>al=new ArrayList<>();
int r1,r2,count,i,rand;
System.out.println("From:");
r1=s.nextInt();
System.out.println("to:");
r2=s.nextInt();
count=s.nextInt();
for(i=0;i<count;){
rand=r.nextInt(r2);
if(rand%2==0&&rand%5==0){
al.add(rand);
i++;
}}
Collections.sort(al,new numcmp());
System.out.println("Sorted Numbers are"+al);
s.close();
}}
package javaapplication11;
import java.util.*;
class Customer{
String name;
int balance,id;
name=a;
balance=b;
id=c; }
}};
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;
System.out.println("StringBuffer to String="+i);
System.out.println("StringBuilder to Stirng="+k);
}}
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.*;
str.getChars(2,7,b,0);
System.out.println("enter 2 strings");
}}
6) Implement a java program to illustrate the use of different types of StringBuffer methods
package javaapplication16;
stringBuffer.append(" World!");
stringBuffer.delete(5, 7);
stringBuffer.reverse();
stringBuffer.replace(0, 5, "Hi");
stringBuffer.ensureCapacity(50);
stringBuffer.setLength(5);
stringBuffer.append("!");
} }
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() {
jfrm.setSize(420, 490);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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)
b.addActionListener(new ActionListener() {
} });
jfrm.setLocationRelativeTo(null);
jfrm.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
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>
</head>
<body>
<br>
</form>
</body>
</html>
src.java
import java.io.*;
import jakarta.servlet.*;
throws IOException {
String msg=request.getParameter("t1");
out.println("Hai "+msg);
out.println("\n");
}}
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>
Enter Username:
Enter USN:
</form>
</body>
</html>
Src.java
import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
String n=request.getParameter("t1");
String u=request.getParameter("t2");
String m1=request.getParameter("t3");
String m2=request.getParameter("t4");
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>
</head>
<body>
<%
String id =request.getParameter("empid");
response.addCookie(ck);
%>
</body>
</html>
Read.jsp
<html>
<head>
</head>
<body>
<%
Cookie []c=request.getCookies();
for(int i=0;i<c.length;i++){
%>
</body>
</html>
Index.html
<html>
<head>
<title>Cookies in JSP</title>
</head>
<body>
<center>
</form>
</body>
</html>