0% found this document useful (0 votes)
30 views21 pages

WT Lab Edited

Uploaded by

mrrr.raj9119
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views21 pages

WT Lab Edited

Uploaded by

mrrr.raj9119
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

1.Program for method overloading and method overriding.

class Calculator {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
public double add(double a, double b) {
return a + b;
}
}

// Method Overriding
class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


@Override
public void sound() {
System.out.println("Dog barks");
}
}

public class Main {


public static void main(String[] args) {
Calculator calc = new Calculator();

// Method Overloading
System.out.println("Sum of 5 and 10: " + calc.add(5, 10));
System.out.println("Sum of 5, 10, and 15: " + calc.add(5, 10, 15));
System.out.println("Sum of 5.5 and 10.5: " + calc.add(5.5, 10.5));

// Method Overriding
Animal animal = new Animal();
animal.sound();

Dog dog = new Dog();


dog.sound();
}
}

OUTPUT-
Sum of 5 and 10: 15
Sum of 5, 10, and 15: 30
Sum of 5.5 and 10.5: 16.0
Animal makes a sound
Dog barks
2.Program for error handling and multithreading.
import java.util.Random;
class ErrorHandlingExample {
public static void main(String[] args) {
try {
int result = divide(10, 0);
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero");
}}
public static int divide(int a, int b) {
return a / b;
}}
// Multithreading
class MyThread extends Thread {
private String threadName;
public MyThread(String name) {
this.threadName = name;
}
public void run() {
System.out.println("Thread " + threadName + " is running.");
Random rand = new Random();
for (int i = 0; i < 5; i++) {
try {
int sleepTime = rand.nextInt(1000); // Sleep for random time up to 1
second
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " is still running.");
}
System.out.println("Thread " + threadName + " exiting.");
}}
public class Main {
public static void main(String[] args) {
// Multithreading
MyThread thread1 = new MyThread("Thread 1");
MyThread thread2 = new MyThread("Thread 2");
thread1.start();
thread2.start();
}}
OUTPUT-
Thread Thread 2 is running.
Thread Thread 1 is running.
Thread Thread 1 is still running.
Thread Thread 1 is still running.
Thread Thread 2 is still running.
Thread Thread 1 is still running.
Thread Thread 1 is still running.
Thread Thread 2 is still running.
Thread Thread 1 is still running.
Thread Thread 1 exiting.
Thread Thread 2 is still running.
Thread Thread 2 is still running.
3.Write a HTML program for creating registration form for E-
Mail id
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Registration Form</title>
<style>
body {
font-family: Arial, sans-serif;
}
.container {
max-width: 400px;
margin: 50px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
.container h2 {
text-align: center;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
.form-group input[type="email"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
.form-group input[type="submit"] {
width: 100%;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: #fff;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h2>Email Registration Form</h2>
<form action="#" method="post">
<div class="form-group">
<label for="email">Email Address:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<input type="submit" value="Register">
</div>
</form>
</div>
</body>
</html>
OUTPUT-
4. Write a HTML program for home page for Prayagraj Tourism
using CSS.
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prayagraj Tourism</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
header {
background-color: #333;
color: #fff;
padding: 20px 0;
text-align: center;
}
h1 {
margin: 0;
font-size: 36px;
}
nav {
background-color: #007bff;
padding: 10px;
text-align: center;
}
nav a {
color: #fff; text-decoration: none; margin: 0 10px;
font-size: 18px;
}
.container {
max-width: 1200px;
margin: 20px auto;
padding: 20px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.hero-image {
background-image: url('prayagraj.jpg');
background-size: cover;
height: 500px;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
text-align: center;
}
.hero-text {
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}
footer {
background-color: #333;
color: #fff;
padding: 20px 0;
text-align: center;
position: absolute;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<header>
<h1>Prayagraj Tourism</h1>
</header>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Places to Visit</a>
<a href="#">Events</a>
<a href="#">Contact</a>
</nav>
<div class="container">
<div class="hero-image">
<div class="hero-text">
<h2>Welcome to Prayagraj</h2>
<p>Discover the beauty and history of Prayagraj, the city of
confluence.</p>
<a href="#" style="background-
color:#007bff;color:#fff;padding:10px 20px;border-radius:5px;text-
decoration:none;">Explore Now</a>
</div>
</div>
<h2>About Prayagraj</h2>
<p>Prayagraj, formerly known as Allahabad, is a city in the Indian state of
Uttar Pradesh. It is one of the oldest cities in India and plays a central role in
Hindu mythology. Prayagraj is famous for its confluence of rivers Ganges,
Yamuna, and Saraswati, known as Triveni Sangam.</p>
<p>Explore the rich cultural heritage, historical monuments, and vibrant
festivals of Prayagraj with us!</p>
</div>
<footer>
<p>&copy; 2024 Prayagraj Tourism. All rights reserved.</p>
</footer>
</body>
</html>
OUTPUT-
5.Write a Java Script function to validate phone number, email and
password (minimum length of 8 character).
function validatePhoneNumber(phoneNumber) {
var phoneRegex = /^\d{10}$/;
return phoneRegex.test(phoneNumber);
}
function validateEmail(email) {
var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
function validatePassword(password) {
// Check if password has minimum length of 8 characters
return password.length >= 8;
}
// Example usage:
var phoneNumber = "1234567890";
var email = "[email protected]";
var password = "password123";
console.log("Phone number valid: " + validatePhoneNumber(phoneNumber));
console.log("Email valid: " + validateEmail(email));
console.log("Password valid: " + validatePassword(password));
OUTPUT-
Phone number valid: true
Email valid: true
Password valid: true

6.Write a java script program which takes a number as input from


user and generate table of that number using table tag in the
following way.
2 *1 = 2
2*2 = 4
2*3 = 6

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiplication Table</title>
<style>
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: center;
}
</style>
</head>
<body>
<div id="tableContainer"></div>

<script>
function generateMultiplicationTable(number) {
var table = document.createElement("table");
var tableContainer = document.getElementById("tableContainer");
tableContainer.innerHTML = ""; // Clear previous table

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


var row = table.insertRow();
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);

cell1.textContent = number + " * " + i;


cell2.textContent = "=";
cell3.textContent = number * i;
}
tableContainer.appendChild(table);
}
var userInput = prompt("Enter a number:");
var number = parseInt(userInput);
if (!isNaN(number)) {
generateMultiplicationTable(number);
} else {
alert("Invalid input. Please enter a valid number.");
}
</script>
</body>
</html>
7.Write a java programs to demonstrate server socket program
and client socket program.
SERVER SOCKET-
import java.io.*;
import java.net.*;
public class ServerSocketProgram {
public static void main(String[] args) {
final int PORT = 12345;
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Server is running on port " + PORT);
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected from: " +
clientSocket.getInetAddress().getHostAddress());
BufferedReader reader = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(),
true);
String message = reader.readLine();
System.out.println("Client: " + message);
writer.println("Server: Hello from Server!");
} catch (IOException e) {
e.printStackTrace();
}
}
}

CLIENT SOCKET-
import java.io.*;
import java.net.*;

public class ClientSocketProgram {


public static void main(String[] args) {
final String SERVER_ADDRESS = "localhost";
final int PORT = 12345;

try (Socket socket = new Socket(SERVER_ADDRESS, PORT)) {


System.out.println("Connected to server on port " + PORT);

BufferedReader reader = new BufferedReader(new


InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
writer.println("Hello from Client!");
String response = reader.readLine();
System.out.println("Server: " + response);

} catch (IOException e) {
e.printStackTrace();
}
}
}

8.Write a jsp program which accepts name of a user as request


and display its name using a heading tag.
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<%
// Get the name parameter from the request
String name = request.getParameter("name");

// Check if name is not null and not empty


if (name != null && !name.isEmpty()) {
%>
<h1>Welcome, <%= name %>!</h1>
<%
} else {
%>
<h1>Welcome!</h1>
<p>Please provide your name in the URL parameter 'name'.</p>
<%
}
%>
</body>
</html>

9.Write a programs to demonstrate AJAX.


HTML file (index.html):
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AJAX Example</title>
<script src="script.js"></script>
</head>
<body>
<h2>Fetch Data with AJAX</h2>
<button onclick="fetchData()">Get Data</button>
<div id="data"></div>
</body> </html>

JavaScript file (script.js):


function fetchData() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// If request is successful, display the fetched data
document.getElementById("data").innerHTML = this.responseText;
}};
xhr.open("GET", "data.txt", true);
xhr.send();}

Text file (data.txt):


This is data fetched with AJAX!

10.Write a jsp program which displays the data from any table
stored in database using JDBC.
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data from Database</title>
</head>
<body>
<h2>Data from Database</h2> <%
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "your_username";
String password = "your_password";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Establish connection to the database
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(url, username, password);
String query = "SELECT * FROM your_table_name";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
%>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<!-- Add more columns as needed -->
</tr>
<% while (rs.next()) { %>
<tr>
<td><%= rs.getInt("id") %></td>
<td><%= rs.getString("name") %></td>
<!-- Add more columns as needed -->
</tr>
<%
}
%>
</table>
<%
} catch (ClassNotFoundException | SQLException e) {
out.println("Error: " + e.getMessage());
} finally {

if (rs != null) rs.close();


if (stmt != null) stmt.close();
if (con != null) con.close();
}
%>
</body>
</html>

You might also like