0% found this document useful (0 votes)
3 views4 pages

Aman jAVA

The document contains two Java programs: one for accessing array elements based on user input and another for performing mathematical operations on command line arguments. The first program handles exceptions related to array access and number formatting, while the second program calculates the sum and average of five integers provided as arguments. Both programs demonstrate error handling and user interaction in Java.

Uploaded by

aaditya Rajak
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)
3 views4 pages

Aman jAVA

The document contains two Java programs: one for accessing array elements based on user input and another for performing mathematical operations on command line arguments. The first program handles exceptions related to array access and number formatting, while the second program calculates the sum and average of five integers provided as arguments. Both programs demonstrate error handling and user interaction in Java.

Uploaded by

aaditya Rajak
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

ASSIGNMENT

Aman Tomar
BCAN1CA22097

1). import java.util.Scanner;

public class ArrayElementAccess {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

try {
System.out.println("Enter the number of elements in the array");
int n = Integer.parseInt(scanner.nextLine());

int[] array = new int[n];


System.out.println("Enter the elements in the array");

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


array[i] = Integer.parseInt(scanner.nextLine());
}

System.out.println("Enter the index of the array element you want to access");


int index = Integer.parseInt(scanner.nextLine());

System.out.println("The array element at index " + index + " = " + array[index]);


System.out.println("The array element successfully accessed");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("java.lang.ArrayIndexOutOfBoundsException");
} catch (NumberFormatException e) {
System.out.println("java.lang.NumberFormatException");
}
}
}

OUTPUT:
Enter the number of elements in the array
2
Enter the elements in the array
50
80
Enter the index of the array element you want to access
1
The array element at index 1 = 80
The array element successfully accessed
2). public class MathOperation {
public static void main(String[] args) {
try {
// Check if 5 arguments are provided
if (args.length != 5) {
throw new IllegalArgumentException("Exactly 5 integers are required.");
}

// Initialize the array


int[] numbers = new int[5];
int sum = 0;

// Parse command line arguments and populate the array


for (int i = 0; i < 5; i++) {
numbers[i] = Integer.parseInt(args[i]);
sum += numbers[i];
}

// Calculate the average


double average = sum / 5.0;

// Display the sum and average


System.out.println("Sum of the array elements: " + sum);
System.out.println("Average of the array elements: " + average);

} catch (NumberFormatException e) {
System.out.println("java.lang.NumberFormatException: Please enter valid integers.");
} catch (ArithmeticException e) {
System.out.println("java.lang.ArithmeticException: An arithmetic error occurred.");
} catch (IllegalArgumentException e) {
System.out.println("java.lang.IllegalArgumentException: " + e.getMessage());
} catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
}
}
}

OUTPUT:
javac MathOperation.java
java MathOperation 10 20 30 40 50

Sum of the array elements: 150


Average of the array elements: 30.0

You might also like