0% found this document useful (0 votes)
4 views33 pages

SY Java Journal 2023 24

The document is a certification template for students completing practical work in Java Based Application Development at various colleges. It outlines the practical exercises completed by students, including concepts like constructor overloading, inheritance, abstract classes, and JDBC programming. The document also includes sample code for various Java applications demonstrating these concepts.

Uploaded by

rambhiatwinkle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views33 pages

SY Java Journal 2023 24

The document is a certification template for students completing practical work in Java Based Application Development at various colleges. It outlines the practical exercises completed by students, including concepts like constructor overloading, inheritance, abstract classes, and JDBC programming. The document also includes sample code for various Java applications demonstrating these concepts.

Uploaded by

rambhiatwinkle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

JNAN VIKAS MANDAL’S

PADMASHREE DR. R.T.DOSHI DEGREE COLLEGE OF INFORMATION


TECHNOLOGY

MOHANLAL RAICHAND MEHTA COLLEGE OF COMMERCE

DIWALIMAA DEGREE COLLEGE OF SCIENCE

CERTIFICATE

This is to certify that the Mr./Miss.


having Roll No of B.Sc. CS Semester III has completed
the practical work in the subject of Java Based Application Development under
the guidance of Prof Janhavi Kshirsagar during the Academic year 2023-24 being
the partial requirement for the fulfillment of the curriculum of Degree of Bachelor
of Science in Computer Science, University of Mumbai.

Place: Date:

Sign of Subject In Charge Sign of External Examiner

Sign of H.O.D
Sr.no Practical Date Sign

1 a. Write a program to create a class and implement the


concepts of Constructor Overloading, Method
Overloading, Static methods
b. Write a program to implement the concept of
Inheritance and Method Overriding
2. a. Write a program to implement the concepts of
Abstract classes and methods
b. Write a program to implement the concept of
interfaces
3.
Write a program to define user-definedd exceptions and
raise them as per the requirements

4. Write a program to demonstrate the methods of:


a. List interface
b. Set interface
5. a. Write a Program in Java Using Swing to illustrate
the use of JButton.
b. Write a Program in Java Using Swing to illustrate
the use of JTextField.
c. Write a Program in Java Using Swing to illustrate
the use of JList.
6. a. Write a JDBC program that displays the data of
a given table
b. Write A Program in Java using JDBC to insert
record in Employee table using PreparedStatement
c. Write a JDBC program to insert / update / delete
records into a given table
7. a. Write a Program Using Servlet to find the
factorial of a number. Enter number through
num.html
b. Write a program to enter the username and
password. If entered username and password is
correct redirect to other page otherwise display
the error message
8 a. Write a Program using JSP to Check whether the
number is Palindrome or Not.
b. Write a jsp Program to insert data into database
9. Write Java application to encoding and decoding JSON
in Java.
Practical No : 1
Write a program to create a class and implement the concepts of Constructor
Overloading, Method Overloading, Static methods

Constructor Overloading :

class Number
{
int a,b;
Number()
{
a=0;
b=0;
}
Number(int x,int y)
{
a=x;
b=y;
}
Number(int z)
{
a=b=z;
}
void put()
{
System.out.println("Values of a abd b are : ");
System.out.println("a : "+a);
System.out.println("b : "+b);
}
}
class ConOverloadDemo
{
public static void main(String args[])
{
Number N1=new Number(10,20);
System.out.println("Variables are initialized using Constructor : ");
N1.put();
Number N2=new Number(50);
System.out.println("After Overloading a Constructor : ");
N2.put();
}
}
Output :
Method Overloading :

class Area
{
int area(int x)
{
int a1;
a1=x*x;
return a1;
}
double area(double r,double p)
{
double a2;
a2=p*r*r;
return a2;
}

}
class MethOverload
{
public static void main(String args[])
{
Area O = new Area();
int a1;
double a2;
a1=O.area(5);
a2=O.area(7.0,3.14);
System.out.println();
System.out.println("The area of Square is "+a1+" sq.unit");
System.out.println("The area of Circle is "+a2+" sq.unit");
}
}

