Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Eit Practical File (8719139)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

PRACTICAL FILE

OF
“Essential of Information
Technology”
Submitted for the award of degree of B.tech

(Computer Science & Engineering)

Submitted by: Submitted to :


Mohd. Zahid Ms. Divya
8719139 Chaudhary
5th Sem

Department of Computer Science Engineering

State Institute of Engineering & Technology, Nilokheri(karnal)

Kurukshetra University, Kurukshetra

1
2
Table of contents

S. No. Experiment Page. No. Signature


1. Design a simple test application to 3
demonstrate dynamic polymorphism.
2. Develop with suitable hierarchy, class for 5
point, shape rectangle, square, circle,
ellipse, triangle, polygenetic.
3. Design a class for Complex numbers in 9
Java. Add methods for basic operations on
complex numbers, provide a method to
return the number of active objects created.
4. Write a java package with stack and queue 12
classes.
5. Design a java interface for ADT Stack. 15
6. Develop two different classes that 21
implement this interface. One using array
and other using linked list.
7. Develop a simple paint like program that 28
can draw basic graphical primitives
8. Develop a scientific calculator using event 31
driven programming.
9. Develop a template for linked list class 38
along with its members in Java.
10. Write a program to insert and view data 50
using Servlets.

3
Practical – 1.
Aim : Design a simple test application to demonstrate dynamic
polymorphism

Source code :
package com.company;

