Java Programming Lab Workbook for MCA
Java Programming Lab Workbook for MCA
Laboratory Workbook
Prepared by,
Name : Priyanka Nitin Sonawane
Roll No. 20137
Div : A
Program:-
for(int i=0;i<5;i++) {
[Link]("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++) {
[Link]("Welcome to SIMCA");
[Link](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;
[Link]("First"+n+"terms:");
for(int i=1;i<=n;i++) {
[Link](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 [Link];
int num = 0;
int reversesum = 0;
while(num!=0) {
num = num/10;
[Link]();
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;
[Link]("d3.Data1:"+d3.data1);
[Link]("d3.Data2:"+d3.data2);
Output:-
d3.Data1:20
d3.Data2:40
Program:-
g1 = null;
g2 = null;
[Link]();
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<[Link];i++) {
String s = args[i];
[Link](s);
Output:-
at [Link]([Link])
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 = [Link](args[0]);
y = [Link](args[1]);
add = x + y;
[Link]("Addition of numbers="+add);
sub= x - y;
[Link]("Subtraction of numbers="+sub);
mul= x * y;
[Link]("Multiplication of numbers="+mul);
div= x / y;
[Link]("Division of numbers="+div);
Output:-
at [Link]([Link])
10. Read a number from command line and check whether it is prime or not.
Program:-
int n = [Link](args[0]);
int cnt = 0;
for(int i=2;i<=n/2;i++) {
if(n%i==0) {
cnt = 1;
if(cnt==0) {
[Link]("Number is prime"+n);
}
else {
Output:-
at [Link]([Link])
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
[Link](2.5);
[Link](11,12);
Output:-
Program:-
@SuppressWarnings("serial")
public OddEven(String s) {
super(s);
class OddEven12{
int x;
x = [Link](args[0]);
if(x%2==0) {
else
try {
catch(OddEven e) {
[Link]("Caught");
[Link]([Link]());
Output:-
Exception in thread "main" [Link]: Index 0 out of bounds for length 0
at [Link]([Link])
Program:-
try {
[Link]("Result:"+output);
catch(ArithmeticException e){
try {
a[11] = 9;
catch(ArrayIndexOutOfBoundsException e1){
[Link]("ArrayIndexOutOfBounds");
try
[Link](num);
catch(NumberFormatException e) {
[Link]("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 = [Link](args[0]);
int cnt = 0;
for(int i=2;i<=n/2;i++) {
if(n%i==0) {
cnt = 1;
if(cnt==0) {
[Link]("Number is Prime"+n);
else
try {
throw new NotPrimeException("NotPrimeException");
catch(NotPrimeException e) {
[Link]("Caught");
[Link]([Link]());
15. Write a java program using interface, which performs simple arithmetic operations such as addition,
subtraction, multiplication, division.
Program:-
import [Link];
interface ArithmeticOperations{
@Override
int c;
c = a + b;
[Link](c);
@Override
int c;
c = a - b;
[Link](c);
@Override
int c;
c = a * b;
[Link](c);
@Override
int c;
c = a / b;
[Link](c);
int x = [Link]();
int y = [Link]();
[Link]("Additon is:");
[Link](x, y);
[Link]("Substraction is:");
[Link](x, y);
[Link]("Multiplication is:");
[Link](x, y);
[Link]("Division is:");
[Link](x, y);
[Link]();
Output:-
Additon is:
Substraction is:
-1
Multiplication is:
Division is:
16. Write a java code to create a thread object and start it.
Program:-
[Link]("Thread is running....");
[Link]();
}
}
Output:-
Thread is running....
17. Write a program that creates and run the following threads-
Program:-
public PrintChar() {
[Link] = charToPrint;
[Link] = times;
@Override
for(int i=0;i<=times;i++) {
[Link](charToPrint);
[Link] = lastNum;
}
@Override
for(int i = 0;i<lastNum;i++) {
[Link](" "+i);
[Link]();
[Link]();
[Link]();
return null;
return null;
}
}
Program:-
try
[Link]("Thread"+[Link]().getId()+"is running..");
catch(Exception e)
[Link]("Exception is caught");
int n = 8;
[Link]();
Output:-
Thread19is running..
Thread20is running..
Thread16is running..
Thread15is running..
Thread14is running..
Thread18is running..
Program:-
[Link]();
[Link]("MyThread");
[Link]("Get Name"+[Link]());
[Link]("set t1 piority");
[Link](Thread.MAX_PRIORITY);
Output:-
T1 thread name:Thread-0
Get NameMyThread
T1 thread priority:5
set t1 piority
Program:-
try {
catch(ArithmeticException e)
finally {
[Link]("This is proca");
try {
catch(ArithmeticException e)
finally {
[Link]("This is procb");
try {
catch(ArithmeticException e)
finally {
[Link]("This is procc");
[Link]();
[Link]();
[Link]();
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
[Link]("implementation of method1");
[Link]("implementation of method2");
obj.method1();
Output:-
implementation of method1
Source Code:
import [Link];
import [Link];
int roll;
String name;
int marks;
[Link]=roll;
[Link]=name;
[Link]=marks;
}
[Link](d1);
[Link](d2);
[Link](d3);
[Link](d4);
[Link](d5);
for (Student i : s)
if([Link]>60)
[Link]([Link]+"\t");
[Link]([Link]+"\t");
[Link]([Link]+"\t");
[Link]();
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 [Link].*;
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
[Link]("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>();
[Link](e1);
[Link](e2);
[Link](e3);
[Link](e4);
[Link](e5);
[Link]("Employee records are:");
for(Employee i: e)
{
[Link]("Employee id:"+i.emp_id);
[Link]("Employee Name:"+i.emp_name);
[Link]("Employee Salary:"+i.emp_sal);
[Link]();
}
[Link]("Updated Records are:");
for(Employee i: e)
{
double sal=(i.emp_sal/100)*10;
i.emp_sal=i.emp_sal+sal;
[Link]("Employee id:"+i.emp_id);
[Link]("Employee Name:"+i.emp_name);
[Link]("Employee Salary:"+i.emp_sal);
[Link]();
}
Output:
24 Write the program to demonstrate the TreeSet class.
Source Code:
import [Link].*;
public class tree {
Output:
25 Write the program to demonstrate the HashMap class.
Source Code:
import [Link].*;
public class hash {
[Link]("Iterating Hashmap...");
for([Link] m:[Link]())
{
[Link]([Link]()+" "+[Link]());
}
}
}
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:
[Link]
<!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>
[Link]
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
String mobile=[Link]("mobile");
String email=[Link]("email");
String addr=[Link]("addr");
String g=[Link]("gender");
String city=[Link]("city");
String course=[Link]("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 [Link].*;
import [Link].*;
public class AdapterExample extends WindowAdapter{
Frame f;
AdapterExample(){ f=new
Frame();
[Link](this);
[Link](400,400);
[Link](null);
[Link](true);
}
public void windowClosing(WindowEvent e) {
[Link]();
}
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 [Link].*;
import [Link];
import [Link];
public class mouse {
public static void main(String args[]) {
Frame frame = new Frame("AnonymousEventExample");
Label l=new Label();
[Link](20,40,100,20);
[Link](l);
addMouseMotionListener(frame);
[Link](400,400);
[Link](null);
[Link](true);
new MouseMotionListener()
{
}
};
}
}
Output:
29 Design a JDBC program to displays the details of employees (eno, ename,
Source Code:
import [Link].*;
try
[Link]("[Link]");
"root","");
[Link](1,"Management");
ResultSet rs=[Link]();
while([Link]())
[Link]([Link](1)+"\n");
[Link]([Link](2)+"\n");
[Link]([Link](3)+"\n");
[Link]([Link](4));
[Link]();
}
catch(Exception e)
[Link](e);
Output:
30 Design JDBC application for Student Feedback Form [Assume suitable table
structure].
Source Code:
[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Student Feedback</title>
</head>
<body>
</form>
</body>
</html>
[Link]
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
String name=[Link]("username");
String mobile=[Link]("mobile");
String email=[Link]("email");
String addr=[Link]("addr");
String feed=[Link]("feed");
try
{ [Link]("[Link]");
Connection con=[Link]("jdbc:mysql://localhost:3306/lab","root","");
[Link](2,name);
[Link](3,mobile);
[Link](4,email);
[Link](5,addr);
[Link](6,feed);
int i=[Link]();
if(i>0)
else
catch(Exception e)
[Link](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 [Link].*;
import [Link].*;
try
int empno=[Link]();
int sal=[Link]();
[Link]("[Link]");
Connection con=[Link]("jdbc:mysql://localhost:3306/lab","root","");
[Link](1,sal);
[Link](2,empno);
int i=[Link]();
if(i>0)
}
else
[Link]("Error");
[Link](1,empno);
ResultSet rs=[Link]();
if([Link]())
[Link]([Link](1));
[Link]([Link](2));
[Link]([Link](3));
[Link]([Link](4));
[Link]([Link](5));
catch(Exception e)
{ [Link](
e);
}
Output:
32 Design a servlet program illustrating its lifecycle.
Source Code:
[Link]
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Source Code:
[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Book Publisher</title>
</head>
<body>
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public bp() {
super();
pw;
[Link]("text/html");
pw=[Link]();
String name=[Link]("name");
String bname=[Link]("bname");
int bpy=[Link]("bpy");
String email=[Link]("email");
try
[Link]("[Link]");
Connection con=[Link](“jdbc:mysql://localhost:3306/lab
,”root”,””);
[Link](1, name);
[Link](2, bname);
[Link](3,bpy);
[Link](4, email);
int x=[Link]();
if(x==1)
catch(Exception e)
[Link]();
}
[Link]();
[Link]
[Link].*; import
[Link].*; import
[Link].*;
[Link]("text/html");
[Link]("<html><body>");
try
[Link]("[Link]");
while ([Link]())
String n = [Link]("aname");
String bn = [Link]("bname");
} [Link]("</table>");
[Link]("</html></body>");
[Link]();
catch (Exception e)
[Link](e);
[Link]
<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:
[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Book Publisher</title>
</head>
<body>
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public zrf() {
super();
pw;
[Link]("text/html");
pw=[Link]();
String name=[Link]("name");
String email=[Link]("email");
int age=[Link]("age");
try
[Link]("[Link]");
Connection con=[Link](“jdbc:mysql://localhost:3306/lab
,”root”,””);
[Link](1, name);
[Link](2, email);
[Link](3, age);
int x=[Link]();
if(x==1)
catch(Exception e)
[Link]();
} [Link]();
}
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:
[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Book Publisher</title>
</head>
<body>
<h1>Cookie Example</h1>
</form>
</body>
</html>
[Link]
[Link].*; import
[Link].*; import
[Link];
HttpServletResponse response) {
try{ [Link]("text/html");
[Link]("Hello "+name);
[Link](c1);
[Link](c2);
[Link]();
}catch(Exception exp)
{ [Link](exp
);
[Link]
[Link].*; import
[Link].*;
HttpServletResponse response){
try{ [Link]("text/html");
Cookie c[]=[Link]();
[Link]("Name: "+c[1].getValue());
[Link]("Date: "+c[2].getValue());
[Link]();
}catch(Exception exp)
{ [Link](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:
[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Employee Details</title>
</head>
<body>
</form>
</body>
</html>
[Link]
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
String dept=[Link]("dept");%>
<div>
<h3>Employee Details</h3>
</div>
</body>
</html>
[Link]
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:
[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Division</title>
</head>
<body>
</form>
</body>
</html>
[Link]
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Division</title>
</head>
<body>
String num2=[Link]("n2");
int a=[Link](num1);
int b=[Link](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:
[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Division</title>
</head>
<body>
</form>
</body>
</html>
[Link]
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Division</title>
</head>
<body>
String num2=[Link]("n2");
int a=[Link](num1);
int b=[Link](num2);
int c=a/b;
%>
</body>
</html>
[Link]
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:
[Link]
import [Link].*;
import [Link].*;
try{
[Link]("[Link]);
Connection
con=[Link]("jdbc:mysql://localhost:3306/lab","root","");
PreparedStatement stmt=[Link]("select * from
from bp where bname=?");
[Link](1,bname);
ResultSet rs=[Link]();
if([Link]())
return [Link]("bpy");
break;
}
catch(Exception e)
[Link](e);
try{
[Link]("[Link]);
Connection
con=[Link]("jdbc:mysql://localhost:3306/lab","root","");
PreparedStatement stmt=[Link]("select * from
from bp where bname=?");
[Link](1,bname);
ResultSet rs=[Link]();
if([Link]())
return [Link]("author");
break;
catch(Exception e)
[Link](e);
}
try{
[Link]("[Link]);
Connection con=[Link]("jdbc:mysql://localhost:3306/lab","root","");
[Link](1,bname);
ResultSet rs=[Link]();
if([Link]())
return [Link]("bname");
break;
catch(Exception e)
[Link](e);
[Link]=bname;
}
}
[Link]
<html>
<head>
</head>
<body>
</jsp:useBean>
<p>Book Name:
</p>
<p>Author Name:
</p>
</p>
</body>
</html>
[Link]
import [Link].*;
import [Link].*;
try{
[Link]("[Link]);
Connection
con=[Link]("jdbc:mysql://localhost:3306/lab","root","");
PreparedStatement stmt=[Link]("select * from
from stud where sid=?");
[Link](1,sid);
ResultSet rs=[Link]();
if([Link]())
return [Link]("sid");
break;
catch(Exception e)
{
[Link](e);
try{
[Link]("[Link]);
Connection
con=[Link]("jdbc:mysql://localhost:3306/lab","root","");
PreparedStatement stmt=[Link]("select * from
from stud where sid=?");
[Link](1,sid);
ResultSet rs=[Link]();
if([Link]())
return [Link]("sname");
break;
catch(Exception e)
[Link](e);
{
try{
[Link]("[Link]);
Connection con=[Link]("jdbc:mysql://localhost:3306/lab","root","");
[Link](1,sid);
ResultSet rs=[Link]();
if([Link]())
return [Link]("company");
break;
catch(Exception e)
[Link](e);
[Link]=sid;
[Link]
<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: