· 6 months ago · Mar 16, 2025, 06:35 PM
1Slip 1
2==========
3Question 1: Write a Java program using Multithreading to display all the alphabets between 'A' to 'Z' after every 2 seconds.
4--------------------------------------------------------------------------------
5public class AlphabetThread extends Thread {
6 public void run() {
7 for (char ch = 'A'; ch <= 'Z'; ch++) {
8 System.out.println(ch);
9 try {
10 Thread.sleep(2000); // Sleep for 2 seconds
11 } catch (InterruptedException e) {
12 System.out.println("Thread interrupted.");
13 }
14 }
15 }
16
17 public static void main(String[] args) {
18 AlphabetThread thread = new AlphabetThread();
19 thread.start();
20 }
21}
22--------------------------------------------------------------------------------
23
24Question 2: Write a Java program to accept the details of Employee (Eno, EName, Designation, Salary) from a user and store it into the database. (Use Swing)
25--------------------------------------------------------------------------------
26import javax.swing.*;
27import java.awt.*;
28import java.awt.event.*;
29import java.sql.*;
30
31public class EmployeeForm extends JFrame {
32 private JTextField enoField, enameField, desgField, salaryField;
33 private JButton submitButton;
34
35 public EmployeeForm() {
36 setTitle("Employee Details");
37 setLayout(new GridLayout(5, 2));
38 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39
40 add(new JLabel("Employee No:"));
41 enoField = new JTextField();
42 add(enoField);
43
44 add(new JLabel("Employee Name:"));
45 enameField = new JTextField();
46 add(enameField);
47
48 add(new JLabel("Designation:"));
49 desgField = new JTextField();
50 add(desgField);
51
52 add(new JLabel("Salary:"));
53 salaryField = new JTextField();
54 add(salaryField);
55
56 submitButton = new JButton("Submit");
57 add(submitButton);
58
59 submitButton.addActionListener(e -> saveToDatabase());
60
61 pack();
62 setVisible(true);
63 }
64
65 private void saveToDatabase() {
66 String eno = enoField.getText();
67 String ename = enameField.getText();
68 String desg = desgField.getText();
69 String salary = salaryField.getText();
70
71 try {
72 Class.forName("com.mysql.cj.jdbc.Driver");
73 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
74 String query = "INSERT INTO Employee (Eno, EName, Designation, Salary) VALUES (?, ?, ?, ?)";
75 PreparedStatement ps = con.prepareStatement(query);
76 ps.setString(1, eno);
77 ps.setString(2, ename);
78 ps.setString(3, desg);
79 ps.setString(4, salary);
80 ps.executeUpdate();
81 JOptionPane.showMessageDialog(this, "Employee details saved successfully!");
82 con.close();
83 } catch (Exception ex) {
84 JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
85 }
86 }
87
88 public static void main(String[] args) {
89 new EmployeeForm();
90 }
91}
92--------------------------------------------------------------------------------
93
94Slip 2
95==========
96Question 1: Write a Java program to accept a string from the user and count the number of vowels in it.
97--------------------------------------------------------------------------------
98import java.util.Scanner;
99
100public class VowelCounter {
101 public static void main(String[] args) {
102 Scanner sc = new Scanner(System.in);
103 System.out.print("Enter a string: ");
104 String str = sc.nextLine().toLowerCase();
105 int vowelCount = 0;
106
107 for (char ch : str.toCharArray()) {
108 if ("aeiou".indexOf(ch) != -1) {
109 vowelCount++;
110 }
111 }
112
113 System.out.println("Number of vowels: " + vowelCount);
114 sc.close();
115 }
116}
117--------------------------------------------------------------------------------
118
119Question 2: Write a JSP program to accept two numbers from the user and display their sum.
120--------------------------------------------------------------------------------
121<html>
122<body>
123<h2>Sum of Two Numbers</h2>
124<form method="post">
125 Enter First Number: <input type="text" name="num1"><br>
126 Enter Second Number: <input type="text" name="num2"><br>
127 <input type="submit" value="Calculate">
128</form>
129<%
130 String num1Str = request.getParameter("num1");
131 String num2Str = request.getParameter("num2");
132 if (num1Str != null && num2Str != null) {
133 int num1 = Integer.parseInt(num1Str);
134 int num2 = Integer.parseInt(num2Str);
135 int sum = num1 + num2;
136%>
137<p>Sum: <%= sum %></p>
138<% } %>
139</body>
140</html>
141--------------------------------------------------------------------------------
142
143Slip 3
144==========
145Question 1: Write a JSP program to display the details of Patient (PNo, PName, Address, Age, Disease) in tabular form on browser.
146--------------------------------------------------------------------------------
147<%@ page import="java.sql.*" %>
148<html>
149<body>
150<h2>Patient Details</h2>
151<table border="1">
152 <tr>
153 <th>PNo</th>
154 <th>PName</th>
155 <th>Address</th>
156 <th>Age</th>
157 <th>Disease</th>
158 </tr>
159 <%
160 try {
161 Class.forName("com.mysql.cj.jdbc.Driver");
162 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
163 Statement stmt = con.createStatement();
164 ResultSet rs = stmt.executeQuery("SELECT * FROM Patient");
165 while (rs.next()) {
166 %>
167 <tr>
168 <td><%= rs.getString("PNo") %></td>
169 <td><%= rs.getString("PName") %></td>
170 <td><%= rs.getString("Address") %></td>
171 <td><%= rs.getInt("Age") %></td>
172 <td><%= rs.getString("Disease") %></td>
173 </tr>
174 <%
175 }
176 con.close();
177 } catch (Exception e) {
178 out.println("Error: " + e.getMessage());
179 }
180 %>
181</table>
182</body>
183</html>
184--------------------------------------------------------------------------------
185
186Question 2: Write a Java program to create LinkedList of String objects and perform the following...
187--------------------------------------------------------------------------------
188import java.util.LinkedList;
189import java.util.Collections;
190
191public class LinkedListDemo {
192 public static void main(String[] args) {
193 LinkedList<String> list = new LinkedList<>();
194
195 // Adding elements
196 list.add("Apple");
197 list.add("Banana");
198 list.add("Cherry");
199
200 // i. Add element at the end
201 list.addLast("Dragonfruit");
202 System.out.println("After adding at end: " + list);
203
204 // ii. Delete first element
205 list.removeFirst();
206 System.out.println("After deleting first element: " + list);
207
208 // iii. Display in reverse order
209 Collections.reverse(list);
210 System.out.println("In reverse order: " + list);
211 }
212}
213--------------------------------------------------------------------------------
214
215Slip 8
216==========
217Question 1: Write a Java program to define a thread for printing text on output screen...
218--------------------------------------------------------------------------------
219public class TextThread extends Thread {
220 private String text;
221 private int times;
222
223 public TextThread(String text, int times) {
224 this.text = text;
225 this.times = times;
226 }
227
228 public void run() {
229 for (int i = 0; i < times; i++) {
230 System.out.println(text);
231 try {
232 Thread.sleep(500); // Small delay for visibility
233 } catch (InterruptedException e) {
234 e.printStackTrace();
235 }
236 }
237 }
238
239 public static void main(String[] args) {
240 TextThread t1 = new TextThread("COVID19", 10);
241 TextThread t2 = new TextThread("LOCKDOWN2020", 20);
242 TextThread t3 = new TextThread("VACCINATED2021", 30);
243
244 t1.start();
245 t2.start();
246 t3.start();
247 }
248}
249--------------------------------------------------------------------------------
250
251Question 2: Write a JSP program to check whether a given number is prime or not...
252--------------------------------------------------------------------------------
253<html>
254<body>
255<h2>Prime Number Check</h2>
256<form method="post">
257 Enter Number: <input type="text" name="num">
258 <input type="submit" value="Check">
259</form>
260<%
261 String numStr = request.getParameter("num");
262 if (numStr != null) {
263 int num = Integer.parseInt(numStr);
264 boolean isPrime = true;
265 if (num <= 1) isPrime = false;
266 for (int i = 2; i <= Math.sqrt(num); i++) {
267 if (num % i == 0) {
268 isPrime = false;
269 break;
270 }
271 }
272%>
273<p style="color:red;">
274<%
275 if (isPrime) {
276 out.println(num + " is a Prime Number");
277 } else {
278 out.println(num + " is not a Prime Number");
279 }
280%>
281</p>
282<% } %>
283</body>
284</html>
285--------------------------------------------------------------------------------
286
287Slip 12
288===========
289Question 1: Write a Java program to create an ArrayList of integers and find the maximum and minimum values.
290--------------------------------------------------------------------------------
291import java.util.ArrayList;
292import java.util.Collections;
293
294public class ArrayListMinMax {
295 public static void main(String[] args) {
296 ArrayList<Integer> numbers = new ArrayList<>();
297 numbers.add(45);
298 numbers.add(12);
299 numbers.add(78);
300 numbers.add(23);
301 numbers.add(56);
302
303 int max = Collections.max(numbers);
304 int min = Collections.min(numbers);
305
306 System.out.println("Maximum value: " + max);
307 System.out.println("Minimum value: " + min);
308 }
309}
310--------------------------------------------------------------------------------
311
312Question 2: Write a Servlet program to accept a number from the user and check if it’s even or odd.
313--------------------------------------------------------------------------------
314<!-- evenodd.html -->
315<html>
316<body>
317<h2>Check Even or Odd</h2>
318<form action="EvenOddServlet" method="post">
319 Enter Number: <input type="text" name="number"><br>
320 <input type="submit" value="Check">
321</form>
322</body>
323</html>
324
325<!-- EvenOddServlet.java -->
326import java.io.*;
327import javax.servlet.*;
328import javax.servlet.http.*;
329
330public class EvenOddServlet extends HttpServlet {
331 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
332 response.setContentType("text/html");
333 PrintWriter out = response.getWriter();
334
335 String numStr = request.getParameter("number");
336 int num = Integer.parseInt(numStr);
337
338 out.println("<html><body>");
339 out.println("<h2>Number Check</h2>");
340 if (num % 2 == 0) {
341 out.println("<p>" + num + " is Even</p>");
342 } else {
343 out.println("<p>" + num + " is Odd</p>");
344 }
345 out.println("</body></html>");
346 }
347}
348--------------------------------------------------------------------------------
349
350Slip 15
351===========
352Question 1: Write a Java program to display name and priority of a Thread.
353--------------------------------------------------------------------------------
354public class ThreadInfo {
355 public static void main(String[] args) {
356 Thread t1 = new Thread("Thread-1");
357 Thread t2 = new Thread("Thread-2");
358
359 t1.setPriority(Thread.MIN_PRIORITY); // 1
360 t2.setPriority(Thread.MAX_PRIORITY); // 10
361
362 System.out.println("Thread Name: " + t1.getName() + ", Priority: " + t1.getPriority());
363 System.out.println("Thread Name: " + t2.getName() + ", Priority: " + t2.getPriority());
364 }
365}
366--------------------------------------------------------------------------------
367
368Question 2: Write a SERVLET program which counts how many times a user has visited a web page...
369--------------------------------------------------------------------------------
370import java.io.*;
371import javax.servlet.*;
372import javax.servlet.http.*;
373
374public class VisitCounterServlet extends HttpServlet {
375 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
376 response.setContentType("text/html");
377 PrintWriter out = response.getWriter();
378
379 Cookie[] cookies = request.getCookies();
380 int visitCount = 0;
381
382 if (cookies != null) {
383 for (Cookie cookie : cookies) {
384 if (cookie.getName().equals("visitCount")) {
385 visitCount = Integer.parseInt(cookie.getValue());
386 }
387 }
388 }
389
390 visitCount++;
391 Cookie visitCookie = new Cookie("visitCount", String.valueOf(visitCount));
392 response.addCookie(visitCookie);
393
394 out.println("<html><body>");
395 if (visitCount == 1) {
396 out.println("<h2>Welcome! This is your first visit.</h2>");
397 } else {
398 out.println("<h2>You have visited this page " + visitCount + " times.</h2>");
399 }
400 out.println("</body></html>");
401 }
402}
403--------------------------------------------------------------------------------
404
405Slip 16
406===========
407Question 1: Write a Java program to create a TreeSet, add some colors...
408--------------------------------------------------------------------------------
409import java.util.TreeSet;
410
411public class TreeSetDemo {
412 public static void main(String[] args) {
413 TreeSet<String> colors = new TreeSet<>();
414 colors.add("Red");
415 colors.add("Blue");
416 colors.add("Green");
417 colors.add("Yellow");
418
419 System.out.println("Colors in ascending order: " + colors);
420 }
421}
422--------------------------------------------------------------------------------
423
424Question 2: Write a Java program to accept the details of Teacher...
425--------------------------------------------------------------------------------
426import java.sql.*;
427
428public class TeacherDetails {
429 public static void main(String[] args) {
430 try {
431 Class.forName("com.mysql.cj.jdbc.Driver");
432 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
433 Statement stmt = con.createStatement();
434 stmt.executeUpdate("CREATE TABLE Teacher (TNo INT, TName VARCHAR(50), Subject VARCHAR(50))");
435
436 PreparedStatement ps = con.prepareStatement("INSERT INTO Teacher VALUES (?, ?, ?)");
437 ps.setInt(1, 1); ps.setString(2, "John"); ps.setString(3, "JAVA"); ps.executeUpdate();
438 ps.setInt(1, 2); ps.setString(2, "Alice"); ps.setString(3, "Python"); ps.executeUpdate();
439 ps.setInt(1, 3); ps.setString(2, "Bob"); ps.setString(3, "JAVA"); ps.executeUpdate();
440 ps.setInt(1, 4); ps.setString(2, "Emma"); ps.setString(3, "C++"); ps.executeUpdate();
441 ps.setInt(1, 5); ps.setString(2, "Mike"); ps.setString(3, "JAVA"); ps.executeUpdate();
442
443 ps = con.prepareStatement("SELECT * FROM Teacher WHERE Subject = ?");
444 ps.setString(1, "JAVA");
445 ResultSet rs = ps.executeQuery();
446 while (rs.next()) {
447 System.out.println(rs.getInt("TNo") + " " + rs.getString("TName") + " " + rs.getString("Subject"));
448 }
449 con.close();
450 } catch (Exception e) {
451 e.printStackTrace();
452 }
453 }
454}
455--------------------------------------------------------------------------------
456
457Slip 18
458===========
459Question 1: Write a Java program using Multithreading to display all the vowels from a given String...
460--------------------------------------------------------------------------------
461import java.util.Scanner;
462
463public class VowelThread extends Thread {
464 private String input;
465
466 public VowelThread(String input) {
467 this.input = input;
468 }
469
470 public void run() {
471 for (char ch : input.toLowerCase().toCharArray()) {
472 if ("aeiou".indexOf(ch) != -1) {
473 System.out.println(ch);
474 try {
475 Thread.sleep(3000); // 3 seconds delay
476 } catch (InterruptedException e) {
477 e.printStackTrace();
478 }
479 }
480 }
481 }
482
483 public static void main(String[] args) {
484 Scanner sc = new Scanner(System.in);
485 System.out.print("Enter a string: ");
486 String str = sc.nextLine();
487 VowelThread thread = new VowelThread(str);
488 thread.start();
489 sc.close();
490 }
491}
492--------------------------------------------------------------------------------
493
494Question 2: Write a SERVLET program in Java to accept details of student...
495--------------------------------------------------------------------------------
496<!-- student.html -->
497<html>
498<body>
499<h2>Enter Student Details</h2>
500<form action="StudentServlet" method="post">
501 Seat No: <input type="text" name="seatNo"><br>
502 Name: <input type="text" name="studName"><br>
503 Class: <input type="text" name="className"><br>
504 Total Marks: <input type="text" name="totalMarks"><br>
505 <input type="submit" value="Submit">
506</form>
507</body>
508</html>
509
510<!-- StudentServlet.java -->
511import java.io.*;
512import javax.servlet.*;
513import javax.servlet.http.*;
514
515public class StudentServlet extends HttpServlet {
516 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
517 response.setContentType("text/html");
518 PrintWriter out = response.getWriter();
519
520 String seatNo = request.getParameter("seatNo");
521 String studName = request.getParameter("studName");
522 String className = request.getParameter("className");
523 double totalMarks = Double.parseDouble(request.getParameter("totalMarks"));
524
525 double percentage = (totalMarks / 500.0) * 100; // Assuming 500 is max marks
526 String grade = (percentage >= 80) ? "A" : (percentage >= 60) ? "B" : (percentage >= 40) ? "C" : "D";
527
528 out.println("<html><body>");
529 out.println("<h2>Student Details</h2>");
530 out.println("Seat No: " + seatNo + "<br>");
531 out.println("Name: " + studName + "<br>");
532 out.println("Class: " + className + "<br>");
533 out.println("Total Marks: " + totalMarks + "<br>");
534 out.println("Percentage: " + percentage + "%<br>");
535 out.println("Grade: " + grade);
536 out.println("</body></html>");
537 }
538}
539--------------------------------------------------------------------------------
540
541Slip 19
542===========
543Question 1: Write a Java program to accept ‘N’ Integers from a user store them into LinkedList...
544--------------------------------------------------------------------------------
545import java.util.LinkedList;
546import java.util.Scanner;
547
548public class NegativeIntegers {
549 public static void main(String[] args) {
550 Scanner sc = new Scanner(System.in);
551 LinkedList<Integer> numbers = new LinkedList<>();
552
553 System.out.print("Enter the number of integers (N): ");
554 int n = sc.nextInt();
555
556 System.out.println("Enter " + n + " integers:");
557 for (int i = 0; i < n; i++) {
558 numbers.add(sc.nextInt());
559 }
560
561 System.out.println("Negative integers:");
562 for (int num : numbers) {
563 if (num < 0) {
564 System.out.println(num);
565 }
566 }
567 sc.close();
568 }
569}
570--------------------------------------------------------------------------------
571
572Question 2: Write a Java program using SERVLET to accept username and password...
573--------------------------------------------------------------------------------
574<!-- login.html -->
575<html>
576<body>
577<h2>Login</h2>
578<form action="LoginServlet" method="post">
579 Username: <input type="text" name="username"><br>
580 Password: <input type="password" name="password"><br>
581 <input type="submit" value="Login">
582</form>
583</body>
584</html>
585
586<!-- LoginServlet.java -->
587import java.io.*;
588import java.sql.*;
589import javax.servlet.*;
590import javax.servlet.http.*;
591
592public class LoginServlet extends HttpServlet {
593 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
594 response.setContentType("text/html");
595 PrintWriter out = response.getWriter();
596
597 String username = request.getParameter("username");
598 String password = request.getParameter("password");
599
600 try {
601 Class.forName("com.mysql.cj.jdbc.Driver");
602 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
603 PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?");
604 ps.setString(1, username);
605 ps.setString(2, password);
606 ResultSet rs = ps.executeQuery();
607
608 out.println("<html><body>");
609 if (rs.next()) {
610 out.println("<h2>Login Successful!</h2>");
611 } else {
612 out.println("<h2>Error: Invalid username or password</h2>");
613 }
614 out.println("</body></html>");
615 con.close();
616 } catch (Exception e) {
617 out.println("Error: " + e.getMessage());
618 }
619 }
620}
621--------------------------------------------------------------------------------
622
623Slip 22
624===========
625Question 1: Write a Menu Driven program in Java for the following...
626--------------------------------------------------------------------------------
627import java.sql.*;
628import java.util.Scanner;
629
630public class EmployeeMenu {
631 public static void main(String[] args) {
632 Scanner sc = new Scanner(System.in);
633 try {
634 Class.forName("com.mysql.cj.jdbc.Driver");
635 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
636 Statement stmt = con.createStatement();
637 stmt.executeUpdate("CREATE TABLE IF NOT EXISTS Employee (ENo INT, EName VARCHAR(50), Salary DOUBLE)");
638
639 while (true) {
640 System.out.println("\n1. Insert\n2. Update\n3. Display\n4. Exit");
641 System.out.print("Enter choice: ");
642 int choice = sc.nextInt();
643
644 switch (choice) {
645 case 1: // Insert
646 System.out.print("Enter ENo: ");
647 int eno = sc.nextInt();
648 System.out.print("Enter EName: ");
649 String ename = sc.next();
650 System.out.print("Enter Salary: ");
651 double salary = sc.nextDouble();
652 PreparedStatement ps = con.prepareStatement("INSERT INTO Employee VALUES (?, ?, ?)");
653 ps.setInt(1, eno);
654 ps.setString(2, ename);
655 ps.setDouble(3, salary);
656 ps.executeUpdate();
657 System.out.println("Record inserted.");
658 break;
659
660 case 2: // Update
661 System.out.print("Enter ENo to update: ");
662 eno = sc.nextInt();
663 System.out.print("Enter new Salary: ");
664 salary = sc.nextDouble();
665 ps = con.prepareStatement("UPDATE Employee SET Salary = ? WHERE ENo = ?");
666 ps.setDouble(1, salary);
667 ps.setInt(2, eno);
668 ps.executeUpdate();
669 System.out.println("Record updated.");
670 break;
671
672 case 3: // Display
673 ResultSet rs = stmt.executeQuery("SELECT * FROM Employee");
674 while (rs.next()) {
675 System.out.println(rs.getInt("ENo") + " " + rs.getString("EName") + " " + rs.getDouble("Salary"));
676 }
677 break;
678
679 case 4: // Exit
680 con.close();
681 sc.close();
682 System.exit(0);
683
684 default:
685 System.out.println("Invalid choice!");
686 }
687 }
688 } catch (Exception e) {
689 e.printStackTrace();
690 }
691 }
692}
693--------------------------------------------------------------------------------
694
695Question 2: Write a JSP program which accepts UserName in a TextField and greets the user...
696--------------------------------------------------------------------------------
697<%@ page import="java.util.Calendar" %>
698<html>
699<body>
700<h2>Greeting</h2>
701<form method="post">
702 Enter Username: <input type="text" name="username">
703 <input type="submit" value="Greet">
704</form>
705<%
706 String username = request.getParameter("username");
707 if (username != null) {
708 Calendar cal = Calendar.getInstance();
709 int hour = cal.get(Calendar.HOUR_OF_DAY);
710 String greeting;
711 if (hour < 12) {
712 greeting = "Good Morning";
713 } else if (hour < 17) {
714 greeting = "Good Afternoon";
715 } else {
716 greeting = "Good Evening";
717 }
718%>
719<p><%= greeting %>, <%= username %>!</p>
720<% } %>
721</body>
722</html>
723--------------------------------------------------------------------------------
724
725Slip 23
726===========
727Question 1: Write a Java program using Multithreading to accept a String from a user...
728--------------------------------------------------------------------------------
729import java.util.Scanner;
730
731public class VowelDisplayThread extends Thread {
732 private String input;
733
734 public VowelDisplayThread(String input) {
735 this.input = input;
736 }
737
738 public void run() {
739 for (char ch : input.toLowerCase().toCharArray()) {
740 if ("aeiou".indexOf(ch) != -1) {
741 System.out.println(ch);
742 try {
743 Thread.sleep(3000);
744 } catch (InterruptedException e) {
745 e.printStackTrace();
746 }
747 }
748 }
749 }
750
751 public static void main(String[] args) {
752 Scanner sc = new Scanner(System.in);
753 System.out.print("Enter a string: ");
754 String str = sc.nextLine();
755 VowelDisplayThread thread = new VowelDisplayThread(str);
756 thread.start();
757 sc.close();
758 }
759}
760--------------------------------------------------------------------------------
761
762Question 2: Write a Java program to accept 'N' student names through command line...
763--------------------------------------------------------------------------------
764import java.util.ArrayList;
765import java.util.Iterator;
766import java.util.ListIterator;
767
768public class StudentNames {
769 public static void main(String[] args) {
770 if (args.length == 0) {
771 System.out.println("Please provide student names as command-line arguments.");
772 return;
773 }
774
775 ArrayList<String> students = new ArrayList<>();
776 for (String name : args) {
777 students.add(name);
778 }
779
780 System.out.println("Using Iterator:");
781 Iterator<String> iterator = students.iterator();
782 while (iterator.hasNext()) {
783 System.out.println(iterator.next());
784 }
785
786 System.out.println("\nUsing ListIterator (reverse order):");
787 ListIterator<String> listIterator = students.listIterator(students.size());
788 while (listIterator.hasPrevious()) {
789 System.out.println(listIterator.previous());
790 }
791 }
792}
793--------------------------------------------------------------------------------
794
795Slip 25
796===========
797Question 1: Write a Java program using multithreading to print even numbers from 1 to 20 with a 1-second delay.
798--------------------------------------------------------------------------------
799public class EvenNumberThread extends Thread {
800 public void run() {
801 for (int i = 1; i <= 20; i++) {
802 if (i % 2 == 0) {
803 System.out.println(i);
804 try {
805 Thread.sleep(1000); // 1-second delay
806 } catch (InterruptedException e) {
807 e.printStackTrace();
808 }
809 }
810 }
811 }
812
813 public static void main(String[] args) {
814 EvenNumberThread thread = new EvenNumberThread();
815 thread.start();
816 }
817}
818--------------------------------------------------------------------------------
819
820Question 2: Write a JSP program to display the current date and time.
821--------------------------------------------------------------------------------
822<%@ page import="java.util.Date" %>
823<html>
824<body>
825<h2>Current Date and Time</h2>
826<p>Current Date and Time: <%= new Date() %></p>
827</body>
828</html>
829--------------------------------------------------------------------------------
830
831Slip 26
832===========
833Question 1: Write a Java program to delete the details of given employee...
834--------------------------------------------------------------------------------
835import java.sql.*;
836import java.util.Scanner;
837
838public class DeleteEmployee {
839 public static void main(String[] args) {
840 Scanner sc = new Scanner(System.in);
841 System.out.print("Enter Employee ID to delete: ");
842 int eno = sc.nextInt();
843
844 try {
845 Class.forName("com.mysql.cj.jdbc.Driver");
846 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
847 PreparedStatement ps = con.prepareStatement("DELETE FROM Employee WHERE ENo = ?");
848 ps.setInt(1, eno);
849 int rows = ps.executeUpdate();
850 if (rows > 0) {
851 System.out.println("Employee with ENo " + eno + " deleted successfully.");
852 } else {
853 System.out.println("No employee found with ENo " + eno);
854 }
855 con.close();
856 } catch (Exception e) {
857 e.printStackTrace();
858 }
859 sc.close();
860 }
861}
862--------------------------------------------------------------------------------
863
864Question 2: Write a JSP program to calculate sum of first and last digit of a given number...
865--------------------------------------------------------------------------------
866<html>
867<body>
868<h2>Sum of First and Last Digit</h2>
869<form method="post">
870 Enter Number: <input type="text" name="num">
871 <input type="submit" value="Calculate">
872</form>
873<%
874 String numStr = request.getParameter("num");
875 if (numStr != null) {
876 int num = Integer.parseInt(numStr);
877 int firstDigit = num;
878 while (firstDigit >= 10) {
879 firstDigit /= 10;
880 }
881 int lastDigit = num % 10;
882 int sum = firstDigit + lastDigit;
883%>
884<p style="color:red; font-size:18px;">Sum of first and last digit: <%= sum %></p>
885<% } %>
886</body>
887</html>
888--------------------------------------------------------------------------------
889
890Slip 28
891===========
892Question 1: Write a JSP script to accept a String from a user and display it in reverse order.
893--------------------------------------------------------------------------------
894<html>
895<body>
896<h2>Reverse String</h2>
897<form method="post">
898 Enter String: <input type="text" name="str">
899 <input type="submit" value="Reverse">
900</form>
901<%
902 String str = request.getParameter("str");
903 if (str != null) {
904 StringBuilder reversed = new StringBuilder(str).reverse();
905%>
906<p>Reversed String: <%= reversed %></p>
907<% } %>
908</body>
909</html>
910--------------------------------------------------------------------------------
911
912Question 2: Write a Java program to display name of currently executing Thread in multithreading.
913--------------------------------------------------------------------------------
914public class CurrentThreadDemo extends Thread {
915 public CurrentThreadDemo(String name) {
916 super(name);
917 }
918
919 public void run() {
920 System.out.println("Currently executing thread: " + Thread.currentThread().getName());
921 }
922
923 public static void main(String[] args) {
924 CurrentThreadDemo t1 = new CurrentThreadDemo("Thread-1");
925 CurrentThreadDemo t2 = new CurrentThreadDemo("Thread-2");
926
927 t1.start();
928 t2.start();
929 }
930}
931--------------------------------------------------------------------------------
932
933Slip 29
934===========
935Question 1: Write a Java program to display information about all columns in the DONAR table...
936--------------------------------------------------------------------------------
937import java.sql.*;
938
939public class DonarMetaData2 {
940 public static void main(String[] args) {
941 try {
942 Class.forName("com.mysql.cj.jdbc.Driver");
943 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
944 PreparedStatement ps = con.prepareStatement("SELECT * FROM DONAR");
945 ResultSet rs = ps.executeQuery();
946 ResultSetMetaData rsmd = rs.getMetaData();
947
948 int columnCount = rsmd.getColumnCount();
949 System.out.println("Total Columns: " + columnCount);
950 for (int i = 1; i <= columnCount; i++) {
951 System.out.println("Column " + i + ":");
952 System.out.println("Name: " + rsmd.getColumnName(i));
953 System.out.println("Type: " + rsmd.getColumnTypeName(i));
954 System.out.println("Size: " + rsmd.getColumnDisplaySize(i));
955 System.out.println();
956 }
957 con.close();
958 } catch (Exception e) {
959 e.printStackTrace();
960 }
961 }
962}
963--------------------------------------------------------------------------------
964
965Question 2: Write a Java program to create LinkedList of integer objects and perform the following...
966--------------------------------------------------------------------------------
967import java.util.LinkedList;
968
969public class LinkedListOperations {
970 public static void main(String[] args) {
971 LinkedList<Integer> list = new LinkedList<>();
972
973 // Adding some initial elements
974 list.add(10);
975 list.add(20);
976 list.add(30);
977
978 // i. Add element at first position
979 list.addFirst(5);
980 System.out.println("After adding at first position: " + list);
981
982 // ii. Delete last element
983 list.removeLast();
984 System.out.println("After deleting last element: " + list);
985
986 // iii. Display the size of the list
987 System.out.println("Size of the list: " + list.size());
988 }
989}
990--------------------------------------------------------------------------------