2nd and 3rd exp
2nd and 3rd exp
import java.util.Scanner;
int n = scanner.nextInt();
// Get the values from the user and store them in the array
arr[i] = scanner.nextInt();
}
// Initialize largest and smallest elements with the first element of the array
// Loop through the array to find the largest and smallest elements
largest = arr[i];
smallest = arr[i];
scanner.close();
2nd of 2
import java.util.Scanner;
while (true) {
System.out.println("\nMenu:");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
if (choice == 5) {
System.out.println("Exiting...");
break;
switch (choice) {
case 1:
// String Concatenation
break;
case 2:
// String Reversal
break;
case 3:
// StringBuffer Concatenation
sb1.append(sb2);
case 4:
// StringBuffer Reversal
break;
default:
scanner.close();
3rd of 2
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
stack.add(element);
public T pop() {
if (stack.isEmpty()) {
System.out.println("Stack is empty.");
return null;
public T peek() {
if (stack.isEmpty()) {
System.out.println("Stack is empty.");
return null;
System.out.print("Stack: ");
while (iterator.hasNext()) {
System.out.println();
queue.add(element);
}
// Dequeue element from queue
public T dequeue() {
if (queue.isEmpty()) {
System.out.println("Queue is empty.");
return null;
return queue.remove(0);
public T peek() {
if (queue.isEmpty()) {
System.out.println("Queue is empty.");
return null;
return queue.get(0);
System.out.print("Queue: ");
while (iterator.hasNext()) {
System.out.println();
while (true) {
System.out.println("\nMenu:");
System.out.println("9. Exit");
if (choice == 9) {
System.out.println("Exiting program...");
break;
switch (choice) {
case 1:
stack.push(stackElement);
break;
case 2:
if (poppedElement != null) {
break;
case 3:
if (peekedStackElement != null) {
System.out.println("Top element of stack: " + peekedStackElement);
break;
case 4:
stack.display();
break;
case 5:
queue.enqueue(queueElement);
break;
case 6:
if (dequeuedElement != null) {
break;
case 7:
if (peekedQueueElement != null) {
break;
case 8:
queue.display();
break;
default:
}
scanner.close();
1st of 3
import java.util.Scanner;
class Box {
double length;
double width;
double height;
box.length = scanner.nextDouble();
box.width = scanner.nextDouble();
scanner.close();
2nd of 3
import java.util.Scanner;
class Box {
double length;
double width;
double height;
this.length = length;
this.width = width;
this.height = height;
scanner.close();
3rd of 3
import java.util.Scanner;
class Box {
double length;
double width;
double height;
this.length = length;
this.width = width;
this.height = height;
}
System.out.println("Length: " + this.length + ", Width: " + this.width + ", Height: " + this.height);
box1.display();
box2.display();
combinedBox.display();
scanner.close();
4th of 3
import java.util.Scanner;
class Box {
double width;
double height;
this.length = length;
this.width = width;
this.height = height;
System.out.println("Length: " + this.length + ", Width: " + this.width + ", Height: " + this.height);
double totalVolume = 0;
totalVolume += box.calculateVolume();
return totalVolume;
}
public class Main {
System.out.println("\nEnter the dimensions of the third box (optional, leave 0 for all dimensions if not needed):");
Box box3 = (length3 != 0 && width3 != 0 && height3 != 0) ? new Box(length3, width3, height3) : null;
if (box3 != null) {
System.out.println("\nTotal volume of all boxes: " + Box.totalVolume(box1, box2, (box3 != null) ? box3 : new Box(0, 0, 0)));
scanner.close();