0% found this document useful (0 votes)
11 views

Assignment

The document contains four Java programs demonstrating different functionalities. The first program finds the minimum and maximum values from user-inputted numbers, the second manages an ArrayList by adding and removing elements, the third calculates the sum of a 3x3 matrix, and the fourth sorts a list of integers both in ascending and descending order. Each program utilizes basic Java constructs such as arrays, loops, and collections.

Uploaded by

c43311990
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)
11 views

Assignment

The document contains four Java programs demonstrating different functionalities. The first program finds the minimum and maximum values from user-inputted numbers, the second manages an ArrayList by adding and removing elements, the third calculates the sum of a 3x3 matrix, and the fourth sorts a list of integers both in ascending and descending order. Each program utilizes basic Java constructs such as arrays, loops, and collections.

Uploaded by

c43311990
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/ 4

1.

import java.util.Scanner;

public class MinMaxFinder {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] numbers = new int[n];

System.out.println("Enter " + n + " elements:");


for (int i = 0; i < n; i++) {
numbers[i] = scanner.nextInt();
}
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;

for (int num : numbers) {


if (num < min) {
min = num;
}
if (num > max) {
max = num;
}
}
System.out.println("Smallest number: " + min);
System.out.println("Largest number: " + max);

scanner.close();
}
}
2.
import java.util.ArrayList;

public class IntegerArrayList {


public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();

for (int i = 1; i <= 10; i++) {


list.add(i);
}

// Remove the element at index 4


if (list.size() > 4) {
list.remove(4);
}

System.out.println("Updated List: " + list);


}
}
3.
public class SumOf2DArray {
public static void main(String[] args) {
int[][] boxNumbers = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int sum = 0;

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


for (int j = 0; j < 3; j++) {
sum += boxNumbers[i][j];
}
}

System.out.println("Sum of all elements in the 3x3 matrix: " +


sum);
}
}
4.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class ArrayListSortingUserInput {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<>();

System.out.println("Enter 10 integers:");

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


numbers.add(scanner.nextInt());
}
System.out.println("Original List: " + numbers);

Collections.sort(numbers);
System.out.println("Sorted List (Ascending): " + numbers);

Collections.reverse(numbers);
System.out.println("Sorted List (Descending): " + numbers);

scanner.close();
}
}

You might also like