Ist of 2
import java.util.Scanner;
public class FindLargestAndSmallest {
public static void main(String[] args) {
// Create a Scanner object to take input from the console
Scanner scanner = new Scanner(System.in);
// Ask the user for the size of the array
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
// Create an array to store the input values
int[] arr = new int[n];
// Get the values from the user and store them in the array
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
// Initialize largest and smallest elements with the first element of the array
int largest = arr[0];
int smallest = arr[0];
// Loop through the array to find the largest and smallest elements
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
if (arr[i] < smallest) {
smallest = arr[i];
// Output the results
System.out.println("The largest element in the array is: " + largest);
System.out.println("The smallest element in the array is: " + smallest);
// Close the scanner to avoid resource leak
scanner.close();
2nd of 2
import java.util.Scanner;
public class SimpleStringOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nMenu:");
System.out.println("1. String Concatenation");
System.out.println("2. String Reversal");
System.out.println("3. StringBuffer Concatenation");
System.out.println("4. StringBuffer Reversal");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline left by nextInt()
if (choice == 5) {
System.out.println("Exiting...");
break;
switch (choice) {
case 1:
// String Concatenation
System.out.print("Enter first string: ");
String str1 = scanner.nextLine();
System.out.print("Enter second string: ");
String str2 = scanner.nextLine();
System.out.println("Concatenated String: " + str1 + str2);
break;
case 2:
// String Reversal
System.out.print("Enter a string to reverse: ");
String str = scanner.nextLine();
String reversedStr = new StringBuilder(str).reverse().toString();
System.out.println("Reversed String: " + reversedStr);
break;
case 3:
// StringBuffer Concatenation
System.out.print("Enter first string: ");
StringBuffer sb1 = new StringBuffer(scanner.nextLine());
System.out.print("Enter second string: ");
StringBuffer sb2 = new StringBuffer(scanner.nextLine());
sb1.append(sb2);
System.out.println("Concatenated StringBuffer: " + sb1);
break;
case 4:
// StringBuffer Reversal
System.out.print("Enter a string to reverse using StringBuffer: ");
StringBuffer sb = new StringBuffer(scanner.nextLine());
System.out.println("Reversed StringBuffer: " + sb.reverse());
break;
default:
System.out.println("Invalid choice, please try again.");
scanner.close();
3rd of 2
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class StackAndQueueUsingArrayList {
// Stack implementation using ArrayList
static class Stack<T> {
private ArrayList<T> stack = new ArrayList<>();
// Push element onto stack
public void push(T element) {
stack.add(element);
// Pop element from stack
public T pop() {
if (stack.isEmpty()) {
System.out.println("Stack is empty.");
return null;
return stack.remove(stack.size() - 1);
// Peek top element of stack
public T peek() {
if (stack.isEmpty()) {
System.out.println("Stack is empty.");
return null;
return stack.get(stack.size() - 1);
// Display stack elements using Iterator
public void display() {
Iterator<T> iterator = stack.iterator();
System.out.print("Stack: ");
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
System.out.println();
// Queue implementation using ArrayList
static class Queue<T> {
private ArrayList<T> queue = new ArrayList<>();
// Enqueue element to queue
public void enqueue(T element) {
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);
// Peek front element of queue
public T peek() {
if (queue.isEmpty()) {
System.out.println("Queue is empty.");
return null;
return queue.get(0);
// Display queue elements using Iterator
public void display() {
Iterator<T> iterator = queue.iterator();
System.out.print("Queue: ");
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
System.out.println();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Stack<Integer> stack = new Stack<>();
Queue<Integer> queue = new Queue<>();
while (true) {
System.out.println("\nMenu:");
System.out.println("1. Push (Stack)");
System.out.println("2. Pop (Stack)");
System.out.println("3. Peek (Stack)");
System.out.println("4. Display Stack");
System.out.println("5. Enqueue (Queue)");
System.out.println("6. Dequeue (Queue)");
System.out.println("7. Peek (Queue)");
System.out.println("8. Display Queue");
System.out.println("9. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
if (choice == 9) {
System.out.println("Exiting program...");
break;
switch (choice) {
case 1:
System.out.print("Enter element to push onto stack: ");
int stackElement = scanner.nextInt();
stack.push(stackElement);
break;
case 2:
Integer poppedElement = stack.pop();
if (poppedElement != null) {
System.out.println("Popped element: " + poppedElement);
break;
case 3:
Integer peekedStackElement = stack.peek();
if (peekedStackElement != null) {
System.out.println("Top element of stack: " + peekedStackElement);
break;
case 4:
stack.display();
break;
case 5:
System.out.print("Enter element to enqueue to queue: ");
int queueElement = scanner.nextInt();
queue.enqueue(queueElement);
break;
case 6:
Integer dequeuedElement = queue.dequeue();
if (dequeuedElement != null) {
System.out.println("Dequeued element: " + dequeuedElement);
break;
case 7:
Integer peekedQueueElement = queue.peek();
if (peekedQueueElement != null) {
System.out.println("Front element of queue: " + peekedQueueElement);
break;
case 8:
queue.display();
break;
default:
System.out.println("Invalid choice. Please try again.");
}
scanner.close();
1st of 3
import java.util.Scanner;
class Box {
// Attributes of the box
double length;
double width;
double height;
// Method to calculate volume of the box
public double calculateVolume() {
return length * width * height;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create an instance of the Box class
Box box = new Box();
// Taking input for dimensions of the box
System.out.print("Enter the length of the box: ");
box.length = scanner.nextDouble();
System.out.print("Enter the width of the box: ");
box.width = scanner.nextDouble();
System.out.print("Enter the height of the box: ");
box.height = scanner.nextDouble();
// Calculate and display the volume of the box
double volume = box.calculateVolume();
System.out.println("The volume of the box is: " + volume);
scanner.close();
2nd of 3
import java.util.Scanner;
class Box {
// Attributes of the box
double length;
double width;
double height;
// Constructor to initialize box dimensions
public Box(double length, double width, double height) {
// Using 'this' to refer to the instance variables
this.length = length;
this.width = width;
this.height = height;
// Method to calculate volume of the box
public double calculateVolume() {
return this.length * this.width * this.height; // Using 'this' to refer to instance variables
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Taking input for dimensions of the box
System.out.print("Enter the length of the box: ");
double length = scanner.nextDouble();
System.out.print("Enter the width of the box: ");
double width = scanner.nextDouble();
System.out.print("Enter the height of the box: ");
double height = scanner.nextDouble();
// Create an instance of the Box class using the constructor
Box box = new Box(length, width, height);
// Calculate and display the volume of the box
double volume = box.calculateVolume();
System.out.println("The volume of the box is: " + volume);
scanner.close();
3rd of 3
import java.util.Scanner;
class Box {
// Attributes of the box
double length;
double width;
double height;
// Constructor to initialize box dimensions
public Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
// Method to calculate the volume of the box
public double calculateVolume() {
return this.length * this.width * this.height;
// Method to display the dimensions of the box
public void display() {
System.out.println("Length: " + this.length + ", Width: " + this.width + ", Height: " + this.height);
// Method to add two boxes (returning a new Box object)
public Box addBoxes(Box otherBox) {
// Returning a new Box object with summed dimensions
return new Box(this.length + otherBox.length, this.width + otherBox.width, this.height + otherBox.height);
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Taking input for the first box
System.out.println("Enter the dimensions of the first box:");
System.out.print("Enter the length: ");
double length1 = scanner.nextDouble();
System.out.print("Enter the width: ");
double width1 = scanner.nextDouble();
System.out.print("Enter the height: ");
double height1 = scanner.nextDouble();
// Create the first Box object
Box box1 = new Box(length1, width1, height1);
// Taking input for the second box
System.out.println("\nEnter the dimensions of the second box:");
System.out.print("Enter the length: ");
double length2 = scanner.nextDouble();
System.out.print("Enter the width: ");
double width2 = scanner.nextDouble();
System.out.print("Enter the height: ");
double height2 = scanner.nextDouble();
// Create the second Box object
Box box2 = new Box(length2, width2, height2);
// Display the volumes of both boxes
System.out.println("\nVolume of the first box: " + box1.calculateVolume());
System.out.println("Volume of the second box: " + box2.calculateVolume());
// Display the dimensions of both boxes
System.out.println("\nDimensions of the first box:");
box1.display();
System.out.println("Dimensions of the second box:");
box2.display();
// Adding the two boxes and displaying the result
Box combinedBox = box1.addBoxes(box2);
System.out.println("\nDimensions of the combined box:");
combinedBox.display();
System.out.println("Volume of the combined box: " + combinedBox.calculateVolume());
scanner.close();
4th of 3
import java.util.Scanner;
class Box {
// Attributes of the box
double length;
double width;
double height;
// Constructor to initialize box dimensions
public Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
// Method to calculate volume of the box
public double calculateVolume() {
return this.length * this.width * this.height;
// Method to display the dimensions of the box
public void display() {
System.out.println("Length: " + this.length + ", Width: " + this.width + ", Height: " + this.height);
// Method to add two boxes (returning a new Box object)
public Box addBoxes(Box otherBox) {
return new Box(this.length + otherBox.length, this.width + otherBox.width, this.height + otherBox.height);
// Method to calculate total volume of multiple boxes using varargs
public static double totalVolume(Box... boxes) {
double totalVolume = 0;
for (Box box : boxes) {
totalVolume += box.calculateVolume();
return totalVolume;
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Taking input for the first box
System.out.println("Enter the dimensions of the first box:");
System.out.print("Enter the length: ");
double length1 = scanner.nextDouble();
System.out.print("Enter the width: ");
double width1 = scanner.nextDouble();
System.out.print("Enter the height: ");
double height1 = scanner.nextDouble();
// Create the first Box object
Box box1 = new Box(length1, width1, height1);
// Taking input for the second box
System.out.println("\nEnter the dimensions of the second box:");
System.out.print("Enter the length: ");
double length2 = scanner.nextDouble();
System.out.print("Enter the width: ");
double width2 = scanner.nextDouble();
System.out.print("Enter the height: ");
double height2 = scanner.nextDouble();
// Create the second Box object
Box box2 = new Box(length2, width2, height2);
// Taking input for the third box (optional)
System.out.println("\nEnter the dimensions of the third box (optional, leave 0 for all dimensions if not needed):");
System.out.print("Enter the length: ");
double length3 = scanner.nextDouble();
System.out.print("Enter the width: ");
double width3 = scanner.nextDouble();
System.out.print("Enter the height: ");
double height3 = scanner.nextDouble();
// Create the third Box object (if the user entered non-zero dimensions)
Box box3 = (length3 != 0 && width3 != 0 && height3 != 0) ? new Box(length3, width3, height3) : null;
// Display volumes of the boxes
System.out.println("\nVolume of the first box: " + box1.calculateVolume());
System.out.println("Volume of the second box: " + box2.calculateVolume());
// Display the third box if created
if (box3 != null) {
System.out.println("Volume of the third box: " + box3.calculateVolume());
// Using Varargs to calculate total volume of multiple boxes
System.out.println("\nTotal volume of all boxes: " + Box.totalVolume(box1, box2, (box3 != null) ? box3 : new Box(0, 0, 0)));
scanner.close();