0% found this document useful (0 votes)
2 views33 pages

2java File

The document contains multiple Java programs demonstrating various programming concepts including type casting, scope operators, operators, loops, conditional statements, switch cases, patterns, matrix operations, constructors, abstract classes, exception handling, and multithreading. Each program is structured to illustrate specific functionalities and features of Java, such as arithmetic operations, control flow, and object-oriented principles. Overall, it serves as a comprehensive guide for understanding fundamental Java programming techniques.

Uploaded by

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

2java File

The document contains multiple Java programs demonstrating various programming concepts including type casting, scope operators, operators, loops, conditional statements, switch cases, patterns, matrix operations, constructors, abstract classes, exception handling, and multithreading. Each program is structured to illustrate specific functionalities and features of Java, such as arithmetic operations, control flow, and object-oriented principles. Overall, it serves as a comprehensive guide for understanding fundamental Java programming techniques.

Uploaded by

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

1-Program for Type Casting?

import [Link].*;
class Tc
{
public static void main(String[] args) {
// Implicit Type Casting
int a = 10;
double b = a; // int to double
[Link]("Implicit Type Casting:");
[Link]("Integer value: " + a);
[Link]("Double value: " + b);
// Explicit Type Casting
double x = 25.75;
int y = (int) x;
[Link]("\nExplicit Type Casting:");
[Link]("Double value: " + x);
[Link]("Integer value: " + y);
}

2- Program for Scope Operators?


import [Link].*;
class Scop {
int instanceVar = 10;
static int staticVar = 20;
void showScopes() {
int localVar = 30;
[Link](localVar);
[Link](instanceVar);
[Link](staticVar);
if (localVar > 20) {
int blockVar = 40;
[Link](blockVar);
}
}
public static void main(String[] args) {
ScopeDemo obj = new ScopeDemo();
[Link]();
[Link](staticVar);
}
}
3-Program for Operators?
import [Link].*;
class Operators {
public static void main(String[] args) {
int a = 10, b = 3; // Arithmetic Operators
[Link](a + b);
[Link](a - b);
[Link](a * b);
[Link](a / b);
[Link](a % b); // Relational Operators
[Link](a > b);
[Link](a < b);
[Link](a == b);
[Link](a != b);
[Link](a >= b);
[Link](a <= b); // Logical Operators
boolean x = true, y = false;
[Link](x && y);
[Link](x || y);
[Link](!x); // Assignment Operators
int c = 5;
c += 2;
[Link](c);
c -= 1;
[Link](c);
c *= 2;
[Link](c);
c /= 2;
[Link](c); // Unary Operators
int d = 7;
[Link](++d);
[Link](d++);
[Link](--d);
[Link](d--); // Ternary Operator
int max = (a > b) ? a : b;
[Link](max); // Bitwise Operators
int m = 5, n = 3;
[Link](m & n);
[Link](m | n);
[Link](m ^ n);
[Link](~m); // Shift Operators
[Link](m << 1);
[Link](m >> 1);
[Link](m >>> 1); } }

4-Program for loops?


import [Link].*;
class Al{
public static void main(String[] args) {
// for loop
for (int i = 1; i <= 5; i++) {
[Link](i);
}
// while loop
int j = 1;
while (j <= 5) {
[Link](j);
j++;
}
// do-while loop
int k = 1;
do {
[Link](k);
k++;
} while (k <= 5);
// nested loop
for (int a = 1; a <= 3; a++) {
for (int b = 1; b <= 3; b++) {
[Link](a + " " + b);
}
}
}}
5-Program for if ,else ,nested ifand else?
import [Link].*;
class Grade {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
[Link]("Grade A");
} else if (marks >= 75) {
[Link]("Grade B");
} else if (marks >= 60) {
[Link]("Grade C");
} else if (marks >= 40) {
[Link]("Grade D");
} else {
[Link]("Fail");
}
}
}
6-Program for Switch?
import [Link].*;
class Switch{
public static void main(String[] args) {
int a = 20;
int b = 10;
for (int choice = 1; choice <= 5; choice++) {
switch (choice) {
case 1:
[Link]("Addition = " + (a + b));
break;
case 2:
[Link]("Subtraction = " + (a - b));
Continue;
case 3:
[Link]("Multiplication = " + (a * b));
break;
case 4:
[Link]("Division = " + (a / b));
Break;
case 5:
[Link]("Modulus = " + (a % b));
break;
default:
[Link]("Invalid Choice");
}
[Link]("Operation Finished");
} }}
7- 1
22
333
4444
import [Link].*;
class Pattern {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
[Link](i);
}
[Link]();
}
}
}

