Adv Java Slip Sol
Adv Java Slip Sol
continuously.
import [Link].*;
import [Link].*;
import [Link].*;
public class BlinkText extends JFrame implements Runnable
{
Thread t;
Label l1;
int f;
public BlinkText()
{
t=new Thread(this);
[Link]();
setLayout(null);
l1=new Label("Hello JAVA");
[Link](100,100,100,40);
add(l1);
setSize(300,300);
setVisible(true);
f=0;
}
public void run()
{
try
{
if(f==0)
{
[Link](200);
[Link]("");
f=1;
}
if(f==1)
{
[Link](200);
[Link]("Hello Java");
f=0;
}
}catch(Exception e)
{
[Link](e);
}
run();
}
public static void main(String args[])
{
new BlinkText();
}
}
B) Write a socket program in java for chatting application.(Use Swing)
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
st=new ServerSocket(1281);
s=[Link]();
dos=new DataOutputStream([Link]());
dis=new DataInputStream([Link]());
while(true)
{
r=[Link]();
[Link]("client:"+r+"\n");
}
}
public void actionPerformed(ActionEvent e)
{
String cmd=[Link]();
if([Link]("send"))
{
try
{
r=[Link]();
[Link](r);
}
catch(Exception p)
{
}
}
}
}
class Slip1_BChatServer
{
public static void main(String[] d ) throws IOException
{
new MyServer();
Slip 2 A)Write a jsp program to check whether given number is Perfect or not. (Use Include
directive).
<center>
<h2>Perfect Number Checker</h2>
<form method="post">
Enter a Number:
<input type="number" name="number" required/>
<input type="submit" value="Check"/>
</form>
</body>
</html>
// Display divisors
for(int i = 1; i < num; i++) {
if(num % i == 0) {
[Link](i + " ");
}
}
[Link]("<br>Sum of divisors = " + sum);
[Link]("</div>");
} else {
[Link]("<div style='color:red; font-size:18px; margin-top:20px;'>");
[Link]("❌ " + num + " is NOT a perfect number!");
[Link]("<br>Sum of divisors = " + sum);
[Link]("</div>");
}
}
%>
/*
<applet code="FlagApplet" width="400" height="300">
</applet>
*/
[Link]();
[Link]();
[Link]();
}
}
class StripeThread extends Thread {
Applet app;
Color color;
int y;
} catch (Exception e) {
[Link](e);
}
}
}
Slip 3 A) Write a socket program in Java to check whether given number is prime or not.
Display result on client terminal
Server Program ([Link])
import [Link].*;
import [Link].*;
Socket s = [Link]();
[Link]("Client connected");
if (isPrime)
[Link]("Number is Prime");
else
[Link]("Number is Not Prime");
[Link]();
[Link]();
} catch (Exception e) {
[Link](e);
}
}
}
[Link](num);
[Link]();
} catch (Exception e) {
[Link](e);
}
}
}
B) Write a java program using applet for bouncing ball, for each bounce color of ball should
change randomly.
import [Link].*;
import [Link];
import [Link];
/*
* <applet code="[Link]" width=400 height=300></applet>
*/
[Link](ballColor);
[Link](x - radius, y - radius, radius * 2, radius * 2);
[Link](buffer, 0, 0, this);
}
B) Write a SERVLET program that provides information about a HTTP request from a 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 names of
currently loaded servlets.
import [Link].*;
import [Link].*;
import [Link].*;
[Link]("text/html");
PrintWriter out = [Link]();
// Client Info
[Link]("<html><body>");
[Link]("<h2>Client Information:</h2>");
[Link]("IP Address: " + [Link]() + "<br>");
[Link]("Browser: " + [Link]("User-Agent") + "<br><br>");
// Server Info
[Link]("<h2>Server Information:</h2>");
[Link]("OS: " + [Link]("[Link]") + "<br>");
[Link]("Server Name: " + [Link]() + "<br>");
[Link]("Server Port: " + [Link]() + "<br>");
[Link]("Java Version: " + [Link]("[Link]") + "<br><br>");
// Loaded Servlets
[Link]("<h2>Loaded Servlets:</h2>");
[Link]("- RequestInfoServlet (current)<br>");
[Link]("- Default Servlet<br>");
[Link]("- JSP Servlet<br>");
[Link]("</body></html>");
}
}
Slip 5 A) Write a JSP program to calculate sum of first and last digit of a given number.
Display sum in Red Color with font size 18.
<center>
<h2>Sum of First and Last Digit</h2>
<form method="post">
<input type="number" name="n" required placeholder="Enter any number"/>
<input type="submit" value="Find Sum"/>
</form>
<%
String n = [Link]("n");
if (n != null) {
int num = [Link](n);
num = [Link](num);
</body>
</html>
Thread thread;
String currentLight = "RED";
// Display instruction
if ([Link]("RED")) {
[Link]("STOP!", 130, 330);
} else if ([Link]("YELLOW")) {
[Link]("READY!", 130, 330);
} else {
[Link]("GO!", 130, 330);
}
}
}
import [Link].*;
import [Link].*;
Image img;
boolean visible = true;
Thread t;
public BlinkImage() {
// Load image (keep image in same folder or give full path)
img = [Link]().getImage("[Link]");
setSize(400, 400);
setTitle("Blinking Image");
setVisible(true);
// Close window
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
[Link](0);
}
});
// Start thread
t = new Thread(this);
[Link]();
}
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)
import [Link];
import [Link];
import [Link].*;
import [Link].*;
public class Slip6Q2 extends HttpServlet{
static int i=1;
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException {
[Link]("text/html");
PrintWriter out=[Link]();
String k=[Link](i);
Cookie c=new Cookie("visit",k);
[Link](c);
int j=[Link]([Link]());
if(j==1) {
[Link]("Welcome to web page ");
}
else {
[Link]("You are visited at "+i+" times");
}
i++;
}
}
<center>
<h2>Email ID Validation</h2>
<form method="post">
Email: <input type="text" name="email" />
<input type="submit" value="Check" />
</form>
<%
String email = [Link]("email");
</body>
</html>
import [Link].*;
import [Link].*;
@Override
public void run() {
for (int i = 1; i<=100; i++){
[Link]([Link](i)+"\n");
}
}
}
slip 8 A) Write a Java Program to display all the employee names whose initial character of
a name is ‘A’
import [Link].*;
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
Thread t1 = null;
int hours = 0, minutes = 0, seconds = 0;
String time = "";
public void init() {
setBackground( [Link]);
}
public void start() {
t1 = new Thread( this );
[Link]();
}
Slip 9 A) Write a Java Program to create a Emp (ENo, EName, Sal) table and insert record
into it. (Use PreparedStatement Interface)
import [Link].*;
try {
// 1. Load and register JDBC driver
[Link]("[Link]");
// 2. Establish connection
Connection con = [Link](
"jdbc:mysql://localhost:3306/testdb",
"root",
"password"
);
// 3. Create Statement
Statement stmt = [Link]();
[Link](createTableSQL);
[Link]("✓ Emp table created successfully!");
[Link]("\nEmployee Records:");
[Link]("-----------------------------------");
while([Link]()) {
[Link]("ENo: " + [Link]("ENo") +
", EName: " + [Link]("EName") +
", Sal: " + [Link]("Sal"));
}
// 7. Close connections
[Link]();
[Link]();
[Link]();
[Link]();
} catch (ClassNotFoundException e) {
[Link]("JDBC Driver not found: " + e);
} catch (SQLException e) {
[Link]("SQL Error: " + e);
}
}
}
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 and
print the total. (Use Session)
Slip 11 A) Write a java program to display IPAddress and name of client machine
import [Link].*;
// Get IP Address
String ipAddress = [Link]();
// Display information
[Link]("=== Client Machine Information ===");
[Link]("Host Name: " + hostName);
[Link]("IP Address: " + ipAddress);
} catch (UnknownHostException e) {
[Link]("Error: Unable to get host information");
[Link]();
}
}
}
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).
import [Link].*;
import [Link];
[Link]("[Link]");
Connection con = [Link]("jdbc:mysql://localhost:3306/testdb", "root", "password");
Scanner sc = new Scanner([Link]);
ResultSet rs = [Link]();
[Link]("\nPID\tPName\t\tQty\tRate\tAmount\tSaleDate");
[Link]("------------------------------------------------");
double total = 0;
while([Link]()) {
[Link]([Link]("PID") + "\t" +
[Link]("PName") + "\t" +
[Link]("Qty") + "\t" +
[Link]("Rate") + "\t" +
[Link]("Amount") + "\t" +
[Link]("SaleDate"));
total += [Link]("Amount");
}
[Link]("------------------------------------------------");
[Link]("TOTAL: " + total);
[Link]();
[Link]();
}
}
import [Link].*;
[Link]();
[Link]();
[Link]();
} catch(SQLException e) {
[Link]("Error: " + [Link]());
}
}
}
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.
import [Link];
} catch (InterruptedException e) {
[Link]("" + threadName + " was interrupted");
}
// Thread is dead
[Link]("" + threadName + " is DEAD (Thread execution completed)");
[Link]("-------------------------------------------");
}
}
// Start threads
[Link]();
[Link]();
[Link]();
B) Write a JSP program to display the details of College (CollegeID, Coll_Name, Address) in
tabular form on browser.
<center>
<h2>College Details</h2>
</body>
</html>
Slip 14 A) Write a JSP program to accept Name and Age of Voter and check whether he is
eligible for voting or not.
<center>
<h2>Voter Eligibility Check</h2>
<form method="post">
Name: <input type="text" name="name" required/><br/><br/>
Age: <input type="number" name="age" required/><br/><br/>
<input type="submit" value="Check Eligibility"/>
</form>
<%
String name = [Link]("name");
String ageStr = [Link]("age");
</body>
</html>
B) Write a Java program to display given extension files from a specific directory on server
machine.
import [Link].*;
if(!found) {
[Link]("No files found with " + extension + " extension");
}
} else {
[Link]("Directory not found!");
}
}
}
Slip 15 A) Write a java program to display each alphabet after 2 seconds between ‘a’ to ‘z’.
try {
[Link](2000); // Wait for 2 seconds
} catch(InterruptedException e) {
[Link]("Thread interrupted!");
}
}
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).
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
// Swing Components
JLabel lblRNo, lblName, lblPer, lblGender, lblClass;
JTextField txtRNo, txtName, txtPer;
JRadioButton rbMale, rbFemale;
ButtonGroup genderGroup;
JComboBox<String> cmbClass;
JButton btnSave, btnReset, btnExit;
// Database connection
Connection con;
PreparedStatement pstmt;
public StudentForm() {
setTitle("Student Registration Form");
setSize(500, 400);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Initialize components
lblRNo = new JLabel("Roll Number:");
[Link](50, 50, 100, 25);
add(lblRNo);
add(rbMale);
add(rbFemale);
String[] classes = {"FY BCA", "SY BCA", "TY BCA", "FY BSc", "SY BSc", "TY BSc"};
cmbClass = new JComboBox<>(classes);
[Link](180, 210, 200, 25);
add(cmbClass);
// Connect to database
connectDB();
setVisible(true);
}
void connectDB() {
try {
[Link]("[Link]");
con = [Link](
"jdbc:mysql://localhost:3306/testdb",
"root",
"password"
);
void saveStudent() {
try {
// Get values from form
int rno = [Link]([Link]());
String name = [Link]();
double per = [Link]([Link]());
if(result > 0) {
[Link](this, "Student Record Saved Successfully!");
resetForm();
}
} catch(NumberFormatException e) {
[Link](this, "Please enter valid numbers!");
} catch(SQLException e) {
if([Link]().contains("Duplicate")) {
[Link](this, "Roll Number already exists!");
} else {
[Link](this, "Database Error: " + [Link]());
}
} catch(Exception e) {
[Link](this, "Error: " + [Link]());
}
}
void resetForm() {
[Link]("");
[Link]("");
[Link]("");
[Link]();
[Link](0);
[Link]();
}
Slip 18 A) Write a java program to calculate factorial of a number. (Use sleep () method).
import [Link];
long factorial = 1;
try {
[Link](1000); // 1 second delay between steps
} catch(InterruptedException e) {
[Link]("Thread interrupted!");
}
}
import [Link].*;
import [Link].*;
import [Link];
while(true) {
// Receive message from client
clientMessage = [Link]();
[Link]("Client: " + clientMessage);
if([Link]("bye")) {
[Link]("Client disconnected!");
break;
}
if([Link]("bye")) {
[Link]("Server disconnected!");
break;
}
}
[Link]();
[Link]();
[Link]();
} catch(Exception e) {
[Link]("Error: " + [Link]());
}
}
}
[Link]
import [Link].*;
import [Link].*;
import [Link];
while(true) {
// Send message to server
[Link]("You: ");
clientMessage = [Link]();
[Link](clientMessage);
if([Link]("bye")) {
[Link]("You left the chat!");
break;
}
if([Link]("bye")) {
[Link]("Server disconnected!");
break;
}
}
[Link]();
[Link]();
} catch(Exception e) {
[Link]("Error: " + [Link]());
}
}
}
Slip 20 A) Write a JDBC program to delete the details of given employee (ENo EName
Salary). Accept employee ID through command line.
import [Link].*;
try {
// Connect to database
Connection con = [Link](
"jdbc:mysql://localhost:3306/testdb",
"root",
"password"
);
if([Link]()) {
// Display employee details before deletion
[Link]("Employee Found:");
[Link]("ID: " + [Link]("ENo"));
[Link]("Name: " + [Link]("EName"));
[Link]("Salary: " + [Link]("Sal"));
// Delete employee
String deleteQuery = "DELETE FROM Emp WHERE ENo = ?";
PreparedStatement pstmt = [Link](deleteQuery);
[Link](1, empId);
if(rowsDeleted > 0) {
[Link]("\n✅ Employee deleted successfully!");
}
[Link]();
} else {
[Link](" Employee with ID " + empId + " not found!");
}
[Link]();
[Link]();
[Link]();
} catch(SQLException e) {
[Link]("Database Error: " + [Link]());
} catch(NumberFormatException e) {
[Link]("Invalid Employee ID! Please enter a number.");
}
}
}
import [Link].*;
import [Link].*;
Thread thread;
int flag = 0;
int lightState = 0;
// Draw sun
[Link]([Link]);
[Link](500, 50, 60, 60);
// Temple base
[Link](new Color(210, 180, 140)); // Sand color
[Link](150, 250, 300, 100);
// Temple walls
[Link]([Link]);
[Link](180, 200, 80, 50);
[Link](340, 200, 80, 50);
// Temple door
[Link](new Color(139, 69, 19)); // Brown
[Link](270, 250, 60, 100);
// Door arch
[Link]([Link]);
[Link](270, 250, 60, 50, 0, 180);
// Pillars
[Link]([Link]);
[Link](160, 250, 20, 100);
[Link](420, 250, 20, 100);
// Flags on top
[Link]([Link]);
[Link](297, 55, 6, 30);
[Link](new int[]{297, 330, 297}, new int[]{55, 65, 75}, 3);
// Title
[Link]([Link]);
[Link](new Font("Arial", [Link], 20));
[Link]("HAPPY TEMPLE", 240, 400);
// Animation text
[Link](new Font("Arial", [Link], 12));
[Link]("Temple Lights Changing...", 240, 430);
}
}