Java Programming Lab
Java Programming Lab
I B.COM ISM
Name of Student :
Batch Number :
1
COLLEGE
92 / 1 & 9, Muthamizh Nagar,
Kodungaiyur, Chennai – 118.
B.COM ISM
INFORMATION MANAGEMENT SYSTEM
JAVA PROGRAMMING LAB
Name of Student :
Batch Number :
4
Ex.No:01 AN INTEGER AND THEN PRINTS OUT ALL THE
Date: PRIME NUMBERS UP TO THAT INTEGER
AIM:
To write a java program that prompts the user for an integer and then prints
out all the prime numbers up to that Integer.
PROCEDURE:
Step 1- Start
Step 9- Stop
5
SOURCE CODING:
6
OUTPUT:
RESULTS:
Thus, the above java program executed successfully and output verified.
7
Ex.No:02 WRITE A JAVA PROGRAM TO MULTIPLY TWO
Date: GIVEN MATRICES
AIM:
PROCEDURE:
Step 4: For each row of 'a', iterate through each column of 'b'.
8
SOURCE CODING:
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,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
Ex.No:03 WRITE A JAVA PROGRAM THAT DISPLAYS THE
Date: NUMBER OF CHARACTERS, LINES AND WORDS
IN A TEXT?
AIM:
Write a Java program that displays the number of characters, lines and
words in a text.
PROCEDURE:
Step 8: Read the next line from the file and go back to step 4.
11
SOURCE CODING:
import java.io.*;
String line;
int wordCount = 0;
int characterCount = 0;
int paraCount = 0;
int whiteSpaceCount = 0;
int sentenceCount = 0;
if (line.equals("")) {
paraCount += 1;
} else {
characterCount += line.length();
12
String words[] = line.split("\\s+");
wordCount += words.length;
whiteSpaceCount += words.length - 1;
sentenceCount += sentence.length;
if (sentenceCount >= 1) {
paraCount++;
13
OUTPUT:
Number of paragraphs = 4
RESULTS:
Thus, the above java program executed successfully and output verified.
14
Ex.No:04 GENERATE RANDOM NUMBERS BETWEEN TWO
Date: GIVEN LIMITS USING RANDOM CLASS AND
PRINT MESSAGES ACCORDING TO THE RANGE
OF THE VALUE GENERATED
AIM:
PROCEDURE:
Step 8: Repeat steps 7 for two more times to generate and print two
additional random numbers.
15
SOURCE CODING:
import java.io.*;
import java.util.*;
class GFG {
int max=100,min=50;
16
OUTPUT:
76
89
53
RESULTS:
Thus, the above java program executed successfully and output verified.
17
Ex.No:05-A WRITE A PROGRAM TO DO STRING
MANIPULATION USING CHARACTER ARRAY AND
Date:
PERFORM THE FOLLOWING STRING
OPERATIONS:
“A-STRING LENGTH”
AIM:
PROCEDURE:
Step 3: Declare a string variable s and initialize it with the value "
Welcome to ISM department".
Step 4: Print the length of the string s using the length() method.
18
SOURCE CODING:
System.out.println(s.length());
19
OUTPUT:
26
RESULTS:
Thus, the above java program executed successfully and output verified.
20
Ex.No:05-B WRITE A PROGRAM TO DO STRING
MANIPULATION USING CHARACTER ARRAY AND
Date: PERFORM THE FOLLOWING STRING
OPERATIONS:
“B- FINDING A CHARACTER AT A PARTICULAR
POSITION”
AIM:
PROCEDURE:
Step 5: Check if the position is within the valid range (greater than
or equal to 0 and less than the length of the string).
21
SOURCE CODING:
int position = 7;
} else {
22
OUTPUT:
Character at position 7: W
RESULTS:
Thus, the above java program executed successfully and output verified.
23
Ex.No:05-C WRITE A PROGRAM TO DO STRING
MANIPULATION USING CHARACTER ARRAY AND
Date:
PERFORM THE FOLLOWING STRING
OPERATIONS:
“C-Concatenating two strings”
AIM:
PROCEDURE:
Step 3: Declare two char arrays "str1" and "str2" containing the
characters of "Hello" and " World" respectively.
Step 4: Create a new char array "result" with a length equal to the
sum of the lengths of "str1" and "str2".
Step 7: Convert the "result" char array to a String and print it,
displaying the concatenated string with space between
"Hello" and "World".
24
SOURCE CODING:
25
OUTPUT:
RESULTS:
Thus, the above java program executed successfully and output verified.
26
Ex.No:06-A
Date: String operations using String class,
“A-String Concatenation”
AIM:
Write a Java program that displays the String operations using String
class, “String Concatenation”
PROCEDURE:
Step 3: Initialize two strings "str1" with the value "Hello, " and
"str2" with the value "World!".
Step 4: Concatenate the strings "str1" and "str2" using the '+'
operator, storing the result in a new string variable "result".
27
SOURCE CODING:
System.out.println(result);
28
OUTPUT:
Hello, World!
RESULTS:
Thus, the above java program executed successfully and output verified.
29
Ex.No:06-B
Date: String operations using String class,
“B-Search a substring”
AIM:
Write a Java program that displays the String operations using String
class, “Search a substring”
PROCEDURE:
Step 5: Use the contains method of the String class to check if the
"mainString" contains the "substring".
Step 7: If the substring is not found within the main string, print a
message indicating its absence.
30
SOURCE CODING:
// Example string
String mainString = "The quick brown fox jumps over the lazy dog.";
if (mainString.contains(substring)) {
} else {
31
OUTPUT:
RESULTS:
Thus, the above java program executed successfully and output verified.
32
Ex.No:06-C
Date: String operations using String class,
“C-To extract substring from given string”
AIM:
Write a Java program that displays the String operations using String
class, “To extract substring from given string”
PROCEDURE:
33
SOURCE CODING:
// Input string
// Extracting a substring
34
OUTPUT:
RESULTS:
Thus, the above java program executed successfully and output verified.
35
Ex.No:07-A
Date: String operations using StringBuffer class,
“A-Length of a string”
AIM:
PROCEDURE:
36
SOURCE CODING:
// Input string
37
OUTPUT:
RESULTS:
Thus, the above java program executed successfully and output verified.
38
Ex.No:07-B
Date: String operations using StringBuffer class,
“B-Reverse a string”
AIM:
PROCEDURE:
39
SOURCE CODING:
// Input string
stringBuffer.reverse();
40
OUTPUT:
RESULTS:
Thus, the above java program executed successfully and output verified.
41
Ex.No:07-C
Date: String operations using StringBuffer class,
“C-Delete a substring from the given string”
AIM:
PROCEDURE:
42
SOURCE CODING:
// Input string
// Substring to delete
mainString.delete(mainString.indexOf(substringToDelete),
mainString.indexOf(substringToDelete) + substringToDelete.length());
43
OUTPUT:
RESULTS:
Thus, the above java program executed successfully and output verified.
44
Ex.No:08 That implements a multi-thread application that has three
Date: threads. First thread generates random integer every 1 second
and if the 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.
AIM:
PROCEDURE:
45
Step 9 : Define the main method in the LAB3B class.
Step 11: Start the thread by calling the start() method on the
Number object.
46
SOURCE CODING:
import java.util.Random;
int x;
Square(int n)
x = n;
int sqr = x * x;
int x;
Cube(int n)
x = n;
int cub = x * x * x;
s.start();
c.start();
try {
Thread.sleep(1000);
{
48
System.out.println(ex);
n.start();
49
OUTPUT:
Square of 42 = 1764
Cube of 42 = 74088
Square of 57 = 3249
Cube of 57 = 185193
Square of 89 = 7921
Cube of 89 = 704969
Square of 15 = 225
Cube of 15 = 3375
Square of 37 = 1369
Cube of 37 = 50653
Square of 61 = 3721
Cube of 61 = 226981
Square of 43 = 1849
Cube of 43 = 79507
Cube of 99 = 970299
Square of 39 = 1521
Cube of 39 = 59319
Square of 56 = 3136
Cube of 56 = 175616
RESULTS:
Thus, the above java program executed successfully and output verified.
51
Ex.No:09
Which uses the same method asynchronously to print the
Date:
numbers 1 to 10 using thread-0 and to print 90 to 100 using
thread-1
AIM:
PROCEDURE:
Step 9: Use a while loop to wait until the current task value
modulo 10 equals the "modValue".
Step 10: Check if the current task value is 101, if so, break the
loop.
Step 11: Print the thread name and the current task value.
52
Step 12: Increment the current task value.
53
SOURCE CODING:
package com.xxxx.simpleapp;
import java.util.ArrayList;
import java.util.List;
@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
Ex.No:10 Write a program to demonstrate the use of following
Date: exceptions.
a) Arithmetic Exception
b) Number Format Exception
c) Array Index Out of Bound Exception
d) Negative Array Size Exception
AIM:
PROCEDURE:
57
Step 3.C: Handle Array Index Out of Bound Exception:
- Create an integer array arr with a length of 3.
- Access an element at index 5 of the array arr.
- Catch the ArrayIndexOutOfBoundsException and print a
message along with the exception's message.
58
SOURCE CODING:
59
}
}
}
60
OUTPUT:
RESULTS:
Thus, the above java program executed successfully and output verified.
61