8-****
***
**
*
import [Link].*;
class StarPattern {
public static void main(String[] args) {
for (int i = 4; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
}
}

9-Program to calculate volume of cube and cylinder?


import [Link].*;
class VolumeSwitch {
public static void main(String[] args) {
for (int choice = 1; choice <= 3; choice++) {
switch (choice) {
case 1:
int side = 5;
int cubeVolume = side * side * side;
[Link]("Volume of Cube = " + cubeVolume);
break;
case 2:
double radius = 3;
double height = 7;
double cylinderVolume = 3.14 * radius * radius * height;
[Link]("Volume of Cylinder = " + cylinderVolume);
continue;
case 3:
[Link]("End of Program");
break;
default:
[Link]("Invalid choice");
}
[Link]("Calculation Done");
} }}
10-Program of matrix?
import [Link].*;
class Matrix {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[i].length; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
}
}
11- Program for matrix addition,substraction and transpose?
import [Link].*;
class MatrixOperations {
public static void main(String[] args) {
int[][] A = {
{1, 2, 3},
{4, 5, 6}
};
int[][] B = {
{6, 5, 4},
{3, 2, 1}
};
int rows = [Link];
int cols = A[0].length;
int[][] add = new int[rows][cols];
int[][] sub = new int[rows][cols];
[Link]("Matrix Addition:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
add[i][j] = A[i][j] + B[i][j];
[Link](add[i][j] + " ");
}
[Link]();
}
[Link]("\nMatrix Subtraction:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sub[i][j] = A[i][j] - B[i][j];
[Link](sub[i][j] + " ");
}
[Link]();
}
[Link]("\nTranspose of Matrix A:");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
[Link](A[j][i] + " ");
}
[Link]();
}
}
}
12-Program to print of a matrix boundary elements?
import [Link].*;
class BoundaryElements {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} };
int rows = [Link];
int cols = matrix[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) {
[Link](matrix[i][j] + " ");
} else {
[Link](" ");
}
}
[Link]();
}
}
}

13-Program for Diagonal matrix?


import [Link].*;
class DiagonalMatrix {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int rows = [Link];
int cols = matrix[0].length;

[Link]("Main Diagonal Elements:");


for (int i = 0; i < rows; i++) {
[Link](matrix[i][i] + " ");
}
[Link]("\nSecondary Diagonal Elements:");
for (int i = 0; i < rows; i++) {
[Link](matrix[i][cols - i - 1] + " ");
}
}
}

14-Program for matrix multiplication?


import [Link].*;

class MatrixMultiplication {
public static void main(String[] args) {
int[][] A = {
{1, 2, 3},
{4, 5, 6}
};

int[][] B = {
{7, 8},
{9, 10},
{11, 12}
};

int r1 = [Link];
int c1 = A[0].length;
int r2 = [Link];
int c2 = B[0].length;
int[][] result = new int[r1][c2];
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
result[i][j] = 0;
for (int k = 0; k < c1; k++) {
result[i][j] += A[i][k] * B[k][j];
} }
}
[Link]("Result of Matrix Multiplication:");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
[Link](result[i][j] + " ");
}
[Link]();
}
}
}

15-Program for all types of constructors?


class ConstructorDemo {
int a;
String name;
ConstructorDemo() {
a = 0;
name = "Default";
[Link]("Default Constructor: " + a + " " + name);
}ConstructorDemo(int x, String n) {
a = x;
name = n;
[Link]("Parameterized Constructor: " + a + " " + name);
}
ConstructorDemo(ConstructorDemo obj) {
a = obj.a;
name = [Link];
[Link]("Copy Constructor: " + a + " " + name);
}
Public static void main(String[] args) {
ConstructorDemo obj1 = new ConstructorDemo();
ConstructorDemo obj2 = new ConstructorDemo(10, "Java");
ConstructorDemo obj3 = new ConstructorDemo(obj2);
}
}

16-Program for constructor overloading?


class ConOver {
int a;
double b;
ConOver() {
a = 0;
b = 0;
[Link]("Default Constructor: " + a + " " + b);
}ConOver(int x) {
a = x;
[Link]("One Parameter Constructor: " + a);
}
ConOver(int x, double y) {
a = x;
b = y;
[Link]("Two Parameter Constructor: " + a + " " + b);
}
public static void main(String[] args) {
ConOverobj1 = new ConOver();
ConOver obj2 = new ConOver(10);
ConOver obj3 = new ConOver(5, 7.5);
}
}

18-Program for abstract classes?


