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

Java 268

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

Program-01

Program-Write a java package with stack and queue classes.


Code-
public class ArrayQueue {

private E[] data;

private int frontIndex;

private int queueSize;}

public ArrayQueue(int capacity) {

data = (E[]) new Object[capacity];

queueSize = 0;

frontIndex = 0;}

public ArrayQueue() {

this(1000);}

public int size() {

return queueSize;}

public boolean isEmpty() {

return (queueSize == 0);}

public void enqueue(E e) throws IllegalStateException {

if (queueSize == data.length)

throw new IllegalStateException("Queue is full");

int avail = (frontIndex + queueSize) % data.length;

data[avail] = e;

queueSize++;}

1|Page
public E first() throws IllegalStateException {

if (queueSize == data.length)

throw new IllegalStateException("Queue is empty");

return data[frontIndex];}

public E dequeue() throws IllegalStateException {

if (queueSize == data.length)

throw new IllegalStateException("Queue is empty");

E answer = data[frontIndex];

data[frontIndex] = null;

frontIndex = (frontIndex + 1) % data.length;

queueSize--;

return answer;}

public static void main(String[] args) {

System.out.println("Ishu kumar 2821268");

ArrayQueue queue = new ArrayQueue();

queue.enqueue(18);

System.out.println("Element at front : " + queue.first());

System.out.println("Element removed from front : " + queue.dequeue());

System.out.println("Queue is Empty : " + queue.isEmpty());

queue.enqueue(79);

queue.enqueue(90);

System.out.println("Size of the queue : " + queue.size());

System.out.println("Element removed from front end : " +


queue.dequeue());}}

2|Page
Output-

public class ArrayStack<E> {

public static final int CAPACITY = 1000;

private int topIndex;

private E[] data;}

public ArrayStack() {

this(CAPACITY);}

public ArrayStack(int capacity) {

topIndex = -1;

data = (E[]) new Object[capacity]; }

public int size() {

return (topIndex + 1);}

public boolean empty() {

return (topIndex == -1);}

public void push(E e) throws IllegalStateException {

if (size() == data.length)

throw new IllegalStateException("Stack is full");

data[++topIndex] = e;}

public E peek() throws EmptyStackException {

3|Page
if (empty())

throw new EmptyStackException();

return data[topIndex];}

public E pop() throws EmptyStackException {

if (empty())

throw new EmptyStackException();

E answer = data[topIndex];

data[topIndex] = null;

topIndex--;

return answer;}

public static void main(String args[]) {

System.out.println("Ishu kumar 2821268");

ArrayStack<Integer> mystack = new ArrayStack<>();

mystack.push(9);

mystack.push(3);

mystack.push(8);

System.out.println("Element at the top is :" + mystack.peek());

System.out.println("Element removed is : " + mystack.pop());

System.out.println("The size of the stack is : " + mystack.size());

System.out.println("Element removed is : " + mystack.pop());

System.out.println("Element at the top is : " + mystack.peek());

mystack.push(10);

System.out.println("Stack is empty : " + mystack.empty()); }}

4|Page
Output-

5|Page
Program-02
Program-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.

Code-
class Complex {

int real, imaginary;

Complex() {}

Complex(int tempReal, int tempImaginary) {

real = tempReal;

imaginary = tempImaginary;}

Complex addComp(Complex C1, Complex C2) {

Complex temp = new Complex();

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

temp.imaginary = C1.imaginary + C2.imaginary;

return temp;}

Complex subtractComp(Complex C1, Complex C2) {

Complex temp = new Complex();

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

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

return temp;}

void printComplexNumber() {

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

public classGFG {

6|Page
public static void main(String[] args) {

System.out.println("Ishu kumar 2821268");

Complex C1 = new Complex(3, 2);

C1.printComplexNumber();

Complex C2 = new Complex(9, 5);

C2.printComplexNumber();

Complex C3 = new Complex();

C3 = C3.addComp(C1, C2);

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

C3.printComplexNumber();

C3 = C3.subtractComp(C1, C2);

System.out.print("Difference of ");

C3.printComplexNumber();}

Output-

7|Page
Program-03
Program-Develop with suitable hierarchy, class for point, shape
rectangle, square, circle, ellipse, triangle,polygenetic.

Code-
class point {

void display() {

System.out.println("All shapes are formed with combination of points.");}}

class shape extends point {

void display() {

System.out.println("There are various types of shapes.");}

void printArea(double l1, double l2) }

class rectangle extends shape {

void display() {

System.out.println("This is rectangle.");}

void printArea(double l, double b) {

double area = l * b;

System.out.println("Area of rectangle is : " + area);}}

class square extends shape {

void display() {

System.out.println("This is square.");}

void printArea(double s, double x) {

double area = s * s;

System.out.println("Area of square is : " + area);}}

class circle extends shape {

8|Page
void display() {

System.out.println("This is circle.");}

void printArea(double r, double x) {

double area = 3.14 * r * r;

System.out.println("Area of circle is : " + area);}}

class ellipse extends shape {

void display() {

System.out.println("This is ellipse.");}

void printArea(double a, double b) {

double area = 3.14 * a * b;

System.out.println("Area of ellipse is : " + area);}}

class triangle extends shape {

void display() {

System.out.println("This is triangle.");}

void printArea(double b, double h) {

double area = (b * h) / 2;

System.out.println("Area of triangle is : " + area);}}

class findArea {

public static void main(String[] args) {

System.out.println("Ishu kumar 2821268");

point p = new point();

p.display();

shape s = new shape();

9|Page
s.display();

rectangle r = new rectangle();

r.display();

r.printArea(2, 6);

square sq = new square();

sq.display();

sq.printArea(5, 1);

circle c = new circle();

c.display();

c.printArea(4, 1);

ellipse e = new ellipse();

e.display();

e.printArea(5, 3);

triangle t = new triangle();

t.display();

t.printArea(9, 1);}

}Output-

10 | P a g e
Program-04
Program-Design a Simple test application to demonstrate dynamic
polymorphism.

Code-
class TestElement {

public void displayTest() {

System.out.println("This is a generic text element.");}}

class PlainTest extends TestElement {

public void displayTest() {

System.out.println("This is a plain test.");}}

class HeadingTest extends TestElement {

public void displayTest() {

System.out.println("This is a heading test.");}}

public class TestApplication {

public static void main(String[] args) {

System.out.println("Ishu kumar 2821268");

TestElement test1 = new PlainTest();

TestElement test2 = new HeadingTest();

test1.displayTest();

test2.displayTest(); }}

Output-

11 | P a g e
12 | P a g e

You might also like