Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Programming

Question 1
Create your own exception called InvalidDirectionException it should display an
message called “You are going in wrong direction”. Your code should get two
input’s which is HouseDirection and GpsLocation.

If both are equal “You are going in correct direction”.


If both are not equal InvalidDirectionException is Thrown “You are going in wrong
direction”.
Question 1
Sample Input1: Sample Output1:
North You are going in correct direction
North

Sample Input2: Sample Output2:


North InvalidDirectionException: You are
south going in wrong direction
1 import java.util.Scanner;
2 class InvalidDirectionException extends Exception
3 {
4 public InvalidDirectionException(String message)
5 {
6 super(message);
7 }
8 }
9 class Map
10 {
11 public void CheckDirection(String HouseDirection, String GpsLocation) throws
12 InvalidDirectionException
13 {
14 if(HouseDirection.equals(GpsLocation))
15 {
16 System.out.print("You are going in correct direction");
17 }
18 else
19 throw new InvalidDirectionException("You are going in wrong direction");
20 }
21 }
22
1 public class Main
2 {
3 public static void main(String[] args)
4 {
5 Scanner sc = new Scanner(System.in);
6 String HouseDirection = sc.next();
7 String GpsLocation = sc.next();
8 Map map = new Map();
9 try
10 {
11 map.CheckDirection(HouseDirection, GpsLocation);
12 }
13 catch(Exception e)
14 {
15 System.out.print(e);
16 }
17 }
18 }
19
20
21
22
Question 2
Create your own exception called CheckUserMailException it should display an
message called “Email Already Registered”. Your code should get two input from
the user which are list of valid mail id’s and mail id which user want to create.

If the mail entered is doesn’t exist on the list of valid mail id’s, then the output is
“New user”.

If the mail entered is exist on the list of valid mail id’s, an exception is thrown called
CheckUserMailException.
Question 2
Sample Input: Sample Output:
ironman@gmail.com CheckUserMailException: Email
godofthunder@gmail.com Already Registered
captainamerica@gmail.com
nicolasfury@gmail.com
hulk@gmail.com

godofthunder@gmail.com
1 import java.util.*;
2 class CheckUserMailException extends Exception
3 {
4 public CheckUserMailException(String message)
5 {
6 super(message);
7 }
8 }
9 class RegistrationService
10 {
11 public void validateEmail(List registeredEmails,String email) throws CheckUserMailException
12 {
13 if (registeredEmails.contains(email))
14 {
15 throw new CheckUserMailException("Email Already Registered");
16 }
17 else
18 System.out.print("New user");
19 }
20 }
21
22
1 public class RegistrationServiceClient {
2 public static void main(String[] args) {
3 Scanner sc = new Scanner(System.in);
4 String str[]= new String[5];
5 for(int i=0;i<5;i++)
6 {
7 str[i]=sc.next();
8 }
9 String mail = sc.next();
10 List<String> registeredEmails = Arrays.asList(str);
11 RegistrationService service = new RegistrationService();
12 try{
13 service.validateEmail(registeredEmails, mail);
14 }
15 catch (CheckUserMailException e) {
16 System.out.print(e);
17 }
18 }
19 }
20
21
22
Question 3
Create your own exception called IncorrectPinException it should display an
message called “Please Try again”. Your code should get two input from the user
which are setpin and pin.

If the setpin matches with the pin, then the output is “Mobile Unlocked”.

If the setpin doesn’t matches with the pin, an exception is thrown called
IncorrectPinException.
Question 3
Sample Input1: Sample Output1:
475 IncorrectPinException: Please Try
485 again

Sample Input2: Sample Output2:


