Java Programs For Record 6-10
Java Programs For Record 6-10
AIM:
To write a Java program to perform the following string operations using String class:
a)String concatenation b)Search substring c)To extract substring from given string
ALGORITHM:
Step 1:Start
Step 3:Using the string class concat() method,join the two strings
Step 5:Extract the substring from the given string using substring method.
Step 6:Stop
SOURCE CODE:
class Stringexample {
String findstr="Java";
System.out.println(joinedString.substring(5,12));
RESULT:
The program has been executed successfully and the output is verified.
EX.NO:7 STRING BUFFER CLASS OPERATIONS
AIM:
a) Length of a string b) Reverse a string c) Delete a substring from the given string
ALGORITHM:
Step 1:Start
Step 6:Stop
SOURCE CODE:
class Stringbufferexample {
sb.append(" ");
sb.append("World");
System.out.println(sb);
System.out.println(sb.length());
System.out.println(sb.delete(5,11));
System.out.println(sb.reverse());
RESULT:
The program has been executed successfully and the output is verified.
EX.NO:8 MULTI-THREAD APPLICATION
AIM:
To write a Java program to implement a multi-thread application that has three threads to print
random numbers,print square of even random numbers and cube of odd random numbers
ALGORITHM:
Step 1:Start
import java.util.Random;
int x;
Square(int n)
x=n;
int sqr=x*x;
System.out.println("Square of"+x+"="+sqr );
int x;
Cube(int n)
{
x=n;
int cub=x*x*x;
System.out.println("Cube of"+x+"="+cub );
Random random=newRandom();
c.start();
}
try
Thread.sleep(1000);
}
catch(InterruptedException ex)
System.out.println(ex);
RESULT:
The program has been executed successfully and the output is verified.
EX.NO:9 THREAD
AIM:
To write a Java program for thread program which uses the same method asynchronously to
print the numbers 1 to 10 and to print 90 to 100 using 2 threads.
ALGORITHM:
Step 1:Start
Step 3:Define a function for printing the numbers using for loop.
Step 4:Use the first thread to print 1-10 by calling the function
Step 5:Use the second thread to print 90-100 by calling the function
Step 6:Stop
SOURCE CODE:
class NumberPrinter
Thread thread2=newThread();
thread1.start();
thread2.start(); try
{
thread1.join();
thread2.join();
}
catch(InterruptedException e)
e.printStackTrace();
System.out.println(Thread.currentThread().getName()+":"+i);
RESULT:
The program has been executed successfully and the output is verified.
EX.NO:10 EXCEPTION HANDLING
AIM:
ALGORITHM:
Step 1:Start
Step 2:Divide a number by 0,to throw divide by zero arithmetic exception in try
block.Otherwise display the result in catch in block.
Step 4:Define an array and display it in try block,accessing the element out of an array index
throws array index out of bound exception
Step 5:Define a array size with negative value to demonstrate negative array size exception
Step 6:Stop
SOURCE CODE:
class ExceptionDemo
try
catch(ArithmeticException e)
System.out.println("Divide by Zero");
int ar[]={1,2,3,4,5};
for(int i=0;i<=ar.length;i++)
System.out.print(ar[i]+"");
}
catch(Exception e)
int x = Integer.parseInt(str1);
catch(NumberFormatException e)
try
catch(NegativeArraySizeException e)
RESULT:
The program has been executed successfully and the output is verified.