0% found this document useful (0 votes)
70 views2 pages

Exception

Uploaded by

Abhishek Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views2 pages

Exception

Uploaded by

Abhishek Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Handle exception in number

Problem statement:
Get the input String from user and parse it to integer, if it is not a number it
will throw number format exception Catch it and print "Entered input is not a valid
format for an integer." or else print the square of that number. (Refer Sample
Input and Output).
Sample input and output 1:
Enter an integer: 12
The square value is 144
The work has been done successfully
Sample input and output 2:
Enter an integer: Java
Entered input is not a valid f

Write a program that takes as input the size of the array and the elements in the
array. The program then asks the user to enter a particular index and prints the
element at that index. Index starts from zero.

This program may generate Array Index Out Of Bounds Exception or


NumberFormatException . Use exception handling mechanisms to handle this
exception.

Sample Input and Output 1:


Enter the number of elements in the array
2
Enter the elements in the array
50
80
Enter the index of the a
Enter the index of the array element you want to access
9
java.lang.ArrayIndexOutOfBoundsException

Sample Input and Output 3:


Enter the number of elements in the array
2
Enter the elements in the array
30
j
java.lang.NumberFormatException

Aswin C9:19 AM
Write a Program to take care of Number Format Exception if user enters values
other than integer for calculating average marks of 2 students. The name of the
students and marks in 3 subjects are taken from the user while executing the
program.
In the same Program write your own Exception classes to take care of Negative
values and values out of range (i.e. other than in the range of 0-100)

Abstract

Create an abstract class Compartment to represent a rail coach. Provide an abstract


function notice in this class. Derive FirstClass, Ladies, General, Luggage classes
from the compartment class. Override the notice function in each of them to print
notice suitable to the type of the compartment.
Create a class TestCompartment . Write main function to do the following:
Declare an array of Compartment of size 10.
Create a compartment of a type as decided by a randomly generated integer in the
range 1 to 4.
Check the polymorphic behavior of the notice method.

INTERFACE

Write an interface called Playable, with a method


void play();
Let this interface be placed in a package called music.

Write a class called Veena which implements Playable interface. Let this class be
placed in a package music.string

Write a class called Saxophone which implements Playable interface. Let this class
be placed in a package music.wind

Write another class Test in a package called live. Then,


a. Create an instance of Veena and call play() method
b. Create an instance of Saxophone
which implements Playable interface. Let this class be placed in a package
music.wind

Write another class Test in a package called live. Then,


a. Create an instance of Veena and call play() method
b. Create an instance of Saxophone and call play() method
c. Place the above instances in a variable of type Playable and then call play()

You might also like