0% found this document useful (0 votes)
14 views31 pages

Jo 63

The document contains multiple Java programming tasks, including converting numbers to words, finding second maximum and minimum values, creating a menu-driven ArrayList program, manipulating strings, and implementing a servlet for voting eligibility. It also includes JSP programs for generating Fibonacci and prime numbers, as well as designing a shopping cart. Each section provides code snippets along with example outputs for clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views31 pages

Jo 63

The document contains multiple Java programming tasks, including converting numbers to words, finding second maximum and minimum values, creating a menu-driven ArrayList program, manipulating strings, and implementing a servlet for voting eligibility. It also includes JSP programs for generating Fibonacci and prime numbers, as well as designing a shopping cart. Each section provides code snippets along with example outputs for clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

PART-A

1.Write a program to convert number into words using enumeration with


construction ,methods and instance variable(input range -0 to 99999)

package num2word;

public enum Number {

ZERO (0),

ONE (1),

TWO (2),

THREE (3),

FOUR (4),

FIVE (5),

SIX (6),

SEVEN (7),

EIGHT (8),

NINE (9),

TEN (10),

ELEVEN (11),

TWELVE (12),

THIRTEEN (13),

FOURTEEN (14),

FIFTEEN (15),

SIXTEEN (16),

SEVENTEEN (17),

EIGHTEEN (18),

NINETEEN (19),

TWENTY (20),

THIRTY (30),

U05CI22S0062 ADVANCE JAVA


FORTY (40),

FIFTY (50),

SIXTY (60),

SEVENTY (70),

EIGHTY (80),

NINETY (90),

HUNDRED (100),

THOUSAND (1000);

private int number;

private Number (int num){

this.number=num;

public static String getWord(int n){

return Number.values()[n]+""; }

public String convert(int n){

if(n<0){

return "NEGATIVE"+convert(-n); }

if(n==0){

return Number.getWord(n); }

if(n>99999){

return "ERROR:out of range!"; }

String result="";

if(n>=20000){

result +=""+Number.getWord(18+(n/10000))+"";

n%=10000; }

if(n>=1000){

result +="" +Number.getWord(n/1000)+"THOUSAND";

U05CI22S0062 ADVANCE JAVA


n%=1000; }

if(n>=100){

result +=" "+Number.getWord(n/100)+"HUNDRED";

n%=100; }

if(n>=20){

result +=""+Number.getWord(18+(n/10))+"";

n%=10; }

if(n>0){

return result +=""+Number.getWord(n); }

return result; }

package num2word;

import java.util.Scanner;

public class Num2Word {

public static void main(String[] args) {

Number num=Number.EIGHTEEN;

Scanner s=new Scanner(System.in);

int n;

System.out.print("Enter a number:");

n=s.nextInt();

System.out.println(num.convert(n));

OUTPUT:

Enter a number:4589

FOURTHOUSAND FIVEHUNDREDEIGHTYNINE

BUILD SUCCESSFUL (total time: 5 seconds)

U05CI22S0062 ADVANCE JAVA


2.Find the second maximum and second minimum in a set of number using auto boxing and
unboxing

package maxmin;

import java.util.*;

public class Maxmin {

public static void main(String[] args) {

Set<Integer>sortedSet=new

TreeSet<Integer>();

int n;

Scanner in=new Scanner(System.in);

System.out.println("Enter total number of element in the set:");

n=in.nextInt();

if(n<2){

System.out.println("Please enter at least two elements");

}else{

System.out.println("Enter the value of "+n+"elements of the set");

for(int i=0;i<n;i++)

sortedSet.add(in.nextInt());

Integer[]arr=sortedSet.toArray(new Integer[0]);

System.out.println("Seconf maximum element:"+arr[arr.length-2]);

System.out.println("Second minimum element:"+arr[1]);

in.close();

U05CI22S0062 ADVANCE JAVA


OUTPUT:

Enter total number of element in the set:

Enter the value of 5elements of the set

12 24 78 102 96

Seconf maximum element:96

Second minimum element:24

BUILD SUCCESSFUL (total time: 23 seconds)

U05CI22S0062 ADVANCE JAVA


3.Write a menu driven program to create an ArrayList and perform the following operations

I. Adding element
II. Sorting element
III. Replace an element with another
IV. Removing an element
V. Displaying all the element
VI. Adding an element between two element.

package arraylist;

import java.util.*;

public class ArrayListDemo {

public static void main(String[] args) {

int choice=7;

Scanner in=new Scanner(System.in);

ArrayList<Integer>alist=new ArrayList<Integer>();

int val,fval,pos;

do{

System.out.println("-----------");

System.out.println("1.Add");

System.out.println("2.Sort");

System.out.println("3.Replace");

System.out.println("4.Remove");

System.out.println("5.Display");

System.out.println("6.Add in between");

System.out.println("7.Exit");

System.out.println("------------");

System.out.println("Enter your choice->");

choice=in.nextInt();

switch(choice){

case 1:

U05CI22S0062 ADVANCE JAVA


System.out.println("Enter a number:");

val=in.nextInt();

alist.add(val);

System.out.println("Item added to the list");

break;

case 2:

System.out.println("Sorting");

Collections.sort(alist);

System.out.println("Sorting complete");

break;

case 3:

System.out.println("Enter value to find:");

fval=in.nextInt();

if(alist.contains(fval)){

System.out.println("Enter the replacement values");

val=in.nextInt();

Collections.replaceAll(alist,fval,val);

System.out.println("Replacement completed");

}else{

System.out.println("Element does not exit");

break;

case 4:

System.out.println("Enter the element to remove");

val=in.nextInt();

if(alist.contains(val)){

alist.remove((Integer)val);

U05CI22S0062 ADVANCE JAVA


System.out.println("Elemet removed");

}else{

System.out.println("Element not found");

case 5:

System.out.println("List:"+alist);

break;

case 6:

System.out.println("Enter the index position");

pos=in.nextInt();

if(pos<alist.size()){

System.out.println("Enter the value of new element:");

val=in.nextInt();

alist.add(pos,val);

System.out.println("Element inserted");

}else{

System.out.println("Position out of bound");

break;

case 7:

System.out.println("Thank You");

return;

default:

System.out.println("Wrong choice!\n Try Again");

}while(true);

}}

U05CI22S0062 ADVANCE JAVA


9
OUTPUT:
Item added to the list
-----------
-----------
1.Add
1.Add
2.Sort
2.Sort
3.Replace
3.Replace
4.Remove
4.Remove
5.Display
5.Display
6.Add in between
6.Add in between
7.Exit
7.Exit
------------
------------
Enter your choice->
Enter your choice->
1
1
Enter a number:
Enter a number:
8
4
Item added to the list
Item added to the list
-----------
-----------
1.Add
1.Add
2.Sort
2.Sort
3.Replace
3.Replace
4.Remove
4.Remove
5.Display
5.Display
6.Add in between
6.Add in between
7.Exit
7.Exit
------------
------------
Enter your choice->
Enter your choice->
1
1
Enter a number:

U05CI22S0062 ADVANCE JAVA


Enter a number: 5

5 List:[8, 9, 4, 5, 6]

Item added to the list -----------

----------- 1.Add

1.Add 2.Sort

2.Sort 3.Replace

3.Replace 4.Remove

4.Remove 5.Display

5.Display 6.Add in between

6.Add in between 7.Exit

7.Exit ------------

------------ Enter your choice->

Enter your choice-> 2

1 Sorting

Enter a number: Sorting complete

6 -----------

Item added to the list 1.Add

----------- 2.Sort

1.Add 3.Replace

2.Sort 4.Remove

3.Replace 5.Display

4.Remove 6.Add in between

5.Display 7.Exit

6.Add in between ------------

7.Exit Enter your choice->

------------ 3

Enter your choice-> Enter value to find:

U05CI22S0062 ADVANCE JAVA


6 Enter your choice->
Enter the replacement values 4
12 Enter the element to remove
Replacement completed 20
----------- Element not found
1.Add List:[4, 5, 12, 8, 9]
2.Sort -----------
3.Replace 1.Add
4.Remove 2.Sort
5.Display 3.Replace
6.Add in between 4.Remove
7.Exit 5.Display
------------ 6.Add in between
Enter your choice-> 7.Exit
3 ------------
Enter value to find: Enter your choice->
20 6
Element does not exit Enter the index position
----------- 1
1.Add Enter the value of new element:
2.Sort 24
3.Replace Element inserted
4.Remove -----------
5.Display 1.Add
6.Add in between 2.Sort
7.Exit 3.Replace
------------ 4.Remove

U05CI22S0062 ADVANCE JAVA


5.Display

6.Add in between

7.Exit

------------

Enter your choice->

List:[4, 24, 5, 12, 8, 9]

-----------

1.Add

2.Sort

3.Replace

4.Remove

5.Display

6.Add in between

7.Exit

------------

Enter your choice->

Thank You

BUILD SUCCESSFUL (total time: 1 minute 16 seconds)

U05CI22S0062 ADVANCE JAVA


4.Write a java program to find with even number of characters in a string pair of character in
those words and also toggle the character in agiven string

package p1;

import java.util.*;

public class WordManipulation {

public static void main(String[] args){

Scanner in=new Scanner(System.in);

String str;

System.out.println("Enter a String:");

str=in.nextLine();

int start=0;

String word=" ";

String togWord=" ";

str=str.trim()+" ";

String punct = " . , ? ! : ; \n \t ";

for(int i=0;i<str.length();i++){

if(punct.contains(str.charAt(i)+" ")){

word=str.substring(start,i);

start=i+1;

word=word.trim();

if(word.length()>0 && word.length()%2==0){

StringBuilder sb=new StringBuilder(word);

char tchar;

for(int j=1;j<word.length();j+=2){

tchar=sb.charAt(j);

sb.setCharAt(j,sb.charAt(j-1));

sb.setCharAt(j-1,tchar);

U05CI22S0062 ADVANCE JAVA


}

System.out.println(" "+sb);

StringBuilder tog=new StringBuilder(word);

for(int j=1;j<tog.length();j++){

if(Character.isUpperCase(tog.charAt(j))){

tog.setCharAt(j,Character.toLowerCase(tog.charAt(j)));

}else if(Character.isLowerCase(tog.charAt(j))){

tog.setCharAt(j,Character.toUpperCase(tog.charAt(j)));

togWord+=tog;

togWord+=str.charAt(i);

System.out.println("\n"+togWord);

OUTPUT:

Enter a String:

Good Morning everyone.

oGdo vereoyen

gOOD mORNING EVERYONE.

BUILD SUCCESSFUL (total time: 10 seconds)

5.Write a servlet program that accepts the age and name and display if the user is eligible for
voting or not.

U05CI22S0062 ADVANCE JAVA


Index.html:

<!DOCTYPE html>

<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="CheckVoter">

<table>

<tr>

<td>Name</td>

U05CI22S0062 ADVANCE JAVA


<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>

CheckVoter:

package com;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class CheckVoter extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

U05CI22S0062 ADVANCE JAVA


throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

out.println("<!DOCTYPE html>");

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet CheckVoter</title>");

out.println("</head>");

out.println("<body>");

String name=request.getParameter("uname");

int age=Integer.parseInt(request.getParameter("age"));

if(age>18){

out.println("<h4 style=\"color:green\">" +name +"You are eligible for vote</h4>");

}else{

out.println("<h4 style=\"color:brown\">" +name+"You are not eligible for


vote</h4>");

out.println("<a href=\"index.html\">Home</a>");

out.println("</body>");

out.println("</html>");

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

U05CI22S0062 ADVANCE JAVA


@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

@return a String containing servlet description

@Override

public String getServletInfo() {

return "Short description";

OUTPUT:

6.Write a JSP program to prime first 10 fibonacci and 10 prime number.

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

U05CI22S0062 ADVANCE JAVA


<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>

<h4>Fibonacci Series</h4>

<%

int a=0,b=1,c,i;

for(i=1;i<=10;i++){

c=a+b;

out.println(c+"&nbsp;&nbsp;");

a=b;

b=c;

%>

<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;

U05CI22S0062 ADVANCE JAVA


if(isPrime){

out.println(pn+"&nbsp;&nbsp;");

count++;

pn++;

%>

</body>

</html>

OUTPUT:

Fibonacci Series

1 2 3 5 8 13 21 34 55 89

Prime Numbers:

2 3 5 7 11 13 17 19 23 29

U05CI22S0062 ADVANCE JAVA


7. Write JSP program to design a shopping cart to add items, remove item and to display item
from the cart using session

Shopping.jsp:-

<%@page import="com.Item"%>

<%@page import="java.util.ArrayList"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta>

<title>shopping</title>

</head>

<body>

<h1> Online Shopping</h1>

<%

ArrayList<Item> cart;

if (request.getSession().getAttribute("cart") == null) {

cart=new ArrayList<Item>();

request.getSession().setAttribute("cart", cart);

} else {

cart = (ArrayList<Item>) request.getSession().getAttribute("cart");

%>

<table width="100%";

<tr>

<td>

<form method ="POST">

U05CI22S0062 ADVANCE JAVA


<img src="img/Coke.jpg.jpg"alt="coke"height="200px"/>

<h4>Coke</h4>

<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"width="2"style="width:20px">

<br>

<input type = "submit" name="addBtn" value="Add">

</form>

</td>

<td>

<form method = "POST">

<img src="img/Dew.jpg.jpg"alt="dew"height="200px"/>

<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" width="2" style="width:20px">

<br>

<input type = "submit" name="addBtn" value="Add">

</form>

</td>

<td>

U05CI22S0062 ADVANCE JAVA


<form method = "POST">

<img src="img/Pepsi.jpg.jpg" alt="pepsi" height="200px"/>

<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" width="2" style="width:20px">

<br>

<input type = "submit" name="addBtn" value="Add">

</form>

</td>

<td>

<form method = "POST">

<img src="img/ThumpsUp.jpg.jpg" alt="thumpsUp" height="200px"/>

<h4>Thumps Up</h4>

<input type = "hidden" value="Thumps Up" name="name">

Price : Rs.33

<input type = "hidden" value="33" name="price">

<br>

Quantity:

<input type = "number" name="qty" value="1" width="2" style="width:20px">

<br>

<input type = "submit" name="addBtn" value="Add">

</form>

U05CI22S0062 ADVANCE JAVA


</td>

</tr>

</table>

<%

if (request.getParameter("removeBtn") != null) {

int index = Integer.parseInt(request.getParameter("ino"));

cart.remove(index);

out.println("<h4 style=\"color:green\">Item is remove</h4>");

if (request.getParameter("addBtn") != null) {

int qty = Integer.parseInt(request.getParameter("qty"));

if (qty < 0) {

out.println("<h4 style=\"color:red\">please enter a positive value for value for


quality</h4>");

} else {

String name = request.getParameter("name");

boolean ItemFound = false;

for (int i = 0; i < cart.size(); i++) {

Item item = cart.get(i);

if (item.getName().equals(name)) {

item.setQty(item.getQty() + qty);

out.println("<h4 style=\"color:blue\">Item:" + name+ " added to the cart</h4>");

ItemFound = true;

break;

if (!ItemFound) {

U05CI22S0062 ADVANCE JAVA


double price = Double.parseDouble(request.getParameter("price"));

Item item = new Item(name, qty, price);

cart.add(item);

out.println("<h4 style=\"color:blue\">Item: " + name+ " added to the cart</h4>");

if (cart.size() > 0) {

%>

<h2>Cart Details</h2>

<table border="2">

<tr>

<th>Item Name</th>

<th>Quantity </th>

<th>Unit Price</th>

<th>Total </th>

<th>Action</th>

</tr>

<% for (int i = 0; i < cart.size(); i++) {

Item item = cart.get(i);

%>

<tr>

<td><%=item.getName()%></td>

<td><%=item.getQty()%></td>

<td><%=item.getPrice()%></td>

<td><%=item.getQty() * item.getPrice()%></td>

<td>

U05CI22S0062 ADVANCE JAVA


<form method="POST">

<input type="hidden" value ="<%= i%>" name="ino">

<input type="submit" value="remove" name="removeBtn">

</form>

</td>

</tr>

<% } %>

</table>

<%}%>

</body>

</html>

Item.java:-

package com;

import java.io.Serializable;

public class Item implements Serializable{

private String name;

private int qty;

private double price;

public Item(){

public Item(String name,int qty, double price){

this.name=name;

this.qty=qty;

this.price=price;

U05CI22S0062 ADVANCE JAVA


}

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public int getQty() {

return qty;

public void setQty(int qty) {

this.qty = qty;

public double getPrice() {

return price;

public void setPrice(double price) {

this.price = price;

OUTPUT:

U05CI22S0062 ADVANCE JAVA


U05CI22S0062 ADVANCE JAVA
8.Write a java servlet program to download the 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).

<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="filedownloader?fname=mycv.txt">Download File</a>

</body>

</html>

package com;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletOutputStream;

import javax.servlet.annotation.WebServlet;

@WebServlet(name = "FileDownloader", urlPatterns ={"/FileDownloader"})

public class filedownloader extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

U05CI22S0062 ADVANCE JAVA


throws ServletException, IOException

String fname = request.getParameter("fname");

response.setContentType("text/plaintext");

response.setHeader("Content-Disposition","attachment;filename=\""+fname+"\"");

OutputStream os = response.getOutputStream();

FileInputStream fis = new FileInputStream("C:\\BCA\\mycv.txt");

int i=0;

while( (i = fis.read()) != -1){

os.write(i);

fis.close();

os.close();

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

@Override

public String getServletInfo() {

return "Short description";

U05CI22S0062 ADVANCE JAVA


}

OUTPUT:

U05CI22S0062 ADVANCE JAVA

You might also like