Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

Practical No: 1

Question:

Write a program to create a class named shape. In this class we have three sub
classes circle,triangle and square each class has two member function named draw
() and erase (). Create these using polymorphism concepts.

Code:
class shape
{void draw(){

System.out.println("Shape is drawn");

}
void erase(){
System.out.println("Shape is erased");
}
}
class circle extends shape
{
void draw()
{
System.out.println("Circle is drawn");
}
void erase()
{
System.out.println("Circle is erased");
}
} class triangle extends shape
{
void draw()
{
System.out.println("Triangle is drawn");
}
void erase()
{
System.out.println("Triangle is erased");
}

}
class square extends shape
{

1904313902 Avinash Chandravansi 1


void draw()
{
System.out.println("Square is drawn");
}
void erase() {
System.out.println("Square is erased");
}
} class practical1
{
public static void main(String[] args)
{
shape obj1 = new shape(); //creating base class object
obj1.draw(); //calling base class method draw()
obj1.erase();
shape obj2 = new circle();
obj2.draw(); //calling derived class method draw()
obj2.erase();
shape obj3 = new triangle();
obj3.draw(); //calling derived class traingle method draw()
obj3.erase();
shape obj4 = new square();
obj4.draw(); //calling derived class square method draw()
obj4.erase();
}
}

Output :

1904313902 Avinash Chandravansi 2


Practical No: 2
Question:

Write a java program to create an abstract class named Shape that contains two
integers and an empty method named printArea(). Provide three classes named
Rectangle, Triangle and Circle such that each one of the classes extends the class
Shape. Each one of the classes contain only the method printArea( ) that prints the
area of the given shape.

Code:
abstract class shape
{
int x=10;int y=20;
abstract void printArea();

}
class Rectangle extends shape
{
void printArea(){
System.out.println("Area of rectangle is :"+(x*y));
}
}
class Triangle extends shape
{
void printArea(){
System.out.println("Area of Triangle is :"+(0.5*x*y));
}
}
class Circle extends shape
{
void printArea(){
System.out.println("Area of Circle is :"+(3.14*x*x));

}
}

class practical2
{
public static void main(String[] args)
{
Rectangle ob1 = new Rectangle();

1904313902 Avinash Chandravansi 3


ob1.printArea();
Triangle ob2 = new Triangle();
ob2.printArea();
Circle ob3 = new Circle();
ob3.printArea();
}
}

Output:

1904313902 Avinash Chandravansi 4


Practical No: 3
Question:

Write a program to create interface named test. In this interface the member
function is square. Implement this interface in arithmetic class. Create one new
class called ToTestInt in this class use the object of arithmetic class.

Code:

