CH3 Programs
CH3 Programs
/*
*/
import java.util.*;
System.out.println("m1 method");
try
arr[i] = s.nextInt();
String.class.getMethod("m2");
catch (ArrayIndexOutOfBoundsException e)
/*
* Q_87: Write a program to handle InterruptedException, IllegalArgumentException using try-cat-finally and throw
*/
import java.util.Scanner;
System.out.println("Hello");
try
Thread.sleep(5000);
catch(InterruptedException e)
System.out.println("Hi");
try
int m = sc.nextInt();
catch(IllegalArgumentException i)
finally
/*
* Q_88: Write a method for computing x ^ y by doing repetitive multiplication. x and y are of type integer and are
to be given as command line arguments. Raise and handle exception(s) for invalid values of x and y. Also define
method main.
*/
powerException(String s)
super(s);
}
}
class Q_88
long p = 1;
p=p*x;
class mainQ_88
try
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
ob88.power(x, y);
catch(RuntimeException e)
System.out.println("INVALID INPUT!!");
}
/*
* Q_89: Write an application that searches through its command-line argument. If an argument is found that does
not begin with and upper case letter, display error message and terminate.
*/
caseSensitiveException(String s)
super(s);
String s = args[0] ;
char c = s.charAt(0);
System.out.println(c);
/*
*Q_90: Write a program to create user define exception MyException2. Define a class ExceptionDemo2, that has a
method named compute(int a ) which throws a MyException object, when compute(int a )’s integer parameter is
greater than 10
*/
import java.util.Scanner;
MyException2(String s)
super(s);
class ExceptionDemo2
void compute(int a)
else
System.out.println("Valid Input");
class runQ_90
int n = sc.nextInt();
ob.compute(n);
}
// Q_91 //using Scanner
import java.util.Scanner;
public InvalidStatusException(String s) {
super(s);
class ResourcesStatus {
public ResourcesStatus() {
try {
statusRef[i][j] = status;
} catch (Exception e) {
statusRef[i][j] = 0;
}
public void processStatusCount() {
switch (statusRef[i][j]) {
case 0:
freeCount++;
break;
case 1:
occupiedCount++;
break;
case 2:
inaccessibleCount++;
break;
System.out.println("Error: total number of occupied resources exceeds total number of free resources.");
if (statusRef[i][j] == 2) {
statusRef[i][j] = 0;
class Main1 {
resourcesStatus.processStatusCount();
/************************************* CLA
******************************************************/
super(message);
class ResourcesStatus {
int row = i / 3;
int col = i % 3;
try {
statusRef[row][col] = status;
statusRef[row][col] = 0;
switch (statusRef[i][j]) {
case 0:
freeCount++;
break;
case 1:
occupiedCount++;
break;
case 2:
inaccessibleCount++;
break;
System.out.println("Error: total number of occupied resources exceeds total number of free resources.");
for (int i = 0; i < 3; i++) {
if (statusRef[i][j] == 2) {
statusRef[i][j] = 0;
class Main {
if (args.length != 9) {
return;
resourcesStatus.processStatusCount();
}/*
*Q_92: Write a complete program to accept N integer numbers from the command line. Raise and handle
exceptions for following cases :-
Skip the number if an exception is raised for it, otherwise add it to find total sum
*/
{
invalidException(String s)
super(s);
int sum = 0 ;
int n = Integer.parseInt(args[i]);
if ( n < 0)
try
catch(invalidException e)
continue;
if ( n % 10 == 0)
try
}
catch(invalidException e)
continue;
try
throw new invalidException("number is greater than 1000 and less than 2000");
catch(invalidException e)
continue;
try
catch(invalidException e)
continue;
}
else
sum =sum +n ;
/*
*Q_93: Declare a class called coordinate to represent 3 dimensional Cartesian coordinates( x, y and z).Define
following methods:- constructor- display, to print values of members- add_coordinates, to add three such
coordinate objects to produce a resultantcoordinate object. Generate and handle exception if x, y and z
coordinates of the resultare zero. - main, to show use of above methods.
*/
coordinateZeroException(String s)
super(s);
class coordinate
int x , y , z ;
this.x = x ;
this.y = y ;
this.z = z ;
void display()
try
else
res.display();
catch(Exception e)
class resultantcoordinate
}/*
*/
String s = null ;
try
System.out.println(s.length());
catch(NullPointerException e)
/*
* not divisible by 5.
*/
import java.util.Scanner;
MyException(String s)
{
super(s);
class ExceptionDemo
void compute(int a)
if(a % 7 == 0 && a % 5 != 0)
else
System.out.println("Valid Input");
class runQ_95
int n = sc.nextInt();
ob.compute(n);
/*
*/
negativeException(String s)
super(s);
int a = Integer.parseInt(args[0]);
if( a < 0)
else
/*
* If first command line argument is found zero then display error message.
*/
class entryException9 extends RuntimeException
entryException9(String s)
super(s);
int a = Integer.parseInt(args[0]);
if( a == 0)
else
}/*
* If sum of first and second command line argument is found 10 then display error message.
*/
entryException(String s)
{
super(s);
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a+b ;
if( c == 10)
else
/*
* Q_99 : write a program to enter the five subject marks out of 100 of student.
* If any subject marks is less than 35 then programm will generate exception
*/
import java.util.Scanner;
failException(String s)
super(s);
}
class Q_99
marks[i] = sc.nextDouble();
else
continue;
//this is for reference only for better preparations and there may be some errors. Don’t consider it as final solution.