EIT Practical File (28200109)
EIT Practical File (28200109)
(PC-CS309LA)
2021-2022
B.Tech CSE 3A
INDEX
[i]
INDEX
[ii]
Gaurav 2820109
PROGRAM NO. - 1
Program(Stack):-
import java.io.*;
import java.util.*;
class Main
{
if(pos == -1)
1
Gaurav 2820109
stack_push(stack);
stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
}
}
2
Gaurav 2820109
Output: -
Program(Queue):-
import java.util.*;
class QueueDemo{
3
Gaurav 2820109
System.out.println("\n\nGaurav -- 2820109\n");
System.out.println("head:"+queue.element());
System.out.println("head:"+queue.peek());
System.out.println("iterating the queue elements:");
Iterator itr=queue.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
queue.remove();
queue.poll();
}
}
}
Output: -
4
Gaurav 2820109
PROGRAM NO. – 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
5
Gaurav 2820109
objects created.
class complex
{
double real;
double imag;
public complex(double real, double imag)
{
this.real=real;
this.imag=imag;
}
public static void main(String args[]) {
System.out.println(“\n\nGaurav -- 2820109\n”);
complex n1 = new complex(2.3, 4.5);
complex n2 = new complex(3.4, 5.0);
complex temp;
temp = add(n1, n2);
System.out.printf("Sum = %.1f + %.1fi", temp.real, temp.imag);
}
return(temp);
}
}
Output: -
6
Gaurav 2820109
PROGRAM NO. - 3
Develop with suitable hierarchy, class for point, shape rectangle, square, circle,
ellipse, triangle, polygenetic.
class Shape {
String shapeVar="This automatically tells the shape";
int lengthOfSide1=0, lengthOfSide2=0;
final double PI=3.14;
7
Gaurav 2820109
void calcArea(){
System.out.print("This method will calculate the area of the shape
selected\n");
}
// POINT
class point extends Shape{
point(){
setShape("Point represents a dimensionless shape");
}
void calcArea(){
8
Gaurav 2820109
void getSides(){
System.out.print("No of sides in point is : infinite\n");
}
}
// RECTANGLE
class rectangle extends Shape{
rectangle(){
setShape("A rectangle is closed flat shape, having four sides, and each
angle equal to 90 degrees. The opposite sides of the rectangle are equal and
parallel.");
}
void calcArea(){
setSides(6, 7);
System.out.print("Area is: "+lengthOfSide1*lengthOfSide2);
}
void getSides(){
System.out.print("No of sides in point is :"+4+"\n");
}
}
// SQUARE
class square extends Shape{
square(){
setShape("Square is a plane figure with four equal sides and four right
(90°) angles. A square is a special kind of rectangle (an equilateral one) and a
special kind of parallelogram (an equilateral and equiangular one).");
}
void calcArea(){
9
Gaurav 2820109
setSides(8);
System.out.print("Area is: "+lengthOfSide1*lengthOfSide1);
}
void getSides(){
System.out.print("No of sides in point is : "+4+"\n");
}
}
// CIRCLE
class circle extends Shape{
circle(){
setShape("A circle is a round-shaped figure that has no corners or edges.");
}
void calcArea(){
setSides(8);
System.out.print("Area is: "+PI*lengthOfSide1*lengthOfSide1);
}
void getSides(){
System.out.print("No of sides in point is : infinite\n");
}
}
// ELLIPSE
class ellipse extends Shape{
ellipse(){
setShape("An ellipse is a circle that has been stretched in one direction, to
give it the shape of an oval.");
}
void calcArea(){
System.out.print("I will not calculate the area of ellipse. It needs extreme
hard work\n");
10
Gaurav 2820109
void getSides(){
System.out.print("No of sides in point is : infinite\n");
}
}
// TRIANGLE
class triangle extends Shape{
triangle(){
setShape("A triangle is a three-sided polygon, which has three vertices.
The three sides are connected with each other end to end at a point, which forms
the angles of the triangle. The sum of all three angles of the triangle is equal to
180 degrees.");
}
void calcArea(){
setSides(8, 5);
System.out.print("Area is: "+ 0.5*lengthOfSide1*lengthOfSide2);
}
void getSides(){
System.out.print("No of sides in point is : "+3+"\n");
}
}
// POLYGENETIC
class polygenetic extends Shape{
polygenetic(){
setShape("Polygenetic is similar to bell-shaped.");
}
void calcArea(){
System.out.print("Do you know what you are asking me to do?\n");
}
11
Gaurav 2820109
void getSides(){
System.out.print("No of sides in point is : infinite\n");
}
}
Output: -
12
Gaurav 2820109
PROGRAM NO. - 4
13
Gaurav 2820109
import java.io.*;
import java.util.*;
class Lab4
{
public static void main(String args[])
{
System.out.println("\n\nGaurav -- 2820109\n");
//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: -
14
Gaurav 2820109
PROGRAM NO. - 5
import java.io.*;
15
Gaurav 2820109
interface StackOperation
{
public void push(int i);
public void pop();
16
Gaurav 2820109
Output: -
17
Gaurav 2820109
PROGRAM NO. - 6
Develop two different classes that implement this interface. One using array and other
using linked list.
18
Gaurav 2820109
import java.io.*;
interface Mystack
{
public void pop();
public void push();
public void display();
}
}
}
class Link
{
public int data;
public Link nextLink;
public Link(int d)
{
data= d;
nextLink=null;
}
public void printLink()
{
System.out.print(" --> "+data);
}
}
20
Gaurav 2820109
{
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);
}
}
21
Gaurav 2820109
22
Gaurav 2820109
break;
case 3:
stk1.display();
break;
default:
System.exit(0);
}
}
while(ch<5);
}
}
23
Gaurav 2820109
Output: -
24
Gaurav 2820109
PROGRAM NO. - 7
Develop a simple paint like program that can draw basic graphical primitives.
25
Gaurav 2820109
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A simple applet where the user can sketch curves in a variety of
* colors. A color palette is shown along the right edge of the applet.
* The user can select a drawing color by clicking on a color in the
* palette. Under the colors is a "Clear button" that the user
* can press to clear the sketch. The user draws by clicking and
* dragging in a large white area that occupies most of the applet.
* The user's drawing is not persistent. It is lost whenever
* the applet is repainted for any reason.
* <p>The drawing that is done in this example violates the rule
* that all drawing should be done in the paintComponent() method.
* Although it works, it is NOT good style.
* <p>This class also contains a main program, and it can be run as
* a stand-alone application that has exactly the same functionality
* as the applet.
*/
public class SimplePaint extends JApplet {
/**
* The main routine opens a window that displays a drawing area
* and color palette. This main routine allows this class to
* be run as a stand-alone application as well as as an applet.
* The main routine has nothing to do with the function of this
* class as an applet.
*/
public static void main(String[] args) {
JFrame window = new JFrame("Simple Paint");
SimplePaintPanel content = new SimplePaintPanel();
window.setContentPane(content);
window.setSize(600,480);
window.setLocation(100,100);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
/**
* The init method of the applet simply creates a SimplePaintPanel and
* uses it as the content pane of the applet. All the work is done
* by the SimplePaintPanel.
26
Gaurav 2820109
*/
public void init() {
setContentPane( new SimplePaintPanel() );
}
/**
* A simple paint panel contains a large white drawing surface where
* the user can draw curves and a color palette that the user can click
* to select the color to be used for drawing. When this class is used
* as an applet, the content pane of the applet is a SimplePaintPanel.
* When this class is run as a standalone application, the content pane
* is a SimplePaintPanel. All the real work is done in the
* SimplePaintPanel class.
*/
public static class SimplePaintPanel extends JPanel
implements MouseListener, MouseMotionListener {
/**
* Some constants to represent the color selected by the user.
*/
private final static int BLACK = 0,
RED = 1,
GREEN = 2,
BLUE = 3,
CYAN = 4,
MAGENTA = 5,
YELLOW = 6;
private int currentColor = BLACK; // The currently selected drawing color,
// coded as one of the above constants.
/* The following variables are used when the user is sketching a
curve while dragging a mouse. */
private int prevX, prevY; // The previous location of the mouse.
private boolean dragging; // This is set to true while the user is drawing.
private Graphics graphicsForDrawing; // A graphics context for the panel
// that is used to draw the user's curve.
/**
* Constructor for SimplePaintPanel class sets the background color to be
27
Gaurav 2820109
28
Gaurav 2820109
29
Gaurav 2820109
return;
/* Remove the hilite from the current color, by drawing over it in gray.
Then change the current drawing color and draw a hilite around the
new drawing color. */
Graphics g = getGraphics();
g.setColor(Color.GRAY);
g.drawRect(width-55, 1 + currentColor*colorSpacing, 53, colorSpacing);
g.drawRect(width-54, 2 + currentColor*colorSpacing, 51, colorSpacing-2);
currentColor = newColor;
g.setColor(Color.WHITE);
g.drawRect(width-55, 1 + currentColor*colorSpacing, 53, colorSpacing);
g.drawRect(width-54, 2 + currentColor*colorSpacing, 51, colorSpacing-2);
g.dispose();
} // end changeColor()
/**
* This routine is called in mousePressed when the user clicks on the drawing area.
* It sets up the graphics context, graphicsForDrawing, to be used to draw the
user's
* sketch in the current color.
*/
private void setUpDrawingGraphics() {
graphicsForDrawing = getGraphics();
switch (currentColor) {
case BLACK:
graphicsForDrawing.setColor(Color.BLACK);
break;
case RED:
graphicsForDrawing.setColor(Color.RED);
break;
case GREEN:
graphicsForDrawing.setColor(Color.GREEN);
break;
case BLUE:
graphicsForDrawing.setColor(Color.BLUE);
break;
case CYAN:
graphicsForDrawing.setColor(Color.CYAN);
break;
case MAGENTA:
graphicsForDrawing.setColor(Color.MAGENTA);
break;
30
Gaurav 2820109
case YELLOW:
graphicsForDrawing.setColor(Color.YELLOW);
break;
}
} // end setUpDrawingGraphics()
/**
* This is called when the user presses the mouse anywhere in the applet.
* There are three possible responses, depending on where the user clicked:
* Change the current color, clear the drawing, or start drawing a curve.
* (Or do nothing if user clicks on the border.)
*/
public void mousePressed(MouseEvent evt) {
int x = evt.getX(); // x-coordinate where the user clicked.
int y = evt.getY(); // y-coordinate where the user clicked.
int width = getWidth(); // Width of the panel.
int height = getHeight(); // Height of the panel.
if (dragging == true) // Ignore mouse presses that occur
return; // when user is already drawing a curve.
// (This can happen if the user presses
// two mouse buttons at the same time.)
if (x > width - 53) {
// User clicked to the right of the drawing area.
// This click is either on the clear button or
// on the color palette.
if (y > height - 53)
repaint(); // Clicked on "CLEAR button".
else
changeColor(y); // Clicked on the color palette.
}
else if (x > 3 && x < width - 56 && y > 3 && y < height - 3) {
// The user has clicked on the white drawing area.
// Start drawing a curve from the point (x,y).
prevX = x;
prevY = y;
dragging = true;
setUpDrawingGraphics();
}
} // end mousePressed()
31
Gaurav 2820109
/**
* Called whenever the user releases the mouse button. If the user was drawing
* a curve, the curve is done, so we should set drawing to false and get rid of
* the graphics context that we created to use during the drawing.
*/
public void mouseReleased(MouseEvent evt) {
if (dragging == false)
return; // Nothing to do because the user isn't drawing.
dragging = false;
graphicsForDrawing.dispose();
graphicsForDrawing = null;
}
/**
* Called whenever the user moves the mouse while a mouse button is held down.
* If the user is drawing, draw a line segment from the previous mouse location
* to the current mouse location, and set up prevX and prevY for the next call.
* Note that in case the user drags outside of the drawing area, the values of
* x and y are "clamped" to lie within this area. This avoids drawing on the color
* palette or clear button.
*/
public void mouseDragged(MouseEvent evt) {
if (dragging == false)
return; // Nothing to do because the user isn't drawing.
int x = evt.getX(); // x-coordinate of mouse.
int y = evt.getY(); // y-coordinate of mouse.
if (x < 3) // Adjust the value of x,
x = 3; // to make sure it's in
if (x > getWidth() - 57) // the drawing area.
x = getWidth() - 57;
if (y < 3) // Adjust the value of y,
y = 3; // to make sure it's in
if (y > getHeight() - 4) // the drawing area.
y = getHeight() - 4;
graphicsForDrawing.drawLine(prevX, prevY, x, y); // Draw the line.
prevX = x; // Get ready for the next line segment in the curve.
prevY = y;
32
Gaurav 2820109
} // end mouseDragged()
public void mouseEntered(MouseEvent evt) { } // Some empty routines.
public void mouseExited(MouseEvent evt) { } // (Required by the MouseListener
public void mouseClicked(MouseEvent evt) { } // and MouseMotionListener
public void mouseMoved(MouseEvent evt) { } // interfaces).
} // End class SimplePaintPanel
Output: -
33
Gaurav 2820109
PROGRAM NO. - 8
34
Gaurav 2820109
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
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() {
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);
}
JPanel pan = new JPanel();
35
Gaurav 2820109
36
Gaurav 2820109
action();
textfield.setText("");
}
else
{
number = true;
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();
}
}
}
}
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);
}
}
}
37
Gaurav 2820109
Output: -
38
Gaurav 2820109
39
Gaurav 2820109
PROGRAM NO. - 9
Develop a template for linked list class along with its members in Java.
class Node{
Node next;
int data;
Node(int data){
this.data = data;
next = null;
}
}
class LinkedList{
Node head;
public static LinkedList insert(LinkedList list, int data){
Node new_node = new Node(data);
new_node.next = null;
if(list.head == null){
list.head = new_node;
}
else{
Node last = list.head;
while(last.next != null){
last = last.next;
}
last.next = new_node;
}
return list;
}
41
Gaurav 2820109
Output: -
PROGRAM NO. - 10
@WebServlet(“/MovieServlet”)
Public class MovieServlet extends HttpServlet
42
Gaurav 2820109
44
Gaurav 2820109
Output: -
45
Gaurav 2820109
46