class A {
void m1() {
System.out.println("Inside A's m1method");
}}
class B extends A {
void m1() {
System.out.println("Inside B's m1method");
}}
class C extends A{
void m1() {
System.out.println("Inside C's m1method");
}}
public class Practical_1 {
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();

A ref;

4
ref = a;
ref.m1();

ref =b;
ref.m1();

ref = c;
ref.m1();

Output :

Practical – 2
Aim : Develop with suitable hierarchy, class for point, shape
rectangle, square, circle, ellipse, triangle, polygenetic.

Source code :
package com.company;

5
class point{
void show(){
System.out.println("This is the Point Base class");
}}
class shape extends point {
void display(){ System.out.println("Different shapes can be developed with
different number of points");
}}
class rectangle extends shape{
int l,b;
void getdata(int x,int y){
l=x;b=y; }
void area(){
System.out.println("Length:"+l);
System.out.println("Breadth:"+b);
System.out.println("Area:"+(l*b));
}}
class square extends shape{
int a;
void gdata(int x){
a=x;
}
void area(){
System.out.println("Side:"+a);
System.out.println("Area:"+(a*a));
} } class circle extends shape{
int r;
void get(int x){
6
r=x;
}
void area(){
System.out.println("Radius:"+r);
System.out.println("Area:"+(3.14*r*r));
}}
class triangle extends shape{
int b,h;
void tdata(int x,int y){
b=x;h=y;
}
void area(){
System.out.println("Base:"+b);
System.out.println("Height:"+h);
System.out.println("Area:"+(0.5*b*h));
}}
class ellipse extends shape{
int a,b;
void edata(int x,int y){
a=x;b=y;
} void area(){
System.out.println("MajorAxis:"+a);
System.out.println("MinorAxis:"+b);
System.out.println("Area:"+(3.14*a*b));
}}
public class Practical_2 {
public static void main(String args[]){

7
rectangle r = new rectangle();
square s = new square();
circle c = new circle();
triangle t = new triangle();
ellipse e = new ellipse();
r.show();
s.display();
System.out.println("");
System.out.println("Rectangle:");
System.out.println();
r.getdata(12,6);
r.area();
System.out.println("");
System.out.println("Square:");
System.out.println();
s.gdata(7);
s.area();
System.out.println("");
System.out.println("Circle:");
System.out.println();
c.get(5);
c.area();
System.out.println("");
System.out.println("Triangle:");
System.out.println();
t.tdata(4,7);
t.area();

8
System.out.println("");
System.out.println("Ellipse:");
System.out.println();
e.edata(4,7);
e.area(); } }

Output :

Practical – 3
Aim : Design a class for Complex numbers in Java. Add methods for
basic operations on complex numbers, provide a method to return the
number of active objects created.

Source code :
package com.company;
import java.io.*;
class complex {
int a,b;
9
public static int c;
public complex(int x,int y)
{
a=x;b=y;
c++;
}
public static String add(complex n1,complex n2)
{
int a1=n1.a+n2.a;
int b1=n1.b+n2.b;
if(b1<0) return (a1+" "+b1+"i");
else return (a1+" "+b1+"i");
}
public static String sub(complex n1,complex n2)
{
int a1=n1.a-n2.a;
int b1=n1.b-n2.b;
if(b1<0)
return (a1+" "+b1+"i");
else
return (a1+" "+b1+"i");
}
public static String mul(complex n1,complex n2)
{
int a1=n1.a*n2.a;
int b1=n1.b*n2.b;
int v1=n1.a*n2.b;

10
int v2=n2.a*n1.b;
int vi=v1+v2;
if(vi<0)
return(a1-b1+" "+vi+"i");
else return(a1-b1+"+"+vi+"i");
}
}
public class Practical_3
{
public static void main(String a[])throws IOException
{
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
int x,y;
System.out.println("enter the no for complex1:");
x=Integer.parseInt(in.readLine());
y=Integer.parseInt(in.readLine());
System.out.println("enter the no for complex2:");
int m=Integer.parseInt(in.readLine());
int n=Integer.parseInt(in.readLine());
complex c1=new complex(x,y);
complex c2=new complex(m,n);
System.out.println("addition:"+complex.add(c1,c2));
System.out.println("subtraction:"+complex.sub(c1,c2));
System.out.println("multiplication:"+complex.mul(c1,c2));
System.out.println("count="+complex.c);
}}

11
Output :

Practical – 4
Aim : Write a java package with stack and queue classes.

Source code :
Queue package:
package queuepackage;
public class queue2
{
private int maxsize;
private long[] queArray;
private int front;
private int rear;
private int nitems;
public queue2(int s)
{
maxsize=s;
queArray=new long[maxsize];
front=0;
rear=-1;
nitems=0;
}
public void insert(long j)
{
if(rear==maxsize-1)
rear=-1;
queArray[++rear]=j;
nitems++;
12
}
public long remove()
{
long temp=queArray[front++];
if(front==maxsize)
front=0;
nitems--;
return temp;
}
public long peekFront()
{
return queArray[front];
}
public boolean isEmpty()
{
return(nitems==0);
}
public boolean isFull()
{
return(nitems==maxsize);
}
public int size()
{
return nitems;
}
}
Stack package:

package stackpackage;
public class stack2
{
int []a;
int top;
public stack2(int n)
{
a=new int[n];
top=-1;
}
public void push(int val)
{
if(top==a.length-1)
{
System.out.println("stack overflow");
13
}
else
{
top++;
a[top]=val;
}
}
public void pop()
{
if(top==-1)
{
System.out.println("stack underflow");
}
else
{
System.out.println("element popped"+a[top]);
top--;
}
}
public void display()
{
if(top==-1)
{
System.out.println("stack empty");
}
else
{
for(int i=top;i>=0;i--)
{
System.out.println("sstack element :"+a[i]);
}
}
}
}

Main program:

import queuepackage.queue2;
import stackpackage.stack2;
import java.io.*;
public class usestackqueue2
{
14
public static void main(String args[])
{
BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));
int c;
stack2 s;
int n;
try
{
do
{
System.out.println("1.stack 2.queue");
c=Integer.parseInt(sc.readLine());
switch(c)
{
case 1:
System.out.println("enter the size of stack");
n=Integer.parseInt(sc.readLine());
s=new stack2(n);
int choice;
do
{
System.out.println("1.push,2.pop,3.display,0.exit,enter your choice:");
choice=Integer.parseInt(sc.readLine());
switch(choice)
{
case 1:
int value;
System.out.println("enter the element to push:");
value=Integer.parseInt(sc.readLine());
s.push(value);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 0:
break;
default:System.out.println("invalid choice");
}
}while(choice!=0);
break;
15
case 2:
queue2 thequeue = new queue2(5);
thequeue.insert(10);
thequeue.insert(20);
thequeue.insert(30);
thequeue.insert(40);
thequeue.remove();
thequeue.remove();
thequeue.remove();
thequeue.insert(50);
thequeue.insert(60);
thequeue.insert(70);
thequeue.insert(80);
while(!thequeue.isEmpty())
{
long n1= thequeue.remove();
System.out.print(n1);
System.out.print("");
}
System.out.println("");
break;
}
}while(c!=0);
}
catch(Exception e)
{}
}
}

Output :

16
Practical – 5
Aim : Design a java interface for ADT Stack

