Java and JavaScript Lab Experiments: Classes, Inheritance, JSP and DOM

Experiment 1: Classes, Objects, Looping, and Branching in Java

Aim: To write a Java program demonstrating classes, objects, looping, and branching statements.

Code:

class Calculator {
int add(int a, int b) {
return a + b;
}
}
public class Exp1 {
public static void main(String[] args) {
Calculator calc = new Calculator();
// Branching (if-else)
int num = 10;
if (num > 0) {
System.out.println(num + ” is positive.”);
} else {
System.out.println(num + ” is non-positive.”);
}
// Looping (for loop)
System.out.println(“Counting from 1 to 5:”);
for (int i = 1; i <= 5; i++) {
System.out.println(“Count: ” + i);
int result = calc.add(i, 5);
System.out.println(“Result using object method (i + 5): ” + result);
}
}
}

Conclusion: Successfully implemented classes, objects, conditional branching, and looping structures in Java.

Experiment 2: Method and Constructor Overloading in Java

Aim: To write a Java program demonstrating method overloading and constructor overloading.

Code:

class Box {
double width, height, depth;
// Default constructor
Box() {
width = height = depth = 0;
}
// Parameterized constructor (Constructor Overloading)
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
double volume() {
return width * height * depth;
}
// Method Overloading
int multiply(int a, int b) {
return a * b;
}
double multiply(double a, double b) {
return a * b;
}
}
public class Exp2 {
public static void main(String[] args) {
Box box1 = new Box();
Box box2 = new Box(3, 4, 5);
System.out.println(“Volume of default box: ” + box1.volume());
System.out.println(“Volume of parameterized box: ” + box2.volume());
Box calc = new Box();
System.out.println(“Multiplication (int): ” + calc.multiply(4, 5));
System.out.println(“Multiplication (double): ” + calc.multiply(2.5, 3.5));
}
}

Conclusion: Successfully demonstrated constructor and method overloading to achieve polymorphism in Java.

Experiment 3.1: Single Inheritance in Java

Aim: To write a Java program implementing Single Inheritance.

Code:

// Superclass
class Animal {
void eat() {
System.out.println(“This animal eats food.”);
}
}
// Subclass inheriting Animal
class Dog extends Animal {
void bark() {
System.out.println(“The dog barks.”);
}
}
public class SingleInheritanceDemo {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited method
myDog.bark(); // Own method
}
}

Conclusion: Successfully implemented single inheritance where a subclass inherits properties from a single superclass.

Experiment 3.2: Hierarchical Inheritance in Java

Aim: To write a Java program implementing Hierarchical Inheritance.

Code:

// Superclass
class Animal {
void eat() {
System.out.println(“This animal eats food.”);
}
}
// Subclass 1
class Dog extends Animal {
void bark() {
System.out.println(“The dog barks.”);
}
}
// Subclass 2
class Cat extends Animal {
void meow() {
System.out.println(“The cat meows.”);
}
}
public class HierarchicalInheritanceDemo {
public static void main(String[] args) {
Dog myDog = new Dog();
Cat myCat = new Cat();
System.out.println(“Dog actions:”);
myDog.eat();
myDog.bark();
System.out.println(“\nCat actions:”);
myCat.eat();
myCat.meow();
}
}

Conclusion: Successfully implemented hierarchical inheritance where multiple subclasses inherit from a single superclass.

Experiment 3.3: Hybrid Inheritance in Java

Aim: To write a Java program implementing Hybrid Inheritance using classes and interfaces.

Code:

// Superclass
class Animal {
void eat() {
System.out.println(“This animal eats food.”);
}
}
// Subclass 1
class Mammal extends Animal {
void walk() {
System.out.println(“This mammal walks on land.”);
}
}
// Interface for additional behavior
interface Pet {
void play();
}
// Subclass inheriting Mammal and implementing Pet (Hybrid Inheritance structure)
class Dog extends Mammal implements Pet {
public void play() {
System.out.println(“The dog plays with its owner.”);
}
void bark() {
System.out.println(“The dog barks.”);
}
}
public class HybridInheritanceDemo {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // From Animal
myDog.walk(); // From Mammal
myDog.play(); // From Pet Interface
myDog.bark(); // From Dog
}
}

