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