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

Exp 7 Java

The document discusses three topics: multithreading in Java using Thread and Runnable interfaces, using Vectors in Java, and exception handling in Java. It provides code examples and outputs for each topic.

Uploaded by

Samiksha Bargude
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Exp 7 Java

The document discusses three topics: multithreading in Java using Thread and Runnable interfaces, using Vectors in Java, and exception handling in Java. It provides code examples and outputs for each topic.

Uploaded by

Samiksha Bargude
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Threads:

1) class Multi extends Thread{


public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
Output:
C:\Users\ITCS\Documents>javac Multi.java
C:\Users\ITCS\Documents>java Multi
thread is running...

2) class Multi3 implements Runnable{


public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
}
Output:
C:\Users\ITCS\Documents>javac Multi3.java
C:\Users\ITCS\Documents>java Multi3
thread is running...

3) Multithreading:
class Hi extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("Hi");
try{Thread.sleep(100);}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class Hello extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("Hello");
try{Thread.sleep(100);}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
public class ThreadDemo
{
public static void main(String args[])
{
Hi obj1=new Hi();
Hello obj2=new Hello();
obj1.start();
try{Thread.sleep(10);}
catch(Exception e)
{
System.out.println(e);
}
obj2.start();
}
}
Output:
C:\Users\ITCS\Documents>javac ThreadDemo.java
C:\Users\ITCS\Documents>java ThreadDemo
Hi
Hello
Hi
Hello
Hi
Hello
Hi
Hello
Hi
Hello

Vectors:
1) import java.util.*;
public class Simple{
public static void main (String[] args) {

Vector<String> v = new Vector<String>(); // It creates a default vector

v.add(0,"1"); // Adds 1 at the index 0


v.add(1,"Java"); // Adds "Java" at the index 1
v.add(2,"is"); // Adds "is" at the index 2
v.add(3,"Fun"); // Adds "Fun" at the index 3
v.add(4,"!!!"); // Adds "Fun" at the index 4
System.out.println("Vector: " + v );
System.out.println("The element at index 3 is " + v.get(3));
System.out.println("The first element is " + v.firstElement());
System.out.println("The last element is " + v.lastElement());
v.remove(1);
System.out.println("Vector after removal " + v );
System.out.println("The vector size is " + v.size());
System.out.println("The vector capacity is " + v.capacity());
v.trimToSize();
System.out.println("The vector capacity is after trimToSize: " + v.capacity());

}
}
Output:
C:\Users\ITCS\Documents>javac Simple.java
C:\Users\ITCS\Documents>java Simple
Vector: [1, Java, is, Fun, !!!]
The element at index 3 is Fun
The first element is 1
The last element is !!!
Vector after removal [1, is, Fun, !!!]
The vector size is 4
The vector capacity is 10
The vector capacity is after trimToSize: 4

Exception Handling:
1) import java.util.Scanner;
public class S1 {
static void divide(int num1, int num2){
try{
int result=num1/num2;
System.out.println("Result after division : "+result);
}
catch(ArithmeticException e){
System.out.println("Exception while performing division : "+e.toString());
}
finally{
System.out.println("I am inside finally block");
}
}

public static void main(String args[]){


Scanner scanner = new Scanner(System.in);
System.out.println("Enter number 1 : ");
int num1 = scanner.nextInt();
System.out.println("Enter number 2 : ");
int num2 = scanner.nextInt();
divide(num1,num2);
System.out.println("Finally block is always executed even when there is an exception");
}
}
Output:
C:\Users\ITCS\Documents>javac S1.java
C:\Users\ITCS\Documents>java S1
Enter number 1 :
23
Enter number 2 :
0
Exception while performing division : java.lang.ArithmeticException: / by zero
I am inside finally block
Finally block is always executed even when there is an exception

C:\Users\ITCS\Documents>javac S1.java
C:\Users\ITCS\Documents>java S1
Enter number 1 :
25
Enter number 2 :
5
Result after division : 5
I am inside finally block
Finally block is always executed even when there is an exception

2) import java.util.Scanner;
public class S2 {
//Throw exception if number 1 is greater than 900
static int add(int num1, int num2){
if (num1 > 900){
throw new ArithmeticException("Num 1 is greater than 900 and hence Exception is
thrown");
}else{
System.out.println("Both parameters are correct!!");
}
return num1+num2;
}
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number 1 : ");
int num1 = scanner.nextInt();
System.out.println("Enter number 2 : ");
int num2 = scanner.nextInt();
int result=add(num1,num2);
System.out.println("Result is : "+result);
}
}
Output:
C:\Users\ITCS\Documents>javac S2.java
C:\Users\ITCS\Documents>java S2
Enter number 1 :
23
Enter number 2 :
3
Both parameters are correct!!
Result is : 26

C:\Users\ITCS\Documents>javac S2.java
C:\Users\ITCS\Documents>java S2
Enter number 1 :
4563
Enter number 2 :
45
Exception in thread "main" java.lang.ArithmeticException: Num 1 is greater than 900 and hence
Exception is thrown
at S2.add(S2.java:6)
at S2.main(S2.java:18)

2) public class S3
{
public static void main(String args[])
{
int marks[] = { 40, 50, 60 };
System.out.println("Array is:");
for(int i=0;i<3;i++)
{
System.out.println(marks[i]);
}
try
{
int m1 = marks[3];
System.out.println("Marks are " + m1);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Do not cross size of array." + e );
}
System.out.println("Out of try-catch");

}
}
Output:
C:\Users\ITCS\Documents>javac S3.java
C:\Users\ITCS\Documents>java S3
Array is:
40
50
60
Do not cross size of array.java.lang.ArrayIndexOutOfBoundsException: 3
Out of try-catch

You might also like