abstract class BankAccount {
double balance;
abstract void deposit(double amount);
abstract void withdraw(double amount);
}
class SavingsAccount extends BankAccount {
void deposit(double amount) {
balance += amount;
[Link]("Savings Deposit: " + amount);
[Link]("Balance: " + balance);
}
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
[Link]("Savings Withdraw: " + amount);
} else {
[Link]("Insufficient Balance in Savings");
}
[Link]("Balance: " + balance);
}
}
class CurrentAccount extends BankAccount {
void deposit(double amount) {
balance += amount;
[Link]("Current Deposit: " + amount);
[Link]("Balance: " + balance);
}
void withdraw(double amount) {
balance -= amount;
[Link]("Current Withdraw: " + amount);
[Link]("Balance: " + balance);
}
}
class Main {
public static void main(String[] args) {
SavingsAccount sa = new SavingsAccount();
[Link](1000);
[Link](500);
CurrentAccount ca = new CurrentAccount();
[Link](2000);
[Link](2500);
}}
14-Program to use of this key word
class ThisDemo {
int a;
ThisDemo(int a) {
this.a = a;
}
void show() {
[Link]("Value of a = " + a);
}
public static void main(String[] args) {
ThisDemo obj = new ThisDemo(10);
[Link]();
}
}
15-Program to Demonstrate Exception Handling using try-catch-finally
import [Link].*;
class ExceptionDemo {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
int result = a / b;
[Link](result);
}
catch (ArithmeticException e) {
[Link]("Cannot divide by zero");
}
finally {
[Link]("Finally block executed");
}
[Link]("Program continues...");
}
}
16-Program to Create and Use a User-Defined Exception Class
class MyEx extends Exception{
MyEx(String m){
super(m);
}
}

class T{
public static void main(String[] a){
try{
int x=10;
if(x<18){
throw new MyEx("Not Eligible");
}
else{
[Link]("Eligible");
}
}
catch(MyEx e){
[Link]([Link]());
}
}
}
17-Program to Demonstrate Thread Creation Using Thread Class
class T extends Thread{
public void run(){
for(int i=1;i<=3;i++){
[Link](getName()+" "+i);
try{
[Link](300);
}catch(Exception e){}
}
}
}
class M{
public static void main(String[] a){
T t1=new T();
T t2=new T();
[Link]("T1");
[Link]("T2");
[Link](8);
[Link](3);
[Link]();
[Link]();
}
}
[Link] to Demonstrate Multithreading Using Runnable Interface with
Synchronization and Sleep
class X{
synchronized void show(int n){
[Link]("Start");
for(int i=1;i<=5;i++){
[Link](n*i);
try{
[Link](500);
}catch(Exception e){
[Link]("Error");
}
}
[Link]("End");
}
}
class Y implements Runnable{
X obj;
int n;
Y(X obj,int n){
[Link]=obj;
this.n=n;
}
public void run(){
[Link](n);
}
}

class M{
public static void main(String[] a){
X ob=new X();
Thread t1=new Thread(new Y(ob,2));
Thread t2=new Thread(new Y(ob,5));
Thread t3=new Thread(new Y(ob,10));
[Link]();
[Link]();
[Link]();
}
}
[Link] of multithreading (without synchronization)
class X{
void show(int n){
[Link]("Start");
for(int i=1;i<=5;i++){
[Link](n*i);
try{
[Link](500);
}catch(Exception e){
[Link]("Error");
}
}
[Link]("End");
}
}
class Y implements Runnable{
X obj;
int n;
Y(X obj,int n){
[Link]=obj;
this.n=n;
}
public void run(){
[Link](n);
}
}
class M{
public static void main(String[] a){
X ob=new X();
Thread t1=new Thread(new Y(ob,2));
Thread t2=new Thread(new Y(ob,5));
Thread t3=new Thread(new Y(ob,10));
[Link]();
[Link]();
[Link]();
}
}
20-Program to Demonstrate Multithreading with Exception Handling (try-
catch and join method)
import [Link];
class Message{
String msg;
public void readMessage(){
try{
Scanner sc=new Scanner([Link]);
[Link]("Type the Message: ");
msg=[Link]();
}catch(Exception e){
[Link]("Error");
}
}
public void writeMessage(){
[Link]("Message: "+msg);
}
}
class Race extends Thread{
Message ms;
Race(Message ms){
[Link]=ms;
}
public void run(){
[Link]();
}
}
class Print extends Thread{
Message ms;
Print(Message ms){
[Link]=ms;
}
public void run(){
[Link]();
}
}
public class New{
public static void main(String[] args){
Message myMessage=new Message();
Race raceThread=new Race(myMessage);
Print printThread=new Print(myMessage);
[Link]();
try{
[Link]();
}catch(InterruptedException e){
[Link]();
}
[Link]();
}
}

You might also like