Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Code and Op PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Coding

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();

System.out.println("\n Prime numbers are:\n\t");


for(i=1;i<=e;i++)
if (isPrime(i))
System.out.print("\t"+ i);
}
public static boolean isPrime(int n)
{int i;
if (n <= 1)
return false; // 0 and 1 are not prime numbers
for (i = 2; i < n; i++)
{ if (n % i == 0) return false; }
return true;
}
}

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;

public class cwlcc


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int l = 0, w = 0, c = 0;
System.out.println("Enter your text");

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
l++;
c += line.length();

// Count words using String.split()


String[] wordsArray = line.split("\\s+");
w+= wordsArray.length;
}

System.out.println("Lines: " + l);


System.out.println("Words: " + w);
System.out.println("Characters: " + c);
}
}

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;

public class rng {


public static void main(String[] args) {
// Define the limits
int ll = 10;
int ul = 50;

// Create an instance of the Rclass


Random r= new Random();

// Generate a rnumber between ll (inclusive) and ul (inclusive)


int rn= r.nextInt((ul - ll) + 1) + ll;

// Print the generated rnumber


System.out.println("Generated Random Number: " + rn);

// Print a message according to the range of the value generated


if (rn< 20) {
System.out.println("The number is less than 20.");
} else if (rn>= 20 && rn<= 40) {
System.out.println("The number is between 20 and 40.");
} else {
System.out.println("The number is greater than 40.");
}
}
}

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.*;

public class strman {


public static void main(String[] args) {
String s1 = "disney", s2 = " world";
char[] c1 = s1.toCharArray(), c2 = s2.toCharArray();

int length = c1.length;


System.out.println("Length of " +s1 +" is: "+length);
length = c2.length;
System.out.println("Length of " +s2 +" is: "+length);

char character = c1[2];


System.out.println("Character at 3 place: " + character);

char[] c3 = new char[c1.length + c2.length];


System.arraycopy(c1, 0, c3, 0, c1.length);
System.arraycopy(c2, 0, c3, c1.length, c2.length);
String concatenatedString = new String(c3);
System.out.println("Concatenated: " + concatenatedString);
}
}

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

public class stropr {


public static void main(String[] args) {
// Example strings
String str1 = "Hello";
String str2 = "World";
String str3 = "Java Programming";

// 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) );
}
}

class Cube extends Thread


{
int x;
Cube(int n)
{
x = n;
}
public void run()
{
System.out.println("Cube of " + x + " = " + (x*x*x) );
}
}

class Number extends Thread


{
public void run()
{
Random r = new Random();
int rs = r.nextInt(100);

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();}
}
}

public class squcub {


public static void main(String args[])
{
Number n = new Number();
n.start();
}
}

Sample output
Random Integer generated : 50 is even
Square of 50 = 2500

Random Integer generated : 43 is odd


Cube of 43 = 79507

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);
}
}
}

class Thread2 extends Thread {


public void run() {
for (int i = 90; i <= 100; i++) {
System.out.println("Thread2: " + i);
}
}
}

public class asyncprt {


public static void main(String[] args) {
Thread1 thread1 = new Thread1();
Thread2 thread2 = new Thread2();

thread1.start();
thread2.start();

try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("Both threads have finished.");


}
}
Sample output
Thread2: 90
Thread2: 91
Thread1: 1
Thread2: 92
Thread2: 93
Thread2: 94
Thread2: 95
Thread1: 2
Thread2: 96
Thread1: 3
Thread1: 4
Thread1: 5
Thread2: 97
Thread2: 98
Thread2: 99
Thread2: 100
Thread1: 6
Thread1: 7
Thread1: 8
Thread1: 9
Thread1: 10
Both threads have finished.

Result
Thus, the above program has been executed and verified successfully.
Coding

public class excep


{
public static void main(String[] args)
{

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.

You might also like