interface test
{
void square();
}
class arithmetic implements test
{
int no;
arithmetic(int num) //using parameterized constructor
{
no = num;
}
public void square(){
System.out.println("The square root of "+no+ " is : "+Math.sqrt(no));
}

}
class ToTestInt
{
void ans(int value)
{
arithmetic ob = new arithmetic(value);
ob.square();
}
}
class practical3
{
public static void main(String[] args)
{
ToTestInt obj = new ToTestInt();
obj.ans(Integer.parseInt(args[0]));
}

1904313902 Avinash Chandravansi 5


Output:

1904313902 Avinash Chandravansi 6


Practical No: 4
Question:

Write a program to create a package named mypack (to calculate area) and import
it in circle class.

Code:
Circle.java
package mypack;

public class circle


{
public void displayarea(int x)
{
System.out.println("The area of circle is :"+ (3.14*x*x));
}
}

Practical4.java
import mypack.circle;
class practical4
{
public static void main(String [] args)
{
circle ob = new circle();
ob.displayarea(Integer.parseInt(args[0]));
}
}

Output:

1904313902 Avinash Chandravansi 7


Practical No: 5
Question:

Write a program to calculate the following


· Find the length of array.
· Demonstrate a one-dimensional array.
· Demonstrate a two-dimensional array.
· Demonstrate a multi-dimensional array.
Code:

import java.util.Scanner;
class practical5
{
static void single()
{
Scanner ob = new Scanner(System. in);
System.out.println("Enter the length of array");
int len = ob.nextInt();
int arr[] = new int[len];
System.out.println("Enter the array elements");
for(int i=0;i<len;i++)
{
arr[i]=ob.nextInt();

}
System.out.println("Final array is :");
for(int i=0;i<len;i++)
{

System.out.print(arr[i]+" ");
}

System.out.println("\nThe length of array is: "+arr.length);

}
static void twodim()
{
Scanner ob = new Scanner(System. in);

System.out.println("Enter the no of rows in array");

1904313902 Avinash Chandravansi 8


int column = ob.nextInt();

System.out.println("Enter the no of column in array");


int row = ob.nextInt();
int[][] arr = new int[row][column];
for(int i=0;i<row;i++ )
{
for(int j=0;j<column;j++)
{
System.out.println("Enter the element in ["+i+"]["+j+"]");

arr[i][j]= ob.nextInt();
}
}
System.out.println("The output array is :");
for(int i=0;i<row;i++ )
{
for(int j=0;j<column;j++)
{
System.out.print("["+arr[i][j]+"]");

}
System.out.println();

}
System.out.println("The Numbers of elements in this array are : "+(row*column));
}

static void multidim()


{

Scanner ob = new Scanner(System. in);


System.out.println("Enter the no of tables in array");
int tab = ob.nextInt();

System.out.println("Enter the no of rows in array");


int column = ob.nextInt();

System.out.println("Enter the no of column in array");

int row = ob.nextInt();


int[][][] arr = new int[tab][row][column];

1904313902 Avinash Chandravansi 9


for(int i=0;i<tab;i++ )
{
for(int j=0;j<row;j++)
{
for(int k=0;k<column;k++ )
{
System.out.println("Enter the element in ["+i+"]["+j+"]["+k+"]");

arr[i][j][k]= ob.nextInt();
}
}
}

for(int i=0;i<tab;i++ )
{
for(int j=0;j<row;j++)
{
for(int k=0;k<column;k++ )
{
System.out.print("[" +arr[i][j][k]+"]");

}
System.out.println();
}
}

System.out.println("The Numbers of elements in this array are : "+(tab*row*column));

public static void main(String[] args )


{
Scanner obj = new Scanner(System.in);

int n;
do{
System.out.println("#########################################");
System.out.println("Enter your choice : \n1. To use Single Dimensional array \n2. To use
Two Dimensional array. \n3. To use Multi-Dimensional array. \n0. To exit. ");

1904313902 Avinash Chandravansi 10


n= obj.nextInt();
switch(n)
{
case 1 :
single();
break;
case 2 :
twodim();
break;

case 3 :
multidim();
break;
default :
n=0;
break;
}

}
while(n!=0);
}
}

Output:

1904313902 Avinash Chandravansi 11


1904313902 Avinash Chandravansi 12
Practical No: 6
Question:

Write a program for types of inheritance in Java.

· Single inheritance
· Multi-level inheritance
· Hierarchical inheritance
· Multiple inheritance

Code:
Single inheritance

class base
{
void display(int x) //method to display value
{
System.out.println("The output value is :"+x);
}
}

class child extends base


{
void square(int x) //method to calculate square
{
display(x*x);
}
}

class singleinherit
{
public static void main(String[] args)
{
int z = 12; //Finding square of this number
child ob = new child();
ob.square(z);

}
}

1904313902 Avinash Chandravansi 13


Output:

Multi-level inheritance
class a
{
void method1()
{
System.out.println("This is class A");
}
}
class b extends a
{
void method2()
{
System.out.println("This is class B");
}

}
class c extends b
{
void method3()
{
method1(); //method of class A called
method2(); //method of class B called
System.out.println("This is class C");

}
}

class multilevel
{
public static void main(String[] args)
{
c ob = new c();
ob.method3();
}
}

1904313902 Avinash Chandravansi 14


Output:

Hierarchical inheritance

class X
{
void method1()
{
System.out.println("Method1() from class X");
}
void method2()
{
System.out.println("Method2() from class X");
}
}
class Y extends X
{
void callb()
{
method1();
}

}
class Z extends X
{
void callc()
{
method2();
}
}

class hierinherit
{
public static void main(String[] args)
{
Y ob1 = new Y();

1904313902 Avinash Chandravansi 15


Z ob2 = new Z();
ob1.callb();
ob2.callc();

}
}
Output:

Multiple inheritance

interface interface1
{
String msg1 = "I Love";
}
interface interface2 extends interface1
{
String msg2 = "GNU/" ;
}
class class3 implements interface2
{
void display()
{
System.out.println(msg1+" "+msg2+"Linux");
}

}
class multiple
{
public static void main(String[] args)
{
class3 ob = new class3();
ob.display();
}
}

Output :

1904313902 Avinash Chandravansi 16


Practical No: 7
Question:

Write HTML/Java scripts to display your CV in navigator, your Institute website,

Department Website and Tutorial website for specific subject

Code:

<html>

<head>
<title>Practical 7</title>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=yes" />
<style>
.navbar a:hover,
.dropdown:hover .dropbtn .dropdown-content a:hover {
background-color: #d81003;
}

.navbar a {
float: left;
font-size: 18px;
color: white;
text-align: center;
padding: 14px 40px;
text-decoration: none;
font-weight: 300;
}

#outer {
background-color: #d81003;
height: 50px;
}

.navbar {
overflow: hidden;
background-color: #0d2d62;
font-family: Arial;
font-weight: 300;
}

.dropdown {

1904313902 Avinash Chandravansi 17


float: left;
overflow: hidden;
margin-top: 5px;
font-size: 18px;
}

.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 10px 16px;
background-color: #0d2d62;
font-family: inherit;
margin: 0;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #0d2d62;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);

.dropdown-content a {
float: none;
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}

.dropdown:hover .dropdown-content {
display: block;
}

*{
box-sizing: border-box;
}

/* Set a background color */


body {

1904313902 Avinash Chandravansi 18


background-color: #474e5d;
font-family: Helvetica, sans-serif;
}

.timeline {
position: relative;
max-width: 1200px;
margin: 0 auto;
}

.timeline::after {
content: '';
position: absolute;
width: 6px;
background-color: #d81003;
top: 0;
bottom: 0;
left: 50%;
margin-left: -3px;
}

.container {
padding: 10px 40px;
position: relative;
background-color: inherit;
width: 50%;
}

.container::after {
content: '';
position: absolute;
width: 25px;
height: 25px;
right: -17px;
background-color: white;
border: 4px solid #d81003;
top: 15px;
border-radius: 50%;
z-index: 1;
}

.left {
left: 0;
}

1904313902 Avinash Chandravansi 19


.right {
left: 50%;
}

.left::before {
content: " ";
height: 0;
position: absolute;
top: 22px;
width: 0;
z-index: 1;
right: 30px;
border: medium solid white;
border-width: 10px 0 10px 10px;
border-color: transparent transparent transparent #0d2d62;
}

.right::before {

content: " ";


height: 0;
position: absolute;
top: 22px;
width: 0;
z-index: 1;
left: 30px;
border: medium solid rgb(168, 155, 155);
border-width: 10px 10px 10px 0;

border-color: transparent #0d2d62 transparent transparent;


}

.right::after {
left: -16px;
}

.content {
padding: 20px 30px;
background-color: #0d2d62;
position: relative;
border-radius: 6px;
color: white;

}
</style>

1904313902 Avinash Chandravansi 20


</head>

<body style="background-color:grey">
<div id="outer" style="height:auto;width:1300px;border:1px solid black;margin:0px
auto;border-radius: 10px;">

<div id="header"
style="border-radius:1px solid black;height: 150px;width: 100%;background-
color:white;border-radius: 10px 10px 0 0 ;">
<div id="logo" style="float:left;height:100%;width:150px;background-image:
url('logo.png');border-radius: 10px;">

</div>
<div id="title" style="height:120px;width:900px;float:left;text-align:center;margin-
top:5px;margin:0px auto">
&nbsp;
<br>
<span style="font-size:33px;font-weight:800;text-align:center;color:#2b2a2a;font-
family:'Agency FB'">बु न्दे लखण्ड
अभियान्त्रिकी एवं प्रौद्योगिकी सं स्थान,झाँसी </span><br />
<span style="font-size:34px;font-weight:800;text-align:center;color:#2b2a2a;font-
family:'Agency FB'">Bundelkhand
Institute of Engineering and Technology, Jhansi</span>
</div>
<div id="help"
style="background-color:#0d2d62;margin:0px
auto;height:70px;width:auto;float:left;margin-top:16px;text-align:center;border-radius:20px;">
&nbsp; &nbsp;&nbsp; &nbsp; <span style="color:white;font-weight:500;font-size:20px">
&#9742 Helpline Number
</span> &nbsp; &nbsp; &nbsp; &nbsp;
<span style="color:white;font-weight:300"></br> 0510-2980211 </br>
For General Queries
</span>

</div>

</div>

1904313902 Avinash Chandravansi 21


<div id="menu-outer" style="background-color:#d81003;width:100%;height:50px">
<div class="navbar" style="background-color:#0d2d62;width:100%;height:45px; font-
size:25px;font-weight: 300; ">
<a href="index.html">Home </a>
<a href="college.html">My College</a>
<a href="department.html">My Department</a>
<div class="dropdown">
<button class="dropbtn"> <span style="font-size: 18px;"> Tutorial </span>
</button>
<div class="dropdown-content">

</div>

</div>
</div>

<div id="image" style="width:100%;height:400px;background-image:


url('myimage.jpg');margin-top:5px">
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

<div id="mytitle"
style="opacity:0.9; height:200px;width:500px;background-color:black;margin-
left:730px;opacity: 0.5;border-radius: 10px;text-align: center; ">
<br>
<span style="color:white;text-align: center;font-size: 30px;font-weight: 600;"> Avinash
Chandravansi</span>
<hr>
<br>
<span style="color:white;font-size:18px ; font-weight: 200;">Former System
Administrator at Wipro Ltd. </span>
<br>
<span style="color:white;font-size:18px;font-weight: 200 ;"> and a Cyber Security
Enthusiast </span>
<br>
<span style="color:white;font-size:18px;font-weight: 200 ;"> NDG Linux - Associate
Level by CISCO Network

1904313902 Avinash Chandravansi 22


Acedamy </span>
<br>
<span style="color:white;font-size:18px;font-weight: 200 ;"> Currently pursuing B.Tech
in I.T </span>
<span style="color:white;font-size:18px;font-weight: 200 ;"> from BIET,Jhansi </span>
</div>

</div>

<div id="content" style="background-color: white;margin-top:0px">


<hr style="border:3px solid #d81003;margin-top:-3px">
<br>

<center>
<h1>My Journey So far...</h1>
</center>
<div class="timeline">
<div class="container left" style="back">
<div class="content" style="background-color:#0d2d62;color:white">
<h1>2014 - High School</h1>
<h3>Completed High School from Sacred Heart Convent High School, Khajuraho(M.P)
affiliated to I.C.S.E</h3>

</div>
</div>
<div class="container right">
<div class="content">
<h1>2018 - Diploma in I.T</h1>
<h3> Completed Diploma in Information Technology from Hewett Polytechnic
affiliated to BTEUP. </h3>
</div>
</div>
<div class="container left" style="back">
<div class="content" style="background-color:#0d2d62;color:white">
<h1>2018 - Joined Wipro Ltd.</h1>
<h3>Joined Wipro Limited as System Administrator.</h3>

</div>
</div>
<div class="container right">
<div class="content">
<h1>2019 - Left Wipro Ltd.</h1>
<h3> Left Wipro Limited after completing 1 year. </h3>
</div>

1904313902 Avinash Chandravansi 23


</div>
<div class="container left" style="back">
<div class="content" style="background-color:#0d2d62;color:white;">
<h1>2019- Started studying in BIET Jhansi</h1>
<h3>Stared studying in BIET,Jhansi in Information Technology branch after UPSEE
Councelling.</h3>

</div>
</div>
</div>
<br>
<div id="footer" style="background-color:#d81003;width:100%;height:50px;margin-top:
-25px;">
<div style="background-color:#0d2d62;width:100%;height:45px;">
<center>
<p style="color:white; font-size:20px;font-weight: 300;line-height: 45px;"> &#169;
Avinash
Chandravansi&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Technology used: HTML
and CSS </p>
</center>

</div>

</div>
</div>

</div>

</body>

</html>

Output :

1904313902 Avinash Chandravansi 24


1904313902 Avinash Chandravansi 25
Practical No: 8
Question:

Write programs using Java script for Web Page to display browsers information.
Code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Calculator extends Applet implements ActionListener
{
TextField inp;

public void init()

setBackground(Color.white);

setLayout(null);

int i;

inp = new TextField();

inp.setBounds(150,100,270,50);

this.add(inp);

Button button[] = new Button[10];

for(i=0;i<10;i++)

button[i] = new Button(String.valueOf(9-i));

button[i].setBounds(150+((i%3)*50),150+((i/3)*50),50,50);

this.add(button[i]);

button[i].addActionListener(this);

1904313902 Avinash Chandravansi 26


}

Button dec=new Button(".");

dec.setBounds(200,300,50,50);

this.add(dec);

dec.addActionListener(this);

Button clr=new Button("C");

clr.setBounds(250,300,50,50);

this.add(clr);

clr.addActionListener(this);

Button operator[] = new Button[5];

operator[0]=new Button("/");

operator[1]=new Button("*");

operator[2]=new Button("-");

operator[3]=new Button("+");

operator[4]=new Button("=");

for(i=0;i<4;i++)

operator[i].setBounds(300,150+(i*50),50,50);

this.add(operator[i]);

operator[i].addActionListener(this);

operator[4].setBounds(350,300,70,50);

1904313902 Avinash Chandravansi 27


this.add(operator[4]);

operator[4].addActionListener(this);

String num1="";

String op="";

String num2="";

public void actionPerformed(ActionEvent e)

String button = e.getActionCommand();

char ch = button.charAt(0);

if(ch>='0' && ch<='9'|| ch=='.')

if (!op.equals(""))

num2 = num2 + button;

else

num1 = num1 + button;

inp.setText(num1+op+num2);

else if(ch=='C')

num1 = op = num2 = "";

inp.setText("");

1904313902 Avinash Chandravansi 28


else if (ch =='=')

if(!num1.equals("") && !num2.equals(""))

double temp;

double n1=Double.parseDouble(num1);

double n2=Double.parseDouble(num2);

if(n2==0 && op.equals("/"))

inp.setText(num1+op+num2+" = Zero Division Error");

num1 = op = num2 = "";

else

if (op.equals("+"))

temp = n1 + n2;

else if (op.equals("-"))

temp = n1 - n2;

else if (op.equals("/"))

temp = n1/n2;

else

temp = n1*n2;

inp.setText(num1+op+num2+" = "+temp);

1904313902 Avinash Chandravansi 29


num1 = Double.toString(temp);

op = num2 = "";
}
}
else
{
num1 = op = num2 = "";

inp.setText("");

else

if (op.equals("") || num2.equals(""))

op = button;

else

double temp;

double n1=Double.parseDouble(num1);

double n2=Double.parseDouble(num2);

if(n2==0 && op.equals("/"))

inp.setText(num1+op+num2+" = Zero Division Error");num1 = op = num2 = "";


}
else
{
if (op.equals("+"))

temp = n1 + n2;
else if (op.equals("-"))

1904313902 Avinash Chandravansi 30


temp = n1 - n2;
else if (op.equals("/"))
temp = n1/n2;
else
temp = n1*n2;

num1 = Double.toString(temp);

op = button;

num2 = ""; }
}
inp.setText(num1+op+num2);

}
}
}

