EIT Practical

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

SWAMI DEVI DYAL INSTITUTE OF

ENGINEERING AND TECHNOLOGY,GOLPURA

Essential of Information Technology

[PC-CS-305]

Submitted To: Submitted By:


Mr. Gautam Lalit Mittal
Computer Science Dept 2021218021
SDDIET, Barwala B.Tech. 5thSemester
Computer Science Dept.
SDDIET, Barwala

1|Page
Index

Sr. Name Page No. Date Signature


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

2|Page
Program – 1

Aim: - Write a Java Package with Stack and queue classes.


Program:
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++;
}
public long remove()
{
long temp=queArray[front++];
if(front==maxsize)
front=0;
nitems--;
return temp;
}
public long peekFront()
{
return queArray[front];
}
3|Page
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");
}
else
{
top++;
a[top]=val;
}
}
public void pop()
{
if(top==-1)
{
System.out.println("stack underflow");

4|Page
}
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
{
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());

5|Page
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;
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);

6|Page
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:

1.stack 2.queue
1
enter the size of stack
5
1.push,2.pop,3.display,0.exit,enter your choice:
1
enter the element to push:
1
1.push,2.pop,3.display,0.exit,enter your choice:
1
enter the element to push:
2
1.push,2.pop,3.display,0.exit,enter your choice:
1
enter the element to push:
3
1.push,2.pop,3.display,0.exit,enter your choice:
3
sstack element :3
sstack element :2
sstack element :1
1.push,2.pop,3.display,0.exit,enter your choice:

7|Page
2
element popped3
1.push,2.pop,3.display,0.exit,enter your choice:
3
sstack element :2
sstack element :1
1.push,2.pop,3.display,0.exit,enter your choice:
0
1.stack 2.queue
2
4050607080
1.stack 2.queue
0

8|Page
Program – 2

Aim: -Design a class for Complex numbers in Java .In addition to methods for
basic operations on complex numbers, provide a method to return the number of
active objects created.
Program:
// Java program to add and subtract two
// complex numbers using Class
+
import java.util.*;

// User Defined Complex class


class Complex {

// Declaring variables
int real, imaginary;

// Empty Constructor
Complex()
{
}

// Constructor to accept
// real and imaginary part
Complex(int tempReal, int tempImaginary)
{
real = tempReal;
imaginary = tempImaginary;
}

// Defining addComp() method


// for adding two complex number
Complex addComp(Complex C1, Complex C2)
{
// creating temporary variable
Complex temp = new Complex();

// adding real part of complex numbers


temp.real = C1.real + C2.real;

9|Page
// adding Imaginary part of complex numbers
temp.imaginary = C1.imaginary + C2.imaginary;

// returning the sum


return temp;
}

// Defining subtractComp() method


// for subtracting two complex number
Complex subtractComp(Complex C1, Complex C2)
{
// creating temporary variable
Complex temp = new Complex();

// subtracting real part of complex numbers


temp.real = C1.real - C2.real;

// subtracting Imaginary part of complex numbers


temp.imaginary = C1.imaginary - C2.imaginary;

// returning the difference


return temp;
}

// Function for printing complex number


void printComplexNumber()
{
System.out.println("Complex number: "
+ real + " + "
+ imaginary + "i");
}
}

// Main Class
public class GFG {

// Main function
public static void main(String[] args)
{

// First Complex number


Complex C1 = new Complex(3, 2);

10 | P a g e
// printing first complex number
C1.printComplexNumber();

// Second Complex number


Complex C2 = new Complex(9, 5);

// printing second complex number


C2.printComplexNumber();

// for Storing the sum


Complex C3 = new Complex();

// calling addComp() method


C3 = C3.addComp(C1, C2);

// printing the sum


System.out.print("Sum of ");
C3.printComplexNumber();

// calling subtractComp() method


C3 = C3.subtractComp(C1, C2);

// printing the difference


System.out.print("Difference of ");
C3.printComplexNumber();
}
}

Output:
Complex number: 3 + 2i
Complex number: 9 + 5i
Sum of Complex number: 12 + 7i
Difference of Complex number: -6 + -3i

11 | P a g e
Program – 3

