0% found this document useful (0 votes)
2 views14 pages

1 Java

The document presents five Java programs that demonstrate fundamental array operations and implementations of data structures like Stack and Queue using arrays. Each program includes source code and simulated output, covering tasks such as calculating the sum of array elements, finding the largest and smallest elements, searching for an element, and implementing stack and queue operations. The examples illustrate key concepts in Java programming related to arrays and data structure management.

Uploaded by

Emediong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

1 Java

The document presents five Java programs that demonstrate fundamental array operations and implementations of data structures like Stack and Queue using arrays. Each program includes source code and simulated output, covering tasks such as calculating the sum of array elements, finding the largest and smallest elements, searching for an element, and implementing stack and queue operations. The examples illustrate key concepts in Java programming related to arrays and data structure management.

Uploaded by

Emediong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Here are five Java programs demonstrating fundamental array operations and implementations of

common data structures (Stack and Queue) using arrays. Each program includes the Java source code
and a simulated output from a sample run.

### 1. Java Program to Find the Sum of All Elements in an Integer Array

This program calculates the sum of all integers stored in an array by iterating through each element and
adding it to a running total.

```java

public class ArraySum {

public static void main(String[] args) {

int[] numbers = {10, 20, 30, 40, 50};

int sum = 0;

for (int number : numbers) {

sum += number;

System.out.println("The array elements are: ");

for (int i = 0; i < numbers.length; i++) {

System.out.print(numbers[i] + (i == numbers.length - 1 ? "" : ", "));

System.out.println("\nSum of all elements in the array: " + sum);

}
```

**Simulated Output:**

```

The array elements are: 10, 20, 30, 40, 50

Sum of all elements in the array: 150

```

### 2. Java Program to Find the Largest and Smallest Elements in an Integer Array

This program determines the maximum and minimum values within an integer array by comparing each
element with the current largest and smallest found so far.

```java

public class ArrayMinMax {

public static void main(String[] args) {

int[] numbers = {15, 7, 23, 9, 42, 1};

if (numbers.length == 0) {

System.out.println("Array is empty.");

return;

int max = numbers[0];

int min = numbers[0];


for (int i = 1; i < numbers.length; i++) {

if (numbers[i] > max) {

max = numbers[i];

if (numbers[i] < min) {

min = numbers[i];

System.out.println("The array elements are: ");

for (int i = 0; i < numbers.length; i++) {

System.out.print(numbers[i] + (i == numbers.length - 1 ? "" : ", "));

System.out.println("\nLargest element: " + max);

System.out.println("Smallest element: " + min);

```

**Simulated Output:**

```

The array elements are: 15, 7, 23, 9, 42, 1

Largest element: 42

Smallest element: 1

```
### 3. Java Program to Check if a Given Element Exists in an Array

This program searches for a specific element within an array and reports whether it is found or not.

```java

public class ArrayElementSearch {

public static void main(String[] args) {

int[] numbers = {5, 12, 8, 20, 3, 15};

int elementToFind = 8; // Element to search for

boolean found = false;

for (int number : numbers) {

if (number == elementToFind) {

found = true;

break; // Element found, no need to continue searching

System.out.println("The array elements are: ");

for (int i = 0; i < numbers.length; i++) {

System.out.print(numbers[i] + (i == numbers.length - 1 ? "" : ", "));

System.out.println("\nSearching for element: " + elementToFind);


if (found) {

System.out.println(elementToFind + " exists in the array.");

} else {

System.out.println(elementToFind + " does not exist in the array.");

// --- Another example ---

int anotherElementToFind = 10;

found = false; // Reset for the new search

for (int number : numbers) {

if (number == anotherElementToFind) {

found = true;

break;

System.out.println("\nSearching for element: " + anotherElementToFind);

if (found) {

System.out.println(anotherElementToFind + " exists in the array.");

} else {

System.out.println(anotherElementToFind + " does not exist in the array.");

```
**Simulated Output:**

```

The array elements are: 5, 12, 8, 20, 3, 15

Searching for element: 8

8 exists in the array.

Searching for element: 10

10 does not exist in the array.

```

### 4. Java Program to Implement a Stack Using an Array

This program implements a basic Last-In, First-Out (LIFO) stack data structure using an array. It includes
`push` (add element), `pop` (remove element), and `display` operations.

