ACADEMIC YEAR 2020-2021 Roll Number: 335 Name: Rishikesh Jagannath Jangam. MCA-I Semester Course - Java Programming Practical Journals
ACADEMIC YEAR 2020-2021 Roll Number: 335 Name: Rishikesh Jagannath Jangam. MCA-I Semester Course - Java Programming Practical Journals
ACADEMIC YEAR 2020-2021 Roll Number: 335 Name: Rishikesh Jagannath Jangam. MCA-I Semester Course - Java Programming Practical Journals
1
Dr. D. Y. Patil Unitech Society’s
Dr. D.Y. PATIL INSTITUTE OF MANAGEMENT & RESEARCH,
Sant Tukaram Nagar, Pimpri, Pune-411018, Maharashtra, India.
(Approved by All India Council for Technical Education & Recognized by the
Savitribai Phule Pune University)
Date: 18-05-2021
CERTIFICATE
2
INDEX
3
_____________________________________________________________________________________
_____________________________________________________________________________________
import java.util.*;
public class Prog1 {
System.out.println("---------------------------------------------------");
System.out.println("\t\t Program 2");
System.out.println("---------------------------------------------------");
int soa=0;
System.out.print("How many numbers you want in an array : ");
Scanner obj = new Scanner(System.in);
soa=obj.nextInt();
int arr[]=new int[soa];
boolean valPresent=false;
System.out.println("---------------------------------------------------");
System.out.println("Enter Numbers");
for (int i=0;i<arr.length;i++)
{
System.out.print("Num "+(i+1)+" : ");
arr[i]=obj.nextInt();
}
System.out.println("---------------------------------------------------");
System.out.println("Entered Numbers are");
for (int i=0;i<arr.length;i++)
{
System.out.println("Num "+(i+1)+": "+arr[i]);
}
System.out.println("---------------------------------------------------");
int searchItem=0;
System.out.print("Enter Number to search in array :");
searchItem=obj.nextInt();
System.out.println("---------------------------------------------------");
System.out.println("---------------Search Result--------------------");
for (int i=0;i<arr.length;i++)
{
if(arr[i]==searchItem)
{
valPresent=true;
1
System.out.println("searchItem is present in an array");
break;
}
}
if(!valPresent)
{
System.out.println("searchItem is not present in an array");
}
System.out.println("---------------------------------------------------");
}
}
------------------------------------------------------------------------------------------------------------------------------------------
Output
------------------------------------------------------------------------------------------------------------------------------------------
2
_____________________________________________________________________________________
Program 02. Write a program to differentiate static and non-static members of class.
_____________________________________________________________________________________
public Prog2()
{
counter++;
staticCounter++;
}
Output
------------------------------------------------------------------------------------------------------------------------------------------
3
_____________________________________________________________________________________
_____________________________________________________________________________________
import java.util.*;
public class Prog3
{
public static void main(String[] args)
{
System.out.println("----------------------------------------------");
System.out.println("------------------Program3--------------------");
System.out.println("----------------------------------------------\n");
Person p1=new Person();
System.out.println("***Default Constructor Object");
System.out.println("P1 object");
p1.printPersonDetails();
Person p2=new Person(1);
System.out.println("***One Parameter Constructor Object");
System.out.println("P2 object");
p2.printPersonDetails();
Person p3=new Person(2,"Rishikesh");
System.out.println("***Two Parameterized Constructor Object");
System.out.println("P3 object");
p3.printPersonDetails();
System.out.println("----------------------------------------------");
}
}
public class Person
{
int number;
String name;
Person()
{
this.number=00;
this.name="Default";
}
Person(int number)
{
this.number=number;
this.name="Default";
}
4
Person(int number,String name)
{
this.number=number;
this.name=name;
}
------------------------------------------------------------------------------------------------------------------------------------------
Output
------------------------------------------------------------------------------------------------------------------------------------------
5
_____________________________________________________________________________________
_____________________________________________________________________________________
package Vehicle;
package Vehicle;
------------------------------------------------------------------------------------------------------------------------------------------
Output
Bike is running.
Speed of Bike: 50 Km/h
------------------------------------------------------------------------------------------------------------------------------------------
6
_____________________________________________________________________________________
_____________________________________________________________________________________
Prog5()
{
super();
this.p5val=00;
}
public void printProg5Vals()
{
super.printStudentDetails();
System.out.println("P5val variable Value ="+1);
}
public static void main(String[] args)
{
System.out.println("----------------------------------------------");
System.out.println("------------------Program5--------------------");
System.out.println("----------------------------------------------\n");
Person p1=new Person();
System.out.println("****No Inheritance");
System.out.println("P1 object Of Class Person");
p1.printPersonDetails();
System.out.println("----------------------------------------------\n");
Student p2=new Student();
System.out.println("****Single Inheritance");
System.out.println("P2 object Of Class Student");
p2.printStudentDetails();
System.out.println("----------------------------------------------\n");
Prog5 p3=new Prog5();
System.out.println("****Single Inheritance");
System.out.println("P3 object Of Class Prog5");
p3.printProg5Vals();
System.out.println("----------------------------------------------");
}
}
public class Person
{
public String gender;
7
public String name;
Person()
{
this.name="Default";
this.gender="Default";
}
Person(String name, String gender)
{
this.name=name;
this.gender=gender;
}
Student()
{
super();
this.Roll_Number=111;
}
Student(int Roll_Number)
{
super();
this.Roll_Number=Roll_Number;
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------
8
Output
------------------------------------------------------------------------------------------------------------------------------------------
9
_____________________________________________________________________________________
_____________________________________________________________________________________
Rectangle(float l, float b)
{
this.length=l;
this.breadth=b;
}
void area()
{
System.out.println("length is :"+length+" cm");
System.out.println("breadth is :"+breadth+" cm");
System.out.println("Area of Rectangle is :"+(length*breadth)+" cm");
}
}
10
{
float side;
Square(float s)
{
this.side=s;
}
void area()
{
System.out.println("side is :"+side+" cm");
System.out.println("Area of Square is :"+(side*4)+" cm");
}
}
public abstract class Shape
{
abstract void area();
}
------------------------------------------------------------------------------------------------------------------------------------------
Output
------------------------------------------------------------------------------------------------------------------------------------------
11
_____________________________________________________________________________________
import java.util.*;
public class Prog7
{
public static void main(String[] args)
{
System.out.println("----------------------------------------------");
System.out.println("------------------Program7--------------------");
System.out.println("----------------------------------------------");
System.out.println("--------Application Of String Tokenizer--------");
System.out.println("----------------------------------------------");
System.out.println("String Tokenizer For WHitespaces");
StringTokenizer st = new StringTokenizer("Rishikesh Jagannath Jangam From Pune"," ");
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
System.out.println("----------------------------------------------");
System.out.println("String Tokenizer For Comma");
StringTokenizer st2 = new StringTokenizer("Rishikesh,Jagannath,Jangam,From,Pune");
System.out.println("Next token is : " + st2.nextToken(","));
System.out.println("----------------------------------------------\n");
System.out.println("----------------------------------------------");
System.out.println("--------Application Of String Builder--------");
System.out.println("----------------------------------------------");
StringBuilder s=new StringBuilder("Rishikesh");
s.reverse();
System.out.println("Reverse of string using String builder");
System.out.println("Reverse String s is : "+s);
System.out.println("----------------------------------------------\n\n");
}
}
------------------------------------------------------------------------------------------------------------------------------------------
12
Output
------------------------------------------------------------------------------------------------------------------------------------------
13
_____________________________________________________________________________________
_____________________________________________________________________________________
import java.io.*;
public class Prog8 {
public static void main(String[] args)
{
try
{
System.out.println("--------------------------------------------------------------");
System.out.println("--------------------------Program 8------------------------");
System.out.println("--------------------------------------------------------------");
int i;
while((i=br.read())!=-1)
{
System.out.print((char)i);
}
br.close();
fr.close();
System.out.println("\n--------------------------------------------------------------");
14
fr2.close();
System.out.println("\n--------------------------------------------------------------");
System.out.println("--------------------------------------------------------------");
} catch (IOException e) {
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------
Output
------------------------------------------------------------------------------------------------------------------------------------------
15
_____________________________________________________________________________________
Program 09. Test any five of standard exception and user Defined Custom Exceptions in java.
_____________________________________________________________________________________
import java.io.*;
class Prog9
{
void validate(int age)throws InvalidAgeException
{
if(age<18)
throw new InvalidAgeException("not valid");
else
System.out.println("welcome to vote");
}
void productCheck(int weight) throws InvalidProductException
{
if(weight<100)
{
throw new InvalidProductException("Product Invalid");
}
}
void InputCheck(int Input) throws InvalidInputException
{
if(Input<100)
{
throw new InvalidInputException("Input Invalid");
}
}
void ChoiceCheck(int Choice) throws InvalidChoiceException
{
if(Choice<100)
{
throw new InvalidChoiceException("Choice Invalid");
}
}
void Withdrawan(int Ammount) throws InvalidAmmountException
{
int balance =5000;
if(Ammount>balance)
{
throw new InvalidAmmountException("Withdrawan cannot happen!!");
}
}
16
public static void main(String args[])
{
System.out.println("--------------------------------------------");
System.out.println("------------------Program 9 ----------------");
System.out.println("--------------------------------------------");
System.out.println("*** Built-in Exceptions ***");
//1
try {
int a = 30, b = 0;
int c = a/b;
System.out.println ("Result = " + c);
}
catch(ArithmeticException e) {
System.out.println ("Can't divide a number by 0");
}
//2
try {
String a = null;
System.out.println(a.charAt(0));
} catch(NullPointerException e) {
System.out.println("NullPointerException..");
}
//3
try {
String a = "This is like chipping "; // length is 22
char c = a.charAt(24); // accessing 25th element
System.out.println(c);
}
catch(StringIndexOutOfBoundsException e) {
System.out.println("StringIndexOutOfBoundsException");
}
//4
try {
File file = new File("E://file.txt");
//5
17
try {
// "akki" is not a number
int num = Integer.parseInt ("akki") ;
System.out.println(num);
} catch(NumberFormatException e) {
System.out.println("Number format exception");
}
//6
try{
int a[] = new int[5];
a[6] = 9;
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println ("Array Index is Out Of Bounds");
}
System.out.println("--------------------------------------------");
System.out.println("--------------------------------------------");
System.out.println("*** User Defined Exceptions ***");
//u1
Prog9 p=new Prog9();
try{
p.validate(13);
}
catch(Exception m){
System.out.println("Exception occured: "+m);
}
//u2
try{
p.productCheck(1);
}
catch(Exception m){
System.out.println("Exception occured: "+m);
}
//u3
try{
p.InputCheck(1);
}
catch(Exception m){
System.out.println("Exception occured: "+m);
18
}
//u4
try{
p.ChoiceCheck(1);
}
catch(Exception m){
System.out.println("Exception occured: "+m);
}
//u5
try{
p.Withdrawan(6000);
}
catch(Exception m){
System.out.println("Exception occured: "+m);
}
System.out.println("--------------------------------------------");
System.out.println("--------------------------------------------");
}
}
19
{
public InvalidChoiceException(String s)
{
super(s);
}
}
class InvalidAmmountException extends Exception
{
public InvalidAmmountException(String s)
{
super(s);
}
}
------------------------------------------------------------------------------------------------------------------------------------------
Output
------------------------------------------------------------------------------------------------------------------------------------------
20
_____________________________________________________________________________________
Program 10. Threads creation and design applications by using Extending the Thread class/
implementing the Runnable Interface.
_____________________________________________________________________________________
public class Prog10 implements Runnable {
public void run() {
System.out.println("Thread has ended");
System.out.println("--------------------------------------------");
}
public static void main(String[] args) {
System.out.println("--------------------------------------------");
System.out.println("------------------Program 10 ---------------");
System.out.println("--------------------------------------------");
System.out.println("*** Thread Application Implementing Runable Interface ***");
Prog10 ex = new Prog10();
Thread t1= new Thread(ex);
t1.start();
System.out.println("* Multiplication Table Of 6 *");
for(int i=1;i<11;i++)
{
System.out.println(i*7);
}
System.out.println("--------------------------------------------");
}
}
------------------------------------------------------------------------------------------------------------------------------------------
Output
----------------------------------------------------------------------------------------------------------------------------------------
21
_____________________________________________________________________________________
_____________________________________________________________________________________
class Prog11
{
public static void main(String args[])
{
System.out.println("--------------------------------------------------------------");
System.out.println("--------------------------Program 11------------------------");
System.out.println("--------------------------------------------------------------");
}
}
class Table
{
synchronized void printTable(int n)
{
System.out.println("Multiplication Table of "+n);
for(int i=1;i<=10;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class MyThread1 extends Thread
{
22
Table t;
MyThread1(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(5);
}
}
class MyThread2 extends Thread
{
Table t;
MyThread2(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(100);
}
}
------------------------------------------------------------------------------------------------------------------------------------------
Output
-----------------------------------------------------------------------------------------------------------------------------------------
23
_____________________________________________________________________________________
_____________________________________________________________________________________
int totalBalance = 0;
Output
----------------------------------------------------------------------------------------------------------------------------------------
24
_____________________________________________________________________________________
Program 13. Design java application using Collection in java such as Array List, Link List
_____________________________________________________________________________________
import java.util.*;
public class Prog13
{
public static void main(String[] args)
{
System.out.println("---------------------------------------------");
System.out.println("------------------Program 13-----------------");
System.out.println("---------------------------------------------");
Scanner sc = new Scanner(System.in);
ArrayList<String> arrList = new ArrayList<String>();
LinkedList<String> lList = new LinkedList<String>();
System.out.println("---------------------------------------------");
System.out.println("How many record you want to add : ");
int n = sc.nextInt();
System.out.println("---------------------------------------------");
//System.out.println("Enter "+n+" items");
for(int i=1;i<=n;i++)
{
System.out.println("Enter "+i+"th Item : ");
String item = sc.next();
arrList.add(item);
lList.add(item);
}
System.out.println("---------------------------------------------");
System.out.println("ArrayList Elements :"+arrList+ "\n ArrayList size :"+arrList.size());
System.out.println("---------------------------------------------");
System.out.println("LinkedList Elements :"+lList+ "\n Linked List Size:"+lList.size());
System.out.println("---------------------------------------------");
System.out.println("Enter Item to remove From ArrayList: ");
String removeString=sc.next();
if(arrList.contains(removeString))
{
arrList.remove(removeString);
}
else
{
System.out.println("String not available");
}
System.out.println("---------------------------------------------");
System.out.println("Enter Item to remove From LinkedList: ");
25
String removeS=sc.next();
if(lList.contains(removeS))
{
lList.remove(removeS);
}
else
{
System.out.println("Item not available");
}
System.out.println("---------------------------------------------");
System.out.println("---------------------------------------------");
System.out.println("***After Remove Operation");
System.out.println("---------------------------------------------");
System.out.println("ArrayList Elements :"+arrList);
System.out.println("---------------------------------------------");
System.out.println("LinkedList Elements :"+lList);
}
}
------------------------------------------------------------------------------------------------------------------------------------------
Output
-----------------------------------------------------------------------------------------------------------------------------------------
26
_____________________________________________________________________________________
Program 14. Design java application using Collection in java such as Queue.
_____________________________________________________________________________________
import java.util.*;
public class Prog14
{
public static void main(String[] args)
{
System.out.println("---------------------------------------------");
System.out.println("------------------Program 14-----------------");
System.out.println("---------------------------------------------");
System.out.println("---------------------------------------------");
System.out.println("---------------------------------------------");
}
}
------------------------------------------------------------------------------------------------------------------------------------------
Output
------------------------------------------------------------------------------------------------------------------------------------------
27
_____________________________________________________________________________________
Program 15. Design java application using Collection in java such as Map
_____________________________________________________________________________________
import java.util.*;
int checker=0;
Map map=new HashMap();
map.put(1,"Amit");
map.put(2,"Jai");
map.put(3,"Rahul");
map.put(4,"Sai");
map.put(5,"Taman");
map.put(6,"Rishikesh");
map.put(7,"Mahendra");
Set set=map.entrySet();
Iterator itr=set.iterator();
System.out.println("Elements of Map");
while(itr.hasNext())
{
Map.Entry entry=(Map.Entry)itr.next();
System.out.println(entry.getKey()+" "+entry.getValue());
}
System.out.println("--------------------------------------------------------------");
Scanner sc=new Scanner(System.in);
System.out.println("Enter name which you want to search in Map : ");
String nm=sc.next();
System.out.println("--------------------------------------------------------------");
Set set1=map.entrySet();
Iterator itr1=set1.iterator();
while(itr1.hasNext())
{
Map.Entry entry1=(Map.Entry)itr1.next();
if(!nm.equals(entry1.getValue()))
{
28
}
else
{
checker=1;
System.out.println("Entered Name is present in map \n Details");
System.out.println(entry1.getKey()+" "+entry1.getValue());
}
}
if(checker==0)
{
System.out.println("Entered Name is not present in map");
}
System.out.println("--------------------------------------------------------------");
}
}
------------------------------------------------------------------------------------------------------------------------------------------
Output
------------------------------------------------------------------------------------------------------------------------------------------
29
_____________________________________________________________________________________
Program 16. Design GUI based java application using AWT, Event Handling.
_____________________________________________________________________________________
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
class AEvent extends Frame implements ActionListener{
TextField tf;
Date d = new Date();
AEvent(){
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("Show Date");
b.setBounds(100,120,80,30);
//register listener
b.addActionListener(this);//passing current instance
30
Output
------------------------------------------------------------------------------------------------------------------------------------------
31
_____________________________________________________________________________________
Program 17. Design a JDBC application to retrieve the data from SQL table
_____________________________________________________________________________________
import java.sql.*;
public class Jdbc {
public static void main(String[] args){
System.out.println("My JDBC Program");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","system");
System.out.println("Connected");
Statement st=con.createStatement();
ResultSet rs= st.executeQuery("select * from login");
while(rs.next()){
System.out.println(rs.getString(1)+"------"+rs.getString(2)+"------"+rs.getString(3));
}
if(rs.next())
{
}
else
{
System.out.println("Rs is empty!");
}
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------
32
Output
------------------------------------------------------------------------------------------------------------------------------------------
33
_____________________________________________________________________________________
_____________________________________________________________________________________
34
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------
Output
------------------------------------------------------------------------------------------------------------------------------------------
35
_____________________________________________________________________________________
_____________________________________________________________________________________
package test;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
@SuppressWarnings("serial")
public class InsertServlet extends GenericServlet
{
public Connection con;
@Override
public void init() throws ServletException
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ADITYA12","system","ma
nager");
}
catch(Exception e) {e.printStackTrace();}
}
public void service(ServletRequest req,ServletResponse res)
throws ServletException,IOException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
String eId=req.getParameter("eid");
String eName=req.getParameter("ename");
long ephno=Long.parseLong(req.getParameter("ephno"));
try
{
PreparedStatement ps=con.prepareStatement("insert into studentdata values(?,?,?)");
ps.setString(1,eId);
ps.setString(2,eName);
ps.setLong(3,ephno);
int k=ps.executeUpdate();
if(k>0)
{
pw.println("Employee Data Inserted Succesfully");
36
}
}
catch(Exception e) {e.printStackTrace();}
}
@Override
public void destroy()
{
try {con.close();}
catch(Exception e) {e.printStackTrace();}
}
}
Input.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="insert" method="post">
Emp Id: <input type="number" name="eid"><br>
Emp Name: <input type="text" name="ename"><br>
Emp Phno: <input type="tel" name="ephno"><br>
<input type="submit" value="Insert">
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------------
37
Output
------------------------------------------------------------------------------------------------------------------------------------------
38
_____________________________________________________________________________________
_____________________________________________________________________________________
Display.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Program2</title>
</head>
<body>
<% String name=request.getParameter("name");
long phno=Long.parseLong(request.getParameter("phno"));
String mid=request.getParameter("mid");
out.println("Name :"+name);
out.println("Phone no :"+phno);
out.println("Email ID :"+mid);
%>
</body>
</html>
Input.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Display.jsp" method="post">
Name:
<input type="text" name="name"><br>
Phone no:
<input type="text" name="phno"><br>
Email ID:
<input type="text" name="mid"><br>
<input type="submit" name="Display">
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------------
39
Output
------------------------------------------------------------------------------------------------------------------------------------------
40