Advance Java Slip Solution
Advance Java Slip Solution
versa continuously.
Answer :
import java.applet.Applet;
import java.awt.*;
int x, y, z;
Thread t;
x = 50;
y = 50;
z = 1;
t = new Thread(this);
t.start();
x = x + 10 * z;
if (x > this.getWidth())
z = -1;
if (x < 0)
z = 1;
repaint();
mpostion();
try {
Thread.sleep(100);
} catch (Exception e) {
g.drawString("SVPM", x, y);
/*
* </applet>
*/
Answer :
//Server
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
Button b1;
TextField t1;
TextArea ta;
Thread t;
BufferedReader br;
PrintWriter pw;
public Slip1B() {
f.setLayout(new FlowLayout());
b1 = new Button("Send");
b1.addActionListener(this);
t1 = new TextField(15);
f.add(t1);
f.add(ta);
f.add(b1);
try {
System.out.println(s);
} catch (Exception e) {
t = new Thread(this);
t.start();
f.setSize(400, 400);
f.setVisible(true);
pw.println(t1.getText());
t1.setText("");
while (true) {
try {
ta.append(str + "\n");
} catch (Exception e) {
//Client
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
Button b1;
TextField t1;
TextArea ta;
Thread t;
Socket s;
BufferedReader br;
PrintWriter pw;
public Slip1B1() {
f.setLayout(new FlowLayout());
b1 = new Button("Send");
b1.addActionListener(this);
t1 = new TextField(15);
f.add(t1);
f.add(ta);
f.add(b1);
try {
s = new Socket("localhost",2000);
} catch (Exception e) {
t = new Thread(this);
t.start();
f.setSize(400, 400);
f.setVisible(true);
}
pw.println(t1.getText());
t1.setText("");
while (true) {
try {
ta.append(str + "\n");
} catch (Exception e) {
SLIP 2
A) Write a JSP program to check whether given number is Perfect or
not. (Use Include
directive).
Answer :
Slip2A.html
<html>
<body>
</form>
</body>
</html>
Slip2A.jsp
<html>
<body>
<%
int n = Integer.parseInt(request.getParameter("no"));
int n1=0;
for(int i=1; i<n; i++){
if(n%i==0){
n1+=i;
if(n1==n){
out.println("Perfect Number");
}else{
%>
</body>
</html>
Answer :
import java.awt.*;
int f = 0;
public Slip2B(){
Signal s = new Signal();
s.start();
setSize(500,500);
setVisible(true);
switch (f){
case 0 :
case 1 :
while(true){
f = (f+1)%2;
repaint();
try{
Thread.sleep(1000);
}catch(Exception e){
}
new Slip2B();
}
SLIP3
Answer :
//Client
import java.io.*;
import java.net.*;
System.out.println("====================");
dos.writeBytes(n + "\n");
System.out.println(dis.readLine());
//Server
import java.io.*;
import java.net.*;
Socket s = ss.accept();
int i, cnt = 0;
if (n % i == 0)
cnt++;
break;
if (cnt == 0)
else
s.close();
}
B) Write a java program using applet for bouncing ball, for each bounce
color of ball
Answer :
import java.awt.*;
import java.awt.event.*;
private int x, y, w, h, f;
public Slip3B() {
setTitle("Bouncing Boll");
setSize(400, 400);
setVisible(true);
w = getWidth();
h = getHeight();
t.start();
switch (f) {
case 0:
y++;
if (y > h - 50) {
f = 1;
break;
case 1:
y--;
if (y < 0) {
f = 0;
repaint();
try {
Thread.sleep(10);
} catch (Exception e) {
}
super.paint(g);
g.setColor(c);
new Slip3B();
}
SLIP4
name is ‘S’.
Answer :
import java.sql.*;
class Slip4A {
Connection con;
Statement stmt;
Class.forName("com.mysql.jdbc.Driver");
stmt = con.createStatement();
con.close();
client, such as IP address and browser type. The servlet also provides
information about
the server on which the servlet is running, such as the operating system type,
and the
Answer :
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
res.setContentType("html/text");
PrintWriter pw = res.getWriter();
pw.println("<html><body><h1><b>INFORMATION OF SERVER</b></h1>");
pw.println("</body></html>");
pw.close();
}
SLIP5
A) Write a JSP program to calculate sum of first and last digit of a given number.
Answer :
<html>
<head>
</head>
<body>
<h2>Enter a Number:</h2>
</form>
<%
if(request.getParameter("num")!=null) {
while(firstDigit>=10) {
%>
<h2 style="color:red; font-size:18px;">Sum of First and Last Digits: <%=sum
%></h2>
<%
%>
</body>
</html>
Output :
Answer :
import java.awt.*;
int f = 0;
public Slip5B() {
s.start();
setSize(500, 500);
setVisible(true);
switch (f) {
case 0:
g.setColor(Color.red);
g.setColor(Color.black);
break;
case 1:
g.setColor(Color.yellow);
g.setColor(Color.black);
break;
case 2:
g.setColor(Color.green);
g.setColor(Color.black);
break;
while (true) {
f = (f + 1) % 3;
repaint();
try {
Thread.sleep(1000);
} catch (Exception e) {
}
public static void main(String args[]) {
new Slip5B();
}
SLIP6
Answer :
import java.awt.*;
int f = 0;
public Slip6A() {
s.start();
setSize(500, 500);
setVisible(true);
while (true) {
f = (f + 1) % 2;
repaint();
try {
Thread.sleep(500);
} catch (Exception e) {
Toolkit t = Toolkit.getDefaultToolkit();
switch (f) {
case 0:
new Slip6A();
}
B) Write a SERVLET program which counts how many times a user has visited a
web
page. If user is visiting the page for the first time, display a welcome message.
If the
user is revisiting the page, display the number of times visited. (Use Cookie)
Answer :
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
int visits = 0;
if (cookies != null) {
if (cookie.getName().equals("visitCount")) {
visits = Integer.parseInt(cookie.getValue());
visits++;
Cookie visitCookie = new Cookie("visitCount", Integer.toString(visits));
response.addCookie(visitCookie);
response.setContentType("text/html");
if (visits == 1) {
out.println("<html><head><title>Welcome</title></head><body>");
out.println("<h2>Welcome to my website!</h2>");
out.println("</body></html>");
} else {
out.println("<html><head><title>Visit Count</title></head><body>");
out.println("</body></html>");
out.close();
SLIP7
Answer :
<%
String email = request.getParameter("email"); // retrieve email from form data
if (email.matches(regex)) {
out.println("Valid email address"); // if email matches the regex, print "Valid email
address"
} else {
out.println("Invalid email address"); // if email does not match the regex, print
"Invalid email address"
%>
Answer :
import java.awt.event.*;
import javax.swing.*;
}
}
System.out.print(a1);
b.addActionListener(new ActionListener() {
a1.start();
});
f.add(b);
f.add(tf);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
SLIP8
A) Write a Java Program to display all the employee names whose initial
character of a
name is ‘A’.
Answer :
import java.io.*;
import java.sql.*;
class Slip8A {
Statement stmt;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
stmt = con.createStatement();
rs = stmt.executeQuery("select ename from emp where ename like 'A
%'");
System.out.println("<<<<Employee Name>>>>>");
System.out.println("==================");
while (rs.next()) {
System.out.println(rs.getString(1));
con.close();
Answer :
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
Thread t;
String str;
t.start();
try {
while (true) {
str = Nowtime.format(date);
repaint();
t.sleep(1000);
} catch (Exception e) {
}
/*
* </applet>
*/
SLIP9
A) Write a Java Program to create a Emp (ENo, EName, Sal) table and insert
record
Answer :
import java.sql.*;
import java.io.*;
class Slip9A {
Connection con;
PreparedStatement pstmt;
int e1, s;
String enm;
Class.forName("com.mysql.jdbc.Driver");
pstmt.executeUpdate();
System.out.println("=====================================");
e1 = Integer.parseInt(din.readLine());
enm = din.readLine();
s = Integer.parseInt(din.readLine());
pstmt.setInt(1, e1);
pstmt.setString(2, enm);
pstmt.setInt(3, s);
pstmt.executeUpdate();
B) Write a JSP program to create an online shopping mall. User must be allowed
to do
purchase from two pages. Each page should have a page total. The third page
should
display a bill, which consists of a page total of whatever the purchase has been
done
Answer :
<%
if (cart == null) {
session.setAttribute("cart", cart);
}
// Add items to the cart
if (request.getParameter("item") != null) {
if (cart.containsKey(item)) {
quantity += cart.get(item);
cart.put(item, quantity);
int pageTotal = 0;
out.println("<h1>Shopping Cart</h1>");
out.println("<table>");
out.println("<tr><th>Item</th><th>Quantity</th><th>Price</th></tr>");
out.println("</table>");
out.println("<form action=\"checkout.jsp\"><input
type=\"submit\" value=\"Checkout\"></form>");
%>
SLIP10
Answer :
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
System.out.println("Hello world");
session.close();
sessionFactory.close();
}
}
Output :
Answer :
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root",
"password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Products");
out.println("<html><head><title>Product
Details</title></head><body><table
border='1'><tr><th>ProdCode</th><th>PName</th><th>Price</th></tr>");
while (rs.next()) {
String prodCode = rs.getString("ProdCode");
String pName = rs.getString("PName");
String price = rs.getString("Price");
out.println("<tr><td>" + prodCode + "</td><td>" + pName +
"</td><td>" + price + "</td></tr>");
}
out.println("</table></body></html>");
con.close();
} catch (Exception e) {
out.println(e);
}
}
}
SLIP11
Answer :
import java.net.InetAddress;
Output :
B) Write a Java program to display sales details of Product (PID, PName, Qty,
Rate,
Amount) between two selected dates. (Assume Sales table is already created).
Answer :
import java.sql.*;
import java.util.Scanner;
// Define SQL query to fetch sales details of product between two dates
String query = "SELECT PID, PName, Qty, Rate, Amount FROM Sales WHERE
SaleDate BETWEEN ? AND ?";
try {
// Establish database connection
Connection conn = DriverManager.getConnection(url, user, password);
// Prepare SQL statement with parameters for start and end dates
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, startDate);
pstmt.setString(2, endDate);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
SLIP12
Answer :
import java.io.*;
import java.sql.*;
class Slip12A {
Statement stmt;
ResultSet rs;
int cnt = 0;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306
/bcadb", "root", "");
stmt = con.createStatement();
while (rs.next()) {
cnt++;
con.close();
B) Write a program in java which will show lifecycle (creation, sleep, and dead)
of a
thread. Program should print randomly the name of thread and value of sleep
time. The
name of the thread should be hard coded through constructor. The sleep time of
a thread
will be a random integer in the range 0 to 4999.
Answer :
import java.util.Random;
multithreading.
Answer :
public class Slip13A {
Thread t = Thread.currentThread();
Answer :
<%@page import="java.sql.*"%>
<html>
<head>
<title>College Details</title>
</head>
<body>
<h1>College Details</h1>
<table border="1">
<tr>
<th>College ID</th>
<th>College Name</th>
<th>Address</th>
</tr>
<%
try {
// Load the JDBC driver and establish a connection to the database
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root",
"password");
// Clean up resources
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
out.println("Error: " + e.getMessage());
}
%>
</table>
</body>
</html>
SLIP14
A) Write a JSP program to accept Name and Age of Voter and check whether he
is
Answer :
Output :
B) Write a Java program to display given extension files from a specific directory
on
server machine.
Answer :
import java.io.File;
import java.util.Scanner;
if (files.length == 0) {
System.out.println("No files found with the given extension in the directory.");
} else {
System.out.println("Files with ." + fileExtension + " extension in the
directory:");
for (File file : files) {
System.out.println(file.getName());
}
}
}
}
SLIP15
A) Write a java program to display each alphabet after 2 seconds between ‘a’ to
‘z’.
Answer :
a1.start();
try {
System.out.println((char)i);
sleep(2000);
} catch (Exception e) {
B) Write a Java program to accept the details of Student (RNo, SName, Per,
Gender,
Class) and store into the database. (Use appropriate Swing Components and
PreparedStatement Interface).
Answer :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public StudentDetails() {
setTitle("Student Details");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
t1 = new JTextField();
t2 = new JTextField();
t3 = new JTextField();
t4 = new JTextField();
b1 = new JButton("Save");
b2 = new JButton("Cancel");
add(p);
b1.addActionListener(this);
b2.addActionListener(this);
setVisible(true);