Source code :
package com.company;
import java.io.*;
class Mystack implements stackoperation
{
int stack[]=new int[5];
int top=-1;
int i;
public void isEmpty(){
if(top<0)
System.out.println("Stack is empty");
else{
System.out.println("Stack is not empty");
}}
public void isFull(){
if(top<=4)
System.out.println("Stack is not full");
else
{ System.out.println("Stack is full");
}}
public void mSize(){
17
System.out.println("Size of stack is 5");
}
public void mPeek(){
if(top<0)
System.out.println("Stack is empty");
else
{ System.out.println("element:"+stack[top]);
}}
public void push(int item) {
if(top>=4)
{ System.out.println("overflow");
}
else { top=top+1;
stack[top]=item;
System.out.println("item pushed"+stack[top]);
}}
public void pop() {
if(top<0) System.out.println("underflow");
else
{ System.out.println("item popped"+stack[top]);
top=top-1;
}}
public void display() {
if(top<0) System.out.println("No Element in stack");
else
{
for(i=0;i<=top;i++)

18
System.out.println("element:"+stack[i]);
}}}
public class Practical_5 {
public static void main(String args[])throws IOException
{ int ch,c;
int i;
Mystack s=new Mystack();
DataInputStream in=new DataInputStream(System.in);
do
{
try
{ System.out.println("ARRAY STACK");
System.out.println("1.push 2.pop 3.display 4.TopElement
5.SizeOfStack 6.isEmpty 7.isFull 8.exit");
System.out.println("enter ur choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1: System.out.println("enter the value to push:");
i=Integer.parseInt(in.readLine());
s.push(i);
break;
case 2:
s.pop();
break;
case 3:
System.out.println("the elements are:");
s.display();
19
break;
case 4:
s.mPeek();
break;
case 5:
s.mSize();
break;
case 6:
s.isEmpty();
break; case 7:
s.isFull();
break;
case 8: break; }}
catch(IOException e) {
System.out.println("io error"); }
System.out.println("Do u want to continue 0 to quit and 1 to continue ");
c=Integer.parseInt(in.readLine());
}while(c==1); }}

Output :

20
Practical – 6
Aim : Develop two different classes that implement this interface.
One using array and other using linked list.

Source code :
package com.company;

import java.io.*;
interface stackoperation
{
public void push(int i);
public void pop();
}
class Astack implements stackoperation
{
21
int stack[];
int top;
Astack()
{
stack=new int[10];
top=0;
}
public void push(int item)
{
if(stack[top]==10)
System.out.println("overflow");
else
{
stack[++top]=item;
System.out.println("item pushed");
}
}
public void pop()
{
if(stack[top]<=0)
System.out.println("underflow");
else
{
stack[top]=top--;
System.out.println("item popped");
}
}

22
public void display()
{
for(int i=1;i<=top;i++)
System.out.println("element:"+stack[i]);
}
}
class liststack implements stackoperation
{
node top,q;
int count;
public void push(int i)
{
node n=new node(i);
n.link=top;
top=n;
count++;
}
public void pop()
{

if(top==null)
System.out.println("under flow");
else
{
int p=top.data;
top=top.link;
count--;

23
System.out.println("popped element:"+p);
}
}
void display()
{
for(q=top;q!=null;q=q.link)
{
System.out.println("the elements are:"+q.data);
}
}
class node
{
int data;
node link;
node(int i)
{
data=i;
link=null;
}
}
}
class Practical_6
{
public static void main(String args[])throws IOException
{
int ch,x=1,p=0,t=0;
DataInputStream in=new DataInputStream(System.in);

24
do
{
try
{
System.out.println("----------------------------------");
System.out.println("1.Arraystack 2.liststack 3.exit");
System.out.println("-----------------------------------");
System.out.println("enter ur choice:");
int c=Integer.parseInt(in.readLine());
Astack s=new Astack();
switch(c)
{
case 1:
do
{
if(p==1)
break;
System.out.println("ARRAY STACK");
System.out.println("1.push 2.pop 3.display 4.exit");
System.out.println("enter ur choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:System.out.println("enter the value to push:");
int i=Integer.parseInt(in.readLine());
s.push(i);
break;

25
case 2:
s.pop();
break;
case 3:
System.out.println("the elements are:");
s.display();
break;
case 4:
p=1;
continue;
}
}while(x!=0);
break;
case 2:
liststack l=new liststack();
do
{
if(t==1)
break;
System.out.println("LIST STACK:");
System.out.println("1.push 2.pop 3.display 4.exit");
System.out.println("enter your choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
System.out.println("enter the value for push:");

26
int a=Integer.parseInt(in.readLine());
l.push(a);
break;
case 2:
l.pop();
break;
case 3:
l.display();
break;

case 4:
t=1;
continue;
}
}
while(x!=0);
break;
case 3:
System.exit(0);
} }
catch(IOException e)
{
System.out.println("io error");
} }
while(x!=0);
}}

27
Output :

Practical – 7
Aim : Develop a simple paint like program that can
draw basic graphical primitives.

Source code :
package com.company;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Practical_7 extends JFrame {


public static final int CANVAS_WIDTH = 640;
public static final int CANVAS_HEIGHT = 480;

28
// which is an inner class called DrawCanvas extending javax.swing.JPanel.
private DrawCanvas canvas;

// Constructor to set up the GUI components and event handlers


public Practical_7() {
canvas = new DrawCanvas(); // Construct the drawing canvas
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH,
CANVAS_HEIGHT));

// Set the Drawing JPanel as the JFrame's content-pane


Container cp = getContentPane();
cp.add(canvas);
// or "setContentPane(canvas);"

setDefaultCloseOperation(EXIT_ON_CLOSE); // Handle the CLOSE button


pack(); // Either pack() the components; or setSize()
setTitle("......"); // "super" JFrame sets the title
setVisible(true); // "super" JFrame show
}
/**
* Define inner class DrawCanvas, which is a JPanel used for custom drawing.
*/
private class DrawCanvas extends JPanel {
// Override paintComponent to perform your own painting @Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// paint parent's background
setBackground(Color.BLACK); // set background color for this JPanel
29
// Your custom painting codes. For example,
// Drawing primitive shapes
g.setColor(Color.YELLOW); // set the drawing color
g.drawLine(30, 40, 100, 200);

g.drawOval(150, 180, 10, 10);


g.drawRect(200, 210, 20, 30);
g.setColor(Color.RED); // change the drawing color
g.fillOval(300, 310, 30, 50);
g.fillRect(400, 350, 60, 50);
// Printing texts
g.setColor(Color.WHITE);
g.setFont(new Font("Monospaced", Font.PLAIN, 12));
g.drawString("Testing custom drawing ...", 10, 20);
}
}
// The entry main method
public static void main(String[] args) {
// Run the GUI codes on the Event-Dispatching thread for thread safety
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Practical_7(); // Let the constructor do the job
} }); } }

