1 Java
1 Java
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
int sum = 0;
sum += number;
}
```
**Simulated Output:**
```
```
### 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
if (numbers.length == 0) {
System.out.println("Array is empty.");
return;
max = numbers[i];
min = numbers[i];
```
**Simulated Output:**
```
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
if (number == elementToFind) {
found = true;
} else {
if (number == anotherElementToFind) {
found = true;
break;
if (found) {
} else {
```
**Simulated Output:**
```
```
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
this.capacity = capacity;
}
// Push operation: Adds an element to the top of the stack
if (top == capacity - 1) {
} else {
stackArray[++top] = item;
// Pop operation: Removes and returns the element from the top of the stack
if (top == -1) {
} else {
return poppedItem;
// Display operation: Prints all elements in the stack from top to bottom
System.out.println("Stack is empty.");
return;
System.out.println();
stack.push(10);
stack.push(20);
stack.display();
stack.push(30);
stack.pop();
stack.display();
stack.push(40);
stack.push(50);
stack.display();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.display();
```
**Simulated Output:**
```
Pushed: 10
Pushed: 20
Pushed: 30
Popped: 30
Pushed: 40
Pushed: 50
Popped: 50
Popped: 40
Popped: 20
Popped: 10
Stack Underflow! Stack is empty.
Stack is empty.
```
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
this.capacity = capacity;
front = 0;
rear = -1;
currentSize = 0;
if (currentSize == capacity) {
} else {
queueArray[rear] = item;
currentSize++;
// Dequeue operation: Removes and returns the element from the front of the queue
if (currentSize == 0) {
} else {
currentSize--;
return dequeuedItem;
}
// Display operation: Prints all elements in the queue from front to rear
if (currentSize == 0) {
System.out.println("Queue is empty.");
return;
System.out.println();
queue.enqueue(10);
queue.enqueue(20);
queue.display();
queue.dequeue();
queue.enqueue(30);
queue.enqueue(40);
queue.display();
queue.enqueue(50);
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.display();
```
**Simulated Output:**
```
Enqueued: 10
Enqueued: 20
Dequeued: 10
Enqueued: 30
Enqueued: 40
Enqueued: 50
Dequeued: 20
Dequeued: 30
Dequeued: 40
Dequeued: 50
Queue is empty.
```