Output:
Static Methods :

class StaticMeth
{
static int c=10;
static void put()
{

System.out.println("Changed value");
c=c+10;
System.out.println("c = "+c);
}
static
{
System.out.println("Block is Initialised");
System.out.println("c = "+c);
}

public static void main(String args[])


{
put();
}
}

Output :
b. Write a program to implement the concept of Inheritance and Method Overriding

Single Inheritance :

class Stud
{
int r;
String n;
void get()
{
r=101;
n="ABC";
}
void put()
{
System.out.println("Roll : "+r);
System.out.println("Name : "+n);
}
}
class Result extends Stud
{
double m1,m2,m3,a;
void get_marks()
{
m1=60;
m2=58;
m3=75;
}
void put_marks()
{
a=(m1+m2+m3)/3;
System.out.println("Subject 1 : "+m1);
System.out.println("Subject 2 : "+m2);
System.out.println("Subject 3 : "+m3);
System.out.println("Average : "+a);
}
}
class SingleInhDemo
{
public static void main(String args[])
{
Result R1=new Result();
R1.get();
R1.put();
R1.get_marks();
R1.put_marks();
}
}
Output :
a. Method Overriding :

class A
{
int a;
A()
{
a=0;
}
A(int x)
{
a=x;
}
void show()
{
System.out.println("a of super class : "+a);
}
}
class B extends A
{
int b;
B()
{
super();
b=0;
}
B(int x,int y)
{
super(x);
b=y;
}
void show()
{
super.show();
System.out.println("b of super class : "+b);
}
}

class MethOverride
{
public static void main(String agrs[])
{
B obj = new B(100,200);
obj.show();
}
}
Output:
a. Abstract Classes and Methods :

abstract class A
{
abstract void show();
void display()
{
System.out.println("Super Class Method");
}
}
class B extends A
{
void show()
{
System.out.println("Sub Class Method");
}
}
class AbstractMeth
{
public static void main(String args[])
{
B b = new B();
b.display();
b.show();
}
}

Output :
b. Write a program to implement the concept of interfaces

Interface :

interface DispInter
{
void print(int n);
}

Class Implementing Interface :

class Demo implements DispInter


{
public void print(int a)
{
System.out.println("A= "+a);
}
}
class InterDemo
{
public static void main(String args[])
{
DispInter I=new Demo();
I.print(10);
}
}

Output:
Practical 3.
Write a program to define user defined exceptions and raise them as per the
requirements

class MyExcp extends Exception


{
int a;
MyExcp(int x)
{
a=x;
}
public String toString()
{
return("-ve Number is not Valid number to find square root ");
}
}
class MyExcpDemo
{
static void Root(int a) throws MyExcp
{
double r;
System.out.println("Method Calulates Square Root of "+a);
if(a<0)
{
throw new MyExcp(a);
}
r=Math.sqrt(a);
System.out.println("The Square root : "+r);
}
public static void main(String args[])
{
try
{
Root(16);
Root(-25);
}
catch(MyExcp e)
{
System.out.println("Exception caught");
System.out.println(e);
}
}
}
Output :
4. Write a program to demonstrate the methods of:

a. List interface

import java.util.List;
import java.util.ArrayList;

class ListInterface
{

public static void main(String[] args)


{
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
System.out.println("List: " + numbers);
int number = numbers.get(2);
System.out.println("Accessed Element: " + number);
int removedNumber = numbers.remove(1);
System.out.println("Removed Element: " + removedNumber);
}
}

Output :
b. Set Interface

import java.util.*;

public class SetInterface


{

public static void main(String[] args)


{

Set<String> Set1 = new HashSet<String>();


Set1.add("Java");
Set1.add("Python");
Set1.add(".NET");
Set1.add("Perl");
Set1.add("HTML");
System.out.println("Elements of Set are : "+Set1);
}
}

