0% found this document useful (0 votes)
6 views

JAVA Fioe

The document describes designing classes for complex numbers, geometric shapes, and dynamic polymorphism. It also includes code for an integer stack interface and implementation using an array.

Uploaded by

yashwardhan6031
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

JAVA Fioe

The document describes designing classes for complex numbers, geometric shapes, and dynamic polymorphism. It also includes code for an integer stack interface and implementation using an array.

Uploaded by

yashwardhan6031
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

PRACTICAL FILE

OF

ESSENTIAL OF INFORMATION TECHNOLOGY LAB

Submitted To: Submitted By:


Er. Rajiv Bansal Nancy

Asst. Prof. 1221258

Deptt. Of CSE CSE (5th sem.)

Department of Computer Science & Engineering

Seth Jai Parkash Mukand Lal Institute of Engineering &Technology,

Radaur – 135133 (Yamuna Nagar)


(Affiliated to Kurukshetra University, Kurukshetra, Haryana, India)
INDEX
S.No. Practical Signature
1. Write a Java Package with Stack and queue classes.

2. 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
3. Develop with suitable hierarchy, class for point,
shape rectangle, square, circle, ellipse, triangle,
polygenetic.
4. Design a simple test application to demonstrate
dynamic polymorphism.
5. Design a java interface for ADT Stack.Develop
class that implement this interface using array.
6. Develop a class that implement this interface using
linkedlist.
7. Develop a simple paint like program that can
drawbasic graphical primitives.
8. Develop a scientific calculator using event driven
programming.

9. Develop a template for linkedlist class along with its


members in Java.
10. Write a program to insert and view data using
Servlets.
PROGRAM 1- Write a Java Package with Stack and queue
classes.
//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");
}
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]);
}
}
}
}
//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];
}
public boolean isEmpty()
{
return(nitems==0);
}
public boolean isFull()
{
return(nitems==maxsize);
}
public int size()
{
return nitems;
}
}
//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());
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);
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:
PROGRAM 2- 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.
import java.io.*;

import java.util.*;

public class Complex

public static void main(String args[])

int ch=0;

float num1,num2,answer;

Complex_Op cal = new Complex_Op() ;

Scanner input = new Scanner(System.in);

System.out.print("Enter the first Number\n");

System.out.print("Real Part:");

num1 = input.nextInt();

System.out.print("Imaginary Part:");

num2 = input.nextInt();

Complex_Op Object1 = new Complex_Op(num1,num2);

System.out.print("Enter the Second Number\n");

System.out.print("Real Part:");

num1 = input.nextInt();
System.out.print("Imaginary Part:");

num2 = input.nextInt();

Complex_Op Object2 = new Complex_Op(num1,num2);

do

System.out.println("1.Add");

System.out.println("2.Subtract");

System.out.println("3.Multiplication");

System.out.println("4.Division");

System.out.println("5.Exit");

System.out.print("Choose ur choice:");

ch = input.nextInt();

switch(ch)

case 1:

cal.AddNumbers(Object1,Object2);

break;

case 2:

cal.SubtractNumbers(Object1,Object2);

break;

case 3:

cal.MultiplyNumbers(Object1,Object2);

break;

case 4:
cal.DivideNumbers(Object1,Object2);

break;

case 5:

break;

}while(ch!=5);

class Complex_Op

private float real,imag;

Complex_Op()

real=0;

imag=0;

Complex_Op(float Comp1,float Comp2)

real=Comp1;

imag=Comp2;

void AddNumbers(Complex_Op C1,Complex_Op C2)