Output :

30
Practical – 8
Aim : Develop a scientific calculator using event driven
programming.

Source code :
package com.company;

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

class Calculator extends JFrame {


private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20);
private JTextField textfield;
private boolean number = true;
private String equalOp = "=";
private CalculatorOp op = new CalculatorOp();

public Calculator() {
31
textfield = new JTextField("", 12);
textfield.setHorizontalAlignment(JTextField.RIGHT);
textfield.setFont(BIGGER_FONT);
ActionListener numberListener = new NumberListener();
String buttonOrder = "1234567890 ";
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 4, 4));
for (int i = 0; i < buttonOrder.length(); i++) {
String key = buttonOrder.substring(i, i+1);
if (key.equals(" ")) {
buttonPanel.add(new JLabel(""));
} else {
JButton button = new JButton(key);
button.addActionListener(numberListener);
button.setFont(BIGGER_FONT);
buttonPanel.add(button);
}
}
ActionListener operatorListener = new OperatorListener();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 4, 4));
String[] opOrder = {"+", "-", "*", "/","=","C","sin","cos","log"};
for (int i = 0; i < opOrder.length; i++) {
JButton button = new JButton(opOrder[i]);
button.addActionListener(operatorListener);
button.setFont(BIGGER_FONT);
panel.add(button);

32
}
JPanel pan = new JPanel();
pan.setLayout(new BorderLayout(4, 4));
pan.add(textfield, BorderLayout.NORTH );
pan.add(buttonPanel , BorderLayout.CENTER);
pan.add(panel , BorderLayout.EAST);
this.setContentPane(pan);
this.pack();
this.setTitle("Calculator");
this.setResizable(false);
}
private void action() {
number = true;
textfield.setText("");
equalOp = "=";
op.setTotal("");
}
class OperatorListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String displayText = textfield.getText();
if (e.getActionCommand().equals("sin"))
{
textfield.setText("" +
Math.sin(Double.valueOf(displayText).doubleValue()));

}else
if (e.getActionCommand().equals("cos"))
{
33
textfield.setText("" +
Math.cos(Double.valueOf(displayText).doubleValue()));

}
else
if (e.getActionCommand().equals("log"))
{
textfield.setText("" +
Math.log(Double.valueOf(displayText).doubleValue()));

}
else if (e.getActionCommand().equals("C"))
{
textfield.setText("");
}

