🌙 DARK

SEATNOTAVAILABLE EXCEPTION

 

SeatNotAvailableException


An organization is organizing a charity fate for the well being of poor kids. Since the manager was running short on time, he asked you to help him with the ticket bookings. You being from a programming background decide to design a program that asks the user about the seat number they want. Seat booking details are stored in an array. If the seat number requested is available booking should be done else print the message " SeatNotAvailableException". If the seat number requested is not in the range throws an exception ArrayIndexOutOfBoundsException.

 

Write a program to throw a custom exception based on the inputs and constraints given.

Create a class SeatNotAvailableException that extends Exception.

In the Main class, create an array of size n*n (n rows each with n seats) which is got from the user. Get the tickets to be booked from the user
 and handle any exception that occurs in Main Class. (Take seat numbers from 0 to (n*n)-1)

Note:

  • Vacant seats are denoted by (0) and booked seats are denoted by (1).
  • Show message as "Already Booked" as a Custom exception.

 

The link to download the template code is given below
Code Template


Input and Output format:


Refer sample Input and Output for formatting specifications.t of the output.

[All Texts in bold corresponds to the input and rest are output]


Sample Input and Output 1:

Enter the number of rows and columns of the show:
3
Enter the number of seats to be booked:
2
Enter the seat number 1
8
Enter the seat number 2
0
The seats booked are:
1 0 0
0 0 0
0 0 1

Sample Input and Output 2:

Enter the number of rows and columns of the show:
3
Enter the number of seats to be booked:
2
Enter the seat number 1
9
java.lang.ArrayIndexOutOfBoundsException: 9
The seats booked are:
0 0 0
0 0 0
0 0 0

Sample Input and Output 3:

Enter the number of rows and columns of the show:
4
Enter the number of seats to be booked:
3
Enter the seat number 1
15
Enter the seat number 2
14
Enter the seat number 3
15
SeatNotAvailableException: Already Booked
The seats booked are:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 1 1



PROGRAM:-
@MRProgrammer89 


CREATE CLASS < SeatNotAvailableException :


public class SeatNotAvailableException extends Exception{

    public SeatNotAvailableException(String s) {

super(s);

}

}


PROGRAM:-
@MRProgrammer89 


CREATE CLASS < Main :

import java.util.Scanner;


public class Main {

    public static void main(String args[]) throws SeatNotAvailableException{

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows and columns of the show:");

int n = sc.nextInt();

int seats = n*n;

int a[] = new int [seats];

int twoDArray[][]=new int[n][n];

System.out.println("Enter the number of seats to be booked:");

int booked = sc.nextInt();

try {

for(int i = 1; i<=booked; i++) {

System.out.println("Enter the seat number "+i);

int noOfSeats = sc.nextInt();

if(a[noOfSeats]==0) {

a[noOfSeats]=1;

int p =0;

for(int j = 0; j<n; j++) {

for(int k=0; k<n; k++) {

twoDArray[j][k]=a[p];

p++;

}

}

}else {

throw new SeatNotAvailableException("Already Booked");

}

}}

catch(Exception e) {

System.out.println(e);

}

finally {

System.out.println("The seats booked are:");

for(int i=0; i<n; i++) {

for(int j=0; j<n; j++) {

System.out.print(twoDArray[i][j]+"");

}System.out.println();

}

}

   }

}



Assignment


Q:
Is there any way to "get around" the strict restrictions placed on methods by the throws clause?
Q:
Differences between exceptions, errors, and runtime exceptions.


Q&A


Q:
What is an exception?
A:
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
Q:
What is error?
A:
An Error indicates that a non-recoverable condition has occurred that should not be caught. Error, a subclass of Throwable, is intended for drastic problems, such as OutOfMemoryError, which would be reported by the JVM itself.
Q:
Which is superclass of Exception?
A:
"Throwable", the parent class of all exception related classes.
Q:
What are the advantages of using exception handling?
A:
Exception handling provides the following advantages over "traditional" error management techniques:
  • Separating Error Handling Code from "Regular" Code.
  • Propagating Errors Up the Call Stack.
  • Grouping Error Types and Error Differentiation.
Q:
Why Runtime Exceptions are Not Checked?
A:
The runtime exception classes (RuntimeException and its subclasses) are exempted from compile-time checking because, in the judgment of the designers of the Java programming language, having to declare such exceptions would not aid significantly in establishing the correctness of programs. Many of the operations and constructs of the Java programming language can result in runtime exceptions. The information available to a compiler, and the level of analysis the compiler performs, are usually not sufficient to establish that such run-time exceptions cannot occur, even though this may be obvious to the programmer. Requiring such exception classes to be declared would simply be an irritation to programmers.
Q:
What is the use of finally block?
A:
The finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not. This is right place to close files, release your network sockets, connections, and perform any other cleanup your code requires.
Note: If the try block executes with no exceptions, the finally block is executed immediately after the try block completes. It there was an exception thrown, the finally block executes immediately after the proper catch block completes
Q:
Can we have the try block without catch block?
A:
Yes, we can have the try block without catch block, but finally block should follow the try block.
Note: It is not valid to use a try clause without either a catch clause or a finally clause.
Q:
What is the difference throw and throws?
A:
throws: Used in a method's signature if a method is capable of causing an exception that it does not handle, so that callers of the method can guard themselves against that exception. If a method is declared as throwing a particular class of exceptions, then any other method that calls it must either have a try-catch clause to handle that exception or must be declared to throw that exception (or its superclass) itself.

A method that does not handle an exception it throws has to announce this:

  public void myfunc(int arg) throws MyException {
        �
    }
throw: Used to trigger an exception. The exception will be caught by the nearest try-catch clause that can catch that type of exception. The flow of execution stops immediately after the throw statement; any subsequent statements are not executed. To throw an user-defined exception within a block, we use the throw command:
  throw new MyException("I always wanted to throw an exception!");
  
Q:
How to create custom exceptions?
A:
By extending the Exception class or one of its subclasses.
Example:
class MyException extends Exception {
  public MyException() { super(); }
  public MyException(String s) { super(s); }
  }
  
Q:
What are the different ways to handle exceptions?
A:
There are two ways to handle exceptions:
  • Wrapping the desired code in a try block followed by a catch block to catch the exceptions.
  • List the desired exceptions in the throws clause of the method and let the caller of the method handle those exceptions.
  • Q:
    What are the types of Exceptions in Java?
    A:
    There are two types of exceptions in Java, unchecked exceptions and checked exceptions.
  • Checked exceptions: A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses. Each method must either handle all checked exceptions by supplying a catch clause or list each unhandled checked exception as a thrown exception.
  • Unchecked exceptions: All Exceptions that extend the RuntimeException class are unchecked exceptions. Class Error and its subclasses also are unchecked.
  • Q:
    Why Errors are Not Checked?
    A:
    A unchecked exception classes which are the error classes (Error and its subclasses) are exempted from compile-time checking because they can occur at many points in the program and recovery from them is difficult or impossible. A program declaring such exceptions would be pointlessly.


    @MRProgrammer89 






    MOTIVATION :


        “The way we spend our time defines who we are 


    Although we may not see each other as often as we’d like, distance is no match for the bond that we share. Thank you for coming to visit. It was fantastic to catch up.


    VISITE MORE BLOGS




    THANK YOU....😊

    Post a Comment

    Thanks for reading the blog. We hope it was useful to you and that you learned something new. Will always be writing on new and interesting topics, so you can visit our website to know the latest updates of our blogs. Thank You!

    Previous Post Next Post

    Contact Form