INDEX
Page
S.NO Date Title sign
.No
An integer and then prints out all
1. the prime numbers up to that
Integer
2. To multiply two given matrices
That displays the number of
3.
characters, lines and words in a text
Generate random numbers between
two given limits using Random
4.
class and print messages according
to the range of the value generated
String Manipulation using
5.A Character Array,
“String Length”
String Manipulation using
Character Array,
5.B
“Finding a character at a
particular position”
String Manipulation using
5.C Character Array,
“Concatenating two strings”
String operations using String
6.A
class, “String Concatenation”
String operations using String
6.B
class, “Search a substring”
String operations using String
6.C class, “To extract substring from
given string”
String operations using
7.A StringBuffer class,
“Length of a string”
3
String operations using
StringBuffer class,
7.B “Reverse a string”
String operations using
StringBuffer class,
7.C
“Delete a substring from the
given string”
That implements a multi-thread
application that has three threads.
First thread generates random
integer every 1 second and if the
8. value is even, second thread
computes the square of the number
and prints. If the value is odd, the
third thread will print the value of
cube of the number.
which uses the same method
asynchronously to print the
9. numbers 1 to 10 using Thread-0
and to print 90 to 100 using
Thread-1
Write a program to demonstrate the
use of following exceptions.
a) Arithmetic Exception
10. b) Number Format Exception
c) Array Index Out of Bound
Exception
d) Negative Array Size
Exception
4
SOURCE CODING:
public class PrimeNumbers
{
public static void main(String args[])
{
int i,n,counter, j;
n= 10;
System.out.printf("Enter the n value is %d ", n);
System.out.printf("\nPrime numbers between 1 to %d are ", n);
for(j=2;j<=n;j++)
{
counter=0;
for(i=1;i<=j;i++)
{
if(j%i==0)
{
counter++;
}
}
if(counter==2)
System.out.print(j+" ");
}
}
}
EX-1
6
OUTPUT:
Enter the n value is 10
Prime numbers between 1 to 10 are 2 3 5 7
RESULTS:
Thus, the above java program executed successfully and output verified.
7
SOURCE CODING:
public class MatrixMultiplicationExample
public static void main(String args[])
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c[i][j]=0;
for(int k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
System.out.print(c[i][j]+" ");
System.out.println();//new line
}
9
OUTPUT:
666
12 12 12
18 18 18
RESULTS:
Thus, the above java program executed successfully and output verified.
10
SOURCE CODING:
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\hp\\Desktop\\TextReader.txt");
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new
InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
int wordCount = 0;
int characterCount = 0;
int paraCount = 0;
int whiteSpaceCount = 0;
int sentenceCount = 0;
while ((line = bufferedReader.readLine()) != null) {
if (line.equals("")) {
paraCount += 1;
} else {
characterCount += line.length();
12
String words[] = line.split("\\s+");
wordCount += words.length;
whiteSpaceCount += words.length - 1;
String sentence[] = line.split("[!?.:]+");
sentenceCount += sentence.length;
if (sentenceCount >= 1) {
paraCount++;
System.out.println("Total word count = " + wordCount);
System.out.println("Total number of sentences = " + sentenceCount);
System.out.println("Total number of characters = " + characterCount);
System.out.println("Number of paragraphs = " + paraCount);
System.out.println("Total number of whitespaces = " + whiteSpaceCount);
13
OUTPUT:
Total word count = 26
Total number of sentences = 3
Total number of characters = 130
Number of paragraphs = 4
Total number of whitespaces = 22
RESULTS:
Thus, the above java program executed successfully and output verified.
14
SOURCE CODING:
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
Random rand = new Random();
int max=100,min=50;
System.out.println("Generated numbers are within "+min+" to "+max);
System.out.println(rand.nextInt(max - min + 1) + min);
System.out.println(rand.nextInt(max - min + 1) + min);
System.out.println(rand.nextInt(max - min + 1) + min);
16
OUTPUT:
Generated numbers are within 50 to 100
76
89
53
RESULTS:
Thus, the above java program executed successfully and output verified.
17
SOURCE CODING:
public class Stringoperation5
public static void main(String args[])
String s=" Welcome to ISM department";
System.out.println(s.length());
19
OUTPUT:
26
RESULTS:
Thus, the above java program executed successfully and output verified.
20
SOURCE CODING:
public class CharAtExample {
public static void main(String[] args) {
String str = "Hello, World!";
int position = 7;
if (position >= 0 && position < str.length()) {
char result = str.charAt(position);
System.out.println("Character at position " + position + ": " + result);
} else {
System.out.println("Invalid position. Please provide a valid position within
the string length.");
22
OUTPUT:
Character at position 7: W
RESULTS:
Thus, the above java program executed successfully and output verified.
23
SOURCE CODING:
public class StringManipulation {
public static void main(String[] args) {
char[] str1 = {'H', 'e', 'l', 'l', 'o'};
char[] str2 = {' ', 'W', 'o', 'r', 'l', 'd'};
char[] result = new char[str1.length + str2.length];
System.arraycopy(str1, 0, result, 0, str1.length);
System.arraycopy(str2, 0, result, str1.length, str2.length);
System.out.println("Concatenated String: " + new String(result));
25
OUTPUT:
Concatenated String: Hello World
RESULTS:
Thus, the above java program executed successfully and output verified.
26
SOURCE CODING:
public class StringConcatenation {
public static void main(String[] args) {
// Initializing two strings
String str1 = "Hello, ";
String str2 = "World!";
// Concatenating the strings
String result = str1 + str2;
// Displaying the result
System.out.println(result);
28
OUTPUT:
Hello, World!
RESULTS:
Thus, the above java program executed successfully and output verified.
29
SOURCE CODING:
public class SubstringSearch {
public static void main(String[] args) {
// Example string
String mainString = "The quick brown fox jumps over the lazy dog.";
// Substring to search for
String substring = "brown";
// Check if the substring exists in the main string
if (mainString.contains(substring)) {
System.out.println("The substring \"" + substring + "\" is found in the main
string.");
} else {
System.out.println("The substring \"" + substring + "\" is not found in the
main string.");
31
OUTPUT:
The substring "brown" is found in the main string
RESULTS:
Thus, the above java program executed successfully and output verified.
32
SOURCE CODING:
public class SubstringExtraction {
public static void main(String[] args) {
// Input string
String mainString = "Hello, World!";
// Extracting a substring
String extractedSubstring = mainString.substring(7, 12);
// Displaying the extracted substring
System.out.println("Extracted Substring: " + extractedSubstring);
34
OUTPUT:
Extracted Substring: World
RESULTS:
Thus, the above java program executed successfully and output verified.
35
SOURCE CODING:
public class StringBufferLength {
public static void main(String[] args) {
// Input string
String inputString = "Hello, World!";
// Creating a StringBuffer from the input string
StringBuffer stringBuffer = new StringBuffer(inputString);
// Getting the length of the string using the length() method
int length = stringBuffer.length();
// Displaying the length of the string
System.out.println("Length of the string: " + length);
37
OUTPUT:
Length of the string: 13
RESULTS:
Thus, the above java program executed successfully and output verified.
38
SOURCE CODING:
public class StringBufferReverse {
public static void main(String[] args) {
// Input string
String inputString = "Hello, World!";
// Creating a StringBuffer from the input string
StringBuffer stringBuffer = new StringBuffer(inputString);
// Reversing the string using the reverse() method
stringBuffer.reverse();
// Displaying the reversed string
System.out.println("Reversed String: " + stringBuffer);
40
OUTPUT:
Reversed String: !dlroW ,olleH
RESULTS:
Thus, the above java program executed successfully and output verified.
41
SOURCE CODING:
public class StringBufferSubstringDeletion {
public static void main(String[] args) {
// Input string
StringBuffer mainString = new StringBuffer("Hello, World!");
// Substring to delete
String substringToDelete = "World";
// Deleting the substring
mainString.delete(mainString.indexOf(substringToDelete),
mainString.indexOf(substringToDelete) + substringToDelete.length());
// Displaying the modified string
System.out.println("String after deletion: " + mainString);
43
OUTPUT:
String after deletion: Hello, !
RESULTS:
Thus, the above java program executed successfully and output verified.
44
SOURCE CODING:
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()
47
{
int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub );
class Number extends Thread
public void run()
Random random = new Random();
for(int i =0; i<10; i++)
int randomInteger = random.nextInt(100);
System.out.println("Random Integer generated : " + randomInteger);
Square s = new Square(randomInteger);
s.start();
Cube c = new Cube(randomInteger);
c.start();
try {
Thread.sleep(1000);
catch (InterruptedException ex)
{
48
System.out.println(ex);
public class LAB3B {
public static void main(String args[])
Number n = new Number();
n.start();
49
OUTPUT:
Random Integer generated : 42
Square of 42 = 1764
Cube of 42 = 74088
Random Integer generated : 57
Square of 57 = 3249
Cube of 57 = 185193
Random Integer generated : 89
Square of 89 = 7921
Cube of 89 = 704969
Random Integer generated : 15
Square of 15 = 225
Cube of 15 = 3375
Random Integer generated : 37
Square of 37 = 1369
Cube of 37 = 50653
Random Integer generated : 61
Square of 61 = 3721
Cube of 61 = 226981
Random Integer generated : 43
Square of 43 = 1849
Cube of 43 = 79507
Random Integer generated : 99
50
Square of 99 = 9801
Cube of 99 = 970299
Random Integer generated : 39
Square of 39 = 1521
Cube of 39 = 59319
Random Integer generated : 56
Square of 56 = 3136
Cube of 56 = 175616
RESULTS:
Thus, the above java program executed successfully and output verified.
51
SOURCE CODING:
package com.xxxx.simpleapp;
import java.util.ArrayList;
import java.util.List;
public class TenThreads {
public int currentTaskValue = 1;
public static void main(String[] args) {
TenThreads monitor = new TenThreads();
List<ModThread> list = new ArrayList();
for (int i = 0; i < 10; i++) {
ModThread modThread = new ModThread(i, monitor);
list.add(modThread);
}
for (ModThread a : list) {
a.start();
}
}
class ModThread extends Thread {
private int modValue;
private TenThreads monitor;
public ModThread(int modValue, TenThreads monitor) {
this.modValue = modValue;
this.monitor = monitor;
}
@Override
54
public void run() {
synchronized (monitor) {
try {
while (true) {
while (monitor.currentTaskValue % 10 != modValue) {
monitor.wait();
}
if (monitor.currentTaskValue == 101) {
break;
}
System.out.println(Thread.currentThread().getName() + " : "
+ monitor.currentTaskValue + " ,");
monitor.currentTaskValue = monitor.currentTaskValue + 1;
monitor.notifyAll();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
55
OUTPUT:
Thread-1 : 1 ,
Thread-2 : 2 ,
Thread-3 : 3 ,
Thread-4 : 4 ,
Thread-5 : 5 ,
Thread-6 : 6 ,
Thread-7 : 7 ,
Thread-8 : 8 ,
Thread-9 : 9 ,
......
.....
...
Thread-4 : 94 ,
Thread-5 : 95 ,
Thread-6 : 96 ,
Thread-7 : 97 ,
Thread-8 : 98 ,
Thread-9 : 99 ,
Thread-0 : 100 ,
RESULTS:
Thus, the above java program executed successfully and output verified.
56
SOURCE CODING:
public class ExceptionDemo {
public static void main(String[] args) {
// Arithmetic Exception
try {
int result = 10 / 0; // Division by zero
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception caught: " + e.getMessage());
}
// Number Format Exception
try {
String str = "abc";
int num = Integer.parseInt(str);
} catch (NumberFormatException e) {
System.out.println("Number Format Exception caught: " +
e.getMessage());
}
// Array Index Out of Bound Exception
try {
int[] arr = new int[3];
int value = arr[5]; // Accessing index out of bound
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index Out of Bound Exception caught: " +
e.getMessage());
}
// Negative Array Size Exception
try {
int[] negativeArr = new int[-5]; // Creating array with negative size
} catch (NegativeArraySizeException e) {
System.out.println("Negative Array Size Exception caught: " +
e.getMessage());
59
}
}
}
60
OUTPUT:
Arithmetic Exception caught: / by zero
Number Format Exception caught: For input string: "abc"
Array Index Out of Bound Exception caught: Index 5 out of bounds for length 3
Negative Array Size Exception caught: -5
RESULTS:
Thus, the above java program executed successfully and output verified.
61