Java Classes, Inheritance, DOM, JSP and Animation Practicals

Practical 1: Program on Class and Object in Java

Practical 1: Program on Class and Object in Java

class Student {
String name;
int rollNo;

Student(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}

static void displayInfo(Student student) {
System.out.println(“Student Name: ” + student.name);
System.out.println(“Roll Number: ” + student.rollNo);
}
}

class Main {
public static void main(String[] args) {

Student student1 = new Student(“Aayush Gautam”, 38);

Student student2 = new Student(“Kritarth Dubey”, 32);

Student.displayInfo(student1);
Student.displayInfo(student2);
}
}

Practical 2: Method Overloading and Constructor Overloading

class Calculator {

Calculator() {
System.out.println(“Default constructor called”);
}

Calculator(int a, int b) {
System.out.println(“Parameterized constructor called”);
System.out.println(“Sum: ” + (a + b));
}

int add(int a, int b) {
return a + b;
}

double add(double a, double b) {
return a + b;
}

String add(String a, String b) {
return a + ” ” + b;
}
}

class Main {
public static void main(String[] args) {

Calculator calc1 = new Calculator();

Calculator calc2 = new Calculator(5, 10);

Calculator calc = new Calculator();

System.out.println(
“Sum of integers: ” + calc.add(10, 20)
);

System.out.println(
“Sum of doubles: ” + calc.add(10.5, 20.5)
);

System.out.println(
“Concatenation of strings: “
+ calc.add(“Hello”, “World”)
);
}
}

Practical 3: Program on Various Types of Inheritance

class Animal {

protected String species = “Mammal”;

public void makeSound() {
System.out.println(“Animal sound!”);
}
}

class Dog extends Animal {

private String breed = “Golden Retriever”;

public void displayInfo() {
System.out.println(
“Species: ” + species +
“, Breed: ” + breed
);
}
}

class Puppy extends Dog {

private String age = “2 months”;

public void displayPuppyInfo() {
displayInfo();

System.out.println(“Age: ” + age);
}
}

class Main {

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.makeSound();
myDog.displayInfo();

Puppy myPuppy = new Puppy();

myPuppy.makeSound();
myPuppy.displayPuppyInfo();
}
}

Practical 4: JavaScript Animation




JavaScript Animation

#animate{
width:50px;
height:50px;
position:absolute;
background-color:red}

JavaScript Animation


Click Me

const id = setInterval(frame, 5);

function frame() {
if(pos==350){
clearInterval(id);
} else {
pos++;
elem.style.top=pos+’px’;
elem.style.left = pos + “px”;
}
}
}

Practical 5: JavaScript Document Object Model (DOM)

Document Object Model (DOM)

This is the original text.

element.textContent = “The DOM has changed me!”;

document.getElementById(‘output’).innerHTML =
‘The text of the paragraph above has been changed by JavaScript.’;


The JavaScript code located the paragraph above
and changed its text.

Practical 6.1: dispDate.js

out.println(now.toString());
out.println(“
“);

out.println(now.toGMTString());
%>

Practical 6.2: greetings.jsp

3

<%
if (hour < 12) {
%>

Good morning!


<%
} else if (hour < 18) {
%>

Good afternoon!


<%
} else {
%>

Good evening!


<%
}
%>

Welcome to our site, open 24 hours a day.

Practical 6.3: userinfo.jsp

name = request.getParameter(“userName”);
bdate = request.getParameter(“birthDate”);
email = request.getParameter(“emailAddr”);

String luckyValue = request.getParameter(“luckyNo”);
lucky = Integer.parseInt(luckyValue);
%>


Name: <%= name %>
BirthDate: <%= bdate %>
EMail: <%= email %>
Lucky: <%= lucky %>