WT Lab Edited
WT Lab Edited
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");
}
}
// 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();
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>© 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
<!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
CLIENT SOCKET-
import java.io.*;
import java.net.*;
} catch (IOException e) {
e.printStackTrace();
}
}
}
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 {