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

Java Ans

The document contains 6 Java code examples demonstrating exception handling: 1. A program that reads an integer, checks if it's negative, and throws an exception with a message if so. 2. A program that reads an array of integers, checks if any are out of range [0-100], and throws an exception with a message if so. 3. A program that reads an email, checks if it's valid format, and throws an exception with a message if invalid. 4. A program that reads a file name, checks if the file exists, and throws an exception with a message if not found. 5. A program that divides two numbers, checks if the divisor is

Uploaded by

Ch Subhash
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)
8 views

Java Ans

The document contains 6 Java code examples demonstrating exception handling: 1. A program that reads an integer, checks if it's negative, and throws an exception with a message if so. 2. A program that reads an array of integers, checks if any are out of range [0-100], and throws an exception with a message if so. 3. A program that reads an email, checks if it's valid format, and throws an exception with a message if invalid. 4. A program that reads a file name, checks if the file exists, and throws an exception with a message if not found. 5. A program that divides two numbers, checks if the divisor is

Uploaded by

Ch Subhash
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. Write a Java program that reads an integer from the user and checks if it is negative.

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;

public class ExceptionHandling {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = sc.nextInt();
try {
if (num < 0) {
throw new Exception("The number cannot be negative");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

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 RangeException extends Exception {


public RangeException(String message) {
super(message);
}
}

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 InvalidEmailException extends Exception {


public InvalidEmailException(String message) {
super(message);
}
}

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;

public class FileCheck {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the file name: ");
String fileName = sc.nextLine();

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;

public class DivideByZero {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the numerator: ");
int numerator = sc.nextInt();
System.out.print("Enter the denominator: ");
int denominator = sc.nextInt();

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();
}

class Rectangle implements Shape {


private double width;
private double height;

public Rectangle(double width, double height) {


this.width = width;
this.height = height;
}

@Override
public double calculateArea() {
return width * height;
}

@Override
public double calculatePerimeter() {
return 2 * (width + height);
}
}

class Circle implements Shape {


private double radius;

public Circle(double radius) {


this.radius = radius;
}

@Override
public double calculateArea() {
return Math.PI * radius * radius;
}

@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}

You might also like