3

 import java.util.Scanner;

class exception 

{

public static void main(String[] args) 

 {

int a, b, result;

Scanner input = new Scanner(System.in);

System.out.println("Input two integers");

a = input.nextInt();

b = input.nextInt();

try 

 {

result = a / b;

System.out.println("Result = " + result);

}

catch (ArithmeticException e) 

 {

System.out.println("Exception caught: Division by zero.");

}

}

}

////////////////////////////////////////////////////

import java.util.Random;
class SquareThread implements Runnable 
 {
int x;
SquareThread(int x) 
 {
this.x = x;
}
public void run() 
 {
System.out.println("Thread Name:Square Thread and Square of " + x + " 
is: " + x * x);
}
 }
class CubeThread implements Runnable 
 {
int x;
CubeThread(int x)
 {
this.x = x;
}
public void run() 
 {
System.out.println("Thread Name:Cube Thread and Cube of " + x + " is: " 
+ x * x * x);
}
 }
 class RandomThread implements Runnable
 {
String tname;
Random r;
Thread t1, t2;
 public void run() 
 {
 int num = 0;
 r = new Random();
 try 
 {
 while (true) 
 {
 num = r.nextInt(100);
 System.out.println("Main Thread and Generated Number is " + 
num);
 t1 = new Thread(new SquareThread(num));
t1.start();
t2 = new Thread(new CubeThread(num));
t2.start();
Thread.sleep(1000);
System.out.println("--------------------------------------");
}
catch (Exception ex) 
{
System.out.println(ex.getMessage());
}
}
}
public class MainThread
{
public static void main(String[] args) {
RandomThread a = new RandomThread();
Thread threadx = new Thread(a);
threadx.start();
}
}

Comments

Popular posts from this blog

9