Java Code and Op PDF
Java Code and Op PDF
Java Code and Op PDF
import java.util.Scanner;
public class primes
{
public static void main(String[] args)
{
int i,e;
Scanner s=new Scanner(System.in);
System.out.println("Enter the end value: ");
e=s.nextInt();
Sample output
Enter the end value:
50
Prime numbers are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Result
Thus, the above program has been executed and verified successfully.
Coding
import java.io.*;
public class matmul
{
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];
System.out.println("MATRIX MULTIPLICATION");
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( );
}
}
}
Sample output
MATRIX MULTIPLICATION
666
12 12 12
18 18 18
Result
Thus, the above program has been executed and verified successfully.
Coding
import java.util.Scanner;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
l++;
c += line.length();
Sample output
Enter your text
apple
orange
grapes
banana
^Z
Lines: 4
Words: 4
Characters: 23
Result
Thus, the above program has been executed and verified successfully.
Coding
import java.util.Random;
Sample output
Generated Random Number: 38
The number is between 20 and 40.
Result
Thus, the above program has been executed and verified successfully.
Coding
import java.io.*;
Sample output
Length of disney is: 6
Length of world is: 6
Character at 3 place: s
Concatenated: disney world
Result
Thus the above program has been executed and verified successfully.
Coding
// a) String Concatenation
String concatenatedString = str1.concat(" ").concat(str2);
System.out.println("Concatenated String: " + concatenatedString);
// b) Search a substring
String searchString = "Programming";
boolean containsSubstring = str3.contains(searchString);
System.out.println("Does '" + str3 + "' contain '" + searchString + "'? " + containsSubstring);
// c) Extract a substring
int startIndex = 5; // Start index of the substring
int endIndex = 11; // End index of the substring
String extractedSubstring = str3.substring(startIndex, endIndex);
System.out.println("Extracted Substring: " + extractedSubstring);
}
}
Sample output
Concatenated String: Hello World
Does 'Java Programming' contain 'Programming'? true
Extracted Substring: Progra
Result
Thus, the above program has been executed and verified successfully.
Coding
import java.lang.*;
public class sbo
{
public static void main(String[] args)
{
StringBuffer str = new StringBuffer("Hello, World!");
System.out.println("Given String is: " + str);
System.out.println("String Length: " + str.length());
str.reverse();
System.out.println("Reversed string: " + str);
str.reverse();
str.delete(5, 12);
System.out.println("Deleted: " + str);
}
}
Sample output
Given String is: Hello, World!
String Length: 13
Reversed string: !dlroW ,olleH
Deleted: Hello!
Result
Thus, the above program has been executed and verified successfully.
Coding
import java.util.Random;
class Square extends Thread
{
int x;
Square(int n)
{
x = n;
}
public void run()
{
System.out.println("Square of " + x + " = " + (x*x) );
}
}
if(rs%2==0)
{System.out.println("Random Integer generated : " + rs+" is even");
Square s = new Square(rs);
s.start();}
else
{System.out.println("Random Integer generated : " + rs+" is odd");
Cube c = new Cube(rs);
c.start();}
}
}
Sample output
Random Integer generated : 50 is even
Square of 50 = 2500
Result
Thus, the above program has been executed and verified successfully.
Coding
import java.lang.*;
class Thread1 extends Thread {
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Thread1: " + i);
}
}
}
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Result
Thus, the above program has been executed and verified successfully.
Coding
try
{
int result = 10 / 0;
}
catch (ArithmeticException e)
{
System.out.println("ArithmeticException caught: " + e.getMessage());
}
try
{
int num = Integer.parseInt("abc");
}
catch (NumberFormatException e)
{
System.out.println("NumberFormatException caught: " + e.getMessage());
}
try
{
int[] array = new int[5];
int value = array[10];
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBoundsException caught: " + e.getMessage());
}
try
{
int[] negativeArray = new int[-5];
}
catch (NegativeArraySizeException e)
{
System.out.println("NegativeArraySizeException caught: " + e.getMessage());
}
}
}
Sample output
ArithmeticException caught: / by zero
NumberFormatException caught: For input string: "abc"
ArrayIndexOutOfBoundsException caught: Index 10 out of bounds for length 5
NegativeArraySizeException caught: -5
Result
Thus, the above program has been executed and verified successfully.