else
{
if (number)
{

action();
textfield.setText("");

}
else
{
number = true;
34
if (equalOp.equals("="))
{
op.setTotal(displayText);
}else
if (equalOp.equals("+"))
{
op.add(displayText);
}
else if (equalOp.equals("-"))
{
op.subtract(displayText);
}
else if (equalOp.equals("*"))
{
op.multiply(displayText);
}
else if (equalOp.equals("/"))
{
op.divide(displayText);
}

textfield.setText("" + op.getTotalString());
equalOp = e.getActionCommand();
}
}
}
}

35
class NumberListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String digit = event.getActionCommand();
if (number) {
textfield.setText(digit);
number = false;
} else {
textfield.setText(textfield.getText() + digit);
}
}
}
public class CalculatorOp {
private int total;
public CalculatorOp() {
total = 0;
}
public String getTotalString() {
return ""+total;
}
public void setTotal(String n) {
total = convertToNumber(n);
}
public void add(String n) {
total += convertToNumber(n);
}
public void subtract(String n) {
total -= convertToNumber(n);

36
}
public void multiply(String n) {
total *= convertToNumber(n);
}
public void divide(String n) {
total /= convertToNumber(n);
}
private int convertToNumber(String n) {
return Integer.parseInt(n);
}
}
}
class SwingCalculator {
public static void main(String[] args) {
JFrame frame = new Calculator();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output :

37
Practical – 9
Aim : Develop a template for linked list class along with its
members in java.
Source code :
import java.io.*;

// Java program to implement


// a Singly Linked List
public class LinkedList {

Node head; // head of list

// Linked list Node.


// Node is a static nested class
// so main() can access it
static class Node {

int data;
Node next;

// Constructor
Node(int d)
{
data = d;
38
next = null;
}
}

// **************INSERTION**************

// Method to insert a new node


public static LinkedList insert(LinkedList list,
int data)
{
// Create a new node with given data
Node new_node = new Node(data);
new_node.next = null;

// If the Linked List is empty,


// then make the new node as head
if (list.head == null) {
list.head = new_node;
}
else {
// Else traverse till the last node
// and insert the new_node there
Node last = list.head;
while (last.next != null) {
last = last.next;
}

39
// Insert the new_node at last node
last.next = new_node;
}

// Return the list by head


return list;
}

// **************TRAVERSAL**************

// Method to print the LinkedList.


public static void printList(LinkedList list)
{
Node currNode = list.head;

System.out.print("\nLinkedList: ");

// Traverse through the LinkedList


while (currNode != null) {
// Print the data at current node
System.out.print(currNode.data + " ");

// Go to next node
currNode = currNode.next;
}
System.out.println("\n");
}

40
// **************DELETION BY KEY**************

// Method to delete a node in the LinkedList by KEY


public static LinkedList deleteByKey(LinkedList list,
int key)
{
// Store head node
Node currNode = list.head, prev = null;

//
// CASE 1:
// If head node itself holds the key to be deleted

if (currNode != null && currNode.data == key) {


list.head = currNode.next; // Changed head

// Display the message


System.out.println(key + " found and deleted");

// Return the updated List


return list;
}

//
// CASE 2:
// If the key is somewhere other than at head

41
//

// Search for the key to be deleted,


// keep track of the previous node
// as it is needed to change currNode.next
while (currNode != null && currNode.data != key) {
// If currNode does not hold key
// continue to next node
prev = currNode;
currNode = currNode.next;
}

// If the key was present, it should be at currNode


// Therefore the currNode shall not be null
if (currNode != null) {
// Since the key is at currNode
// Unlink currNode from linked list
prev.next = currNode.next;

// Display the message


System.out.println(key + " found and deleted");
}

//
// CASE 3: The key is not present
//

42
// If key was not present in linked list
// currNode should be null
if (currNode == null) {
// Display the message
System.out.println(key + " not found");
}

// return the List


return list;
}

// **************DELETION AT A POSITION**************

// Method to delete a node in the LinkedList by POSITION


public static LinkedList
deleteAtPosition(LinkedList list, int index)
{
// Store head node
Node currNode = list.head, prev = null;

//
// CASE 1:
// If index is 0, then head node itself is to be
// deleted

if (index == 0 && currNode != null) {


list.head = currNode.next; // Changed head

43
// Display the message
System.out.println(
index + " position element deleted");

// Return the updated List


return list;
}

//
// CASE 2:
// If the index is greater than 0 but less than the
// size of LinkedList
//
// The counter
int counter = 0;

// Count for the index to be deleted,


// keep track of the previous node
// as it is needed to change currNode.next
while (currNode != null) {

if (counter == index) {
// Since the currNode is the required
// position Unlink currNode from linked list
prev.next = currNode.next;

44
// Display the message
System.out.println(
index + " position element deleted");
break;
}
else {
// If current position is not the index
// continue to next node
prev = currNode;
currNode = currNode.next;
counter++;
}
}

// If the position element was found, it should be


// at currNode Therefore the currNode shall not be
// null
//
// CASE 3: The index is greater than the size of the
// LinkedList
//
// In this case, the currNode should be null
if (currNode == null) {
// Display the message
System.out.println(
index + " position element not found");
}

45
// return the List
return list;
}

// **************MAIN METHOD**************

// method to create a Singly linked list with n nodes


public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();

//
// ******INSERTION******
//

// Insert the values


list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);

46
// Print the LinkedList
printList(list);

//
// ******DELETION BY KEY******
//

// Delete node with value 1


// In this case the key is ***at head***
deleteByKey(list, 1);

// Print the LinkedList


printList(list);

// Delete node with value 4


// In this case the key is present ***in the
// middle***
deleteByKey(list, 4);

// Print the LinkedList


printList(list);

// Delete node with value 10


// In this case the key is ***not present***
deleteByKey(list, 10);

// Print the LinkedList

47
printList(list);

//
// ******DELETION AT POSITION******
//

// Delete node at position 0


// In this case the key is ***at head***
deleteAtPosition(list, 0);

// Print the LinkedList


printList(list);

// Delete node at position 2


// In this case the key is present ***in the
// middle***
deleteAtPosition(list, 2);

// Print the LinkedList


printList(list);

// Delete node at position 10


// In this case the key is ***not present***
deleteAtPosition(list, 10);

// Print the LinkedList


printList(list);

48
}
}

