Eit Practical File (8719139)
Eit Practical File (8719139)
Eit Practical File (8719139)
OF
“Essential of Information
Technology”
Submitted for the award of degree of B.tech
1
2
Table of contents
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.*;
28
// which is an inner class called DrawCanvas extending javax.swing.JPanel.
private DrawCanvas canvas;
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.*;
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.*;
int data;
Node next;
// Constructor
Node(int d)
{
data = d;
38
next = null;
}
}
// **************INSERTION**************
39
// Insert the new_node at last node
last.next = new_node;
}
// **************TRAVERSAL**************
System.out.print("\nLinkedList: ");
// Go to next node
currNode = currNode.next;
}
System.out.println("\n");
}
40
// **************DELETION BY KEY**************
//
// CASE 1:
// If head node itself holds the key to be deleted
//
// CASE 2:
// If the key is somewhere other than at head
41
//
//
// 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");
}
// **************DELETION AT A POSITION**************
//
// CASE 1:
// If index is 0, then head node itself is to be
// deleted
43
// Display the message
System.out.println(
index + " position element deleted");
//
// CASE 2:
// If the index is greater than 0 but less than the
// size of LinkedList
//
// The counter
int counter = 0;
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++;
}
}
45
// return the List
return list;
}
// **************MAIN METHOD**************
//
// ******INSERTION******
//
46
// Print the LinkedList
printList(list);
//
// ******DELETION BY KEY******
//
47
printList(list);
//
// ******DELETION AT POSITION******
//
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.*;
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