Aim: -Develop with suitable hierarchy, class for point, shape rectangle,
square, circle, ellipse, triangle, polygenetic.
Program:
package date;
public class day
{
int day,month,year,y,m,tot_day=0;
int day_mon[]={31,28,31,30,31,30,31,31,30,31,30,31};
String
week[]={"sunday","monday","tuesday","wednesday","thursday","friday","satur
day"};
public void days(int a,int b,int c)
{
valid(a,b,c);
day=a;
month=b;year=c;
for(y=0;y<year;y++)
if(y%4==0)
tot_day+=366;
else
tot_day+=365;
if(month>2 && year%4==0)
day_mon[1]=29;
for(m=0;m<(month-1);m++)
tot_day+=day_mon[m];
tot_day+=day;
System.out.println(day+"-"+month+"-"+year+" this
is :"+"="+week[(tot_day+4)%7]);
}
public void valid(int dd,int mm,int yy)
{
if(yy<1)
{
System.out.println("invalid....");
System.exit(0);
}
switch(mm)
{

12 | P a g e
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(dd>31 || dd<1)
{
System.out.println("invalid......");
System.exit(0);
}
break;
case 4:
case 6:
case 9:
case 11:
if(dd>30 || dd<1)
{
System.out.println("invalid......");
System.exit(0);
}
break;
case 2:
if(yy%4!=0 && dd>=29 && dd<1)
{
System.out.println("invalid......");
System.exit(0);
}
else if(yy%4==0 || dd>28 || dd<1)
{
System.out.println("invalid......");
System.exit(0);
}break;
default:
System.out.println("invalid......");
System.exit(0);
}
}
}

Main program:

13 | P a g e
import date.*;
import java.io.*;
class chkd
{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
day d=new day();
System.out.println("enter the date:(dd mm yyyy):");
int dd=Integer.parseInt(in.readLine());
int mm=Integer.parseInt(in.readLine());
int yy=Integer.parseInt(in.readLine());
d.days(dd,mm,yy);
}
}

Output:

enter the date:(dd /mm /yyyy):


30
04
1989
30-4-1989 this is :=sunday

enter the date:(dd /mm /yyyy):


30
02
2000
invalid......

14 | P a g e
Program – 4

Aim: -Design a simple test application to demonstrate dynamic polymorphism.


Program:
public class DynamicPolymorphismExample
{
public static void main(String args[])
{
//assigning a child class object to a parent class reference
Fruits fruits = new Mango();
//invoking the method
fruits.color();
}
}
//parent class
class Fruits
{
public void color()
{
System.out.println("Parent class method is invoked.");
}
}
//derived or child class that extends the parent class
class Mango extends Fruits
{
//overrides the color() method of the parent class
@Override
public void color()
{
System.out.println("The child class method is invoked.");
}
}

Output: The child class method is invoked.

15 | P a g e
Program – 5

Aim: - Design a java interface for ADT Stack.


Program:
import java.lang.*;
import java.io.*;
import java.util.*;

interface MyInterface {
int n = 20;
public void pop();
public void push();
public void peek();
public void display();
}

class StackImplementation implements MyInterface {


int arr[] = new int[n];
int top = -1;
public void push() {
try {
DataInputStream dis = new DataInputStream(System.in);
System.out.println("Enter Element");
int ele = Integer.parseInt(dis.readLine());
arr[++top] = ele;
} catch (Exception e) {
System.out.println("e");

16 | P a g e
}
}
public void pop() {
int popper = arr[top];
top--;
System.out.println("popped element " + popper);
}
public void peek() {
int popper = arr[top];
System.out.println("popped element " + popper);
}
public void display() {
if (top < 0) {
System.out.println("Stack is empty");
return;
} else {
String str = " ";
for (int i = 0; i <= top; i++)
str = str + " " + arr[i];
System.out.println("Elements are " + str);
}
}
}
class StackADT {
public static void main(String arg[]) throws IOException {
DataInputStream dis = new DataInputStream(System.in);
StackImplementation stk = new StackImplementation();

17 | P a g e
int menu = 0;
do {
System.out.println("1.push \n2.pop \n3.peek \n4.display \
n5.Exit");
System.out.println();
System.out.print("Enter your choice: ");
menu = Integer.parseInt(dis.readLine());
switch (menu) {
case 1:
stk.push();
break;
case 2:
stk.pop();
break;
case 3:
stk.peek();
break;
case 4:
stk.display();
break;
case 5:
System.exit(0);
}
} while (menu <= 5);
System.out.println();
}
}
Output:

18 | P a g e
1.push
2.pop
3.peek
4.display
5.Exit
Enter your choice: 1
Enter Element5
1.push
2.pop
3.peek
4.display
5.Exit

Enter your choice: 4


Elements are 5
1.push
2.pop
3.peek
4.display
5.Exit

19 | P a g e
Program – 6

Aim: - Develop two different classes that implement this interface. One using
array and other using linked list.
Program:
import java.io.*;
interface Mystack
{
public void pop();
public void push();
public void display();
}
class Stack_array implements Mystack
{
final static int n=5;
int stack[]=new int[n];
int top=-1;
public void push()
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
if(top==(n-1))
{
System.out.println(" Stack Overflow");
return;
}
else
{
System.out.println("Enter the element");
int ele=Integer.parseInt(br.readLine());
stack[++top]=ele;
}
}
catch(IOException e)
{
System.out.println("e");
}

20 | P a g e
}
public void pop()
{
if(top<0)
{
System.out.println("Stack underflow");
return;
}
else
{
int popper=stack[top];
top--;
System.out.println("Popped element:" +popper);
}
}

