Exp 7 Java
Exp 7 Java
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) {
}
}
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");
}
}
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