EXPERIMENT 1 Write a program to demonstrate status of key on an Applet window such as KeyPressed, KeyReleased, KeyUp, KeyDown. import java.applet.Applet; import java.awt.*; import java.awt.event.*; /* <applet code="KeyStatusApplet" width=400 height=300></applet> */ public class KeyStatusApplet extends Applet implements KeyListener { String msg = ""; int x = 50, y = 100; public void init() { addKeyListener(this); setBackground(Color.lightGray); } public void keyPressed(KeyEvent ke) { msg = "Key Down: " + ke.getKeyChar();repaint(); } public void keyReleased(KeyEvent ke) { msg = "Key Released: " + ke.getKeyChar(); repaint(); } public void keyTyped(KeyEvent ke) { msg = "Key Typed: " + ke.getKeyChar(); repaint(); } public void paint(Graphics g) { g.setColor(Color.blue); g.setFont(new Font("Arial", Font.BOLD, 16)); g.drawString(msg, x, y); } } Steps to Execute the Program: Step 1: Save the Java Program Save the above code as KeyStatusApplet.java. Step 2: Compile the Java File Open the terminal/command prompt and navigate to the file location. Compile the Java file using: javac KeyStatusApplet.java Step 3: Create an HTML File Create an HTML file (e.g., KeyApplet.html) with the following content: <html> <body> <applet code="KeyStatusApplet.class" width="400" height="300"></applet> </body> </html> EXPERIEMNT 2 Write a program to create a frame using AWT. Implement mouseClicked, mouseEntered() and mouseExited() events. Frame should become visible when the mouse enters it import java.awt.*; import java.awt.event.*; public class MouseEventDemo extends Frame implements MouseListener { public MouseEventDemo() { // Frame properties setSize(400, 300); setTitle("AWT Mouse Event Demo"); addMouseListener(this); setVisible(false); // Initially, the frame is not visible } public void mouseClicked(MouseEvent e) { System.out.println("Mouse Clicked at X: " + e.getX() + " Y: " + e.getY()); } public void mouseEntered(MouseEvent e) { System.out.println("Mouse Entered the Frame"); setVisible(true);// Show the frame when mouse enters} public void mouseExited(MouseEvent e) { System.out.println("Mouse Exited the Frame"); } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public static void main(String[] args) { new MouseEventDemo(); } } EXPERIEMNT 3 Develop a GUI which accepts the information regarding the marks for all the subjects of a student in the examination. Display the result for a student in a separate window. import java.awt.*; import java.awt.event.*; class StudentMarksEntry extends Frame implements ActionListener { TextField[] marksFields; Button submit; Label resultLabel; public StudentMarksEntry() { setLayout(new GridLayout(6, 2)); setTitle("Student Marks Entry"); setSize(400, 300); marksFields = new TextField[5]; for (int i = 0; i < 5; i++) { add(new Label("Subject " + (i + 1) + " Marks: ")); marksFields[i] = new TextField(10); add(marksFields[i]); } submit = new Button("Submit"); submit.addActionListener(this);add(submit); resultLabel = new Label(""); add(resultLabel); setVisible(true); } public void actionPerformed(ActionEvent e) { int total = 0; for (TextField field : marksFields) { total += Integer.parseInt(field.getText()); } float percentage = total / 5.0f; new ResultWindow(percentage); }class ResultWindow extends Frame { public ResultWindow(float percentage) { setLayout(new FlowLayout()); setTitle("Result"); setSize(200, 100); add(new Label("Percentage: " + percentage + "%")); setVisible(true); } } public static void main(String[] args) { new StudentMarksEntry(); } } EXPERIMENT 4 Write a program to insert and retrieve the data from the database using JDBC. import java.sql.*; public class MySQLdatabase { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost/sqldatabase", "amit", "amitabh"); Statement s = con.createStatement(); s.execute("create table student ( stud_id intege r,stud_name varchar(20),stud_address varchar(30) )"); // create a table s.execute("insert into student values(001,'ARman','Delhi')"); // insert first row into the table s.execute("insert into student values(002,' Robert','Canada')"); // insert second row into the table s.execute("insert into student values(003,'Ahuja','Karnal')"); // insert third row into the table ResultSet rs = s.executeQuery("select * fro m student"); if (rs != null) // if rs == null, then there is no record in ResultSet to show while (rs.next()) // By this line we wi ll step through our data row-by-row { System.out.println(" "); System.out.println("Id of the student:" + rs.getString(1)); System.out.println("Name of student: "+ rs.getString(2)); System.out.println("Address of student:" + rs.getString(3)); System.out.println(" "); } s.close(); // close the Statement to let th e database know we're done with it con.close(); // close the Connection to let the database know we're done with it } catch(SQLException err) { System.out.println("ERROR: " + err); } catch (Exception err) { System.out.println("ERROR: " + err); } } } EXPERIMENT 5 Develop an RMI application which accepts a string or a number and checks that string or number is palindrome or not. one.java importjava.rmi.*; interface one extends Remote{ publicintpalin(String a) throws RemoteException; } two.java importjava.rmi.*; importjava.lang.*; importjava.rmi.server.*; public class two extends UnicastRemoteObject implements one { public two() throws RemoteException { } publicintpalin(String a) throws RemoteException { System.out.println("Hello"); StringBufferstr = new StringBuffer(a); String str1 = str.toString(); System.out.println("Print : " + str1.toString()); StringBuffer str2 = str.reverse(); System.out.println("Print : " + str2.toString()); int b = str1.compareTo(str2.toString()); System.out.println("Print : " + b); if (b == 0) return 1; else return 0; } } rmiserver.java import java.io.*; importjava.rmi.*; import java.net.*; public class rmiserver { public static void main(String args[]) throws Exception { try { twotwox = new two(); Naming.bind("palin", twox); System.out.println("Object registered"); } catch(Exception e) { System.out.println("Exception" + e); } } } rmiclient.java import java.io.*; import java.rmi.*; import java.net.*; public class rmiclient { public static void main(String args[]) throws Exception { try String s1 = "rmi://localhost/palin"; one onex = (one)Naming.lookup(s1); int m = onex.palin("madam"); System.out.println("Print : " + m); if (m == 1) { System.out.println("The given string is a Palindrome"); } else { System.out.println("The given string is not a Palindrome"); } } catch (Exception e) { System.out.println("Exception" + e); } }} EXPERIMENT 6 Write a program to demonstrate the use of Inet Address class and its factory methods import java.net.*; public class InetAddressExample { public static void main(String[] args) { try { // 1. Get local host address InetAddress localHost = InetAddress.getLocalHost(); System.out.println("Local Host Name: " + localHost.getHostName()); System.out.println("Local Host IP: " + localHost.getHostAddress()); // 2. Get InetAddress by domain name InetAddress google = InetAddress.getByName("www.google.com"); System.out.println("\nGoogle Host Name: " + google.getHostName()); System.out.println("Google IP Address: " + google.getHostAddress()); // 3. Get all IP addresses for a domain InetAddress[] addresses = InetAddress.getAllByName("www.microsoft.com"); System.out.println("\nAll IP addresses for www.microsoft.com:"); for (InetAddress addr : addresses) { System.out.println(addr); } } catch (UnknownHostException e) { System.out.println("Host not found: " + e.getMessage()); } } } EXPERIMENT 9 Write a simple JSP page to display a simple message (It may be a simple html page). <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>Simple JSP Page</title> </head> <body> <h1>Welcome to JSP!</h1> <p>This is a simple message displayed using JSP.</p> </body> </html> EXPERIMENT 10 Write a simple JSP page to display a simple message (It may be a simple html page). import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class CalculatorServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html"); PrintWriter out = response.getWriter(); // Read input values double num1 = Double.parseDouble(request.getParameter("num1")); double num2 = Double.parseDouble(request.getParameter("num2")); String operation = request.getParameter("operation"); double result = 0; // Perform operation switch (operation) {case "add": result = num1 + num2; break; case "subtract": result = num1 - num2; break; case "multiply": result = num1 * num2; break; case "divide": result = num2 != 0 ? num1 / num2 : Double.NaN; break;} // Output result out.println("<h2>Result: " + result + "</h2>"); } }