End Term Examination Solutions (Java & JSP)
1. Java Program: Animal Class with Subclass Cheetah
class Animal {
public void move() {
[Link]("Animal is moving");
class Cheetah extends Animal {
@Override
public void move() {
[Link]("Cheetah is running fast!");
public class Main {
public static void main(String[] args) {
Animal a = new Animal();
Cheetah c = new Cheetah();
[Link](); // Output: Animal is moving
[Link](); // Output: Cheetah is running fast!
}
2. Java Program: Search Element in Array List
import [Link];
import [Link];
public class SearchElement {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
Scanner scanner = new Scanner([Link]);
[Link]("Enter element to search: ");
int element = [Link]();
if ([Link](element)) {
[Link]("Element found!");
} else {
[Link]("Element not found.");
[Link]();
}
3. Java Program: Count Occurrences of Digit 5 in a File
import [Link];
import [Link];
import [Link];
public class CountDigit {
public static void main(String[] args) {
if ([Link] != 1) {
[Link]("Usage: java CountDigit <filename>");
return;
String fileName = args[0];
int count = 0;
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = [Link]()) != null) {
for (char ch : [Link]()) {
if (ch == '5') {
count++;
} catch (IOException e) {
[Link]("File not found or unable to read.");
}
[Link]("Number of occurrences of digit 5: " + count);
4. JSP Program: Username and Password Check
<%@ page import="[Link].*, [Link].*" %>
<html>
<body>
<form method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
<%
String correctUsername = "admin";
String correctPassword = "1234";
String username = [Link]("username");
String password = [Link]("password");
if (username != null && password != null) {
if ([Link](correctUsername) && [Link](correctPassword)) {
[Link]("Welcome! <a href='[Link]'>Proceed to Index Page</a>");
} else {
[Link]("Incorrect username or password.");
%>
</body>
</html>