Output :

49
Practical – 10
Aim : Write a program to insert and view data using Servlets.
Source code :
register.html
<!doctype html>
<body>
<form action="servlet/Register" method="post">
<fieldset style="width:20%; background-color:#ccffeb">
<h2 align="center">Registration form</h2><hr>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="userName" required /></td> </tr>
<tr>
<td>Password</td>
<td><input type="password" name="userPass" required /></td> </tr>
<tr>
<td>Email Id</td>
<td><input type="text" name="userEmail" required /></td> </tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="userMobile" required/></td> </tr>
<tr>
<td>Date of Birth</td>
<td><input type="date" name="userDOB" required/></td> </tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="gender" value="male" checked> Male <input type="radio"
name="gender" value="female"> Female </td></tr>
<tr>
<td>Country</td>
<td><select name="userCountry" style="width:130px">
<option>Select a country</option>
<option>India</option>
<option>America</option>
<option>England</option>
<option>other</option></select>
</td>
</tr>
<tr>
<td><input type="reset" value="Reset"/></td>
<td><input type="submit" value="Register"/></td>
</tr>
</table>
</fieldset>
50
</form>
</body>
</html>

Register.java

import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class Register extends HttpServlet


{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

String name = request.getParameter("userName");


String pwd = request.getParameter("userPass"); String
email = request.getParameter("userEmail");
int mobile = Integer.parseInt(request.getParameter("userMobile")); String
dob = request.getParameter("userDOB");
String gender = request.getParameter("gender"); String
country =request.getParameter("userCountry"); try
{
//load the driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//create connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","local","test");
// create the prepared statement object
PreparedStatement ps=con.prepareStatement("insert into registration
values(?,?,?,?,?,?,?)");

ps.setString(1,name);
ps.setString(2,pwd);
ps.setString(3,email);
ps.setInt(4, mobile);
ps.setString(5,dob);
ps.setString(6,gender);
ps.setString(7,country);
51
int i = ps.executeUpdate();
if(i>0)
out.print("You are successfully registered...");

}
catch (Exception ex)
{
ex.printStackTrace();
}
out.close();
}

web.xml

<web-app>
<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>Register</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/servlet/Register</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>register.html</welcome-file>
</welcome-file-list>
</web-app>

Output :

52
Click on Register button.

53
54

You might also like