{
float real,imag;

this.real = (C1.real + C2.real);

this.imag = (C1.imag + C2.imag);

System.out.println("Answer is:("+this.real+") + ("+this.imag+")i" );

void SubtractNumbers(Complex_Op C1,Complex_Op C2)

float real,imag;

this.real = (C1.real - C2.real);

this.imag = (C1.imag - C2.imag);

System.out.println("Answer is:("+this.real+") + ("+this.imag+")i" );

void MultiplyNumbers(Complex_Op C1,Complex_Op C2)

float real,imag;

this.real = (C1.real*C2.real - C1.imag*C2.imag);

this.imag = (C1.real*C2.imag + C2.real*C1.imag);

System.out.println("Answer is:("+this.real+") + ("+this.imag+")i" );

void DivideNumbers(Complex_Op C1,Complex_Op C2)

float real,imag,deno;

deno = (C2.real*C2.real + C2.imag*C2.imag);

this.real = (C1.real*C2.real + C1.imag*C2.imag)/deno;


this.imag = (C2.real*C1.imag - C1.real*C2.imag)/deno;

System.out.println("Answer is:("+this.real+") + ("+this.imag+")i" );

}
PROGRAM 3- Develop with suitable hierarchy, class for point, shape
rectangle, square, circle, ellipse, triangle, polygenetic.
abstract class Shape
{
double dim1;
double dim2;
double PI=3.14;
Shape(double a, double b)
{
dim1 = a;
dim2 = b;
}
abstract double area();
}
class Rectangle extends Shape
{
Rectangle(double a, double b)
{
super(a, b);
}
double area()
{
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Shape
{
Triangle(double a, double b)
{
super(a, b);
}
double area()
{
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class Circle extends Shape
{
Circle(double a, double b)
{
super(a, b);
}
double area()
{
System.out.println("Inside Area for Circle.");
return PI * dim1 * dim1;
}
}
class Ellipse extends Shape
{
Ellipse(double a, double b)
{
super(a, b);
}
double area() {
System.out.println("Inside Area for Ellipse.");
return PI * dim1 * dim2;
}
}
class Square extends Shape
{
Square(double a, double b)
{
super(a, b);
}
double area() {
System.out.println("Inside Area for Square.");
return dim1 * dim1;
}
}
class AbstractAreas {
public static void main(String args[]) {
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Circle c=new Circle(5,5);
Ellipse e=new Ellipse(7,7);
Square s=new Square(6,6);
Shape figref; // this is OK, no object is created
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = c;
System.out.println("Area is " + figref.area());
figref = e;
System.out.println("Area is " + figref.area());
figref = s;
System.out.println("Area is " + figref.area());
}
}
OUTPUT:
PROGRAM 4- Design a simple test application to demonstrate
dynamic polymorphism.
// Dynamic Polymorphism
class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A {
// override callme()
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A {
// override callme()
void callme() {
System.out.println("Inside C's callme method");
}
}
public class Dynamic {
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A

r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}
OUTPUT:
PROGRAM 5: Design a java interface for ADT Stack. Develop class that implement this
interface using array.

// Define an integer stack interface.


interface IntStack {
void push(int item); // store an item
int pop(); // retrieve an item
}
// An implementation of IntStack that uses fixed storage.(using array)
class FixedStack implements IntStack {
private int stck[];
private int tos;
// allocate and initialize stack
FixedStack(int size) {
stck = new int[size];
tos = -1;
}
// Push an item onto the stack
public void push(int item) {
if(tos==stck.length-1) // use length member
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
// Pop an item from the stack
public int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}
class IFTest {
public static void main(String args[]) {
FixedStack mystack1 = new FixedStack(5);
FixedStack mystack2 = new FixedStack(8);
// push some numbers onto the stack
for(int i=0; i<5; i++) mystack1.push(i);
for(int i=0; i<8; i++) mystack2.push(i);
// pop those numbers off the stack
System.out.println("Stack in mystack1:");
for(int i=0; i<5; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<8; i++)
System.out.println(mystack2.pop());
}
}
OUTPUT:
PROGRAM 6: Develop a class that implement this interface using linked list.

// Implement a "growable" stack.(using linked list)


class DynStack implements IntStack {
private int stck[];
private int tos;
// allocate and initialize stack
DynStack(int size) {
stck = new int[size];
tos = -1;
}
// Push an item onto the stack
public void push(int item) {
// if stack is full, allocate a larger stack
if(tos==stck.length-1) {
int temp[] = new int[stck.length * 2]; // double size
for(int i=0; i<stck.length; i++) temp[i] = stck[i];
stck = temp;
stck[++tos] = item;
}
else
stck[++tos] = item;
}
// Pop an item from the stack
public int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}
class IFTest2 {
public static void main(String args[]) {
DynStack mystack1 = new DynStack(5);
DynStack mystack2 = new DynStack(8);
// these loops cause each stack to grow
for(int i=0; i<12; i++) mystack1.push(i);
for(int i=0; i<20; i++) mystack2.push(i);
System.out.println("Stack in mystack1:");
for(int i=0; i<12; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<20; i++)
System.out.println(mystack2.pop()); }}
OUTPUT:
Program 7: Develop a simple paint like program that can draw basic
graphical primitives.

Code for Activity_main.xml:


Code for MainActivity.java: package com.example.exno4;
importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.drawable.BitmapDrawable;
importandroid.os.Bundle;
importandroid.widget.ImageView;
public class MainActivity extends Activity
{
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating a Bitmap Bitmapbg = Bitmap.createBitmap(720, 1280,
Bitmap.Config.ARGB_8888);
//Setting the Bitmap as background for the ImageViewImageView i =
(ImageView) findViewById(R.id.imageView);
i.setBackgroundDrawable(new BitmapDrawable(bg));

//Creating the Canvas Object Canvas canvas = new Canvas(bg);


//Creating the Paint Object and set its color&TextSize Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(50);
//To draw a Rectangle canvas.drawText("Rectangle", 420, 150, paint);
canvas.drawRect(400, 200, 650, 700, paint);
//To draw a Circle canvas.drawText("Circle", 120, 150, paint);
canvas.drawCircle(200, 350, 150, paint);
//To draw a Square canvas.drawText("Square", 120, 800, paint);
canvas.drawRect(50, 850, 350, 1150, paint);
//To draw a Line canvas.drawText("Line", 480, 800, paint);
canvas.drawLine(520, 850, 520, 1150, paint);
}
}

Output:
Program 8:Develop a scientific calculator using event driven programming.

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

public class scientific extends JFrame implements ActionListener


{
JTextField jtx;
double temp,temp1,result,a;
static double m1,m2;
int k=1,x=0,y=0,z=0;
char ch;
JButton
one,two,three,four,five,six,seven,eight,nine,zero,clr,pow2,pow3;
JButton
plus,min,div,lg,rec,mul,eq,plmi,poin,mr,mc,mp,mm,sqrt,sin,cos,tan;
Container cont;
JPanel textPanel,buttonpanel;
scientific()
{
cont=getContentPane();
cont.setLayout(new BorderLayout());
JPanel textpanel=new JPanel();
Font font=new Font("Arial",Font.PLAIN,18);
jtx=new JTextField(25);
jtx.setFont(font);
jtx.setHorizontalAlignment(SwingConstants.RIGHT);
jtx.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent keyevent)
{
char c=keyevent.getKeyChar();
if(c>='0' && c<='9')
{
}
else
{
keyevent.consume();
}
}
});
textpanel.add(jtx);
buttonpanel=new JPanel();
buttonpanel.setLayout(new GridLayout(5,6));
boolean t=true;

sin=new JButton("SIN");
buttonpanel.add(sin);
sin.addActionListener(this);
mr=new JButton("MR");
buttonpanel.add(mr);
mr.addActionListener(this);
seven=new JButton("7");
buttonpanel.add(seven);
seven.addActionListener(this);
eight=new JButton("8");
buttonpanel.add(eight);
eight.addActionListener(this);
nine=new JButton("9");
buttonpanel.add(nine);
nine.addActionListener(this);
clr=new JButton("AC");
buttonpanel.add(clr);
clr.addActionListener(this);

cos=new JButton("COS");
buttonpanel.add(cos);
cos.addActionListener(this);
mc=new JButton("MC");
buttonpanel.add(mc);
mc.addActionListener(this);
four=new JButton("4");
buttonpanel.add(four);
four.addActionListener(this);
five=new JButton("5");
buttonpanel.add(five);
five.addActionListener(this);
six=new JButton("6");
buttonpanel.add(six);
six.addActionListener(this);
mul=new JButton("*");
buttonpanel.add(mul);
mul.addActionListener(this);

tan=new JButton("TAN");
buttonpanel.add(tan);
tan.addActionListener(this);
mp=new JButton("M+");
buttonpanel.add(mp);
mp.addActionListener(this);
one=new JButton("1");
buttonpanel.add(one);
one.addActionListener(this);
two=new JButton("2");
buttonpanel.add(two);
two.addActionListener(this);
three=new JButton("3");
buttonpanel.add(three);
three.addActionListener(this);
min=new JButton("-");
buttonpanel.add(min);
min.addActionListener(this);

pow2=new JButton("x^2");
buttonpanel.add(pow2);
pow2.addActionListener(this);
mm=new JButton("M-");
buttonpanel.add(mm);
mm.addActionListener(this);
zero=new JButton("0");
buttonpanel.add(zero);
zero.addActionListener(this);
plmi=new JButton("+/-");
buttonpanel.add(plmi);
plmi.addActionListener(this);
poin=new JButton(".");
buttonpanel.add(poin);
poin.addActionListener(this);
plus=new JButton("+");
buttonpanel.add(plus);
plus.addActionListener(this);

pow3=new JButton("x^3");
buttonpanel.add(pow3);
pow3.addActionListener(this);
rec=new JButton("1/x");
buttonpanel.add(rec);
rec.addActionListener(this);
sqrt=new JButton("Sqrt");
buttonpanel.add(sqrt);
sqrt.addActionListener(this);
lg=new JButton("log");
buttonpanel.add(lg);
lg.addActionListener(this);
div=new JButton("/");
div.addActionListener(this);
buttonpanel.add(div);
eq=new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);

cont.add("Center",buttonpanel);
cont.add("North",textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();

if(s.equals("1"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"1");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"1");
z=0;
}
}
if(s.equals("2"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"2");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"2");
z=0;
}
}
if(s.equals("3"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"3");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"3");
z=0;
}
}
if(s.equals("4"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"4");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"4");
z=0;
}
}
if(s.equals("5"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"5");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"5");
z=0;
}
}
if(s.equals("6"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"6");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"6");
z=0;
}
}
if(s.equals("7"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"7");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"7");
z=0;
}
}
if(s.equals("8"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"8");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"8");
z=0;
}
}
if(s.equals("9"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"9");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"9");
z=0;
}
}
if(s.equals("0"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"0");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"0");
z=0;
}
}
if(s.equals("AC"))
{
jtx.setText("");
x=0;
y=0;
z=0;
}
if(s.equals("log"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.log(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("1/x"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=1/Double.parseDouble(jtx.getText());
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("x^2"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.pow(Double.parseDouble(jtx.getText()),2);
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("x^3"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.pow(Double.parseDouble(jtx.getText()),3);
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("+/-"))
{
if(x==0)
{
jtx.setText("-"+jtx.getText());
x=1;
}
else
{
jtx.setText(jtx.getText());
}
}
if(s.equals("."))
{
if(y==0)
{
jtx.setText(jtx.getText()+".");
y=1;
}
else
{
jtx.setText(jtx.getText());
}
}
if(s.equals("+"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=0;
ch='+';
}
else
{
temp=Double.parseDouble(jtx.getText());
jtx.setText("");
ch='+';
y=0;
x=0;
}
jtx.requestFocus();
}
if(s.equals("-"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=0;
ch='-';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
jtx.setText("");
ch='-';
}
jtx.requestFocus();
}
if(s.equals("/"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=1;
ch='/';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
ch='/';
jtx.setText("");
}
jtx.requestFocus();
}
if(s.equals("*"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=1;
ch='*';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
ch='*';
jtx.setText("");
}
jtx.requestFocus();
}
if(s.equals("MC"))
{
m1=0;
jtx.setText("");
}
if(s.equals("MR"))
{
jtx.setText("");
jtx.setText(jtx.getText() + m1);
}
if(s.equals("M+"))
{
if(k==1)
{
m1=Double.parseDouble(jtx.getText());
k++;
}
else
{
m1+=Double.parseDouble(jtx.getText());
jtx.setText(""+m1);
}
}
if(s.equals("M-"))
{
if(k==1)
{
m1=Double.parseDouble(jtx.getText());
k++;
}
else
{
m1-=Double.parseDouble(jtx.getText());
jtx.setText(""+m1);
}
}
if(s.equals("Sqrt"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.sqrt(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("SIN"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.sin(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("COS"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.cos(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("TAN"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.tan(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("="))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
temp1 = Double.parseDouble(jtx.getText());
switch(ch)
{
case '+':
result=temp+temp1;
break;
case '-':
result=temp-temp1;
break;
case '/':
result=temp/temp1;
break;
case '*':
result=temp*temp1;
break;
}
jtx.setText("");
jtx.setText(jtx.getText() + result);
z=1;
}
}
jtx.requestFocus();
}
public static void main(String args[])
{
scientific n=new scientific();
n.setTitle("CALCULATOR");
n.setSize(370,250);
n.setResizable(false);
n.setVisible(true);
}
}

OUTPUT:
Program 9. Develop a template for linked list class along with its
members in Java.
package xyz;

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;
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:
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();
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);
}
}

OUTPUT:
10. Write a program to insert and view data using Servlets.
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>
</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
PreparedStatementps=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);
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:
This page doesn’t allow to submit the record without filling the textbox.
After filling all textbox, it allows to submit the record.

Click on Register button.

You might also like