0% found this document useful (0 votes)
0 views15 pages

Java Programs For Record 6-10

The document outlines several Java programs demonstrating string operations using the String and StringBuffer classes, multi-threading applications, and exception handling. Each section includes an aim, algorithm, source code, and a result indicating successful execution. Key operations include string concatenation, substring extraction, random number generation, and handling various exceptions.
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)
0 views15 pages

Java Programs For Record 6-10

The document outlines several Java programs demonstrating string operations using the String and StringBuffer classes, multi-threading applications, and exception handling. Each section includes an aim, algorithm, source code, and a result indicating successful execution. Key operations include string concatenation, substring extraction, random number generation, and handling various exceptions.
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/ 15

EX.

NO:6 STRING CLASS OPERATIONS

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 2:Create two strings

Step 3:Using the string class concat() method,join the two strings

Step 4:Find the index of a character using indexOf() method

Step 5:Extract the substring from the given string using substring method.

Step 6:Stop
SOURCE CODE:

class Stringexample {

public static void main(String[] args) {

String str1 = new String("Java ");

System.out.println("First String: " + str1);

String str2 = "Programming";

System.out.println("Second String: " + str2);

String joinedString = str1.concat(str2);

String findstr="Java";

System.out.println("Joined String: " + joinedString);

int pos = joinedString.indexOf("Pro");

System.out.println("The string "+findstr+" is found at index position: "+ pos);

System.out.print("The extracted substring is : ");

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:

To write a Java program to perform string operations using StringBuffer class:

a) Length of a string b) Reverse a string c) Delete a substring from the given string

ALGORITHM:

Step 1:Start

Step 2:Create a string using StringBuffer class

Step 3:Find the length of the string using length() method.

Step 4:Reverse the given string using reverse() method.


Step 5:Remove the substring using delete() method.

Step 6:Stop
SOURCE CODE:

class Stringbufferexample {

public static void main(String[] args)

StringBuffer sb = new StringBuffer("Hello");

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

Step 2:Create 3 threads using Thread class.

Step 3:First thread generate random numbers using Random class

Step 4:Second thread prints square of the even random number.

Step 5: Third thread prints cube of the odd random number.


Step 6:Stop
SOURCE CODE:

import java.util.Random;

class Square extends Thread


{

int x;

Square(int n)

x=n;

public void run()

int sqr=x*x;

System.out.println("Square of"+x+"="+sqr );

class Cube extends Thread

int x;

Cube(int n)
{

x=n;

public void run()

int cub=x*x*x;

System.out.println("Cube of"+x+"="+cub );

Class Number extends Thread

public void run()

Random random=newRandom();

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


{

int randomInteger = random.nextInt(100);


System.out.println("RandomIntegergenerated:"+randomInteger); if(randomInteger%2==0)
{

Square s=new Square(randomInteger); s.start();


}
else

Cube c=new Cube(randomInteger);

c.start();
}

try

Thread.sleep(1000);

}
catch(InterruptedException ex)

System.out.println(ex);

public class Threadeg

public static void main(String args[])

Number n=new Number();


n.start();
}

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 2:Create 2 threads using Thread class.

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

Public static void main(String[] args)

Thread thread1 = new Thread();

Thread thread2=newThread();

thread1.start();
thread2.start(); try
{

thread1.join();
thread2.join();
}

catch(InterruptedException e)

e.printStackTrace();

private static void printNumbers(int start,int end)

for(int i =start;i <=end; i++)


{

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:

To write a Java program to demonstrate the use of following exceptions.

a) Arithmetic Exception b) Number Format Exception

c) Array Index Out of Bound Exception d) Negative Array Size Exception

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 3:Convert a string to parseInt() to demonstrate Number format exception.

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

public static void main(String[] args)

try

int a=30,b=0; int c =


a / b;
System.out.println("Result="+ c);

catch(ArithmeticException e)

System.out.println("Divide by Zero");

int ar[]={1,2,3,4,5};

System.out.println("Array Values are");


try

for(int i=0;i<=ar.length;i++)
System.out.print(ar[i]+"");
}

catch(Exception e)

System.out.println("Array index out of Bound Exception!");

String str1=”123ABC”; try


{

int x = Integer.parseInt(str1);

catch(NumberFormatException e)

System.out.println("Number Format Exception!");

try

} int[] array=new int[-5];

catch(NegativeArraySizeException e)

System.out.println("Negative Array Size Exception!");


}

RESULT:

The program has been executed successfully and the output is verified.

You might also like