SY Java Journal 2023 24
SY Java Journal 2023 24
CERTIFICATE
Place: Date:
Sign of H.O.D
Sr.no Practical Date Sign
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);
}
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);
}
Output:
Practical 3.
Write a program to define user defined exceptions and raise them as per the
requirements
a. List interface
import java.util.List;
import java.util.ArrayList;
class ListInterface
{
Output :
b. Set Interface
import java.util.*;
Output :
5 a. Write a Program in Java Using Swing to illustrate the use of JButton.
import java.awt.event.*;
import javax.swing.*;
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.*;
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;
}
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;
<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>
</body>
</html>
ii) Palindrome.jsp
<html>
<head>
</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
%>
</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;