Output :
5 a. Write a Program in Java Using Swing to illustrate the use of JButton.

import java.awt.event.*;
import javax.swing.*;

public class JButtonExample


{
public static void main(String[] args)
{
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
tf.setBounds(50,50, 150,20);
JButton b=new JButton("Display");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tf.setText("Welcome to Java.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
5 b. Write a Program in Java Using Swing to illustrate the use of JTextField.

import javax.swing.*;
import java.awt.event.*;
public class JTextFieldExample implements ActionListener
{
JTextField tf1,tf2,tf3;
JButton b1,b2;
JTextFieldExample()
{
JFrame f= new JFrame();
tf1=new JTextField();
tf1.setBounds(50,50,150,20);
tf2=new JTextField();
tf2.setBounds(50,100,150,20);
tf3=new JTextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1=new JButton("+");
b1.setBounds(50,200,50,50);
b2=new JButton("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
f.add(tf1);
f.add(tf2);
f.add(tf3);
f.add(b1);
f.add(b2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1)
{
c=a+b;
}
else
if(e.getSource()==b2)
{
c=a-b;
}
String result=String.valueOf(c);
tf3.setText(result);
}
public static void main(String[] args)
{
new JTextFieldExample();
}
}

Output :
5 c. Write a Program in Java Using Swing to illustrate the use of JList.

import javax.swing.*;
import java.awt.event.*;
public class ListExample
{
ListExample()
{
JFrame f= new JFrame();
final JLabel label = new JLabel();
label.setSize(500,100);
JButton b=new JButton("Show");
b.setBounds(200,150,80,30);
final DefaultListModel<String> l1 = new DefaultListModel<>();
l1.addElement("C");
l1.addElement("C++");
l1.addElement("Java");
l1.addElement("PHP");
final JList<String> list1 = new JList<>(l1);
list1.setBounds(100,100, 75,75);
f.add(list1);
f.add(b);
f.add(label);
f.setSize(450,300);
f.setLayout(null);
f.setVisible(true);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String data = "";
if (list1.getSelectedIndex() != -1)
{
data = "Programming language Selected: " +
list1.getSelectedValue();
label.setText(data);
}
}
});
}
public static void main(String args[])
{
new ListExample();
}
}
Output :
6. a Write a JDBC program that displays the data of a given table

import java.io.*;
import java.sql.*;

public class DispTableRec


{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:DSN123");
Statement s=c.createStatement();
ResultSet rs=s.executeQuery("select *from Stud_Info");
while(rs.next())
{
int rn=rs.getInt("Roll");
String n=rs.getString("Name");
double a=rs.getDouble("Avg");
System.out.println("Roll no : "+rn);
System.out.println("Name : "+n);
System.out.println("Avg : "+a);
System.out.println();
}

s.close();
c.close();
}
catch(Exception e)
{
System.out.println(e);
}}
}

Output :
b. Write A Program in Java using JDBC to insert record in Employee table using
PreparedStatement
import java.io.*;
import java.sql.*;
public class PSInsertValTable
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:DSN1601");
PreparedStatement ps=c.prepareStatement("Insert into Employee values(?,?,?)");
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Emp_No: ");
int eno=Integer.parseInt(br.readLine());
System.out.println("Enter Name : ");
String n=br.readLine();
System.out.println("Enter Salary : ");
double s=Double.parseDouble(br.readLine());
ps.setInt(1,eno);
ps.setString(2,n);
ps.setDouble(3,s);
ps.executeUpdate();
System.out.println("Records added successfully");
ps.close();
c.close();
}
catch(Exception e)
{
System.out.println(e);
}}}
Output:
7. . Write a Program using Servlet to find the factorial of a number. Enter number
through num.html

Num.html