public void display()


{
if(top<0)
{
System.out.println("Stack is empty");
return;
}
else
{
String str=" ";
for(int i=0; i<=top; i++)
str=str+" "+stack[i]+" <--";
System.out.println("Elements are:"+str);
}
}
}
class Link
{
public int data;
public Link nextLink;
public Link(int d)
{
data= d;
nextLink=null;
}
public void printLink()
{

21 | P a g e
System.out.print(" --> "+data);
}
}
class Stack_List implements Mystack
{
private Link first;
public Stack_List()
{
first = null;
}
public boolean isEmpty()
{
return first == null;
}
public void push()
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the element");
int ele=Integer.parseInt(br.readLine());
Link link = new Link(ele);
link.nextLink = first;
first = link;
}
catch(IOException e)
{
System.err.println(e);
}
}
public Link delete()
{
Link temp = first;
try
{
first = first.nextLink;
}
catch(NullPointerException e)
{
throw e;
}
return temp;

22 | P a g e
}
public void pop()
{
try
{
Link deletedLink = delete();
System.out.println("Popped: "+deletedLink.data);
}
catch(NullPointerException e)
{
throw e;
}
}
public void display()
{
if(first==null)
System.out.println("Stack is empty");
else
{
Link currentLink = first;
System.out.print("Elements are: ");
while(currentLink != null)
{
currentLink.printLink();
currentLink = currentLink.nextLink;
}
System.out.println("");
}
}
}
class StackADT
{
public static void main(String arg[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Implementation of Stack using Array");
Stack_array stk=new Stack_array();
int ch=0;
do
{
System.out.println("1.Push 2.Pop 3.Display 4.Exit 5.Use Linked List");
System.out.println("Enter your choice:");

23 | P a g e
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
stk.push();
break;
case 2:
stk.pop();
break;
case 3:
stk.display();
break;
case 4:
System.exit(0);
}
}
while(ch<5);
System.out.println("Implementation of Stack using Linked List");
Stack_List stk1=new Stack_List();
ch=0;
do
{
System.out.println("1.Push 2.Pop 3.Display 4.Exit");
System.out.println("Enter your choice:");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
stk1.push();
break;
case 2:
try
{
stk1.pop();
}
catch(NullPointerException e)
{
System.out.println("Stack underflown");
}
break;
case 3:
stk1.display();
break;

24 | P a g e
default:
System.exit(0);

}
}
while(ch<5);
}
}

Output:
Implementation of Stack using Array
1.Push 2.Pop 3.Display 4.Exit 5.Use Linked List
Enter your choice:
1
Enter the element
10
1.Push 2.Pop 3.Display 4.Exit 5.Use Linked List
Enter your choice:
1
Enter the element
15
1.Push 2.Pop 3.Display 4.Exit 5.Use Linked List
Enter your choice:
1
Enter the element
25
1.Push 2.Pop 3.Display 4.Exit 5.Use Linked List
Enter your choice:
3
Elements are: 10 <-- 15 <-- 25 <--

