Java Ans
Java Ans
If
it is negative, throw an exception with a message "The number cannot be negative".
Catch the exception and display the message.”
1)
import java.util.Scanner;
2. Write a Java program that reads an array of integers from the user and checks if any
of the numbers are out of the range [0, 100]. If any of the numbers are out of range,
throw an exception with a message "The number must be between 0 and 100". Catch
the exception and display the message."
java
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of integers: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the integers:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
try {
for (int i = 0; i < n; i++) {
if (arr[i] < 0 || arr[i] > 100) {
throw new RangeException("The number must be between 0 and 100");
}
}
System.out.println("All numbers are within the range [0, 100]");
} catch (RangeException e) {
System.out.println(e.getMessage());
}
}
}
3. Write a Java program that reads a string from the user and checks if it is a valid email
address. If it is not a valid email address, throw an exception with a message "The
email address is not valid". Catch the exception and display the message."
java
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an email address: ");
String email = sc.nextLine();
try {
if (!email.contains("@") || !email.contains(".")) {
throw new InvalidEmailException("The email address is not valid");
}
System.out.println("The email address is valid");
} catch (InvalidEmailException e) {
System.out.println(e.getMessage());
}
}
}
4. Write a Java program that reads a file name from the user and checks if the file exists.
If the file does not exist, throw an exception with a message "The file does not exist".
Catch the exception and display the message.
java
import java.io.*;
import java.util.Scanner;
try {
File file = new File(fileName);
if (!file.exists()) {
throw new FileNotFoundException("The file does not exist");
}
System.out.println("The file exists");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
5. Write a Java program that divides two numbers and checks if the divisor is zero. If the
divisor is zero, throw an exception with a message "The divisor cannot be zero".
Catch the exception and display the message.
java
import java.util.Scanner;
try {
if (denominator == 0) {
throw new ArithmeticException("The divisor cannot be zero");
}
int result = numerator / denominator;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
}
}
}
6. Write a Java program that creates an interface Shape with methods calculateArea and
calculatePerimeter. Implement the interface in two classes Rectangle and Circle.
java
interface Shape {
double calculateArea();
double calculatePerimeter();
}
@Override
public double calculateArea() {
return width * height;
}
@Override
public double calculatePerimeter() {
return 2 * (width + height);
}
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}