8562 Mobile Unlocked
8562
1 import java.util.Scanner;
2 class IncorrectPinException extends Exception
3 {
4 public IncorrectPinException(String message)
5 {
6 super(message);
7 }
8 }
9 class Pin
10 {
11 public void CheckPin(int setpin,int pin) throws IncorrectPinException {
12 if (setpin == pin)
13 {
14 System.out.print("Mobile Unlocked");
15 }
16 else
17 throw new IncorrectPinException("Please Try again");
18 }
19 }
20
21
22
1 public class Lockscreen
2 {
3 public static void main(String[] args)
4 {
5 Scanner sc = new Scanner(System.in);
6 int setpin = sc.nextInt();
7 int pin = sc.nextInt();
8 Pin num = new Pin();
9 try {
10 num.CheckPin(setpin,pin);
11 }
12 catch (IncorrectPinException e) {
13 System.out.print(e);
14 }
15 }
16 }
17
18
19
20
21
22
Question 4
Write a java program which will throw an Build-in exception called
ArrayIndexOutOfBounds Exception. You should catch that build-in exception and
rethrow that exception using your own exception named
InvalidAccessingDataException with message “There is no such data available”.

Get size of an array ,array elements and the element at which index need be fetched
in the array from the user.
Question 4
If the given index is with in the range, it will display the element.

If the given index is out of bound, it will throw an


ArrayIndexOutOfBoundException. This exception is caught and rethrow using own
exception called InvalidAccessingDataException.
Question 4
Sample Input: Sample Output:
5 Exception in thread "main"
98 InvalidAccessingDataException: There is no such data
65 available
75 at StudentMark.fetchMark(Main.java:18)
at Main.main(Main.java:33)
42
Caused by: java.lang.ArrayIndexOutOfBoundsException:
98
9
9 at StudentMark.fetchMark(Main.java:14)
... 1 more
1 import java.util.Scanner;
2 class InvalidAccessingDataException extends RuntimeException
3 {
4 public InvalidAccessingDataException(String message, Throwable cause)
5 {
6 super(message, cause);
7 }
8 }
9 class StudentMark
10 {
11 public void fetchMark(int arr[], int rollno)
12 {
13 try {
14 System.out.print("Mark of the rollno "+ rollno + " is = "+ arr[rollno]);
15 }
16 catch (ArrayIndexOutOfBoundsException e)
17 {
18 throw new InvalidAccessingDataException("There is no such data available", e);
19 }
20 }
21 }
22
1 public class Main {
2 public static void main(String[] args) {
3 Scanner sc = new Scanner(System.in);
4 int no_of_details = sc.nextInt();
5 int arr[] = new int[no_of_details];
6 for(int i=0;i<no_of_details;i++)
7 {
8 arr[i]=sc.nextInt();
9 }
10 int rollno = sc.nextInt();
11 StudentMark sm = new StudentMark();
12 sm.fetchMark(arr,rollno);
13 }
14 }
15
16
17
18
19
20
21
22
Question 5
Write a java program which will throw an Build-in exception called
NullPointerException. You should catch that build-in exception and rethrow using
your own exception named EnteryourPasswordException with message “Kindly
Enter the Password”.

Your program should get password from the user.

If the user forget to enter the password, it will throw an NullPointerException and
this exception thrown by using your own exception named
EnteryourPasswordException.
Question 5
Sample Input: Sample Output:
No input Exception in thread "main"
EnteryourPasswordException: Kindly Enter the
Password
at LoginPage.CheckPassword(Main.java:26)
at Main.main(Main.java:35)
Caused by: java.lang.NullPointerException
at LoginPage.CheckPassword(Main.java:18)
... 1 more
1 import java.util.Scanner;
2 class EnteryourPasswordException extends RuntimeException
3 {
4 public EnteryourPasswordException(String message, Throwable cause)
5 {
6 super(message, cause);
7 }
8 }
9 class LoginPage
10 {
11 public void CheckPassword(String Password)
12 {
13 if(Password.length()==0)
14 {
15 Password = null;
16 }
17 try {
18 if(Password.equals("Thanos"))
19 System.out.print("Successfully Logged in");
20 else
21 System.out.print("Invalid Password");
22 }
1 catch (NullPointerException e)
2 {
3 throw new EnteryourPasswordException("Kindly Enter the Password", e);
4 }
5 }
6 }
7 public class Main {
8 public static void main(String[] args) {
9 Scanner sc = new Scanner(System.in);
10 String Password = sc.nextLine();
11 LoginPage login = new LoginPage();
12 login.CheckPassword(Password);
13 }
14 }
15
16
17
18
19
20
21
22
THANK YOU

You might also like