25 | P a g e
1.Push 2.Pop 3.Display 4.Exit 5.Use Linked List
Enter your choice:
5
Implementation of Stack using Linked List
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
1
Enter the element
10
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
1
Enter the element
15
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
1
Enter the element
20
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
3
Elements are: --> 20 --> 15 --> 10
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
2
Popped: 20

26 | P a g e
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
3
Elements are: --> 15 --> 10
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
4

27 | P a g e
Program – 7

Aim: - Develop a simple paint like program that can draw basic graphical
primitives.
Program:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;

class DrawPanel extends JPanel {


private void doDrawing(Graphics g) {
var g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
for (int i = 0; i <= 1000; i++) {
var size = getSize();
var insets = getInsets();
int w = size.width - insets.left - insets.right;
int h = size.height - insets.top - insets.bottom;
var r = new Random();
int x = Math.abs(r.nextInt()) % w;
int y = Math.abs(r.nextInt()) % h;
g2d.drawLine(x, y, x, y);
}

28 | P a g e
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
public class PointsEx extends JFrame {
public PointsEx()
initUI();
}
private void initUI() {
var drawPanel = new DrawPanel();
add(drawPanel);
setSize(350, 250);
setTitle("Points");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
var ex = new PointsEx();
ex.setVisible(true);
});
}
}

29 | P a g e
Output:

30 | P a g e
Program – 8

Aim: - Develop a scientific calculator using event driven programming.


Program:
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.event.*;

public class ScientificCalculator extends JFrame implements ActionListener

JTextField tfield;

double temp, temp1, result, a;

static double m1, m2;

int k = 1, x = 0, y = 0, z = 0;

char ch;

JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, zero, clr, pow2, pow3, exp,

fac, plus, min, div, log, rec, mul, eq, addSub, dot, mr, mc, mp,

mm, sqrt, sin, cos, tan;

Container cont;

JPanel textPanel, buttonpanel;

ScientificCalculator()

