III BCA PART-A
1. Write a program to convert numbers into words using Enumerations
with constructors, methods and instance variables.(INPUT RANGE-0 TO
99999) EX: 36 THIRTY SIX.
import java .[Link];
public class NumberToWordsConverter{
enum Units{
ZERO("
"),ONE("ONE"),TWO("TWO"),THREE("THREE"),FOUR("FOUR"),FIVE("
FIVE"),SIX("SIX"),SEVEN("SEVEN"),EIGHT("EIGHT"),NINE("NINE");
private final String word;
Units(String word){
[Link]=word;
}
public String getWord(){
return word;
}
}
enum Teens{
TNE("TEN"),ELEVEN("ELEVEN"),TWELVE("TWELVE"),THIRTEEN("T
HIRTEEN"),FOURTEEN("FOURTEEN"),FIFTEEN("FIFTEEN"),SIXTEEN
("SIXTEEN"),SEVENTEEN("SEVENTEEN"),EIGHTEEN("EIGHTEEN"),
NINTEEN("NINTEEN");
private final String word;
Teens(String word){
[Link]=word;
}
public String getWord(){
Advanced JAVA and J2EE Lab Page 1
III BCA PART-A
return word;
}
}
enum Tens{
TWENTY("TWENTY"),THIRTY("THIRTY"),FOURTY("FOURTY"),FIFT
Y("FIFTY"),SIXTY("SIXTY"),SEVENTY("SEVENTY"),EIGHTY("EIGHT
Y"),NINTY("NINTY");
private final String word;
Tens(String word){
[Link]=word;
}
public String getWord(){
return word;
}
}
enum Thousands{
THOUSAND("THOUSAND");
private final String word;
Thousands(String word){
[Link]=word;
}
public String getWord(){
return word;
}
}
public static String convertToWords(int number){
if(number==0){
Advanced JAVA and J2EE Lab Page 2
III BCA PART-A
return "ZERO";
}
String words=" ";
if(number/1000>0){
words+=convertToWords(number/1000)+"
"+[Link]()+" ";
number%=1000;
}
if(number/100>0){
words+=[Link]()[number/100].getWord()+" HUNDRED ";
number%=100;
}
if(number>=20){
words+=[Link]()[number/10-2].getWord()+" ";
number%=10;
}else if(number>=10)
{
words+=[Link]()[number-10].getWord()+" ";
number=0;
}
if(number>0){
words+=[Link]()[number].getWord();
}
return words;
}
public static void main(String [] args){
Scanner scanner=new Scanner([Link]);
Advanced JAVA and J2EE Lab Page 3
III BCA PART-A
[Link]("Enter a number(0 to 99999):");
int number=[Link]();
[Link]();
if(number<0||number>99999)
{
[Link]("Invalid input! Number must be between 0 and 99999.");
return;
}
[Link](convertToWords(number));
}
}
Output:
Enter a number(0 to 99999):768
SEVEN HUNDRED SIXTY EIGHT
Enter a number(0 to 99999):50007
FIFTY THOUSAND SEVEN
Enter a number(0 to 99999):456790
Invalid input! Number must be between 0 and 99999.
Advanced JAVA and J2EE Lab Page 4
III BCA PART-A
2. Find the second maximum and second minimum in a set of numbers
using auto boxing and unboxing.
import [Link].*;
public class SecondMinMaxFinder{
public static void main(String args[]){
Scanner scanner=new Scanner([Link]);
[Link]("Enter the number of elements:");
int n=[Link]();
List<Integer>numbers=new ArrayList<>();
[Link]("Enter the elements:");
for(int i=0;i<n;i++){
[Link]([Link]());
}
[Link]();
if([Link]()<2){
[Link]("At least two elements are required.");
return;
}
Integer max=[Link](numbers);
Integer min=[Link](numbers);
[Link](max);
[Link](min);
Integer secondMax=[Link](numbers);
Integer secondMin=[Link](numbers);
[Link](max);
[Link](min);
[Link]("Second Maximum:"+secondMax);
Advanced JAVA and J2EE Lab Page 5
III BCA PART-A
[Link]("Second Minimum:"+secondMin);
}
}
Output:
Enter the number of elements:6
Enter the elements:
56
2
890
75
100
43
Second Maximum:100
Second Minimum:43
Advanced JAVA and J2EE Lab Page 6
III BCA PART-A
3. Write a menu driven program to create an Arraylist and perform the
following operations
i. Adding elements
ii. Sorting elements
iii. Replace an element with another
iv. Removing an element
v. Displaying all the elements
vi. Adding an element between two elements
import [Link];
import [Link];
import [Link];
public class ArrayListOperations{
private static ArrayList<Integer> arrayList=new ArrayList<>();
private static Scanner scanner= new Scanner([Link]);
public static void main(String []args){
while(true){
[Link]("\n Menu:");
[Link]("1. Add Elements");
[Link]("2. Sort Elements");
[Link]("3. Replace an element with another");
[Link]("4. Remove an element");
[Link]("5. Display an element");
[Link]("6. Add an element between two elements");
[Link]("7. Exit");
[Link]("Enter your choice:");
int choice=[Link]();
switch(choice){
Advanced JAVA and J2EE Lab Page 7
III BCA PART-A
case 1:
addElements();
break;
case 2:
sortElements();
break;
case 3:
replaceElement();
break;
case 4:
removeElement();
break;
case 5:
displayElements();
break;
case 6:
addElementBetween();
break;
case 7:
[Link]("Exiting program...");
[Link](0);
default:
[Link]("Invalid choice. Please enter a number between 1 and 7.");
}
}
}
private static void addElements(){
Advanced JAVA and J2EE Lab Page 8
III BCA PART-A
[Link]("Enter the number of elements to add:");
int n= [Link]();
[Link]("Enter the elements:");
for( int i=0;i<n;i++){
[Link]([Link]());
}
[Link]("Elements added successfully.");
}
private static void sortElements(){
[Link](arrayList);
[Link]("Elements sorted successfully.");
}
private static void replaceElement(){
[Link]("Enter the index of the element to replace:");
int index= [Link]();
if(index<0 ||index>=[Link]()){
[Link]("Invalid index.");
return;
}
[Link]("Enter the new element:");
int newElement= [Link]();
[Link](index, newElement);
[Link]("Element replaced successfully.");
}
private static void removeElement(){
[Link]("Enter the index of the element to remove:");
int index= [Link]();
Advanced JAVA and J2EE Lab Page 9
III BCA PART-A
if(index<0 ||index>=[Link]()){
[Link]("Invalid index.");
return;
}
[Link](index);
[Link]("Element removed successfully.");
}
private static void displayElements(){
if([Link]()){
[Link]("The list is empty.");
}else{
[Link]("Element in the list:");
for(int num:arrayList){
[Link](num+" ");
}
[Link]();
}
}
private static void addElementBetween(){
[Link]("Enter the index after which to add the element:");
int index= [Link]();
if(index<0 ||index>=[Link]()-1){
[Link]("Invalid index.");
return;
}
[Link]("Enter the element to add:");
int newElement= [Link]();
Advanced JAVA and J2EE Lab Page 10
III BCA PART-A
[Link](index+1, newElement);
[Link]("Element added successfully.");
}
}
Output:
Menu:
1. Add Elements
2. Sort Elements
3. Replace an element with another
4. Remove an element
5. Display an element
6. Add an element between two elements
7. Exit
Enter your choice:
1
Enter the number of elements to add:5
Enter the elements:
43 5 8 69 2
Elements added successfully.
Menu:
1. Add Elements
2. Sort Elements
3. Replace an element with another
4. Remove an element
5. Display an element
Advanced JAVA and J2EE Lab Page 11
III BCA PART-A
6. Add an element between two elements
7. Exit
Enter your choice:
2
Elements sorted successfully.
Menu:
1. Add Elements
2. Sort Elements
3. Replace an element with another
4. Remove an element
5. Display an element
6. Add an element between two elements
7. Exit
Enter your choice:
5
Element in the list:
2 5 8 43 69
Menu:
1. Add Elements
2. Sort Elements
3. Replace an element with another
4. Remove an element
5. Display an element
6. Add an element between two elements
7. Exit
Enter your choice:
3
Advanced JAVA and J2EE Lab Page 12
III BCA PART-A
Enter the index of the element to replace:2
Enter the new element:10
Element replaced successfully.
Menu:
1. Add Elements
2. Sort Elements
3. Replace an element with another
4. Remove an element
5. Display an element
6. Add an element between two elements
7. Exit
Enter your choice:
5
Element in the list:
2 5 10 43 69
Menu:
1. Add Elements
2. Sort Elements
3. Replace an element with another
4. Remove an element
5. Display an element
6. Add an element between two elements
7. Exit
Enter your choice:
4
Enter the index of the element to remove:3
Element removed successfully.
Advanced JAVA and J2EE Lab Page 13
III BCA PART-A
Menu:
1. Add Elements
2. Sort Elements
3. Replace an element with another
4. Remove an element
5. Display an element
6. Add an element between two elements
7. Exit
Enter your choice:
5
Element in the list:
2 5 10 69
Menu:
1. Add Elements
2. Sort Elements
3. Replace an element with another
4. Remove an element
5. Display an element
6. Add an element between two elements
7. Exit
Enter your choice:
6
Enter the index after which to add the element:4
Invalid index.
Menu:
1. Add Elements
2. Sort Elements
Advanced JAVA and J2EE Lab Page 14
III BCA PART-A
3. Replace an element with another
4. Remove an element
5. Display an element
6. Add an element between two elements
7. Exit
Enter your choice:
6
Enter the index after which to add the element:2
Enter the element to add:20
Element added successfully.
Menu:
1. Add Elements
2. Sort Elements
3. Replace an element with another
4. Remove an element
5. Display an element
6. Add an element between two elements
7. Exit
Enter your choice:
5
Element in the list:
2 5 10 20 69
Menu:
1. Add Elements
2. Sort Elements
3. Replace an element with another
4. Remove an element
Advanced JAVA and J2EE Lab Page 15
III BCA PART-A
5. Display an element
6. Add an element between two elements
7. Exit
Enter your choice:
8
Invalid choice. Please enter a number between 1 and 7.
Menu:
1. Add Elements
2. Sort Elements
3. Replace an element with another
4. Remove an element
5. Display an element
6. Add an element between two elements
7. Exit
Enter your choice:
7
Exiting program...
Advanced JAVA and J2EE Lab Page 16
III BCA PART-A
4. Write a java program to find words with even number of characters in
a string, then swap the pair of characters in those words and also toggle
the characters in a given string
EX: Good Morning everyone
Output: oGdo vereoyen
gOOD mORNING EVERYONE
import [Link].*;
public class WordSwap{
public static void main(String[] args){
String input="Good Morning Everyone";
[Link]("Original String:"+input);
[Link]("swapped String:"+Swapped(input));
[Link]("toggled String:"+toggled(input));
}
public static String Swapped(String input){
String[] words=[Link](" ");
StringBuilder result=new StringBuilder();
for(String word:words){
if([Link]()%2==0){
StringBuilder swappedWord=new StringBuilder();
for(int i=0;i<[Link]();i+=2){
[Link]([Link](i+1)).append([Link](i));
}
[Link](swappedWord).append(" ");
}
}
Advanced JAVA and J2EE Lab Page 17
III BCA PART-A
return [Link]().trim();
}
public static String toggled(String input){
String[] words=[Link](" ");
StringBuilder result=new StringBuilder();
for(String word:words){
StringBuilder toggledWord=new StringBuilder();
for(int i=0;i<[Link]();i++){
if([Link]([Link](i))){
[Link]([Link]([Link](i)));
}else{
[Link]([Link]([Link](i)));
}
}
[Link](toggledWord).append(" ");
}
return [Link]().trim();
}
}
Output:
Original String:Good Morning Everyone
swapped String:oGdo vEreoyen
toggled String:gOOD mORNING eVERYONE
Advanced JAVA and J2EE Lab Page 18
III BCA PART-A
5. Write a Servlet program that accepts the age and name and displays if
the user is eligible for voting or not
[Link]
<html>
<head>
<title>Voting Eligibility Test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
table{
background-color:aqua;
width: 200px;
margin-top: 100px;
margin-left: auto;
margin-right: auto;
border: solid 2px;
}
td{
padding: 5px;
}
</style>
</head>
<body>
<form method="post" action="[Link]
<table>
<tr>
<td>Name</td>
Advanced JAVA and J2EE Lab Page 19
III BCA PART-A
<td><input type="text" name="uname"></td>
</tr>
<tr>
<td>Age</td>
<td><input type="number" name="age"></td>
</tr>
<tr>
<td> </td>
<td><input type="Submit" name="uname" value="check voting
eligibility"></td>
</tr>
</table>
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class CheckVoter extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
Advanced JAVA and J2EE Lab Page 20
III BCA PART-A
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>CheckVoter</title>");
[Link]("</head>");
[Link]("<body>");
String name=[Link]("uname");
int age=[Link]([Link]("age"));
if(age>18){
[Link]("<h4 style=\"color:green\">"+name+"\t you are eligible to
vote</h4>");
}else{
[Link]("<h4 style=\"color:brown\">"+name+"\t you are not eligible to
vote</h4>");
}
[Link]("<a href=\"[Link]\">Home</a>");
[Link]("</body>");
[Link]("</html>");
}
} }
Output:
Advanced JAVA and J2EE Lab Page 21
III BCA PART-A
Sahitya you are eligible to vote
Home
Mahith you are not eligible to vote
Home
6. Write a JSP program to print first 10 Fibonacci and 10 prime numbers
Advanced JAVA and J2EE Lab Page 22
III BCA PART-A
[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Fibo and Prime</title>
</head>
<body>
<h4>Fibonacci Series</h4>
<%
int a=0,b=1,c,i;
[Link](a+" "+b+" ");
for(i=1;i<=8;i++){
c=a+b;
[Link](c+" ");
a=b;
b=c;
Advanced JAVA and J2EE Lab Page 23
III BCA PART-A
%>
<h4>Prime Numbers:</h4>
<%
int pn=2,count=1;
boolean isprime;
while(count<=10){
isprime=true;
for(i=2;i<=pn/2;i++){
if(pn%i==0){
isprime=false;
break;
if(isprime){
[Link](pn+ " ");
count ++;
Advanced JAVA and J2EE Lab Page 24
III BCA PART-A
pn ++;
%>
</body>
</html>
Output:
Fibonacci Series
0 1 1 2 3 5 8 13 21 34 55 89
Prime Numbers:
2 3 5 7 11 13 17 19 2
7. Write a JSP Program to design a shopping cart to add items, remove
item and to display items from the cart using Sessions
Advanced JAVA and J2EE Lab Page 25
III BCA PART-A
[Link]
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Shopping</title>
</head>
<body>
<h1>Online Shopping</h1>
<%
ArrayList<Item> cart;
if([Link]().getAttribute("cart")==null){
cart=new ArrayList<Item>();
[Link]().setAttribute("cart",cart);
}else{
cart=(ArrayList<Item>)[Link]().getAttribute("cart");
}
%>
<table width="100%">
<tr>
<td>
<form method="POST">
<img src="images/[Link]" alt="coke"/>
<h4>Coke</h4>
Advanced JAVA and J2EE Lab Page 26
III BCA PART-A
<input type="hidden" value="Coke" name="name">
Price:Rs.35
<input type="hidden" value="35" name="price">
<br>
Quantity:
<input type="number" name="qty" value="1" style="width:20px">
<br>
<input type="submit" name="addBtn" value="Add">
</form>
</td>
<td>
<form method="POST">
<img src="images/[Link]" alt="dew"/>
<h4>Dew</h4>
<input type="hidden" value="Dew" name="name">
Price:Rs.34
<input type="hidden" value="34" name="price">
<br>
Quantity:
<input type="number" name="qty" value="1" style="width:20px">
<br>
<input type="submit" name="addBtn" value="Add">
</form>
</td>
<td>
<form method="POST">
<img src="images/[Link]" alt="pepsi"/>
Advanced JAVA and J2EE Lab Page 27
III BCA PART-A
<h4>Pepsi</h4>
<input type="hidden" value="Pepsi" name="name">
Price:Rs.36
<input type="hidden" value="36" name="price">
<br>
Quantity:
<input type="number" name="qty" value="1" style="width:20px">
<br>
<input type="submit" name="addBtn" value="Add">
</form>
</td>
<td>
<form method="POST">
<img src="images/[Link]" alt="thumsup"/>
<h4>Thums up</h4>
<input type="hidden" value="Thums Up" name="name">
Price:Rs.33
<input type="hidden" value="33" name="price">
<br>
Quantity:
<input type="number" name="qty" value="1" style="width:20px">
<br>
<input type="submit" name="addBtn" value="Add">
</form>
</td>
</tr>
</table>
Advanced JAVA and J2EE Lab Page 28
III BCA PART-A
<%
if([Link]("removeBtn")!=null){
int index=[Link]([Link]("ino"));
[Link](index);
[Link]("<h4 style=\"color:green\"> Item is removed</h4>");
}
if([Link]("addBtn")!=null){
int qty=[Link]([Link]("qty"));
if(qty<=0){
[Link]("<h4 style=\"color:red\"> Please enter a positive value for quantity
</h4>");
}else{
String name=[Link]("name");
boolean ItemFound=false;
for(int i=0;i<[Link]();i++){
Item item=[Link](i);
if([Link]().equals(name)){
[Link]([Link]()+qty);
[Link]("<h4 style=\"color:blue\">Item:"+name+" added to the
cart</h4>");
ItemFound=true;
break;
}
}
if(!ItemFound)
{
double price=[Link]([Link]("price"));
Advanced JAVA and J2EE Lab Page 29
III BCA PART-A
Item itm=new Item(name,qty,price);
[Link](itm);
[Link]("<h4 style=\"color:blue\">Item:"+name+" added to the
cart</h4>");
}
}
}
if([Link]()>0)
{
%>
<h2>cart Details</h2>
<table border="2">
<tr>
<th>Item name</th>
<th>Quantity</th>
<th>Price</th>
<th>Total </th>
<th>Action</th>
</tr>
<%
for(int i=0;i<[Link]();i++){
Item item=[Link](i);
%>
<tr>
<td><%=[Link]()%></td>
<td><%=[Link]()%></td>
<td><%=[Link]()%></td>
Advanced JAVA and J2EE Lab Page 30
III BCA PART-A
<td><%=[Link]()*[Link]()%></td>
<td>
<form method="POST">
<input type="hidden" value="<%=i%>" name="ino">
<input type="submit" name="removeBtn" value="remove">
</form>
</td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
[Link]
package com;
public class Item{
private String name;
private int qty;
private double price;
public Item(){
}
public Item(String name, int qty, double price) {
[Link] = name;
Advanced JAVA and J2EE Lab Page 31
III BCA PART-A
[Link] = qty;
[Link] = price;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
[Link] = qty;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
[Link] = price;
}
}
Advanced JAVA and J2EE Lab Page 32
III BCA PART-A
Output:
Advanced JAVA and J2EE Lab Page 33
III BCA PART-A
8. Write a java Servlet program to Download a file and display it on the
screen( A link has to be provided in HTML, when the link is clicked
corresponding file has to be displayed on screen).
[Link]
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<a href="NewServlet?fileName=[Link]">download cv</a>
</body>
</html>
[Link]
package program8;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class NewServlet extends HttpServlet {
@Override
Advanced JAVA and J2EE Lab Page 34
III BCA PART-A
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
String fname=[Link]("fileName");
[Link]("text/plaintext");
[Link]("Content-Disposition",
attachment;fileName=\""+fname+"\"");
OutputStream os = [Link]();
FileInputStream fis = new FileInputStream(“C://bca//[Link]);
int i;
i = 0;
while((i=[Link]())!=-1){
[Link](i);
}
[Link]();
[Link]();
}
}
Output:
Advanced JAVA and J2EE Lab Page 35
III BCA PART-A
Advanced JAVA and J2EE Lab Page 36