0% found this document useful (0 votes)
1 views

java 5

The document contains two Java programs: the first implements user-defined exception handling for negative amounts entered, and the second creates two threads to print even and odd numbers at specified intervals. The first program throws a custom exception when a negative amount is input, while the second program uses threads to print odd numbers every five seconds and even numbers every two seconds. Both programs demonstrate fundamental concepts of exception handling and multithreading in Java.

Uploaded by

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

java 5

The document contains two Java programs: the first implements user-defined exception handling for negative amounts entered, and the second creates two threads to print even and odd numbers at specified intervals. The first program throws a custom exception when a negative amount is input, while the second program uses threads to print odd numbers every five seconds and even numbers every two seconds. Both programs demonstrate fundamental concepts of exception handling and multithreading in Java.

Uploaded by

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

Experiment no:5

Aim 5(a): Write a Java program to implement user defined exception handling for negative
amount entered.
Code:
import java.util.*;

class NewException extends Exception{

public NewException(float n)

System.out.println(n + " is a negative value");

public class exception{

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Enter the amount : ");

try{

float amt = sc.nextFloat();

if(amt<0)

throw new NewException(amt);

System.out.println("You enter : "+amt);

catch(NewException ex)

System.out.println("Exception");

Output:
5(b): Write a program in java which creates two threads, “Even” thread and “Odd” thread
and print the even no using Even Thread after every two seconds and odd no using Odd
Thread after every five second.
Code:

class odd extends Thread{

public void run(){

for(int i=1;i<10;i+=2)

System.out.println("Odd number : "+i);

try{

Thread.sleep(5000);

catch(InterruptedException e)

System.out.println(e);

class even extends Thread{

public void run(){

for(int i=0;i<10;i+=2)

System.out.println("Even number : "+i);

try{

Thread.sleep(5000);

catch(InterruptedException e)
{

System.out.println(e);

class thread{

public static void main(String args[])

odd obj1 = new odd();

even obj2 = new even();

obj1.start();

obj2.start();

Output:

You might also like