0% found this document useful (0 votes)
10 views

Java Practicals

The document describes a Java program to find the longest palindrome substring in a given string. It defines a longestPalSubstr method that takes a string as input, initializes a 2D boolean table to store results of substring comparisons, and iterates through the string to find the longest palindrome by comparing characters at different offsets. It returns the length of the longest palindrome found. The main method calls this method on a sample input string and prints the output.

Uploaded by

mehaksamra777
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Practicals

The document describes a Java program to find the longest palindrome substring in a given string. It defines a longestPalSubstr method that takes a string as input, initializes a 2D boolean table to store results of substring comparisons, and iterates through the string to find the longest palindrome by comparing characters at different offsets. It returns the length of the longest palindrome found. The main method calls this method on a sample input string and prints the output.

Uploaded by

mehaksamra777
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Practical No:- 9

AIM: Create a Servlet/ application with a facility to print any message on web
browser.

Java Code:-
import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet

private String message;

public void init() throws ServletException {

message = "Hello World";

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<h1>" + message + "</h1>");

Abhishek Kanna 16BCS8034


HTML Code:-

<servlet>

<servlet-name>HelloWorld</servlet-name>

<servlet-class>HelloWorld</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>HelloWorld</servlet-name>

<url-pattern>/HelloWorld</url-pattern>

</servlet-mapping>

Output:-

Abhishek Kanna 16BCS8034


Practical No:- 10
AIM: Create JSP application for addition, multiplication and division

HTML Code:-
<html>

<title>calculator</title>

<head><h1><center>Basic Calculator</center></h1></head>

<body>

<center>

<form action="calculator.jsp" method="get">

<label for="num1"><b>Number 1</b></label>

<input type="text" name ="num1"><br><br>

<label for = "num2"><b>Number 2</b></label>

<input type="text" name="num2"><br><br>

<input type ="radio" name = "r1" value="Add">+

<input type = "radio" name = "r1" value="Sub">-<br>

<input type="radio" name="r1" value ="mul">*

<input type = "radio" name="r1" value="div">/<br><br>

<input type="submit" value="submit">

</center>

</body>

</html>

Abhishek Kanna 16BCS8034


JSP Code:-
<html>

<title>calculator</title>

<head></head>

<body>

<%@page language="java"%>

<%

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

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

String operation = request.getParameter("r1");

if(operation.equals("Add")){

int add=num1+num2;

out.println("Addition is: "+add);

else if(operation.equals("Sub")){

int sub=num1-num2;

out.println("Substraction is: "+sub);

else if(operation.equals("mul")){

int mul=num1*num2;

out.println("multiplication is: "+mul);

else if(operation.equals("div"))

int div = num1/num2;

Abhishek Kanna 16BCS8034


if(num1>=num2)

out.println("division is: "+div);

else

out.println("The division cannot be performed");

%>

</body>

</html>

OUTPUT:-

Abhishek Kanna 16BCS8034


Practical No:- 8
AIM: Create a palindrome creator application for making a longest possible
palindrome out of given input string.

Code:-
public class LongestPalinSubstring

static void printSubStr(String str, int low, int high) {

System.out.println(str.substring(low, high + 1));

static int longestPalSubstr(String str) {

int n = str.length(); // get length of input string

boolean table[][] = new boolean[n][n];

int maxLength = 1;

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

table[i][i] = true;

int start = 0;

for (int i = 0; i < n - 1; ++i) {

if (str.charAt(i) == str.charAt(i + 1)) {

table[i][i + 1] = true;

start = i;

maxLength = 2;

for (int k = 3; k <= n; ++k)

Abhishek Kanna 16BCS8034


{

for (int i = 0; i < n - k + 1; ++i)

int j = i + k - 1;

if (table[i + 1][j - 1] && str.charAt(i) == str.charAt(j))

table[i][j] = true;

if (k > maxLength) {

start = i;

maxLength = k;

System.out.print("Longest palindrome substring is; ");

printSubStr(str, start, start + maxLength - 1);

return maxLength;

public static void main(String[] args)

String str = "forabhishekkehsihbafor";

System.out.println("Length is: " + longestPalSubstr(str));

Abhishek Kanna 16BCS8034


Output:-

Abhishek Kanna 16BCS8034

You might also like