```java

public class ArrayStack {

private int[] stackArray;

private int top; // Index of the top element

private int capacity; // Maximum capacity of the stack

public ArrayStack(int capacity) {

this.capacity = capacity;

stackArray = new int[capacity];

top = -1; // Stack is empty when top is -1

}
// Push operation: Adds an element to the top of the stack

public void push(int item) {

if (top == capacity - 1) {

System.out.println("Stack Overflow! Cannot push " + item);

} else {

stackArray[++top] = item;

System.out.println("Pushed: " + item);

// Pop operation: Removes and returns the element from the top of the stack

public int pop() {

if (top == -1) {

System.out.println("Stack Underflow! Stack is empty.");

return -1; // Or throw an exception

} else {

int poppedItem = stackArray[top--];

System.out.println("Popped: " + poppedItem);

return poppedItem;

// Display operation: Prints all elements in the stack from top to bottom

public void display() {


if (top == -1) {

System.out.println("Stack is empty.");

return;

System.out.print("Stack elements (Top to Bottom): ");

for (int i = top; i >= 0; i--) {

System.out.print(stackArray[i] + " ");

System.out.println();

public static void main(String[] args) {

ArrayStack stack = new ArrayStack(5); // Create a stack with capacity 5

stack.push(10);

stack.push(20);

stack.display();

stack.push(30);

stack.pop();

stack.display();

stack.push(40);

stack.push(50);

stack.push(60); // This will cause Stack Overflow

stack.display();

stack.pop();
stack.pop();

stack.pop();

stack.pop();

stack.pop(); // This will cause Stack Underflow

stack.display();

```

**Simulated Output:**

```

Pushed: 10

Pushed: 20

Stack elements (Top to Bottom): 20 10

Pushed: 30

Popped: 30

Stack elements (Top to Bottom): 20 10

Pushed: 40

Pushed: 50

Stack Overflow! Cannot push 60

Stack elements (Top to Bottom): 50 40 20 10

Popped: 50

Popped: 40

Popped: 20

Popped: 10
Stack Underflow! Stack is empty.

Stack is empty.

```

### 5. Java Program to Implement a Queue Using an Array

This program implements a basic First-In, First-Out (FIFO) queue data structure using an array. It
includes `enqueue` (add element), `dequeue` (remove element), and `display` operations.

```java

public class ArrayQueue {

private int[] queueArray;

private int front; // Index of the front element

private int rear; // Index of the rear element

private int capacity; // Maximum capacity of the queue

private int currentSize; // Current number of elements in the queue

public ArrayQueue(int capacity) {

this.capacity = capacity;

queueArray = new int[capacity];

front = 0;

rear = -1;

currentSize = 0;

// Enqueue operation: Adds an element to the rear of the queue


public void enqueue(int item) {

if (currentSize == capacity) {

System.out.println("Queue Overflow! Cannot enqueue " + item);

} else {

rear = (rear + 1) % capacity; // Circular increment for rear

queueArray[rear] = item;

currentSize++;

System.out.println("Enqueued: " + item);

// Dequeue operation: Removes and returns the element from the front of the queue

public int dequeue() {

if (currentSize == 0) {

System.out.println("Queue Underflow! Queue is empty.");

return -1; // Or throw an exception

} else {

int dequeuedItem = queueArray[front];

front = (front + 1) % capacity; // Circular increment for front

currentSize--;

System.out.println("Dequeued: " + dequeuedItem);

return dequeuedItem;

}
// Display operation: Prints all elements in the queue from front to rear

public void display() {

if (currentSize == 0) {

System.out.println("Queue is empty.");

return;

System.out.print("Queue elements (Front to Rear): ");

for (int i = 0; i < currentSize; i++) {

System.out.print(queueArray[(front + i) % capacity] + " ");

System.out.println();

public static void main(String[] args) {

ArrayQueue queue = new ArrayQueue(5); // Create a queue with capacity 5

queue.enqueue(10);

queue.enqueue(20);

queue.display();

queue.dequeue();

queue.enqueue(30);

queue.enqueue(40);

queue.display();

queue.enqueue(50);

queue.enqueue(60); // This will cause Queue Overflow


queue.display();

queue.dequeue();

queue.dequeue();

queue.dequeue();

queue.dequeue();

queue.dequeue(); // This will cause Queue Underflow

queue.display();

```

**Simulated Output:**

```

Enqueued: 10

Enqueued: 20

Queue elements (Front to Rear): 10 20

Dequeued: 10

Enqueued: 30

Enqueued: 40

Queue elements (Front to Rear): 20 30 40

Enqueued: 50

Queue Overflow! Cannot enqueue 60

Queue elements (Front to Rear): 20 30 40 50

Dequeued: 20

Dequeued: 30
Dequeued: 40

Dequeued: 50

Queue Underflow! Queue is empty.

Queue is empty.

```

You might also like