Output:

Practical No: 10

1904313902 Avinash Chandravansi 31


Question:

Writing program in XML for creation of DTD, which specifies set of rules. Create
a style sheet in CSS/ XSL & display the document in internet explorer.

Code:

dtd.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="style.css"?>
<!DOCTYPE student [
<!ELEMENT student (sno,name,rollno,branch,year)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT rollno (#PCDATA)>
<!ELEMENT branch (#PCDATA)>
<!ELEMENT year (#PCDATA)>
]>
<student>
<sno></sno>
<name>Avinash Chandravansi</name>
<email>avinash.chandravansi@outlook.com</email>
<rollno>1904313902</rollno>
<branch>Information Technology</branch>
<year>3rd year</year>
</student>

style.css

student {
font-size: 20px;
margin: 0px auto;
text-align: center;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;

border: 5px solid #f00;


background: gray;
margin: 10px;
white-space: pre-line;
}

1904313902 Avinash Chandravansi 32


student > * {
white-space: pre-line;
}

Output :

Practical No: 11

1904313902 Avinash Chandravansi 33


Question:

Install TOMCAT web server and APACHE. Access the above developed static
web pages for books web site, using these servers by putting the web pages
developed.

Ouput :

1904313902 Avinash Chandravansi 34


Practical No: 12
Question:

Program to illustrate JDBC connectivity. Program for maintaining database by


sending queries. Design and implement a simple servlet book query with the help
of JDBC & SQL. Create MS Access Database, Create on ODBC link, Compile &
execute JAVA JDBC Socket.

Code:

index.html
<html>

<head>
<title>Practical 12</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="background-color:#202020;color:white">
<center>
<u> <h1>Web Technology - Practical 12 </h1></u>

<form action="queryrunner" method="GET">


<input type="text" id="query" name="query" value="">
<br>
<br>
<input type="submit" name="Run Query">
</form>
<br>
<br>
<br>

1904313902 Avinash Chandravansi 35


<br>
<h3>Database name : Practical12</h3>
<h3>JDBC URL : jdbc:mysql://localhost:3306/practical12</h3>
</center>
</body>
</html>

queryrunner.java
import java.io.*;
import java.sql.DriverManager;
import javax.servlet.*;
import java.sql.*;
public class queryrunner extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException,
IOException {
try {
String pname = "query";
String heading = request.getParameter(pname);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/practical12",
"avinash", "avinash"); Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(heading);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");

1904313902 Avinash Chandravansi 36


out.println("<title>Result</title>"); out.println("</head>");
out.println("<body style=\"color:white\" bgcolor=\"#202020\" >"); out.println("<center>");
out.println("<h1>Output of Query :" + heading + "</h1>");
out.println("<table border=\"1\" cellspacing=\"6\" border=\\\"4\\\">");
while (rs.next()) {
out.println("<tr style=\"border:1px solid black\" >");
out.println("<p>"); out.println("</tr>"); }
out.println("<table>");
out.println("</ center>");
out.println("</body>"); out.println("</html>"); con.close();
} catch(Exception e) {System.out.println(e);}}}

Ouput :

1904313902 Avinash Chandravansi 37


Actual database

1904313902 Avinash Chandravansi 38


Practical No: 13
Question:

Assume four users user1, user2, user3 and user4 having the passwords pwd1,
pwd2, pwd3and pwd4 respectively. Write a servlet for doing the following.

· Create a Cookie and add these four user id’s and passwords to this Cookie.
· Read the user id and passwords entered in the Login form and authenticate
with the values available in the cookies.

Code :

Ouput :

1904313902 Avinash Chandravansi 39

You might also like