31 | P a g e
{

cont = getContentPane();

cont.setLayout(new BorderLayout());

JPanel textpanel = new JPanel();

tfield = new JTextField(25);

tfield.setHorizontalAlignment(SwingConstants.RIGHT);

tfield.addKeyListener(new KeyAdapter() {

public void keyTyped(KeyEvent keyevent) {

char c = keyevent.getKeyChar();

if (c >= '0' && c <= '9') {

else

keyevent.consume();

});

textpanel.add(tfield);

buttonpanel = new JPanel();

buttonpanel.setLayout(new GridLayout(8, 4, 2, 2));

32 | P a g e
boolean t = true;

mr = new JButton("MR");

buttonpanel.add(mr);

mr.addActionListener(this);

mc = new JButton("MC");

buttonpanel.add(mc);

mc.addActionListener(this);

mp = new JButton("M+");

buttonpanel.add(mp);

mp.addActionListener(this);

mm = new JButton("M-");

buttonpanel.add(mm);

mm.addActionListener(this);

b1 = new JButton("1");

buttonpanel.add(b1);

b1.addActionListener(this);

b2 = new JButton("2");

buttonpanel.add(b2);

b2.addActionListener(this);

b3 = new JButton("3");

33 | P a g e
buttonpanel.add(b3);

b3.addActionListener(this);

b4 = new JButton("4");

buttonpanel.add(b4);

b4.addActionListener(this);

b5 = new JButton("5");

buttonpanel.add(b5);

b5.addActionListener(this);

b6 = new JButton("6");

buttonpanel.add(b6);

b6.addActionListener(this);

b7 = new JButton("7");

buttonpanel.add(b7);

b7.addActionListener(this);

b8 = new JButton("8");

buttonpanel.add(b8);

b8.addActionListener(this);

b9 = new JButton("9");

buttonpanel.add(b9);

b9.addActionListener(this);

34 | P a g e
zero = new JButton("0");

buttonpanel.add(zero);

zero.addActionListener(this);

plus = new JButton("+");

buttonpanel.add(plus);

plus.addActionListener(this);

min = new JButton("-");

buttonpanel.add(min);

min.addActionListener(this);

mul = new JButton("*");

buttonpanel.add(mul);

mul.addActionListener(this);

div = new JButton("/");

div.addActionListener(this);

buttonpanel.add(div);

addSub = new JButton("+/-");

buttonpanel.add(addSub);

addSub.addActionListener(this);

dot = new JButton(".");

buttonpanel.add(dot);

35 | P a g e
dot.addActionListener(this);

eq = new JButton("=");

buttonpanel.add(eq);

eq.addActionListener(this);

rec = new JButton("1/x");

buttonpanel.add(rec);

rec.addActionListener(this);

sqrt = new JButton("Sqrt");

buttonpanel.add(sqrt);

sqrt.addActionListener(this);

log = new JButton("log");

buttonpanel.add(log);

log.addActionListener(this);

sin = new JButton("SIN");

buttonpanel.add(sin);

sin.addActionListener(this);

cos = new JButton("COS");

buttonpanel.add(cos);

cos.addActionListener(this);

tan = new JButton("TAN");

36 | P a g e
buttonpanel.add(tan);

tan.addActionListener(this);

pow2 = new JButton("x^2");

buttonpanel.add(pow2);

pow2.addActionListener(this);

pow3 = new JButton("x^3");

buttonpanel.add(pow3);

pow3.addActionListener(this);

exp = new JButton("Exp");

exp.addActionListener(this);

buttonpanel.add(exp);

fac = new JButton("n!");

fac.addActionListener(this);

buttonpanel.add(fac);

clr = new JButton("AC");

buttonpanel.add(clr);

clr.addActionListener(this);

cont.add("Center", buttonpanel);

cont.add("North", textpanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

37 | P a g e
}

public void actionPerformed(ActionEvent e)

String s = e.getActionCommand();

if (s.equals("1"))

if (z == 0)

tfield.setText(tfield.getText() + "1");

else

tfield.setText("");

tfield.setText(tfield.getText() + "1");

z = 0;

if (s.equals("2")) {

if (z == 0) {

tfield.setText(tfield.getText() + "2");

38 | P a g e
}

else

tfield.setText("");

tfield.setText(tfield.getText() + "2");

z = 0;

if (s.equals("3")) {

if (z == 0) {

tfield.setText(tfield.getText() + "3");

else

tfield.setText("");

tfield.setText(tfield.getText() + "3");

z = 0;

if (s.equals("4")) {

39 | P a g e
if (z == 0) {

tfield.setText(tfield.getText() + "4");

else

tfield.setText("");

tfield.setText(tfield.getText() + "4");

z = 0;

if (s.equals("5")) {

if (z == 0) {

tfield.setText(tfield.getText() + "5");

else

tfield.setText("");

tfield.setText(tfield.getText() + "5");

z = 0;

40 | P a g e
}

if (s.equals("6")) {

if (z == 0) {

tfield.setText(tfield.getText() + "6");

else

tfield.setText("");

tfield.setText(tfield.getText() + "6");

z = 0;

if (s.equals("7")) {

if (z == 0) {

tfield.setText(tfield.getText() + "7");

else

tfield.setText("");

tfield.setText(tfield.getText() + "7");

41 | P a g e
z = 0;

if (s.equals("8")) {

if (z == 0) {

tfield.setText(tfield.getText() + "8");

else

tfield.setText("");

tfield.setText(tfield.getText() + "8");

z = 0;

if (s.equals("9")) {

if (z == 0) {

tfield.setText(tfield.getText() + "9");

else

42 | P a g e
tfield.setText("");

tfield.setText(tfield.getText() + "9");

z = 0;

if (s.equals("0"))

if (z == 0) {

tfield.setText(tfield.getText() + "0");

else

tfield.setText("");

tfield.setText(tfield.getText() + "0");

z = 0;

if (s.equals("AC")) {

tfield.setText("");

x = 0;

43 | P a g e
y = 0;

z = 0;

if (s.equals("log"))

if (tfield.getText().equals("")) {

tfield.setText("");

else

a = Math.log(Double.parseDouble(tfield.getText()));

tfield.setText("");

tfield.setText(tfield.getText() + a);

if (s.equals("1/x")) {

if (tfield.getText().equals("")) {

tfield.setText("");

else

44 | P a g e
{

a = 1 / Double.parseDouble(tfield.getText());

tfield.setText("");

tfield.setText(tfield.getText() + a);

if (s.equals("Exp")) {

if (tfield.getText().equals("")) {

tfield.setText("");

else

a = Math.exp(Double.parseDouble(tfield.getText()));

tfield.setText("");

tfield.setText(tfield.getText() + a);

if (s.equals("x^2")) {

if (tfield.getText().equals("")) {

tfield.setText("");

45 | P a g e
}

else

a = Math.pow(Double.parseDouble(tfield.getText()), 2);

tfield.setText("");

tfield.setText(tfield.getText() + a);

if (s.equals("x^3")) {

if (tfield.getText().equals("")) {

tfield.setText("");

else

a = Math.pow(Double.parseDouble(tfield.getText()), 3);

tfield.setText("");

tfield.setText(tfield.getText() + a);

if (s.equals("+/-")) {

46 | P a g e
if (x == 0) {

tfield.setText("-" + tfield.getText());

x = 1;

else

tfield.setText(tfield.getText());

if (s.equals(".")) {

if (y == 0) {

tfield.setText(tfield.getText() + ".");

y = 1;

else

tfield.setText(tfield.getText());

if (s.equals("+"))

47 | P a g e
{

if (tfield.getText().equals(""))

tfield.setText("");

temp = 0;

ch = '+';

else

temp = Double.parseDouble(tfield.getText());

tfield.setText("");

ch = '+';

y = 0;

x = 0;

tfield.requestFocus();

if (s.equals("-"))

if (tfield.getText().equals(""))

48 | P a g e
{

tfield.setText("");

temp = 0;

ch = '-';

else

x = 0;

y = 0;

temp = Double.parseDouble(tfield.getText());

tfield.setText("");

ch = '-';

tfield.requestFocus();

if (s.equals("/")) {

if (tfield.getText().equals(""))

tfield.setText("");

temp = 1;

49 | P a g e
ch = '/';

else

x = 0;

y = 0;

temp = Double.parseDouble(tfield.getText());

ch = '/';

tfield.setText("");

tfield.requestFocus();

if (s.equals("*")) {

if (tfield.getText().equals(""))

tfield.setText("");

temp = 1;

ch = '*';

else

50 | P a g e
{

x = 0;

y = 0;

temp = Double.parseDouble(tfield.getText());

ch = '*';

tfield.setText("");

tfield.requestFocus();

if (s.equals("MC"))

m1 = 0;

tfield.setText("");

if (s.equals("MR"))

tfield.setText("");

tfield.setText(tfield.getText() + m1);

if (s.equals("M+"))

51 | P a g e
{

if (k == 1) {

m1 = Double.parseDouble(tfield.getText());

k++;

else

m1 += Double.parseDouble(tfield.getText());

tfield.setText("" + m1);

if (s.equals("M-"))

if (k == 1) {

m1 = Double.parseDouble(tfield.getText());

k++;

else

m1 -= Double.parseDouble(tfield.getText());

52 | P a g e
tfield.setText("" + m1);

if (s.equals("Sqrt"))

if (tfield.getText().equals(""))

tfield.setText("");

else

a = Math.sqrt(Double.parseDouble(tfield.getText()));

tfield.setText("");

field.setText(tfield.getText() + a);

if (s.equals("SIN"))

if (tfield.getText().equals(""))

53 | P a g e
tfield.setText("");

else

a = Math.sin(Double.parseDouble(tfield.getText()));

tfield.setText("");

tfield.setText(tfield.getText() + a);

if (s.equals("COS"))

if (tfield.getText().equals(""))

tfield.setText("");

else

a = Math.cos(Double.parseDouble(tfield.getText()));

tfield.setText("");

tfield.setText(tfield.getText() + a);

54 | P a g e
}

if (s.equals("TAN")) {

if (tfield.getText().equals("")) {

tfield.setText("");

else

a = Math.tan(Double.parseDouble(tfield.getText()));

tfield.setText("");

tfield.setText(tfield.getText() + a);

if (s.equals("="))

if (tfield.getText().equals(""))

tfield.setText("");

else

55 | P a g e
{

temp1 = Double.parseDouble(tfield.getText());

switch (ch)

case '+':

result = temp + temp1;

break;

case '-':

result = temp - temp1;

break;

case '/':

result = temp / temp1;

break;

case '*':

result = temp * temp1;

break;

tfield.setText("");

tfield.setText(tfield.getText() + result);

z = 1;

56 | P a g e
}

if (s.equals("n!"))

if (tfield.getText().equals(""))

tfield.setText("");

else

a = fact(Double.parseDouble(tfield.getText()));

tfield.setText("");

tfield.setText(tfield.getText() + a);

tfield.requestFocus();

double fact(double x)

int er = 0;

57 | P a g e
if (x < 0)

er = 20;

return 0;

double i, s = 1;

for (i = 2; i <= x; i += 1.0)

s *= i;

return s;

public static void main(String args[])

try

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLook
AndFeel");

catch (Exception e)

ScientificCalculator f = new ScientificCalculator();

58 | P a g e
f.setTitle("ScientificCalculator");

f.pack();

f.setVisible(true);

Output:

59 | P a g e
Program – 9

Aim: - Develop a template for linked list class along with its members in Java.
Program:
import java.io.*;
import java.util.*;
class Link<T>
{
public T data;
public Link nextLink;
public Link(T d) {
data = d;
}
public void printLink() {
System.out.println("item:"+data);
}
}
class LinkList<T>
{
private Link first;
private Link last;
public LinkList() {
first = null;
}
public boolean isEmpty() {
return first == null;
}
public void insert(T d){
Link link = new Link(d);
if(first==null){
link.nextLink = null;
first = link;
last=link;
}
else{
last.nextLink=link;
link.nextLink=null;

60 | P a g e
last=link;
}
}
public Link delete() {
Link temp = first;
first = first.nextLink;
return temp;
}
public void printList() {
Link currentLink = first;
while(currentLink != null) {
currentLink.printLink();
currentLink = currentLink.nextLink;
}
System.out.println("");
}
}
class template {
public static void main(String[] args)
{
int i,c=1,ch,p1=0,p2=0,p3=0;
Scanner in=new Scanner(System.in);
LinkList<Integer> l = new LinkList();
LinkList<String> s=new LinkList();
LinkList<Double> d=new LinkList();
do {
System.out.println("1.INTEGER 2.STRING 3.DOUBLE 4.exit");
System.out.println("enter ur choice:");
c=in.nextInt();
switch(c)
{
case 1:
do {
if(p1==1)break;
System.out.println("1.insert 2.delete 3.display 4.exit");
System.out.println("enter ur choice:");
ch=in.nextInt();
switch(ch)
{
case 1:

61 | P a g e
System.out.println("Integer list");
System.out.println("enter the insert value:");
i=in.nextInt();
l.insert(i);
break;
case 2:
l.delete();
System.out.println("data deleted:");
break;
case 3:
System.out.println("elements are :");
l.printList();
break;
case 4:
p1=1;
continue;
}
}while(c!=0);
break;
case 2:
do {
if(p2==1)break;
System.out.println("1.insert 2.delete 3.display 4.exit");
System.out.println("enter ur choice:");
ch=in.nextInt();
switch(ch)
{
case 1:
System.out.println("STRING list");
System.out.println("enter the insert value:");
String a=in.next();
s.insert(a);
break;
case 2:
s.delete();
System.out.println("data deleted:");
break;
case 3:
System.out.println("elements are :");
s.printList();

62 | P a g e
break;
case 4:
p2=1;
continue;
}
}while(c!=0);
break;
case 3:
do{
if(p3==1)break;
System.out.println("1.insert 2.delete 3.display 4.exit");
System.out.println("enter ur choice:");
ch=in.nextInt();
switch(ch)
{
case 1:
System.out.println("DOUBLE list");
System.out.println("enter the insert value:");
double x=in.nextDouble();
d.insert(x);
break;
case 2:
d.delete();
System.out.println("data deleted:");
break;
case 3:
System.out.println("elements are :");
d.printList();
break;
case 4:
p3=1;
continue;
}
}while(c!=0);
break;
case 4:
System.exit(0);
}
}while(c!=0);

63 | P a g e
}
}

Output:
1.INTEGER 2.STRING 3.DOUBLE 4.exit
enter ur choice:
1
1.insert 2.delete 3.display 4.exit
enter ur choice:
1
Integer list
enter the insert value:
1
1.insert 2.delete 3.display 4.exit
enter ur choice:
1
Integer list
enter the insert value:
2
1.insert 2.delete 3.display 4.exit
enter ur choice:
1
Integer list
enter the insert value:
3
1.insert 2.delete 3.display 4.exit
enter ur choice:
3

64 | P a g e
elements are :
item:1
item:2
item:3

1.insert 2.delete 3.display 4.exit


enter ur choice:
2
data deleted:
1.insert 2.delete 3.display 4.exit
enter ur choice:
3
elements are :
item:2
item:3

1.insert 2.delete 3.display 4.exit


enter ur choice:
4
1.INTEGER 2.STRING 3.DOUBLE 4.exit
enter ur choice:
2
1.insert 2.delete 3.display 4.exit
enter ur choice:
1
STRING list
enter the insert value:

65 | P a g e
niren
1.insert 2.delete 3.display 4.exit
enter ur choice:
1
STRING list
enter the insert value:
kumar
1.insert 2.delete 3.display 4.exit
enter ur choice:
1
STRING list
enter the insert value:
raj
1.insert 2.delete 3.display 4.exit
enter ur choice:
3
elements are :
item:niren
item:kumar
item:raj

1.insert 2.delete 3.display 4.exit


enter ur choice:
2
data deleted:
1.insert 2.delete 3.display 4.exit
enter ur choice:

66 | P a g e
3
elements are :
item:kumar
item:raj

1.insert 2.delete 3.display 4.exit


enter ur choice:
4
1.INTEGER 2.STRING 3.DOUBLE 4.exit
enter ur choice:
4

67 | P a g e
Program – 10

Aim: - Write a program to insert and view data using Servlets.


Program:
First create an employee in Oracle and insert some data as below.
create table employee(empid varchar(10),empname varchar(10),sal int)

insert into employee values('e001','raj',10000)


insert into employee values('e002','harry',20000)
insert into employee values('e003','sunil',30000)
insert into employee values('e004','pollock',40000)
insert into employee values('e005','jonty',50000)
insert into employee values('e006','kallis',60000)
insert into employee values('e007','richard',70000)
Creation of dsn(database source name)

Start-Control Panel- Administrative Tools- Data Sources (ODBC)-go to system


DSN tab-click add button-select a driver for which you want to set up a data
source (for Oracle- Oracle in XE)-select it and click finish-give any name in
data source name textbox-then click ok button.

Program to display data from database through servlet and JDBC


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

public class display extends HttpServlet


{
68 | P a g e
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
IOException, ServletException
{
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.println("<html><body>");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mydsn", "s
ystem", "pintu");
// Here dsnname- mydsn,user id- system(for oracle 10g),password is pin
tu.
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from employee");
out.println("<table border=1 width=50% height=50%>");
out.println("<tr><th>EmpId</th><th>EmpName</th><th>Salary</
th><tr>");
while (rs.next())
{
String n = rs.getString("empid");
String nm = rs.getString("empname");
int s = rs.getInt("sal");
out.println("<tr><td>" + n + "</td><td>" + nm + "</td><td>" + s + "
</td></tr>");
}
out.println("</table>");
out.println("</html></body>");
con.close();

69 | P a g e
}
catch (Exception e)
{
out.println("error");
}
}
}

web.xml setting: -
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more contribu
tor license agreements. See the NOTICE file distributed with this work for addit
ional information regarding copyright ownership. The ASF licenses this file to
You under the Apache License, Version 2.0 (the "License"); you may not use th
is file except in compliance with the License. You may obtain a copy of the Lic
ense at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed u


nder the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTI
ES OR CONDITIONS OF ANY KIND, either express or implied. See the Licen
se for the specific language governing permissions and limitations under the Lic
ense.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://


www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://
java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-
app_2_5.xsd" version="2.5">

70 | P a g e
<servlet>
<servlet-name>display</servlet-name>
<servlet-class>display</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>display</servlet-name>
<url-pattern>/display</url-pattern>
</servlet-mapping>
</web-app>

Output:

Running the servlet in a web browser


First run the tomcat 6.0
71 | P a g e
http://localhost:8081/javaservlet/display

72 | P a g e

You might also like