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

Experiment 9

The document contains Java code for three assignments. The first assignment performs arithmetic operations on two user-input numbers while handling exceptions. The second assignment demonstrates various exception types, and the third assignment validates a Voter-ID Card number, throwing a custom exception for invalid formats.

Uploaded by

tejasbhore24
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)
2 views

Experiment 9

The document contains Java code for three assignments. The first assignment performs arithmetic operations on two user-input numbers while handling exceptions. The second assignment demonstrates various exception types, and the third assignment validates a Voter-ID Card number, throwing a custom exception for invalid formats.

Uploaded by

tejasbhore24
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/ 6

A-12-Tejas Bhore

Java Assignment-9

(a)WAP to read two numbers from the user and perform all arithmetic
operations on these numbers. Display any exception if occurring
while performing those operations.

Code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int a,b;
Scanner sc=new Scanner(System.in);
try
{
System.out.println("Enter a and b: ");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("Addition: "+(a+b));
System.out.println("Substrction: "+(a-b));
System.out.println("Multplication: "+(a*b));
System.out.println("Division: "+(a/b));
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:

(b)WAP to demonstrate following types of exceptions:


(i) NullPointerException.
(ii) ArrayIndexOutOfBoundsException.
(iii) ClassNotFoundException
(iv) NumberFormatException
(v) StringIndexOutOfBoundsException
Code:
public class Main
{
public static void main(String[] args)
{
try
{
String str = null;
System.out.println(str.charAt(30));
}
catch(NullPointerException e)
{
System.out.println("Null Exception : "+e);
}
try
{
int t[] = new int[2];
t[7]=5;
System.out.println(t[7]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array out of bond Exception: "+e);
}
try
{
Class.forName("Mai");
}
catch(ClassNotFoundException e)
{
System.out.println("Class not found Exception: "+e);
}
try
{
Integer mahabharat = new Integer("Ram");
System.out.println(mahabharat);
}
catch(NumberFormatException e)
{
System.out.println("Number Format Exception: "+e);
}
try
{
String str = "Burak";
System.out.println(str.charAt(5));
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println("String IndexOut Of Bounds Exception: "+e);
}
}
}
Output:

C.) The Voter-ID Card number of an Indian citizen consists of 3 alphabets


followed by 7 numeric digits. WAP to read the Voter-ID Card number of the
user and then throw InvalidVoterIDException if the user enters an invalid
Voter-ID Card Number.
Code:
import java.util.Scanner;
class InvalidVoterIDException extends Exception
{
@Override
public String getMessage()
{
return "InvalidVoterIDException";
}
}

public class Main


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter voter ID: ");
String voterId = sc.next();
try
{
for(int i=0;i<7;i++)
{
if(voterId.charAt(i)<'0' || voterId.charAt(i)>'9')
throw new InvalidVoterIDException();
}
for(int i=7;i<voterId.length();i++)
{
if(voterId.charAt(i)<'A' || voterId.charAt(i)>'Z')
throw new InvalidVoterIDException();
}
}
catch(InvalidVoterIDException e)
{
System.out.println("try again "+e.getMessage());
}
System.out.println("Done");

}
}

Output:

You might also like