Laboratory Workbook: Sinhgad Institute of Management & Computer Application (SIMCA)
Laboratory Workbook: Sinhgad Institute of Management & Computer Application (SIMCA)
Laboratory Workbook
Prepared by,
Name : Priyanka Nitin Sonawane
Roll No. 20137
Div : A
Program:-
for(int i=0;i<5;i++) {
System.out.println("Welcome to SIMCA");
Output:-
Welcome to SIMCA
Welcome to SIMCA
Welcome to SIMCA
Welcome to SIMCA
Welcome to SIMCA
Program:-
Constructor2(){
for(int i=0;i<5;i++) {
System.out.println("Welcome to SIMCA");
System.out.println(c1);
Output:-
Welcome to SIMCA
Welcome to SIMCA
Welcome to SIMCA
Welcome to SIMCA
Welcome to SIMCA
Constructor2@71dac704
3. Write a program to Fibonacci series up to user define number.
Program:-
int n = 10, t1 = 0, t2 = 1;
System.out.println("First"+n+"terms:");
for(int i=1;i<=n;i++) {
System.out.println(t1+"+");
t1 = t2;
t2 = sum;
Output:-
3+
5+
8+
13+
21+
34+
4. Write a program to computer reverse number. Accept number from user and display on console.
Program:-
import java.util.Scanner;
int num = 0;
int reversesum = 0;
while(num!=0) {
num = num/10;
sc.close();
Output:-
1234
Program:-
ObjectNumber5(){
count++;
Output:-
Program:-
class Data{
int data1;
int data2;
d1.data1 = 20;
Data d2 = d1;
d2.data2 = 40;
Data d3 = d2;
System.out.println("d3.Data1:"+d3.data1);
System.out.println("d3.Data2:"+d3.data2);
Output:-
d3.Data1:20
d3.Data2:40
Program:-
g1 = null;
g2 = null;
System.gc();
Output:-
8. Read only one parameter from command line argument and display the welcome message also check
the length.
Program:-
for(int i=0;i<args.length;i++) {
String s = args[i];
System.out.println(s);
Output:-
at CommandOperations.main(CommandOperations.java:7)
9. Read two numbers from command line and perform addition, subtraction, multiplication and division.
Program:-
int x,y;
int add,sub,mul;
int div;
x = Integer.parseInt(args[0]);
y = Integer.parseInt(args[1]);
add = x + y;
System.out.println("Addition of numbers="+add);
sub= x - y;
System.out.println("Subtraction of numbers="+sub);
mul= x * y;
System.out.println("Multiplication of numbers="+mul);
div= x / y;
System.out.println("Division of numbers="+div);
Output:-
at CommandOperations.main(CommandOperations.java:9)
10. Read a number from command line and check whether it is prime or not.
Program:-
int n = Integer.parseInt(args[0]);
int cnt = 0;
for(int i=2;i<=n/2;i++) {
if(n%i==0) {
cnt = 1;
if(cnt==0) {
System.out.println("Number is prime"+n);
}
else {
Output:-
at Prime10.main(Prime10.java:6)
11. Write a program to implement the polymorphism by calculating the area of rectangle and circle.
Program:-
class Overload{
void area(double x) {
double z = 3.14 * x * x;
class AreaRectangleCircle11
a1.area(2.5);
a1.area(11,12);
Output:-
Program:-
@SuppressWarnings("serial")
public OddEven(String s) {
super(s);
class OddEven12{
int x;
x = Integer.parseInt(args[0]);
if(x%2==0) {
else
try {
catch(OddEven e) {
System.out.println("Caught");
System.out.println(e.getMessage());
Output:-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at OddEven12.main(OddEven12.java:17)
Program:-
try {
System.out.println("Result:"+output);
catch(ArithmeticException e){
try {
a[11] = 9;
catch(ArrayIndexOutOfBoundsException e1){
System.out.println("ArrayIndexOutOfBounds");
try
System.out.println(num);
catch(NumberFormatException e) {
System.out.println("Number format exception occured");
Output:-
ArrayIndexOutOfBounds
14. Write a program to implement a user define exception “NotPrimeException”. Read number from
command line argument and check whether the number is prime or not. If it is prime then display the
message “Number is prime” and if it is not prime throw the exception “NotPrimeException”.
Program:-
public NotPrimeException(String s) {
super(s);
int n = Integer.parseInt(args[0]);
int cnt = 0;
for(int i=2;i<=n/2;i++) {
if(n%i==0) {
cnt = 1;
if(cnt==0) {
System.out.println("Number is Prime"+n);
else
try {
throw new NotPrimeException("NotPrimeException");
catch(NotPrimeException e) {
System.out.println("Caught");
System.out.println(e.getMessage());
15. Write a java program using interface, which performs simple arithmetic operations such as addition,
subtraction, multiplication, division.
Program:-
import java.util.Scanner;
interface ArithmeticOperations{
@Override
int c;
c = a + b;
System.out.println(c);
@Override
int c;
c = a - b;
System.out.println(c);
@Override
int c;
c = a * b;
System.out.println(c);
@Override
int c;
c = a / b;
System.out.println(c);
int x = sc.nextInt();
int y = sc.nextInt();
System.out.println("Additon is:");
a1.add(x, y);
System.out.println("Substraction is:");
a1.sub(x, y);
System.out.println("Multiplication is:");
a1.mul(x, y);
System.out.println("Division is:");
a1.div(x, y);
sc.close();
Output:-
Additon is:
Substraction is:
-1
Multiplication is:
Division is:
16. Write a java code to create a thread object and start it.
Program:-
System.out.println("Thread is running....");
t1.start();
}
}
Output:-
Thread is running....
17. Write a program that creates and run the following threads-
Program:-
public PrintChar() {
this.charToPrint = charToPrint;
this.times = times;
@Override
for(int i=0;i<=times;i++) {
System.out.println(charToPrint);
this.lastNum = lastNum;
}
@Override
for(int i = 0;i<lastNum;i++) {
System.out.println(" "+i);
t1.start();
t2.start();
t3.start();
return null;
return null;
}
}
Program:-
try
System.out.println("Thread"+Thread.currentThread().getId()+"is running..");
catch(Exception e)
System.out.println("Exception is caught");
int n = 8;
m1.start();
Output:-
Thread19is running..
Thread20is running..
Thread16is running..
Thread15is running..
Thread14is running..
Thread18is running..
Program:-
t1.start();
t1.setName("MyThread");
System.out.println("Get Name"+t1.getName());
System.out.println("set t1 piority");
t1.setPriority(Thread.MAX_PRIORITY);
Output:-
T1 thread name:Thread-0
Get NameMyThread
T1 thread priority:5
set t1 piority
Program:-
try {
catch(ArithmeticException e)
finally {
System.out.println("This is proca");
try {
catch(ArithmeticException e)
finally {
System.out.println("This is procb");
try {
catch(ArithmeticException e)
finally {
System.out.println("This is procc");
e1.proca();
e1.procb();
e1.procc();
Output:-
This is proca
This is procb
This is procc
21. Write a program having interface A containing two methods meth1() and meth2(). interface
B extends A which contain method meth3() and one class which implements B.
Program:-
interface MyInterface
System.out.println("implementation of method1");
System.out.println("implementation of method2");
obj.method1();
Output:-
implementation of method1
Source Code:
import java.util.ArrayList;
import java.util.Iterator;
int roll;
String name;
int marks;
this.roll=roll;
this.name=name;
this.marks=marks;
}
s.add(d1);
s.add(d2);
s.add(d3);
s.add(d4);
s.add(d5);
for (Student i : s)
if(i.marks>60)
System.out.print(i.roll+"\t");
System.out.print(i.name+"\t");
System.out.print(i.marks+"\t");
System.out.println();
Output:
23 Design a program to create an *LinkedList*. Create a *Employee* class, assign emp_id,
emp_name and emp_salary to him using constructor. Store 5 objects of
Employee class in LinkedList. Update the salary of the all the Employees with 10%.
Source Code:
import java.util.*;
public class Employee {
int emp_id;
String emp_name;
double emp_sal;
public Employee(int id,String name,double sal)
{
this.emp_id=id;
this.emp_name=name;
this.emp_sal=sal;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Program to create 5 objects of Employee class Store it in
LinkedList and update salary of all Employees by 10 %."); Employee
Employee e1=new Employee(11,"Aman",25000);
Employee e2=new Employee(12,"Alex",20000);
Employee e3=new Employee(13,"Ruhi",17000);
Employee e4=new Employee(14,"Nikita",15000);
Employee e5=new Employee(15,"Keshav",27000);
<Employee> e=new LinkedList<Employee>();
e.add(e1);
e.add(e2);
e.add(e3);
e.add(e4);
e.add(e5);
System.out.println("Employee records are:");
for(Employee i: e)
{
System.out.println("Employee id:"+i.emp_id);
System.out.println("Employee Name:"+i.emp_name);
System.out.println("Employee Salary:"+i.emp_sal);
System.out.println();
}
System.out.println("Updated Records are:");
for(Employee i: e)
{
double sal=(i.emp_sal/100)*10;
i.emp_sal=i.emp_sal+sal;
System.out.println("Employee id:"+i.emp_id);
System.out.println("Employee Name:"+i.emp_name);
System.out.println("Employee Salary:"+i.emp_sal);
System.out.println();
}
Output:
24 Write the program to demonstrate the TreeSet class.
Source Code:
import java.util.*;
public class tree {
Output:
25 Write the program to demonstrate the HashMap class.
Source Code:
import java.util.*;
public class hash {
System.out.println("Iterating Hashmap...");
for(Map.Entry m:map.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
Output:
26 Write an Application
to fill university examination form. Use text box for name, mobile no, email id. Text area for
address. List box for city. Radio button for gender. Checkbox for subjects. Combo box for
course. Also display submit button. After clicking
submit button display report on console.
Source Code:
P24.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<h3>City: </h3>
</select> <br>
<h3>Course:</h3>
<option value="MCA">MCA</option>
<option value="MBA">MBA</option>
<option value="BCA">BCA</option>
<option value="BBA">BBA</option>
</select><br>
</form>
</body>
</html>
dis.jsp
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
String mobile=request.getParameter("mobile");
String email=request.getParameter("email");
String addr=request.getParameter("addr");
String g=request.getParameter("gender");
String city=request.getParameter("city");
String course=request.getParameter("course");
</body>
</html>
Output:
27 Design a program to open a frame, and on WindowClosing event close the frame window
using adapter class.
Source Code:
import java.awt.*;
import java.awt.event.*;
public class AdapterExample extends WindowAdapter{
Frame f;
AdapterExample(){ f=new
Frame();
f.addWindowListener(this);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public void windowClosing(WindowEvent e) {
f.dispose();
}
public static void main(String[] args) {
new AdapterExample();
}
}
Output:
28 Design a program to display a frame which shows x and y coordinates on
Source Code:
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class mouse {
public static void main(String args[]) {
Frame frame = new Frame("AnonymousEventExample");
Label l=new Label();
l.setBounds(20,40,100,20);
frame.add(l);
addMouseMotionListener(frame);
frame.setSize(400,400);
frame.setLayout(null);
frame.setVisible(true);
new MouseMotionListener()
{
}
};
}
}
Output:
29 Design a JDBC program to displays the details of employees (eno, ename,
Source Code:
import java.sql.*;
try
Class.forName("com.mysql.jdbc.Driver");
"root","");
ps.setString(1,"Management");
ResultSet rs=ps.executeQuery();
while(rs.next())
System.out.printf(rs.getInt(1)+"\n");
System.out.printf(rs.getString(2)+"\n");
System.out.printf(rs.getString(3)+"\n");
System.out.print(rs.getInt(4));
System.out.println();
}
catch(Exception e)
System.out.println(e);
Output:
30 Design JDBC application for Student Feedback Form [Assume suitable table
structure].
Source Code:
sf.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Student Feedback</title>
</head>
<body>
</form>
</body>
</html>
sf.jsp
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
String name=request.getParameter("username");
String mobile=request.getParameter("mobile");
String email=request.getParameter("email");
String addr=request.getParameter("addr");
String feed=request.getParameter("feed");
try
{ Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/lab","root","");
ps.setString(2,name);
ps.setString(3,mobile);
ps.setString(4,email);
ps.setString(5,addr);
ps.setString(6,feed);
int i=ps.executeUpdate();
if(i>0)
else
catch(Exception e)
out.println(e);
%>
</body>
</html>
Output:
31 Design a java program to accept the empno from user and update the salary of employee
and display the updated record on screen. Hint employee table has fields
empno, ename, address, desg, salary.
Source Code:
import java.sql.*;
import java.util.*;
try
int empno=sc.nextInt();
int sal=sc.nextInt();
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/lab","root","");
ps.setInt(1,sal);
ps.setInt(2,empno);
int i=ps.executeUpdate();
if(i>0)
}
else
System.out.println("Error");
ps.setInt(1,empno);
ResultSet rs=ps.executeQuery();
if(rs.next())
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));
System.out.println(rs.getInt(5));
catch(Exception e)
{ System.out.println(
e);
}
Output:
32 Design a servlet program illustrating its lifecycle.
Source Code:
index.jsp
ServletLifecycle.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
Source Code:
bp.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Book Publisher</title>
</head>
<body>
</form>
</body>
</html>
bp.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public bp() {
super();
pw;
response.setContentType("text/html");
pw=response.getWriter();
String name=request.getParameter("name");
String bname=request.getParameter("bname");
int bpy=request.getParameter("bpy");
String email=request.getParameter("email");
try
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/lab
,”root”,””);
pstmt.setString(1, name);
pstmt.setString(2, bname);
pstmt.setInt(3,bpy);
pstmt.setString(4, email);
int x=pstmt.executeUpdate();
if(x==1)
catch(Exception e)
e.printStackTrace();
}
pw.close();
display.java
javax.servlet.*; import
javax.servlet.http.*; import
java.sql.*;
res.setContentType("text/html");
out.println("<html><body>");
try
Class.forName("com.mysql.jdbc.Driver");
while (rs.next())
String n = rs.getString("aname");
String bn = rs.getString("bname");
} out.println("</table>");
out.println("</html></body>");
con.close();
catch (Exception e)
out.println(e);
web.xml
<web-app>
<servlet>
<servlet-name>bp</servlet-name>
<servlet-class>bp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bp</servlet-name>
<url-pattern>bp</url-pattern>
</servlet-mapping>
</web-app>
Output:
34 Design html page to accept Zoom registration form. Design Servlet code to store
registration details in database. Assume suitable table structure.
Source Code:
zrf.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Book Publisher</title>
</head>
<body>
</form>
</body>
</html>
zrf.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public zrf() {
super();
pw;
response.setContentType("text/html");
pw=response.getWriter();
String name=request.getParameter("name");
String email=request.getParameter("email");
int age=request.getParameter("age");
try
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/lab
,”root”,””);
pstmt.setString(1, name);
pstmt.setString(2, email);
pstmt.setInt(3, age);
int x=pstmt.executeUpdate();
if(x==1)
catch(Exception e)
e.printStackTrace();
} pw.close();
}
Output:
35 Design a ser vlet cod e, to cr eate an d ad d co oki e on cli en t’ s machi n e, th at
stores username, current date and display the same.
Source Code:
cookie.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Book Publisher</title>
</head>
<body>
<h1>Cookie Example</h1>
</form>
</body>
</html>
c1.java
javax.servlet.*; import
javax.servlet.http.*; import
java.util.Date;
HttpServletResponse response) {
try{ response.setContentType("text/html");
pwriter.print("Hello "+name);
response.addCookie(c1);
response.addCookie(c2);
pwriter.close();
}catch(Exception exp)
{ System.out.println(exp
);
c2.java
javax.servlet.*; import
javax.servlet.http.*;
HttpServletResponse response){
try{ response.setContentType("text/html");
Cookie c[]=request.getCookies();
pwriter.print("Name: "+c[1].getValue());
pwriter.print("Date: "+c[2].getValue());
pwriter.close();
}catch(Exception exp)
{ System.out.println(exp
);
Output:
36 Cr eate a b ean cl ass of emp l oyee sets i t’ s all th e pr oper ty b y u si n g JSP
code. Create HTML page to accept employee information.
Source Code:
emp.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Employee Details</title>
</head>
<body>
</form>
</body>
</html>
emp.jsp
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
String dept=request.getParameter("dept");%>
<div>
<h3>Employee Details</h3>
</div>
</body>
</html>
Employee.java
Output:
37 Design a program to create HTML form with two text box to enter numbers. When page
is submitted to JSP, it calculate division of given no. if denominator is
zero then generate an error page.
Source Code:
div.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Division</title>
</head>
<body>
</form>
</body>
</html>
div.jsp
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Division</title>
</head>
<body>
String num2=request.getParameter("n2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
int c=a/b;
%>
</body>
</html>
Output:
38 Create an HTML page to accept two numbers from user, and on *divide*
button click send the request to server. *Jsp* page will accept the request And display
division of the numbers as a response to the client.(*use exception handling in jsp*).
Source Code:
div.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Division</title>
</head>
<body>
</form>
</body>
</html>
div.jsp
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Division</title>
</head>
<body>
String num2=request.getParameter("n2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
int c=a/b;
%>
</body>
</html>
err.jsp
Output:
39 Design a JSP code to connect to a database using beans. Display the details of Book
table. Design JSP code to search a student placement details from the
placement database by student id.
Source Code:
bean.java
import java.io.*;
import java.sql.*;
try{
Class.forName("com.mysql.jdbc.Driver);
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/lab","root","");
PreparedStatement stmt=con.prepareStatement("select * from
from bp where bname=?");
ps.setString(1,bname);
ResultSet rs=stmt.executeQuery();
if(rs.next())
return rs.getInt("bpy");
break;
}
catch(Exception e)
System.out.println(e);
try{
Class.forName("com.mysql.jdbc.Driver);
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/lab","root","");
PreparedStatement stmt=con.prepareStatement("select * from
from bp where bname=?");
ps.setString(1,bname);
ResultSet rs=stmt.executeQuery();
if(rs.next())
return rs.getString("author");
break;
catch(Exception e)
System.out.println(e);
}
try{
Class.forName("com.mysql.jdbc.Driver);
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/lab","root","");
ps.setString(1,bname);
ResultSet rs=stmt.executeQuery();
if(rs.next())
return rs.getString("bname");
break;
catch(Exception e)
System.out.println(e);
this.author=bname;
}
}
bean.jsp
<html>
<head>
</head>
<body>
</jsp:useBean>
<p>Book Name:
</p>
<p>Author Name:
</p>
</p>
</body>
</html>
student.java
import java.io.*;
import java.sql.*;
try{
Class.forName("com.mysql.jdbc.Driver);
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/lab","root","");
PreparedStatement stmt=con.prepareStatement("select * from
from stud where sid=?");
ps.setInt(1,sid);
ResultSet rs=stmt.executeQuery();
if(rs.next())
return rs.getInt("sid");
break;
catch(Exception e)
{
System.out.println(e);
try{
Class.forName("com.mysql.jdbc.Driver);
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/lab","root","");
PreparedStatement stmt=con.prepareStatement("select * from
from stud where sid=?");
ps.setInt(1,sid);
ResultSet rs=stmt.executeQuery();
if(rs.next())
return rs.getString("sname");
break;
catch(Exception e)
System.out.println(e);
{
try{
Class.forName("com.mysql.jdbc.Driver);
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/lab","root","");
ps.setInt(1,sid);
ResultSet rs=stmt.executeQuery();
if(rs.next())
return rs.getString("company");
break;
catch(Exception e)
System.out.println(e);
this.sid=sid;
student.jsp
<html>
<head>
<title>get and set properties Example</title>
</head>
<body>
</jsp:useBean>
<p>Student Id:
</p>
<p>Student Name:
</p>
<p>Student Company:
</p>
</body>
</html>
Output: