You are on page 1of 11

JAVA PROGRAMMING

Assessment-3
NAME: Asmit Gupta
REG. NO.:18BCE0904
SLOT: L53+L54
FACULTY: ASIS KUMAR TRIPATHY

Question 1 :-
Write a program to showcase the use of InputMismatchException,
ArithmeticException, ArrayIndexOutOfBoundsException, and
ClassCastException
Solution:-
Code:-
import java.util.*;
class Parent {
String p1;
Parent(String n1){
p1 = n1;
}
public void display() {
System.out.println(p1);
}
}
class Child extends Parent {
String c1;
Child(String n2) {
super(n2);
c1 = n2;
}
public void display() {
System.out.println(c1);
}
}
public class asmitlab3_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();

switch(number){
case 1:
{
int num = sc.nextInt();
System.out.println(num);
}
break;
case 2:
{
int ans = 9/0 ;
System.out.println(ans);
}
break;
case 3:
{
int arr[] = {1,2,3};
System.out.println(arr[4]);
}
break;
case 4:
{
Parent pt2 = new Parent("Sai");
Child ct2 = (Child)pt2;
}
break;

}
sc.close();
}

Output:-
Question 2:-
Write a program to show the use of Throw, Throws keyword in an
exception handling
Code:-
import java.util.Scanner;
public class asmitlab3_3 {
public void fn(int num){
if(num==45)
throw new ArithmeticException("your number is"+num);
else
System.out.println("your numner is not equal to 45");
}
public void Display(int a)throws ArithmeticException{
int n= a/0;
}
public static void main(String[] args) {
asmitlab3_3 obj1= new asmitlab3_3();
Scanner sc= new Scanner(System.in);
System.out.println ("Enter your number");
int num= sc.nextInt();

obj1.fn(num);
System.out.println("enter any number");
int s=sc.nextInt();
try{
obj1.Display(s);
}
catch(ArithmeticException e){
System.out.println("ArithmeticException occured");
}
}
}

Output:-

Question 3:-
Write a program to find the consequence if the try block will not
generate an exception and write a finally block to exit the program
execution.
Code:-
import java.util.*;
public class asmitlab3_3 {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 numbers:");
int a = sc.nextInt();
int b = sc.nextInt();
try{
int data=a+b;
System.out.println(data);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
finally
{
System.out.println("final block called");
System.exit(1);
}
sc.close();
}
}

Output:-
Question 4:-
Write a program to create two threads, one will check the largest
and other will check the smallest among two number, which
numbers you have taken as input.
Code:-
import java.util.*;
class Greater extends Thread
{
int x,y;
Greater(int x, int y)
{
this.x = x;
this.y = y;
}

public void run()


{
if(x>y)
{
System.out.println(x+ " is the greater number");
}
else if(y>x)
{
System.out.println(y+" is the greater number");
}
else
{
System.out.println(" Both are Equal");
}
}

}
class Smaller extends Thread
{
int x,y;
Smaller(int x, int y)
{
this.x = x;
this.y = y;
}

public void run()


{
if(x<y)
{
System.out.println(x+ " is the smaller smaller");
}
else if(y<x)
{
System.out.println(y+" is the smaller number");
}
else
{
System.out.println(" Both are Equal");
}
}

}
public class asmitlab3_4
{

public static void main(String args[])


{
Scanner s = new Scanner(System.in);
System.out.println("Enter 2 numbers:");
int a = s.nextInt();
int b = s.nextInt();
Thread t1 = new Greater(a,b);
Thread t2 = new Smaller(a,b);
t1.start();
t2.start();
s.close();
}
}
Output:-

Question 5:-
First, take input of two integer numbers. Next, create 5 threads to do
addition, subtraction, multiplication, division and modulus among
those two numbers. Give 500 milliseconds sleeping time for each
thread.
Output:-
import java.util.Scanner;

class addition extends Thread{

int a,b;
addition(int a, int b){
this.a = a;
this.b = b;
}
public void run() {
int add;

try {
Thread.sleep(500);
} catch (Exception e) {
System.out.println(e);
}
finally{
add = a + b;
System.out.println("Sum = " + add);
}
}
}
class subtraction extends Thread{
int a,b;
subtraction(int a, int b){
this.a = a;
this.b = b;
}
public void run() {
int sub;

try {
Thread.sleep(500);
} catch (Exception e) {
System.out.println(e);
}
finally {
sub = a - b;
System.out.println("Subtraction = " + sub);
}
}
}
class multiplication extends Thread{

int a,b;
multiplication(int a, int b){
this.a = a;
this.b = b;
}
public void run() {
int mul;

try {
Thread.sleep(500);
} catch (Exception e) {
System.out.println(e);
}
finally {
mul = a * b;
System.out.println("Multiplication = " + mul);
}
}
}
class division extends Thread{

int a,b;
division(int a, int b){
this.a = a;
this.b = b;
}
public void run() {
float div;

try {
Thread.sleep(500);
} catch (Exception e) {
System.out.println(e);
}
finally {
div = (float) a / b;
System.out.println("Division = " + div);
}
}
}
class modulus extends Thread{

int a,b;
modulus(int a, int b){
this.a = a;
this.b = b;
}
public void run() {
int mod;

try {
Thread.sleep(500);
} catch (Exception e) {
System.out.println(e);
}
finally {
mod = a % b;
System.out.println("Modulus = " + mod);
}
}
}

public class asmitlab3_5 {

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();

addition T1 = new addition(a,b);


subtraction T2 = new subtraction(a,b);
multiplication T3 = new multiplication(a,b);
division T4 = new division(a,b);
modulus T5 = new modulus(a,b);

T1.start();
T2.start();
T3.start();
T4.start();
T5.start();
}
}

Output:-

You might also like