Advertisement
dudehost

Java-Slip-Solutions

Mar 16th, 2025 (edited)
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 35.73 KB | None | 0 0
  1. Slip 1
  2. ==========
  3. Question 1: Write a Java program using Multithreading to display all the alphabets between 'A' to 'Z' after every 2 seconds.
  4. --------------------------------------------------------------------------------
  5. public 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.  
  24. Question 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. --------------------------------------------------------------------------------
  26. import javax.swing.*;
  27. import java.awt.*;
  28. import java.awt.event.*;
  29. import java.sql.*;
  30.  
  31. public 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("org.postgresql.Driver");
  73.             Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres", "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.  
  94. Slip 2
  95. ==========
  96. Question 1: Write a Java program to accept a string from the user and count the number of vowels in it.
  97. --------------------------------------------------------------------------------
  98. import java.util.Scanner;
  99.  
  100. public 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.  
  119. Question 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.  
  143. Slip 3
  144. ==========
  145. Question 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("org.postgresql.Driver");
  162.             Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres", "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.  
  186. Question 2: Write a Java program to create LinkedList of String objects and perform the following...
  187. --------------------------------------------------------------------------------
  188. import java.util.LinkedList;
  189. import java.util.Collections;
  190.  
  191. public 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.  
  215. Slip 8
  216. ==========
  217. Question 1: Write a Java program to define a thread for printing text on output screen...
  218. --------------------------------------------------------------------------------
  219. public 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.  
  251. Question 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.  
  287. Slip 12
  288. ===========
  289. Question 1: Write a Java program to create an ArrayList of integers and find the maximum and minimum values.
  290. --------------------------------------------------------------------------------
  291. import java.util.ArrayList;
  292. import java.util.Collections;
  293.  
  294. public 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); // Auto-unboxing from Integer to int
  304.         int min = Collections.min(numbers); // Auto-unboxing from Integer to int
  305.  
  306.         System.out.println("Maximum value: " + max);
  307.         System.out.println("Minimum value: " + min);
  308.     }
  309. }
  310.  
  311. --------------------------------------------------------------------------------
  312.  
  313. Question 2: Write a Servlet program to accept a number from the user and check if it’s even or odd.
  314. --------------------------------------------------------------------------------
  315. <!-- evenodd.html -->
  316. <html>
  317. <body>
  318. <h2>Check Even or Odd</h2>
  319. <form action="EvenOddServlet" method="post">
  320.     Enter Number: <input type="text" name="number"><br>
  321.     <input type="submit" value="Check">
  322. </form>
  323. </body>
  324. </html>
  325.  
  326. <!-- EvenOddServlet.java -->
  327. import java.io.*;
  328. import javax.servlet.*;
  329. import javax.servlet.http.*;
  330.  
  331. public class EvenOddServlet extends HttpServlet {
  332.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  333.         response.setContentType("text/html");
  334.         PrintWriter out = response.getWriter();
  335.  
  336.         String numStr = request.getParameter("number");
  337.         int num = Integer.parseInt(numStr);
  338.  
  339.         out.println("<html><body>");
  340.         out.println("<h2>Number Check</h2>");
  341.         if (num % 2 == 0) {
  342.             out.println("<p>" + num + " is Even</p>");
  343.         } else {
  344.             out.println("<p>" + num + " is Odd</p>");
  345.         }
  346.         out.println("</body></html>");
  347.     }
  348. }
  349.  
  350. <!--web.xml-->
  351. <?xml version="1.0" encoding="UTF-8"?>
  352. <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
  353.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  354.   xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
  355.                      https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
  356.   version="6.0"
  357.   metadata-complete="true">
  358.  
  359. <!-- EvenOdd -->
  360. <servlet>
  361.         <servlet-name>EvenOddServlet</servlet-name>
  362.         <servlet-class>EvenOddServlet</servlet-class>
  363.     </servlet>
  364.  
  365.     <servlet-mapping>
  366.         <servlet-name>EvenOddServlet</servlet-name>
  367.         <url-pattern>/EvenOddServlet</url-pattern>
  368.     </servlet-mapping>
  369.  
  370.    
  371.  
  372.     <error-page>
  373.         <error-code>403</error-code>
  374.         <location>/WEB-INF/jsp/403.jsp</location>
  375.     </error-page>
  376.  
  377. </web-app>
  378. --------------------------------------------------------------------------------
  379.  
  380. Slip 15
  381. ===========
  382. Question 1: Write a Java program to display name and priority of a Thread.
  383. --------------------------------------------------------------------------------
  384. public class ThreadInfo {
  385.     public static void main(String[] args) {
  386.         Thread t1 = new Thread("Thread-1");
  387.         Thread t2 = new Thread("Thread-2");
  388.  
  389.         t1.setPriority(Thread.MIN_PRIORITY); // 1
  390.         t2.setPriority(Thread.MAX_PRIORITY); // 10
  391.  
  392.         System.out.println("Thread Name: " + t1.getName() + ", Priority: " + t1.getPriority());
  393.         System.out.println("Thread Name: " + t2.getName() + ", Priority: " + t2.getPriority());
  394.     }
  395. }
  396. --------------------------------------------------------------------------------
  397.  
  398. Question 2: Write a SERVLET program which counts how many times a user has visited a web page...
  399. --------------------------------------------------------------------------------
  400. import java.io.*;
  401. import javax.servlet.*;
  402. import javax.servlet.http.*;
  403.  
  404. public class VisitCounterServlet extends HttpServlet {
  405.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  406.         response.setContentType("text/html");
  407.         PrintWriter out = response.getWriter();
  408.  
  409.         Cookie[] cookies = request.getCookies();
  410.         int visitCount = 0;
  411.  
  412.         if (cookies != null) {
  413.             for (Cookie cookie : cookies) {
  414.                 if (cookie.getName().equals("visitCount")) {
  415.                     visitCount = Integer.parseInt(cookie.getValue());
  416.                 }
  417.             }
  418.         }
  419.  
  420.         visitCount++;
  421.         Cookie visitCookie = new Cookie("visitCount", String.valueOf(visitCount));
  422.         response.addCookie(visitCookie);
  423.  
  424.         out.println("<html><body>");
  425.         if (visitCount == 1) {
  426.             out.println("<h2>Welcome! This is your first visit.</h2>");
  427.         } else {
  428.             out.println("<h2>You have visited this page " + visitCount + " times.</h2>");
  429.         }
  430.         out.println("</body></html>");
  431.     }
  432. }
  433. --------------------------------------------------------------------------------
  434.  
  435. Slip 16
  436. ===========
  437. Question 1: Write a Java program to create a TreeSet, add some colors...
  438. --------------------------------------------------------------------------------
  439. import java.util.TreeSet;
  440.  
  441. public class TreeSetDemo {
  442.     public static void main(String[] args) {
  443.         TreeSet<String> colors = new TreeSet<>();
  444.         colors.add("Red");
  445.         colors.add("Blue");
  446.         colors.add("Green");
  447.         colors.add("Yellow");
  448.  
  449.         System.out.println("Colors in ascending order: " + colors);
  450.     }
  451. }
  452. --------------------------------------------------------------------------------
  453.  
  454. Question 2: Write a Java program to accept the details of Teacher...
  455. --------------------------------------------------------------------------------
  456. import java.sql.*;
  457.  
  458. public class TeacherDetails {
  459.     public static void main(String[] args) {
  460.         try {
  461.             Class.forName("org.postgresql.Driver");
  462.             Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres", "password");
  463.             Statement stmt = con.createStatement();
  464.             stmt.executeUpdate("CREATE TABLE IF NOT EXISTS Teacher (TNo INT, TName VARCHAR(50), Subject VARCHAR(50))");
  465.  
  466.             PreparedStatement ps = con.prepareStatement("INSERT INTO Teacher VALUES (?, ?, ?)");
  467.             ps.setInt(1, 1); ps.setString(2, "John"); ps.setString(3, "JAVA"); ps.executeUpdate();
  468.             ps.setInt(1, 2); ps.setString(2, "Alice"); ps.setString(3, "Python"); ps.executeUpdate();
  469.             ps.setInt(1, 3); ps.setString(2, "Bob"); ps.setString(3, "JAVA"); ps.executeUpdate();
  470.             ps.setInt(1, 4); ps.setString(2, "Emma"); ps.setString(3, "C++"); ps.executeUpdate();
  471.             ps.setInt(1, 5); ps.setString(2, "Mike"); ps.setString(3, "JAVA"); ps.executeUpdate();
  472.  
  473.             ps = con.prepareStatement("SELECT * FROM Teacher WHERE Subject = ?");
  474.             ps.setString(1, "JAVA");
  475.             ResultSet rs = ps.executeQuery();
  476.             while (rs.next()) {
  477.                 System.out.println(rs.getInt("TNo") + " " + rs.getString("TName") + " " + rs.getString("Subject"));
  478.             }
  479.             con.close();
  480.         } catch (Exception e) {
  481.             e.printStackTrace();
  482.         }
  483.     }
  484. }
  485. --------------------------------------------------------------------------------
  486.  
  487. Slip 18
  488. ===========
  489. Question 1: Write a Java program using Multithreading to display all the vowels from a given String...
  490. --------------------------------------------------------------------------------
  491. import java.util.Scanner;
  492.  
  493. public class VowelThread extends Thread {
  494.     private String input;
  495.  
  496.     public VowelThread(String input) {
  497.         this.input = input;
  498.     }
  499.  
  500.     public void run() {
  501.         for (char ch : input.toLowerCase().toCharArray()) {
  502.             if ("aeiou".indexOf(ch) != -1) {
  503.                 System.out.println(ch);
  504.                 try {
  505.                     Thread.sleep(3000); // 3 seconds delay
  506.                 } catch (InterruptedException e) {
  507.                     e.printStackTrace();
  508.                 }
  509.             }
  510.         }
  511.     }
  512.  
  513.     public static void main(String[] args) {
  514.         Scanner sc = new Scanner(System.in);
  515.         System.out.print("Enter a string: ");
  516.         String str = sc.nextLine();
  517.         VowelThread thread = new VowelThread(str);
  518.         thread.start();
  519.         sc.close();
  520.     }
  521. }
  522. --------------------------------------------------------------------------------
  523.  
  524. Question 2: Write a SERVLET program in Java to accept details of student...
  525. --------------------------------------------------------------------------------
  526. <!-- student.html -->
  527. <html>
  528. <body>
  529. <h2>Enter Student Details</h2>
  530. <form action="StudentServlet" method="post">
  531.     Seat No: <input type="text" name="seatNo"><br>
  532.     Name: <input type="text" name="studName"><br>
  533.     Class: <input type="text" name="className"><br>
  534.     Total Marks: <input type="text" name="totalMarks"><br>
  535.     <input type="submit" value="Submit">
  536. </form>
  537. </body>
  538. </html>
  539.  
  540. <!-- StudentServlet.java -->
  541. import java.io.*;
  542. import javax.servlet.*;
  543. import javax.servlet.http.*;
  544.  
  545. public class StudentServlet extends HttpServlet {
  546.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  547.         response.setContentType("text/html");
  548.         PrintWriter out = response.getWriter();
  549.  
  550.         String seatNo = request.getParameter("seatNo");
  551.         String studName = request.getParameter("studName");
  552.         String className = request.getParameter("className");
  553.         double totalMarks = Double.parseDouble(request.getParameter("totalMarks"));
  554.  
  555.         double percentage = (totalMarks / 500.0) * 100; // Assuming 500 is max marks
  556.         String grade = (percentage >= 80) ? "A" : (percentage >= 60) ? "B" : (percentage >= 40) ? "C" : "D";
  557.  
  558.         out.println("<html><body>");
  559.         out.println("<h2>Student Details</h2>");
  560.         out.println("Seat No: " + seatNo + "<br>");
  561.         out.println("Name: " + studName + "<br>");
  562.         out.println("Class: " + className + "<br>");
  563.         out.println("Total Marks: " + totalMarks + "<br>");
  564.         out.println("Percentage: " + percentage + "%<br>");
  565.         out.println("Grade: " + grade);
  566.         out.println("</body></html>");
  567.     }
  568. }
  569. --------------------------------------------------------------------------------
  570.  
  571. Slip 19
  572. ===========
  573. Question 1: Write a Java program to accept ‘N’ Integers from a user store them into LinkedList...
  574. --------------------------------------------------------------------------------
  575. import java.util.LinkedList;
  576. import java.util.Scanner;
  577.  
  578. public class NegativeIntegers {
  579.     public static void main(String[] args) {
  580.         Scanner sc = new Scanner(System.in);
  581.         LinkedList<Integer> numbers = new LinkedList<>();
  582.  
  583.         System.out.print("Enter the number of integers (N): ");
  584.         int n = sc.nextInt();
  585.  
  586.         System.out.println("Enter " + n + " integers:");
  587.         for (int i = 0; i < n; i++) {
  588.             numbers.add(sc.nextInt());
  589.         }
  590.  
  591.         System.out.println("Negative integers:");
  592.         for (int num : numbers) {
  593.             if (num < 0) {
  594.                 System.out.println(num);
  595.             }
  596.         }
  597.         sc.close();
  598.     }
  599. }
  600. --------------------------------------------------------------------------------
  601.  
  602. Question 2: Write a Java program using SERVLET to accept username and password...
  603. --------------------------------------------------------------------------------
  604. <!-- login.html -->
  605. <html>
  606. <body>
  607. <h2>Login</h2>
  608. <form action="LoginServlet" method="post">
  609.     Username: <input type="text" name="username"><br>
  610.     Password: <input type="password" name="password"><br>
  611.     <input type="submit" value="Login">
  612. </form>
  613. </body>
  614. </html>
  615.  
  616. <!-- LoginServlet.java -->
  617. import java.io.*;
  618. import java.sql.*;
  619. import javax.servlet.*;
  620. import javax.servlet.http.*;
  621.  
  622. public class LoginServlet extends HttpServlet {
  623.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  624.         response.setContentType("text/html");
  625.         PrintWriter out = response.getWriter();
  626.  
  627.         String username = request.getParameter("username");
  628.         String password = request.getParameter("password");
  629.  
  630.         try {
  631.             Class.forName("org.postgresql.Driver");
  632.             Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres", "password");
  633.             PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?");
  634.             ps.setString(1, username);
  635.             ps.setString(2, password);
  636.             ResultSet rs = ps.executeQuery();
  637.  
  638.             out.println("<html><body>");
  639.             if (rs.next()) {
  640.                 out.println("<h2>Login Successful!</h2>");
  641.             } else {
  642.                 out.println("<h2>Error: Invalid username or password</h2>");
  643.             }
  644.             out.println("</body></html>");
  645.             con.close();
  646.         } catch (Exception e) {
  647.             out.println("Error: " + e.getMessage());
  648.         }
  649.     }
  650. }
  651. --------------------------------------------------------------------------------
  652.  
  653. Slip 22
  654. ===========
  655. Question 1: Write a Menu Driven program in Java for the following...
  656. --------------------------------------------------------------------------------
  657. import java.sql.*;
  658. import java.util.Scanner;
  659.  
  660. public class EmployeeMenu {
  661.     public static void main(String[] args) {
  662.         Scanner sc = new Scanner(System.in);
  663.         try {
  664.             Class.forName("org.postgresql.Driver");
  665.             Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres", "password");
  666.             Statement stmt = con.createStatement();
  667.             stmt.executeUpdate("CREATE TABLE IF NOT EXISTS Employee (ENo INT, EName VARCHAR(50), Salary DOUBLE)");
  668.  
  669.             while (true) {
  670.                 System.out.println("\n1. Insert\n2. Update\n3. Display\n4. Exit");
  671.                 System.out.print("Enter choice: ");
  672.                 int choice = sc.nextInt();
  673.  
  674.                 switch (choice) {
  675.                     case 1: // Insert
  676.                         System.out.print("Enter ENo: ");
  677.                         int eno = sc.nextInt();
  678.                         System.out.print("Enter EName: ");
  679.                         String ename = sc.next();
  680.                         System.out.print("Enter Salary: ");
  681.                         double salary = sc.nextDouble();
  682.                         PreparedStatement ps = con.prepareStatement("INSERT INTO Employee VALUES (?, ?, ?)");
  683.                         ps.setInt(1, eno);
  684.                         ps.setString(2, ename);
  685.                         ps.setDouble(3, salary);
  686.                         ps.executeUpdate();
  687.                         System.out.println("Record inserted.");
  688.                         break;
  689.  
  690.                     case 2: // Update
  691.                         System.out.print("Enter ENo to update: ");
  692.                         eno = sc.nextInt();
  693.                         System.out.print("Enter new Salary: ");
  694.                         salary = sc.nextDouble();
  695.                         ps = con.prepareStatement("UPDATE Employee SET Salary = ? WHERE ENo = ?");
  696.                         ps.setDouble(1, salary);
  697.                         ps.setInt(2, eno);
  698.                         ps.executeUpdate();
  699.                         System.out.println("Record updated.");
  700.                         break;
  701.  
  702.                     case 3: // Display
  703.                         ResultSet rs = stmt.executeQuery("SELECT * FROM Employee");
  704.                         while (rs.next()) {
  705.                             System.out.println(rs.getInt("ENo") + " " + rs.getString("EName") + " " + rs.getDouble("Salary"));
  706.                         }
  707.                         break;
  708.  
  709.                     case 4: // Exit
  710.                         con.close();
  711.                         sc.close();
  712.                         System.exit(0);
  713.  
  714.                     default:
  715.                         System.out.println("Invalid choice!");
  716.                 }
  717.             }
  718.         } catch (Exception e) {
  719.             e.printStackTrace();
  720.         }
  721.     }
  722. }
  723. --------------------------------------------------------------------------------
  724.  
  725. Question 2: Write a JSP program which accepts UserName in a TextField and greets the user...
  726. --------------------------------------------------------------------------------
  727. <%@ page import="java.util.Calendar" %>
  728. <html>
  729. <body>
  730. <h2>Greeting</h2>
  731. <form method="post">
  732.     Enter Username: <input type="text" name="username">
  733.     <input type="submit" value="Greet">
  734. </form>
  735. <%
  736.     String username = request.getParameter("username");
  737.     if (username != null) {
  738.         Calendar cal = Calendar.getInstance();
  739.         int hour = cal.get(Calendar.HOUR_OF_DAY);
  740.         String greeting;
  741.         if (hour < 12) {
  742.             greeting = "Good Morning";
  743.         } else if (hour < 17) {
  744.             greeting = "Good Afternoon";
  745.         } else {
  746.             greeting = "Good Evening";
  747.         }
  748. %>
  749. <p><%= greeting %>, <%= username %>!</p>
  750. <% } %>
  751. </body>
  752. </html>
  753. --------------------------------------------------------------------------------
  754.  
  755. Slip 23
  756. ===========
  757. Question 1: Write a Java program using Multithreading to accept a String from a user...
  758. --------------------------------------------------------------------------------
  759. import java.util.Scanner;
  760.  
  761. public class VowelDisplayThread extends Thread {
  762.     private String input;
  763.  
  764.     public VowelDisplayThread(String input) {
  765.         this.input = input;
  766.     }
  767.  
  768.     public void run() {
  769.         for (char ch : input.toLowerCase().toCharArray()) {
  770.             if ("aeiou".indexOf(ch) != -1) {
  771.                 System.out.println(ch);
  772.                 try {
  773.                     Thread.sleep(3000);
  774.                 } catch (InterruptedException e) {
  775.                     e.printStackTrace();
  776.                 }
  777.             }
  778.         }
  779.     }
  780.  
  781.     public static void main(String[] args) {
  782.         Scanner sc = new Scanner(System.in);
  783.         System.out.print("Enter a string: ");
  784.         String str = sc.nextLine();
  785.         VowelDisplayThread thread = new VowelDisplayThread(str);
  786.         thread.start();
  787.         sc.close();
  788.     }
  789. }
  790. --------------------------------------------------------------------------------
  791.  
  792. Question 2: Write a Java program to accept 'N' student names through command line...
  793. --------------------------------------------------------------------------------
  794. import java.util.ArrayList;
  795. import java.util.Iterator;
  796. import java.util.ListIterator;
  797.  
  798. public class StudentNames {
  799.     public static void main(String[] args) {
  800.         if (args.length == 0) {
  801.             System.out.println("Please provide student names as command-line arguments.");
  802.             return;
  803.         }
  804.  
  805.         ArrayList<String> students = new ArrayList<>();
  806.         for (String name : args) {
  807.             students.add(name);
  808.         }
  809.  
  810.         System.out.println("Using Iterator:");
  811.         Iterator<String> iterator = students.iterator();
  812.         while (iterator.hasNext()) {
  813.             System.out.println(iterator.next());
  814.         }
  815.  
  816.         System.out.println("\nUsing ListIterator (reverse order):");
  817.         ListIterator<String> listIterator = students.listIterator(students.size());
  818.         while (listIterator.hasPrevious()) {
  819.             System.out.println(listIterator.previous());
  820.         }
  821.     }
  822. }
  823. --------------------------------------------------------------------------------
  824.  
  825. Slip 25
  826. ===========
  827. Question 1: Write a Java program using multithreading to print even numbers from 1 to 20 with a 1-second delay.
  828. --------------------------------------------------------------------------------
  829. public class EvenNumberThread extends Thread {
  830.     public void run() {
  831.         for (int i = 1; i <= 20; i++) {
  832.             if (i % 2 == 0) {
  833.                 System.out.println(i);
  834.                 try {
  835.                     Thread.sleep(1000); // 1-second delay
  836.                 } catch (InterruptedException e) {
  837.                     e.printStackTrace();
  838.                 }
  839.             }
  840.         }
  841.     }
  842.  
  843.     public static void main(String[] args) {
  844.         EvenNumberThread thread = new EvenNumberThread();
  845.         thread.start();
  846.     }
  847. }
  848. --------------------------------------------------------------------------------
  849.  
  850. Question 2: Write a JSP program to display the current date and time.
  851. --------------------------------------------------------------------------------
  852. <%@ page import="java.util.Date" %>
  853. <html>
  854. <body>
  855. <h2>Current Date and Time</h2>
  856. <p>Current Date and Time: <%= new Date() %></p>
  857. </body>
  858. </html>
  859. --------------------------------------------------------------------------------
  860.  
  861. Slip 26
  862. ===========
  863. Question 1: Write a Java program to delete the details of given employee...
  864. --------------------------------------------------------------------------------
  865. import java.sql.*;
  866. import java.util.Scanner;
  867.  
  868. public class DeleteEmployee {
  869.     public static void main(String[] args) {
  870.         Scanner sc = new Scanner(System.in);
  871.         System.out.print("Enter Employee ID to delete: ");
  872.         int eno = sc.nextInt();
  873.  
  874.         try {
  875.             Class.forName("org.postgresql.Driver");
  876.             Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres", "password");
  877.             PreparedStatement ps = con.prepareStatement("DELETE FROM Employee WHERE ENo = ?");
  878.             ps.setInt(1, eno);
  879.             int rows = ps.executeUpdate();
  880.             if (rows > 0) {
  881.                 System.out.println("Employee with ENo " + eno + " deleted successfully.");
  882.             } else {
  883.                 System.out.println("No employee found with ENo " + eno);
  884.             }
  885.             con.close();
  886.         } catch (Exception e) {
  887.             e.printStackTrace();
  888.         }
  889.         sc.close();
  890.     }
  891. }
  892. --------------------------------------------------------------------------------
  893.  
  894. Question 2: Write a JSP program to calculate sum of first and last digit of a given number...
  895. --------------------------------------------------------------------------------
  896. <html>
  897. <body>
  898. <h2>Sum of First and Last Digit</h2>
  899. <form method="post">
  900.     Enter Number: <input type="text" name="num">
  901.     <input type="submit" value="Calculate">
  902. </form>
  903. <%
  904.     String numStr = request.getParameter("num");
  905.     if (numStr != null) {
  906.         int num = Integer.parseInt(numStr);
  907.         int firstDigit = num;
  908.         while (firstDigit >= 10) {
  909.             firstDigit /= 10;
  910.         }
  911.         int lastDigit = num % 10;
  912.         int sum = firstDigit + lastDigit;
  913. %>
  914. <p style="color:red; font-size:18px;">Sum of first and last digit: <%= sum %></p>
  915. <% } %>
  916. </body>
  917. </html>
  918. --------------------------------------------------------------------------------
  919.  
  920. Slip 28
  921. ===========
  922. Question 1: Write a JSP script to accept a String from a user and display it in reverse order.
  923. --------------------------------------------------------------------------------
  924. <html>
  925. <body>
  926. <h2>Reverse String</h2>
  927. <form method="post">
  928.     Enter String: <input type="text" name="str">
  929.     <input type="submit" value="Reverse">
  930. </form>
  931. <%
  932.     String str = request.getParameter("str");
  933.     if (str != null) {
  934.         StringBuilder reversed = new StringBuilder(str).reverse();
  935. %>
  936. <p>Reversed String: <%= reversed %></p>
  937. <% } %>
  938. </body>
  939. </html>
  940. --------------------------------------------------------------------------------
  941.  
  942. Question 2: Write a Java program to display name of currently executing Thread in multithreading.
  943. --------------------------------------------------------------------------------
  944. public class CurrentThreadDemo extends Thread {
  945.     public CurrentThreadDemo(String name) {
  946.         super(name);
  947.     }
  948.  
  949.     public void run() {
  950.         System.out.println("Currently executing thread: " + Thread.currentThread().getName());
  951.     }
  952.  
  953.     public static void main(String[] args) {
  954.         CurrentThreadDemo t1 = new CurrentThreadDemo("Thread-1");
  955.         CurrentThreadDemo t2 = new CurrentThreadDemo("Thread-2");
  956.  
  957.         t1.start();
  958.         t2.start();
  959.     }
  960. }
  961. --------------------------------------------------------------------------------
  962.  
  963. Slip 29
  964. ===========
  965. Question 1: Write a Java program to display information about all columns in the DONAR table...
  966. --------------------------------------------------------------------------------
  967. import java.sql.*;
  968.  
  969. public class DonarMetaData2 {
  970.     public static void main(String[] args) {
  971.         try {
  972.             Class.forName("org.postgresql.Driver");
  973.             Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres", "password");
  974.             PreparedStatement ps = con.prepareStatement("SELECT * FROM DONAR");
  975.             ResultSet rs = ps.executeQuery();
  976.             ResultSetMetaData rsmd = rs.getMetaData();
  977.  
  978.             int columnCount = rsmd.getColumnCount();
  979.             System.out.println("Total Columns: " + columnCount);
  980.             for (int i = 1; i <= columnCount; i++) {
  981.                 System.out.println("Column " + i + ":");
  982.                 System.out.println("Name: " + rsmd.getColumnName(i));
  983.                 System.out.println("Type: " + rsmd.getColumnTypeName(i));
  984.                 System.out.println("Size: " + rsmd.getColumnDisplaySize(i));
  985.                 System.out.println();
  986.             }
  987.             con.close();
  988.         } catch (Exception e) {
  989.             e.printStackTrace();
  990.         }
  991.     }
  992. }
  993. --------------------------------------------------------------------------------
  994.  
  995. Question 2: Write a Java program to create LinkedList of integer objects and perform the following...
  996. --------------------------------------------------------------------------------
  997. import java.util.LinkedList;
  998.  
  999. public class LinkedListOperations {
  1000.     public static void main(String[] args) {
  1001.         LinkedList<Integer> list = new LinkedList<>();
  1002.  
  1003.         // Adding some initial elements
  1004.         list.add(10);
  1005.         list.add(20);
  1006.         list.add(30);
  1007.  
  1008.         // i. Add element at first position
  1009.         list.addFirst(5);
  1010.         System.out.println("After adding at first position: " + list);
  1011.  
  1012.         // ii. Delete last element
  1013.         list.removeLast();
  1014.         System.out.println("After deleting last element: " + list);
  1015.  
  1016.         // iii. Display the size of the list
  1017.         System.out.println("Size of the list: " + list.size());
  1018.     }
  1019. }
  1020. --------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement