Lab File: Pranveer Singh Institute of Technology, Kanpur
Lab File: Pranveer Singh Institute of Technology, Kanpur
Semester- VI
Lab File
WEB TECHNOLOGY
(KCS652)
Submitted To : Submitted By :
Faculty Name :_________________ Name :_________________
Designation :_________________ Roll No. :_________________
Section :_________________
INDEX
Lab Objective Date Marks Sign.
No.
Write HTML/Java scripts to display your CV in
navigator, your Institute website, Department Website
1
and Tutorial website for specific subject.
Objective: Write HTML/Java scripts to display your CV in navigator, your Institute website,
Department Website and Tutorial website for specific subject.
Code:
<html>
<head>
<title>RESUME </title>
</head>
<body> <!-- BEGIN DIV FOR OVERALL BOX -->
<div id="resume"> <!-- THIS DIV CENTERS OUR HEADING -->
<h1>Siddhant Yadav</h1>
<h2>Aspiring Software Developer</h2>
<h3>Kanpur, India</h3> <br /> <!-- END CENTERING DIV -->
</div>
<h2> CAREER OBJECTIVE</h2>
<p> I am quick learner with academic abilities, ready to expand horizons with additional
knowledge. An Inquisitive computer science specialist skilled in leadership, with a strong
foundation in math, logic, and cross- platform coding by using object-oriented concepts, and
solid development. </p> <br />
<h2>EDUCATION</h2>
<h3>Pranveer Singh Institute of Technology , Kanpur </h3>
<p> Graduating July 2024</p>
<a href="/">www.psit.in</a>
<ul>
<li> Bachelor of Technology | Computer Science and Engineering with specialization in
Artificial Intelligence &
Machine Learning |
2020-2024 | 73% (up to 4th semester)</li>
</ul>
<h2>Projects</h2>
<h3>Crowd Size Prediction and Analysis</h3>
<ul>
<li> Developed a Computer Vision Application where we can analyse crowd size from
camera to generate prediction and routing information. Utilized Python, React, and
HTML/CSS/JavaScript to create a user-friendly web interface
that
provides accurate predictions</li>
<li>Technologies used: Python, Flask, Machine Learning, HTML, CSS, Javascript.</li>
</ul>
<h3>Data Models</h3>
<p> 01/2022-03/2022 </p>
<ul>
<li> Developed a <strong>NLP Framework</strong> that uses DIaloGPT as its backend
and is used for Training chat-bot Models.
Utilized Python,
CSS, and JavaScript to create a responsive and user-friendly web interface.</li>
<li><strong>Technology used: </strong> HTML, CSS, JavaScript, Python</li>
</ul> <br />
<h2>Courses</h2>
<ul>
<li>Programming Foundations with Javascript, HTML and CSS </li>
<p><a href="/">www.webdevelomentbootcamp</a></p>
<li>Supervised machine learning: Regression and Classification </li>
<p><a href="/">www.machinelearning</a></p>
<li>Programming for Everybody </li>
<p><a href="/">www.programmingforeverybody</a></p>
<li>Technical Support Fundamentals </li>
<p><a href="/">www.technicalsupportfundamentals</a></p>
</ul>
</body>
</html>
Output:-
PROGRAM-2
Code:
<html>
<head>
<script type="text/javascript" src="validate.js" ></script>
</head>
<style>
td{
font-size: 17px;
}
</style>
<body bgcolor="aqua" bolder="5" style="padding: 6rem;">
<form action="register.jsp" name="StudentRegistration" method=”post”>
<table cellpadding="2" width="50%" border="10" align="center"cellspacing="2">
<tr>
<td colspan=2>
<center><font size=6><b>Student Registration Form</b></font></center>
</td>
</tr>
<tr>
<td>Name</td>
<td><input type=text name=textnames id="textname" size="35"></td>
</tr>
<tr>
<td>Father Name</td>
<td><input type="text" name="fathername" id="fathername"
size="35"></td>
</tr>
<tr>
<td>Postal Address</td>
<td><input type="text" name="paddress" id="paddress" size="35"></td>
</tr>
<tr>
<td>Personal Address</td>
<td><input type="text" name="personaladdress"
id="personaladdress" size="35"></td>
</tr>
<tr>
<td>Sex</td>
<td><input type="radio" name="sex" value=
"male" size="10">Male
<input type="radio" name="sex" value="Female" size="10">Female</td>
</tr>
<tr>
<td>City</td>
<td><select name="City">
<option value="-1" selected>select..</option>
<option value="New Delhi">NEW DELHI</option>
<option value="Mumbai">MUMBAI</option>
<option value="Goa">GOA</option>
<option value="Patna">PATNA</option>
</select></td>
</tr>
<tr>
<td>Course</td>
<td><select name="Course">
<option value="-1" selected>select..</option>
<option value="B.Tech">B.TECH</option>
<option value="MCA">MCA</option>
<option value="MBA">MBA</option>
<option value="BCA">BCA</option>
</select></td>
</tr>
<tr>
<td>District</td>
<td><select name="District">
<option value="-1" selected>select..</option>
<option value="Nalanda">NALANDA</option>
<option value="UP">UP</option>
<option value="Goa">GOA</option>
<option value="Patna">PATNA</option>
</select></td>
</tr>
<tr>
<td>State</td>
<td><select Name="State">
<option value="-1" selected>select..</option>
<option value="New Delhi">NEW DELHI</option>
<option value="Mumbai">MUMBAI</option>
<option value="Goa">GOA</option>
<option value="Bihar">BIHAR</option>
</select></td>
</tr>
<tr>
<td>PinCode</td>
<td><input type="text" name="pincode" id="pincode" size="35"></td>
</tr>
<tr>
<td>EmailId</td>
<td><input type="text" name="emailid" id="emailid" size="35"></td>
</tr>
<tr>
<td>DOB</td>
<td><input type="text" name="dob" id="dob" size="35"></td>
</tr>
<tr>
<td>MobileNo</td>
<td><input type="text" name="mobileno" id="mobileno" size="35"></td>
</tr>
<tr>
<td><input type="reset"></td>
<td colspan="2"><input type="submit" value="Submit Form" /></td>
</tr>
</table>
</form>
</body>
</html>
Output:-
PROGRAM- 3
Objective: Write program using Java script for Web Page to display browsers information.
Code:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Browser Information</title>
<script language=javascript>
function show()
{
document.write("Name "+navigator.appName+"<br>");
document.write("Version "+navigator.appVersion +"<br>");
document.write("Codename " +navigator.appCodeName +"<br>");
document.write("Cookie enable"+navigator.cookieEnabled +"<br>");
document.write("Java Enable"+navigator.javaEnabled +"<br>");
document.write("Mime type"+navigator.mimeTypes +"<br>");
document.write("Platform"+navigator.platform +"<br>");
document.write("Plug ins"+navigator.plugins +"<br>");
document.write("System Language"+navigator.systemLanguage +"<br>");
document.write("User language"+navigator.userAgent +"<br>");
document.write("User Agent"+navigator.userAgent +"<br>");
}
</script>
</head>
<body>
<form id="form1">
<div>
<input id="Button1" type="button" value="Click me" onclick="show()" />
</div>
</form>
</body>
</html>
Output: -
PROGRAM- 4
Objective: A super class Detail has been defined to store the details of a customer.Define a sub
class Bill to compute the monthly telephone charge of the customer as per the chart given
below:
NUMBER OF CALLS RATE
1-100 only rental Charge
101-200 60 paisa per call + rental charge
201-300 80 paisa per call + rental charge
Above 300 1 rupee per call + rental charge
Code:-
import java.util.Scanner;
class Detail {
protected String name;
protected String address;
protected String telno;
protected float rent;
Output :
PROGRAM- 5
Objective: Write a Java applet/AWT to display the ApplicationProgram screen i.e. calculator
and other.
Code:-
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Cal" width=300 height=300>
</applet>
*/
public class Cal extends Applet
implements ActionListener {
String msg = " ";
int v1, v2, result;
TextField t1;
Button b[] = new Button[10];
Button add, sub, mul, div, clear, mod, EQ;
char OP;
t1.setText("");
} else if (str.equals("sub")) {
v1 = Integer.parseInt(t1.getText());
OP = '-';
t1.setText("");
} else if (str.equals("mul")) {
v1 = Integer.parseInt(t1.getText());
OP = '*';
t1.setText("");
} else if (str.equals("div")) {
v1 = Integer.parseInt(t1.getText());
OP = '/';
t1.setText("");
} else if (str.equals("mod")) {
v1 = Integer.parseInt(t1.getText());
OP = '%';
t1.setText("");
}
if (str.equals("EQ")) {
v2 = Integer.parseInt(t1.getText());
if (OP == '+')
result = v1 + v2;
else if (OP == '-')
result = v1 - v2;
else if (OP == '*')
result = v1 * v2;
else if (OP == '/')
result = v1 / v2;
else if (OP == '%')
result = v1 % v2;
t1.setText("" + result);
}
if (str.equals("clear")) {
t1.setText("");
}
}
}
Output:-
PROGRAM- 6
Objective: Write a Java applet to display the Application Program screen i.e. Colour mixer pallet
Code:-
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
// Create sliders
redSlider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255);
greenSlider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255);
blueSlider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255);
// Add listeners
redSlider.addAdjustmentListener(this);
greenSlider.addAdjustmentListener(this);
blueSlider.addAdjustmentListener(this);
// Create labels
redLabel = new Label("Red: 0");
greenLabel = new Label("Green: 0");
blueLabel = new Label("Blue: 0");
Client.java
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args) {
try {
// Create client socket
Socket clientSocket = new Socket("localhost", 5000);
System.out.println("Connected to server...");
// Create input and output streams
InputStream inputStream = clientSocket.getInputStream();
OutputStream outputStream = clientSocket.getOutputStream();
// Create input and output readers
BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream));
PrintWriter outputWriter = new PrintWriter(outputStream, true);
// Read and write data
BufferedReader userInputReader = new BufferedReader(new InputStreamReader(System.in));
String userInput, serverResponse;
while ((userInput = userInputReader.readLine()) != null) {
outputWriter.println(userInput);
if ((serverResponse = inputReader.readLine()) != null) {
System.out.println("Server: " + serverResponse);
}
if (userInput.equals("Bye")) {
break;
}
}
// Close everything
userInputReader.close();
inputReader.close();
outputWriter.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-
Program 8
Objective: Write a program to illustrate JDBC connectivity and perform CRUD operation on a
table student/employee (at least 5 attributes).
Code:
package webtech;
import java.sql.*;
import java.io.*;
import oracle.jdbc.driver.DBConversion;
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","DIVYA","divya");
do
{ System.out.println("\n");
System.out.println("Menu:");
System.out.println("1.Create table");
System.out.println("2.Insert Record into the Table");
System.out.println("3.Update The Existing Record.");
System.out.println("4.Display all the Records from the Table");
System.out.println("5.Delete");
System.out.println("6.Exit");
System.out.println("Enter your choice: ");
String sql;
switch(ch)
{ case 1:
Statement stmt=con.createStatement();
String ct="create table employee(emp_id number(5),emp_name varchar2(30),emp_sal
number(8,2))";
boolean b=stmt.execute(ct);
if(b==false) System.out.println("table not created");
else {db_created=true;}
stmt.close();
break;
case 2:
if(db_created==true){
state=con.createStatement();
int i,n;
System.out.println("enter the no of values you want to insert");
n=Integer.parseInt(br.readLine());
for(i=0;i<n;i++)
{
System.out.println("Enter Employee Number: ");
no=Integer.parseInt(br.readLine());
System.out.println("Enter Employee Name: ");
name=br.readLine();
System.out.println("Enter Employee Salary: ");
sal=Integer.parseInt(br.readLine());
sql="insert into employee values(?,?,?)";
PreparedStatement p=con.prepareStatement(sql);
p.setInt(1,no);
p.setString(2,name);
p.setInt(3,sal);
p.executeUpdate();
System.out.println("Record Added");
p.close();
}}
else
System.out.println("create table first");
break;
case 3:
if(db_created==true){
state=con.createStatement();
System.out.println("Enter Employee Number for the record you wish to Update: ");
no = Integer.parseInt(br.readLine());
System.out.println("Enter new Name: ");
name = br.readLine();
System.out.println("Enter new Salary: ");
sal = Integer.parseInt(br.readLine());
sql="update employee set Name=?, Salary=? where Code=?";
PreparedStatement p=con.prepareStatement(sql);
p.setString(1,name);
p.setInt(2,sal);
p.setInt(3,no);
p.executeUpdate();
System.out.println("Record Updated");
p.close(); }
else
System.out.println("create table first");
break;
case 4:
if(db_created==true){
state=con.createStatement();
sql="select * from employee";
rs=state.executeQuery(sql);
while(rs.next())
{
System.out.println("\n");
System.out.print("\t" +rs.getInt(1));
System.out.print("\t" +rs.getString(2));
System.out.print("\t" +rs.getInt(3));
} }
else
System.out.println("create table first");
break;
case 5:
if(db_created==true){
state=con.createStatement();
sql="delete from employee where emp_id='1'";
rs=state.executeQuery(sql);
state.close();
}
else
System.out.println("create table first");
break;
case 6:
System.exit(1);
break;
default:
System.out.println("Invalid Choice");
break;
}
}while(ch!=6);
con.close();
}catch(Exception e)
{
System.out.println(e);
}
} }
Output:
Program 9
Code:
BatchUpdateDemo_Connection.java
package webtech;
import java.sql.*;
public class BatchUpdateDemo_Connection {
public static Connection getOracleConnection()throws Exception{
String driver="oracle.jdbc.driver.OracleDriver";
String URL="jdbc:oracle:thin:@localhost:1521:xe";
String Username="DIVYA";
String password="divya";
Class.forName(driver);
return DriverManager.getConnection(URL,Username,password); } }
BatchUpdate.java
package webtech;
import java.sql.*;
public class BatchUpdate {
static Savepoint sp1;
public static void main(String args[]) throws Exception
{ Connection con=BatchUpdateDemo_Connection.getOracleConnection();
Statement stmt=null;
try{ con.setAutoCommit(false);
stmt=con.createStatement();
stmt.addBatch("delete from emplo");
stmt.addBatch("insert into emplo values(104,'ABC',5694)");
stmt.addBatch("insert into emplo values(105,'DEF',6694)");
int[] uc=stmt.executeBatch();
sp1 = con.setSavepoint("spv1");
stmt.addBatch("insert into emplo values(106,'GHI',7694)");
stmt.addBatch("insert into emplo2 values(107,'JKl',8694)");
int[] uc1=stmt.executeBatch();
System.out.println("...."+uc.length);
for(int i=0;i<uc.length;i++)
{ System.out.println(uc[i]); }
con.commit(); }
catch(BatchUpdateException e)
{ System.out.println(e);
int[] abc=e.getUpdateCounts();
System.out.println("...."+abc.length);
for(int i=0;i<abc.length;i++)
{ System.out.println(abc[i]); }
con.rollback(sp1);
con.commit(); } } }
Output:
Program 10
Objective: Install a database (Mysql or Oracle). Create a table which should contain at least
the following fields: name, password, email-id, phone number Write a java program/servlet/JSP
to connect to that database and extract data from the tables and display them. Insert the
details of the users who register with the web site, whenever a new user clicks the submit
button in the registration page.
Code:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1><a href="RegisterForm.html">Register new user</a><br>
<a href="Display.jsp">Display existing users</a></h1>
</body>
</html>
RegisterForm.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head> <body>
<form action="Register.jsp" method="post">
<table>
<tr> <td>Name:</td>
<td><input type="text" name="name"></td> </tr>
<tr> <td>Password:</td>
<td><input type="text" name="password"></td> </tr>
<tr> <td>Email:</td>
<td><input type="email" name="email"></td> </tr>
<tr> <td>Phone Number:</td>
<td><input type="number" name="phone"></td> </tr>
<tr> <td></td>
<td><input type="submit" value="Register"></td> </tr>
</table>
</form>
</body> </html>
Register.jsp
Display.jsp
<tr>
<td><%= name %></td>
<td><%= password %></td>
<td><%= email %></td>
<td><%= phone %></td>
</tr>
<%}}%>
</tbody>
</table>
<% }
catch(Exception e) {
System.out.println(e);
}
finally {
out.println("<br><a href='index.html'>Go to home page</a>");
}
%>
</body></html>
Connect.java
package connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Output:
Program 11
Objective: To Develop a student Mark sheet by using Servlet and HTML with database Oracle.
Code:
index.html
<html>
<body>
<form action="servlet/Search">
Enter your Rollno:<input type="text" name="roll"/><br/>
Search.java
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String rollno=request.getParameter("roll");
int roll=Integer.valueOf(rollno);
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
ResultSet rs=ps.executeQuery();
out.print("</tr>");
/* Printing result */
while(rs.next())
{
out.print("<tr><td>"+rs.getInt(1)+"</td><td>"+rs.getString(2)+"
</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td></tr>");
}
out.print("</table>");
}catch (Exception e2) {e2.printStackTrace();}
finally{out.close();}
}
}
web.xml
<web-app>
<servlet>
<servlet-name>Search</servlet-name>
<servlet-class>Search</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Search</servlet-name>
<url-pattern>/servlet/Search</url-pattern>
</servlet-mapping>
</web-app>
Output:
Program 12
Objective: Design and implement a simple servlet for an entry form of student details and
send it to store at database server like SQL, Oracle or MS Access.
Code:
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="./register" method="post">
<table>
<tr> <td>Roll Number:</td>
<td><input type="number" name="roll"></td>
</tr>
<tr> <td>student name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr> <td>Course:</td>
<td><input type="text" name="course"></td>
</tr>
<tr> <td>branch:</td>
<td><input type="text" name="branch"></td>
</tr>
<tr> <td></td>
<td><input type="submit" value="add"></td>
</tr>
</table>
</form>
</body>
</html>
Connect.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
AddInDatabase.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
web.xml
Output:
Program 13
Objective: Write a JSP which insert the details of the 3 or 4 users who register with the web
site by using registration form.
Authenticate the user when he submits the login form using the user name and password from
the database
Code:
Connect.java
package connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Login Page</h1>
<form action="Login.jsp" method="get"><br>
<table>
<tr> <td>Username</td>
<td><input type="text" name="uname"></td> </tr>
<tr> <td>Password</td>
<td><input type="text" name="upass"></td> </tr>
<tr> <td></td>
<td><input type="submit" value="Login"></td> </tr>
<tr> <td></td>
<td><center><a href="RegisterForm.html">
New user? Register here.</a></center></td> </tr>
</table>
</form>
</body>
</html>
RegisterForm.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Registration Page</h1>
<form action="Register.jsp" method="post"><br>
<table>
<tr> <td>Name</td>
<td><input type="text" name="name"></td> </tr>
<tr> <td>Username</td>
<td><input type="text" name="uname"></td> </tr>
<tr> <td>Password</td>
<td><input type="text" name="upass"></td> </tr>
<tr> <td>Email</td>
<td><input type="text" name="email"></td> </tr>
<tr> <td>Phone Number</td>
<td><input type="number" name="phone"></td></tr>
<tr> <td></td>
<td><input type="submit" value="Register here"></td> </tr>
</table>
</form>
<a href="index.html">Go back to home page</a>
</body>
</html>
Login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<%@ page import="connection.Connect" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
try{boolean exist=false;
Statement stmt = Connect.conn().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("select * from users");
if(!rs.next()){
%>
<h1>No users registered! Register first.</h1>
<%
}
else{
rs.previous();
String uname = request.getParameter("uname");
String password = request.getParameter("upass");
while (rs.next()) {
String name = rs.getString(2);
String pass = rs.getString(3);
if(uname.equals(name)&&password.equals(pass)) {
exist=true;
break; }}
if(exist){ %>
<h1>You have successfully logged in <%= rs.getString(1) %></h1>
<br>
<strong>Your username is <%= rs.getString(2)%><br>
Your email is <%= rs.getString(4)%><br>
Your phone number is <%= rs.getInt(5)%></strong>
<% }
else{
%>
<h1>Wrong username or password!</h1>
<%
}}}
catch(Exception e) {
System.out.println(e);
}
finally {
out.println("<br><a href='index.html'>Go to home page</a>");}%>
</body>
</html>
Register.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.Statement" %>
<%@ page import="connection.Connect" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
String name = request.getParameter("name");
String uname = request.getParameter("uname");
String password = request.getParameter("upass");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
try {
Statement stmt = Connect.conn().createStatement();
String query = "insert into users
values('"+name+"','"+uname+"','"+password+"','"+email+"',"+phone+")";
stmt.executeQuery(query);
Connect.conn().close();
stmt.close();
out.println("Registration Successfull");
}catch(java.sql.SQLIntegrityConstraintViolationException e) {
out.println("Error resgistering this user. A student with same username exists!");
}
catch(Exception e) {
out.println("Registration Unsuccessfull!");
System.out.println(e);
}
finally {
out.println("<br><a href='index.html'>Go to home page</a>");
} %>
</body>
</html>
Code:
Program 14
Objective: Store 5 Students (name, branch, rollno, age) objects in list. Perform any sorting
technique so as to display list in ascending order of rollno and display in descending order of
their age.
Code:
import java.util.*;
public class StudentListTest
{
public static void main(String[] args)
{
TreeSet<Student> s1 = new TreeSet<Student>(new RollComparator());
TreeSet<Student> s2 = new TreeSet<Student>(new AgeComparator());
Iterator i1 = s1.iterator();
Iterator i2 = s2.iterator();
while(i1.hasNext())
{
Student e = (Student) i1.next();
System.out.println(e);
}
System.out.println(); System.out.println(); System.out.println();
System.out.println("According to Age(Descending Order)");
while(i2.hasNext())
{
Student e = (Student) i2.next();
System.out.println(e);
}
}
}
class Student
{
String name,branch;
Integer roll,age;
OBJECTIVE: Perform union (AUB), intersection and difference (A-B) operations using set collection.
CODE:
package com.corejava;
import java.util.*;
class SetOperations
col1.add("a");
col1.add("b"); col1.add("c");
col2.add("b");
col2.add("c"); col2.add("d");
col2.add("e");
/*For Union*/
/*For Difference*/
Output-
Program 16
OBJECTIVE: Create a Map that consists of Country-Capital pair to store information about various
countries along with their capital name. Display the entries in sorted order of Country and Capital.
CODE:
package com.democollection;
import java.util.stream.Stream;
countryCapitalMap.put("japan", "tokyo");
.stream();
stream1.sorted(Map.Entry.comparingByKey()).forEachOrdered(
c -> sortedMap1.put(c.getKey(), c.getValue()));
.stream();
stream2.sorted(Map.Entry.comparingByValue()).forEachOrdered(
c -> sortedMap2.put(c.getKey(), c.getValue()));
OUTPUT –