Conclusion: Successfully implemented hybrid inheritance in Java by combining class inheritance and interfaces.

Experiment 3.4: Exception Handling in Java

Aim: To write a Java program demonstrating Exception Handling using try, catch, and finally blocks.

Code:

public class ExceptionHandlingDemo {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
int result = a / b; // Throws ArithmeticException
System.out.println(“Result: ” + result);
} catch (ArithmeticException e) {
System.out.println(“Caught Exception: Division by zero is not allowed.”);
} finally {
System.out.println(“Finally block executed: Clean-up completed.”);
}
}
}

Conclusion: Successfully handled runtime exceptions using try-catch-finally blocks to ensure smooth program execution.

Experiment 4: JavaScript Animation

Aim: To implement a JavaScript animation moving an element inside a container.

Code:

<!DOCTYPE html>
<html>
<style>
#container{
width:400px;
height:400px;
position:relative;

Experiment 5: JavaScript Document Object Model (DOM)

Aim: To demonstrate DOM manipulation by modifying HTML element text content using JavaScript.

Code:

<!DOCTYPE html>
<html>
<body>
<h2>Document Object Model (DOM)</h2>
<p id=’targetText’>This is the original text.</p>
<div id=’output’></div>
<script type=”text/javascript”>
</script>
<p>The JavaScript code located the paragraph above and changed its text.</p>
</body>
</html>

Conclusion: Successfully manipulated the HTML Document Object Model (DOM) to dynamically update element text content using JavaScript.

Experiment 6.1: dispDate.jsp

Aim: To display the current date and time using JSP scriptlets and implicit objects.

Code:

Conclusion: Successfully displayed system date and time formats using JSP scriptlet tags and the out implicit object.

Experiment 6.2: Time-Based Greeting Using JSP

Aim: To display dynamic greeting messages based on the current time of day using JSP.

Code:

<html>
<body bgcolor=”#fff”>
<% java.util.Date clock = new java.util.Date(); %>
<h1>Good Evening!</h1>
<% %>
Welcome to our site, open 24 hours a day.
</body>
</html>

Conclusion: Successfully generated time-based conditional greetings using JSP scriptlets and branching statements.

Experiment 6.3: userinfo1.jsp

Aim: To retrieve and display user information submitted through form parameters using JSP request objects.

Code:

<%@page language=”java” isThreadSafe=”false” import=”java.io.*” %>
<html>
<body bgcolor=”#fff”>
<%
String name, bdate, email;
int lucky;
name = request.getParameter(“userName”);
bdate = request.getParameter(“birthDate”);
email = request.getParameter(“emailAddr”);
lucky = Integer.parseInt(request.getParameter(“luckyNo”));
%>
<ul>
<li> Name: <%= name %> </li>
<li> BirthDate: <%= bdate %> </li>
<li> EMail: <%= email %> </li>
<li> Lucky: <%= lucky %> </li>
</ul>
</body>
</html>

Conclusion: Successfully extracted and displayed HTTP request parameters using JSP expression tags and implicit request objects.

Experiment 7: JavaScript Animation (Complete)

Aim: To implement a JavaScript animation moving an element inside a container.

Code:

<!DOCTYPE html>
<html>
<style>
#container{
width:400px;
height:400px;
position:relative;
background:#ff0;
}
#animate{
width:50px;
height:50px;
position:absolute;
background-color:red;
}
</style>
<body>
<p><button onclick=”myMove()”>Click Me</button></p>
<div id=”container”>
<div id=”animate”></div>
</div>
<script>
function myMove() {
let id = null;
const elem = document.getElementById(‘animate’);
let pos = 0;
clearInterval(id);
id = setInterval(frame, 5);
function frame() {
if (pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + ‘px’;
elem.style.left = pos + ‘px’;
}
}
}
</script>
</body>
</html>

Conclusion: Successfully implemented a JavaScript animation using setInterval and DOM style manipulation.