<html>
<head>
<title>Number Window</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="get" action="FactServlet">
<p> Enter Number : <input type="text" name="txtn"></p>
<p><input type="submit" value="FindFactorial"></p>
</form>
</body>
</html>

FactServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FactServlet extends HttpServlet


{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throw ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try
{
PrintWriter out = response.getWriter();
int n=Integer.parseInt(request.getParameter("txtn"));
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
out.println("Factorial of "+n+" is "+f);
}
catch(Exception e)
{
System.out.println(e);
}
}

}
Output :
7. b. Write a program to enter the username and password. If entered username and
password is correct redirect to other page otherwise display the error message
i) LoginData.html
<html>
<head>
<title>Login Form</title>
</head>
<body>
<form method="get" action="SendRedirectServlet">
<p>UserName : <input type="text" name="uname"></p>
<p>Password : <input type="text" name="pass"></p>
<p><input type="submit" value="Login"></p>
</form>
</body>
</html>

ii) SendRedirect.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SendRedirectServlet extends HttpServlet


{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try
{
PrintWriter out = response.getWriter();
String user1=request.getParameter("uname");
String pass1=request.getParameter("pass");
if(user1.equals("User") && pass1.equals("123"))
response.sendRedirect("Data.html");
else
out.println("Wrong Username and password");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
iii) Data.html

<html>
<title>Valid data display</title>
</head>
<body>
<h1>WelCome to this Page</h1>
</body>
</html>

Output :
8. Write a Program to Check whether the number is Palindrome or
Not

i) Palindrom1.html
<html>

<head>

<title>palindrome program</title>

</head>

<body>

<form method="get" action="palindrome.jsp">

<p>Enter a number=<input type="text" name="num"></p>

<p><input type="submit" value="Click Here">

</form>

</body>

</html>

ii) Palindrome.jsp
<html>

<head>

<title> palindrome program</title>

</head>

<body>

<%

intnum,rem,rev=0,n;

num=Integer.parseInt(request.getParameter("num"));

n=num;
while(num>0)

rem=num%10;
rev=(rev*10)+rem;

num=num/10;
}

if(n==rev)

out.println("Number is Palindrome");

else

out.println("number is not palindrome");

%>

</body>

</html>

OUTPUT:
b.Write a jsp Program to insert data into database

i)Login.html
<html>
<head>
<title> login form </title>
</head>
<body>
<form method="get" action="login2.jsp">
<p>Enter a Username=<input type="text" name="uname"></p>
<p>Enter Password=<input type="text" name="pass"></p>
<input type="submit" value="Click here">
</form>
</body>
</html>

ii)Login.jsp
<html>
<head>
<title>login program</title>
</head>
<body>
<%@page import="java.sql.*"%>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:ds15");
PreparedStatementps =con.prepareStatement("insert into loginjava values(?,?)");
ps.setString(1,request.getParameter("uname"));
ps.setString(2,request.getParameter("pass"));
ps.executeUpdate();
out.println("Record inserted Successfully");
}
catch(Exception e)
{
System.out.println(e);
}
%>
</body>
</html>
OUTPUT :
9. Write Java application to encoding and decoding JSON in Java.

Encode JSON :

import org.json.simple.JSONObject;

class JsonwithJava
{
public static void main(String argu[])
{
JSONObject o1 = new JSONObject();
o1.put("name", "Alex");
o1.put("roll", new Integer(12));
o1.put("total_marks", new Double(684.50));
obj.put("pass", new Boolean(true));
System.out.print(o1);
}
}

Decode :

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class JsonDecodeExample1


{
public static void main(String[] args) {
String s = "{\"name\":\"Alex\",\"marks\":648.50,\"roll\":12}";
Object o1 = JSONValue.parse(s);
JSONObject jsonObj = (JSONObject) o1;
String name = (String) jsonObj.get("name");
double marks = (Double) jsonObj.get("marks");
Integer roll = (Integer) jsonObj.get("roll");
System.out.println(name + " " + marks + " " + roll);
}
}

You might also like