IGNOU BCSL-043-Java Programming Lab, Latest Solved Assignment (July 2023 - January 2024 )
Q3) (b) Write a java program which take age ( in years) of two persons as input and display who is older among them. Make provisions for exception handling in the situation when age entered is either a negative value or more than 150.
Sol)
import java.util.Scanner;
public class AgeComparison {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print(“Enter age of person 1: “);
int age1 = scanner.nextInt();
// Validate age1
if (age1 < 0 || age1 > 150) {
throw new IllegalArgumentException(“Invalid age value”);
}
System.out.print(“Enter age of person 2: “);
int age2 = scanner.nextInt();
// Validate age2
if (age2 < 0 || age2 > 150) {
throw new IllegalArgumentException(“Invalid age value”);
}
if (age1 > age2) {
System.out.println(“Person 1 is older.”);
} else if (age2 > age1) {
System.out.println(“Person 2 is older.”);
} else {
System.out.println(“Both persons are of the same age.”);
}
} catch (IllegalArgumentException e) {
System.out.println(“Error: ” + e.getMessage());
} catch (Exception e) {
System.out.println(“Error: Invalid input.”);
} finally {
scanner.close();
}
}
}
Assignment Submission Last Date
The IGNOU open learning format requires students to submit study Assignments. Here is the final end date of the submission of this particular assignment according to the university calendar.
30th April (if Enrolled in the June Exams)
31st October (if Enrolled in the December Exams)