You are on page 1of 2

1

Assignment-4: Object Oriented Programming Lab (Code: PCC-CS593) (Exception handling, string & threading)

1. Create a user-defined exception named CheckArgument to check the number of arguments


passed through command line. If the number of arguments is less than five, throw the
CheckArgument exception else print the summation of all the five numbers.

2. Consider a student examination database system that prints the mark sheet of students. Input
the following from the keyboard:
a. Student's name
b. Marks in six subjects
These marks should be between 0 and 50. If the marks are not in the specified range, raise a
RangeException (a user-defined exception) else find the total marks and print the
percentage of marks of the students.

3. Write a program to generate and catch an ArrayIndexOutOfBoundsException.

4. Define an object reference and initialize it to null. Try to call a method through this reference.
Now wrap the code in a try-catch clause to catch the exception.

5. Consider a class that represents an account in a bank. Let the minimum balance for the account
be Rs. 1000. Write a program that throws an exception when a withdrawal results in the balance
decreasing to value less than Rs. 1000.

6. Create your own exception class using extends keyword. Write a constructor for this
class that takes a String argument and store it inside object with a String reference. Write a
method that prints out the stored String. Create a try-catch clause to exercise your
new exception.

7. Create a class with two methods first() and second(). Each method throws an exception
of a new type that you have defined. In first(), call second(), catch its exception and in
the catch clause, throw a different exception (of a second type that you have defined). In main()
method call the first() method.

8. Create three new types of exceptions. Write a class with method that throws all the three. In
main(), call the method but only use a single catch clause that will catch all the three types
of exceptions.

9. Write class Student with attributes roll no, name, age and course. Initialize values
through parameterized constructor. If age of student is not in between 15 and 21 then generate
user defined exception "Age Not within Range". If name contains numbers or special
symbols, raise exception "Name not Valid".

10. Write a Java program to find the exception "Marks out of Bounds". Create a class
Student. If mark is greater than 100, it must generate user defined exception called "Marks
out of Bounds" and throw it.

11. Write a program in Java to check if entered data is alphabet or digit. If it is alphabet, then print
if it is capital or small case. If digit is entered throw user defined exception "digit not
allowed".
2

12. Write Java programs to implement the followings (take the input from the keyboard):
a. Find the length of a String.
b. Find the length of a String except whitespaces.
c. Convert a double value to a String.
d. Check whether a String contains characters or digits or both.
e. Input two Strings – check whether the second String is reverse of the first one or
not.
f. Compare two Strings and find if they are equal or not.
g. Remove the 6th to 8th characters from a String.
h. Convert a String to lowercase and uppercase.
i. Reverse a String.

13. Write a Java program to implement Thread concept.

14. Write a program that demonstrates the use of multithreading with the use of three counters
with three threads defined for each. Three threads should represent the counters as follows:
a. One counter starts from 5 and increments with the step of 5.
b. Second counter starts from 10 and increments with the step of 10.
c. Third counter starts with 100 and increments with the step of 100.
Assign proper priority to the threads and then run the threads.

15. Write a Java program to use join() and isAlive() in Multi-Threading.

16. Write a Java program to implement producer-consumer problem by using Inter Thread
Communication in Multi-Threading. [The objective of this program is to have the
Producer class give a value to the Consumer class through the use of a Queue class. The
Producer class places a value on the queue and then waits until the Consumer class retrieves
the value before the Producer class places another value on the queue.]

You might also like