0% found this document useful (0 votes)
9 views2 pages

Java Programming Lab Exercises

The document contains simplified Java programming lab exercises that cover various topics including prime number generation, matrix multiplication, character/word/line counting from a file, random number generation, character array operations, and string manipulations. Each section provides a code snippet demonstrating the respective functionality. These examples serve as practical exercises for learning basic Java programming concepts.

Uploaded by

rsingam464
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Java Programming Lab Exercises

The document contains simplified Java programming lab exercises that cover various topics including prime number generation, matrix multiplication, character/word/line counting from a file, random number generation, character array operations, and string manipulations. Each section provides a code snippet demonstrating the respective functionality. These examples serve as practical exercises for learning basic Java programming concepts.

Uploaded by

rsingam464
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA PROGRAMMING LAB - SIMPLIFIED PROGRAMS

1■■ Prime Numbers


import [Link].*;
class Prime {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
for (int i = 2; i <= n; i++) {
boolean prime = true;
for (int j = 2; j <= i / 2; j++)
if (i % j == 0) { prime = false; break; }
if (prime) [Link](i);
}
}
}

2■■ Matrix Multiplication


import [Link].*;
class MatrixMul {
public static void main(String[] a) {
Scanner s = new Scanner([Link]);
int r1=[Link](), c1=[Link](), r2=[Link](), c2=[Link]();
if (c1!=r2) return;
int[][] m1=new int[r1][c1], m2=new int[r2][c2], res=new int[r1][c2];
for(int i=0;i<r1;i++)for(int j=0;j<c1;j++)m1[i][j]=[Link]();
for(int i=0;i<r2;i++)for(int j=0;j<c2;j++)m2[i][j]=[Link]();
for(int i=0;i<r1;i++)for(int j=0;j<c2;j++)
for(int k=0;k<c1;k++) res[i][j]+=m1[i][k]*m2[k][j];
for(int[] row:res){for(int x:row)[Link](x+" ");[Link]();}
}
}

3■■ Count Characters, Words, Lines


import [Link].*;
class CountFile {
public static void main(String[] args)throws Exception {
BufferedReader br=new BufferedReader(new FileReader("[Link]"));
int chars=0, words=0, lines=0; String str;
while((str=[Link]())!=null){
lines++; String[] w=[Link]().split("\s+");
if(![Link]().isEmpty()){ words+=[Link]; for(String x:w) chars+=[Link](); }
}
[Link]("Chars:"+chars+" Words:"+words+" Lines:"+lines);
}
}

4■■ Random Numbers


import [Link].*;
class RandomNum {
public static void main(String[] a){
Random r=new Random();
for(int i=0;i<5;i++) [Link]([Link](100));
}
}
5■■ String Manipulation (Character Array)
import [Link].*;
class CharArrayOps {
public static void main(String[] args){
Scanner s=new Scanner([Link]);
char[] a=[Link]().toCharArray();
char[] b=[Link]().toCharArray();
[Link]("Length:"+[Link]);
int p=[Link]();
if(p>=0&&p<[Link]) [Link]("Char:"+a[p]);
char[] c=new char[[Link]+[Link]];
[Link](a,0,c,0,[Link]);
[Link](b,0,c,[Link],[Link]);
[Link]("Concat:"+new String(c));
}
}

6■■ String Operations (String Class)


import [Link].*;
class StringOps {
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
String s1=[Link](), s2=[Link]();
[Link]("Concat:"+s1+s2);
String main=[Link](), sub=[Link]();
[Link]([Link](sub)?"Found":"Not Found");
String ex=[Link]();
int st=[Link](), en=[Link]();
[Link]("Substr:"+[Link](st,en));
}
}

Common questions

Powered by AI

Incorrect index handling when accessing characters can lead to ArrayIndexOutOfBoundsException, which occurs if an attempt is made to access an index outside the permissible range of the array. This not only causes runtime errors but may also result in application crashes, jeopardizing program reliability and integrity .

For matrix multiplication to occur, the number of columns in the first matrix (c1) must be equal to the number of rows in the second matrix (r2). If this condition is not met, the program will terminate without performing multiplication .

The program uses the Random class from java.util to generate random numbers. It calls the nextInt method with the argument 100, which generates random integers between 0 (inclusive) and 100 (exclusive).

The program reads two strings, 'main' and 'sub', from input. It then checks if 'sub' is part of 'main' by using the contains method of the String class, which returns true if 'sub' is found within 'main'. The program prints "Found" if true, otherwise "Not Found" .

Concatenation is performed by first determining the length of the resulting char array, which is the sum of the lengths of the two input char arrays. A new char array 'c' is created with this combined length. The System.arraycopy method is used twice: first to copy all elements from the first array 'a' to 'c', and then to copy all elements from the second array 'b' starting at the position after the last element of 'a' in 'c' .

The substring extraction is accomplished using the substring method of the String class. It requires two integer parameters: starting index 'st' and ending index 'en'. The substring from the starting index up to but not including the ending index is returned .

The program checks each number from 2 to n (inclusive) to identify prime numbers. For each number i, it assumes it is prime and then checks if it is divisible by any number j from 2 to i/2. If i is divisible by j, it sets the 'prime' flag to false and breaks out of the loop. If the number remains prime, it is printed as a prime number .

Not checking for valid matrix dimensions could result in incorrect operations or program crashes. If the number of columns in the first matrix does not match the number of rows in the second, multiplication is mathematically invalid, leading to array index errors, null calculations, or undefined behavior in programs .

Buffering is significant in file handling as it reduces the number of I/O operations performed, which enhances performance. When a BufferedReader is used in Java, it reads chunks of characters into a buffer from the file, allowing subsequent reads to occur from the buffer, which is faster than direct reads from a file due to reduced interaction with the disk hardware .

The program reads a text file line by line, incrementing the 'lines' counter each time a line is read. It splits each non-empty, trimmed line into words using the '' delimiter, incrementing the 'words' counter by the number of words found. For each word, it increments the 'chars' counter by the length of the word, aggregating the total number of characters .

You might also like