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

Java Assignment

The document contains several Java programs demonstrating various functionalities, including reversing a 3-digit number, adding two numbers, calculating areas of different shapes, converting days into years, months, weeks, summing matrices, and handling exceptions. Each program is presented with its code and expected output. The document also includes examples of using threads and non-static functions in Java.

Uploaded by

pk1505198
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)
3 views33 pages

Java Assignment

The document contains several Java programs demonstrating various functionalities, including reversing a 3-digit number, adding two numbers, calculating areas of different shapes, converting days into years, months, weeks, summing matrices, and handling exceptions. Each program is presented with its code and expected output. The document also includes examples of using threads and non-static functions in Java.

Uploaded by

pk1505198
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

Page |1

PROGRAM-1

Write a Java Program to Reverse a 3-Digit Number.

import java.u l.*;


class Rev
{
public sta c void main(String args[])
{
Int n = 425, a = 0, b = 0, c = 0, d = 0, Rev =0;
a = n % 10;
b = n / 10;
c = b % 10;
d = b / 10;
Rev = a * 100 + c * 10 + d;
[Link]("The Reverse No = " + Rev);
}
}

Output:
524
Page |2

PROGRAM-2

Write a Java Program to Add Two Numbers.

import java.u l.*;


class Sum
{
public sta c void main(String args[])
{
int a = 10, b = 5, s = 0;
s = a + b;
[Link]("The sum Value = " + s);
}
}

Output:
15
Page |3

PROGRAM-3

Write a Menu Based Program To Calcution Of Area:-


[Link]
[Link]
[Link]

Import java.u l;
class Area
{
public sta c void main(String args[])
{
Scanner sc = new
Scanner([Link]);
int r, l, b, ch;
double area, base, height, x;
[Link]("Calcula on");
[Link]("1. Circle");
[Link]("2. Rectangle");
[Link]("3. Triangle");
[Link]("Enter your choice:");
ch = [Link]();
switch (ch)
{
case 1:
[Link]("Enter value of r: ");
r = [Link]();
Page |4

area = 3.14 * r * r;
[Link]("The value of Area: " + area);
break;
case 2:
[Link]("Enter value of l: ");
l = [Link]();
[Link]("Enter value of b: ");
b = [Link]();
area = l * b;
[Link]("The Area of Rectangle: " + area);
break;
case 3:
[Link]("Enter value of base: ");
base = [Link]();
[Link]("Enter value of height: ");
height = [Link]();
x = 0.5 * base * height;
[Link]("The value of the Area of triangle: " + x);
break;
default:
[Link]("Wrong choice!");
break;
}
}
}
Page |5

PROGRAM-4

WAP In Java To Convert Day Into Year,Month,Week


And Remaining Days.

import java.u l.*;


class Day
{
public sta c void main(String args[])
{
int day = 7653, y = 0, m = 0, w = 0;
int day2 = 0, day3 = 0, day4 = 0;
y = day / 365;
day2 = day % 365;
m = day2 / 30;
day3 = day2 % 30;
w = day3 / 7;
day4 = day3 % 7;
[Link]("Year = " + y);
[Link]("Month = " + m);
[Link]("Week = " + w);
[Link]("rem day = " + day4);
}
}
Page |6

PROGRAM-5

WAP in Java To Sum 3*3 Two Matrix.

public class MatrixSum

{
public sta c void main(String[] args)

int a[][] = {{1,2,3},{4,5,6},{7,8,9}};


int b[][] = {{9,8,7},{6,5,4},{3,2,1}};
int sum[][] = new int[3][3];

for(int i=0; i<3; i++)

{
for(int j=0; j<3; j++)

{
sum[i][j] = a[i][j] + b[i][j];
[Link](sum[i][j] + " ");
}
[Link]();
}
}
}

Output:

10 10 10
10 10 10
10 10 10
Page |7

PROGRAM-6

Java Function (Method) Use To Calculate Area of


Rectangle.

import java.u l.*;


class Rectangle
{
public sta c int Rect(int l, int b)
{ int area = 0; area = l * b;
return area;
}
public sta c void main(String args[])
{
Scanner sc = new Scanner([Link]);
int l, b, a = 0;
[Link]("Enter value of l = ");
l = [Link]();
[Link]("Enter value of b =
"); b = [Link](); a = Rect(l, b);
[Link]("The Area of Rectangle = " + a);
}
}
Page |8

PROGRAM-7

Non-static functions & Object Program.

import java.u l.*;


class Student
{

String name;
int age;

void display()
{
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}

public class Main {


public sta c void main(String[] args)
{

Student s1 = new Student();

[Link] = "Rahul";
[Link] = 20;

method
[Link]();
}
}
Page |9

PROGRAM-8

Life Of A Thread.

import java.u l.*;


class MyThread extends Thread
{
public void run()
{
[Link]("Thread is running...");
}
}
public class Main
{
public sta c void main(String[] args)
{
MyThread t1 = new MyThread();

[Link]("State: " + [Link]());


[Link]();
[Link]("State a er start: " + [Link]());
try
{
[Link]();
}
catch (Excep on e) {}
[Link]("State a er execu on: " + [Link]());
}
}
P a g e | 10

PROGRAM-9

Creating A Thread Using A Thread Class.

class MyThread extends Thread


{

public void run()


{
for(int i = 1; i <= 5; i++)
{
[Link]("Thread is running: " + i);
}
}
}

public class Main


{
public sta c void main(String[] args)
{

MyThread t1 = new MyThread();

[Link]();
}
}
P a g e | 11

PROGRAM-10

WAP In Java Using Try & Catch For Exception Handling.

import java.u l.*;


class Error
{
public sta c void main(String args[])
{
int a = 10;
int b = 5;
int c = 5;
int x, y; try {
x = a / (b - c);
}
catch (Arithme cExcep on e)
{
[Link]("Division by zero");
}
y = a / (b + c);
[Link]("y = " + y);
}
}
Page |1

PROGRAM-1

Write a Java Program to Reverse a 3-Digit Number.

import java.u l.*;


class Rev
{
public sta c void main(String args[])
{
Int n = 425, a = 0, b = 0, c = 0, d = 0, Rev =0;
a = n % 10;
b = n / 10;
c = b % 10;
d = b / 10;
Rev = a * 100 + c * 10 + d;
[Link]("The Reverse No = " + Rev);
}
}

Output:
524
Page |2

PROGRAM-2

Write a Java Program to Add Two Numbers.

import java.u l.*;


class Sum
{
public sta c void main(String args[])
{
int a = 10, b = 5, s = 0;
s = a + b;
[Link]("The sum Value = " + s);
}
}

Output:
15
Page |3

PROGRAM-3

Write a Menu Based Program To Calcution Of Area:-


[Link]
[Link]
[Link]

Import java.u l;
class Area
{
public sta c void main(String args[])
{
Scanner sc = new
Scanner([Link]);
int r, l, b, ch;
double area, base, height, x;
[Link]("Calcula on");
[Link]("1. Circle");
[Link]("2. Rectangle");
[Link]("3. Triangle");
[Link]("Enter your choice:");
ch = [Link]();
switch (ch)
{
case 1:
[Link]("Enter value of r: ");
r = [Link]();
Page |4

area = 3.14 * r * r;
[Link]("The value of Area: " + area);
break;
case 2:
[Link]("Enter value of l: ");
l = [Link]();
[Link]("Enter value of b: ");
b = [Link]();
area = l * b;
[Link]("The Area of Rectangle: " + area);
break;
case 3:
[Link]("Enter value of base: ");
base = [Link]();
[Link]("Enter value of height: ");
height = [Link]();
x = 0.5 * base * height;
[Link]("The value of the Area of triangle: " + x);
break;
default:
[Link]("Wrong choice!");
break;
}
}
}
Page |5

PROGRAM-4

WAP In Java To Convert Day Into Year,Month,Week


And Remaining Days.

import java.u l.*;


class Day
{
public sta c void main(String args[])
{
int day = 7653, y = 0, m = 0, w = 0;
int day2 = 0, day3 = 0, day4 = 0;
y = day / 365;
day2 = day % 365;
m = day2 / 30;
day3 = day2 % 30;
w = day3 / 7;
day4 = day3 % 7;
[Link]("Year = " + y);
[Link]("Month = " + m);
[Link]("Week = " + w);
[Link]("rem day = " + day4);
}
}
Page |6

PROGRAM-5

WAP in Java To Sum 3*3 Two Matrix.

public class MatrixSum

{
public sta c void main(String[] args)

int a[][] = {{1,2,3},{4,5,6},{7,8,9}};


int b[][] = {{9,8,7},{6,5,4},{3,2,1}};
int sum[][] = new int[3][3];

for(int i=0; i<3; i++)

{
for(int j=0; j<3; j++)

{
sum[i][j] = a[i][j] + b[i][j];
[Link](sum[i][j] + " ");
}
[Link]();
}
}
}

Output:

10 10 10
10 10 10
10 10 10
Page |7

PROGRAM-6

Java Function (Method) Use To Calculate Area of


Rectangle.

import java.u l.*;


class Rectangle
{
public sta c int Rect(int l, int b)
{ int area = 0; area = l * b;
return area;
}
public sta c void main(String args[])
{
Scanner sc = new Scanner([Link]);
int l, b, a = 0;
[Link]("Enter value of l = ");
l = [Link]();
[Link]("Enter value of b =
"); b = [Link](); a = Rect(l, b);
[Link]("The Area of Rectangle = " + a);
}
}
Page |8

PROGRAM-7

Non-static functions & Object Program.

import java.u l.*;


class Student
{

String name;
int age;

void display()
{
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}

public class Main {


public sta c void main(String[] args)
{

Student s1 = new Student();

[Link] = "Rahul";
[Link] = 20;

method
[Link]();
}
}
Page |9

PROGRAM-8

Life Of A Thread.

import java.u l.*;


class MyThread extends Thread
{
public void run()
{
[Link]("Thread is running...");
}
}
public class Main
{
public sta c void main(String[] args)
{
MyThread t1 = new MyThread();

[Link]("State: " + [Link]());


[Link]();
[Link]("State a er start: " + [Link]());
try
{
[Link]();
}
catch (Excep on e) {}
[Link]("State a er execu on: " + [Link]());
}
}
P a g e | 10

PROGRAM-9

Creating A Thread Using A Thread Class.

class MyThread extends Thread


{

public void run()


{
for(int i = 1; i <= 5; i++)
{
[Link]("Thread is running: " + i);
}
}
}

public class Main


{
public sta c void main(String[] args)
{

MyThread t1 = new MyThread();

[Link]();
}
}
P a g e | 11

PROGRAM-10

WAP In Java Using Try & Catch For Exception Handling.

import java.u l.*;


class Error
{
public sta c void main(String args[])
{
int a = 10;
int b = 5;
int c = 5;
int x, y; try {
x = a / (b - c);
}
catch (Arithme cExcep on e)
{
[Link]("Division by zero");
}
y = a / (b + c);
[Link]("y = " + y);
}
}
Page |1

PROGRAM-1

Write a Java Program to Reverse a 3-Digit Number.

import java.u l.*;


class Rev
{
public sta c void main(String args[])
{
Int n = 425, a = 0, b = 0, c = 0, d = 0, Rev =0;
a = n % 10;
b = n / 10;
c = b % 10;
d = b / 10;
Rev = a * 100 + c * 10 + d;
[Link]("The Reverse No = " + Rev);
}
}

Output:
524
Page |2

PROGRAM-2

Write a Java Program to Add Two Numbers.

import java.u l.*;


class Sum
{
public sta c void main(String args[])
{
int a = 10, b = 5, s = 0;
s = a + b;
[Link]("The sum Value = " + s);
}
}

Output:
15
Page |3

PROGRAM-3

Write a Menu Based Program To Calcution Of Area:-


[Link]
[Link]
[Link]

Import java.u l;
class Area
{
public sta c void main(String args[])
{
Scanner sc = new
Scanner([Link]);
int r, l, b, ch;
double area, base, height, x;
[Link]("Calcula on");
[Link]("1. Circle");
[Link]("2. Rectangle");
[Link]("3. Triangle");
[Link]("Enter your choice:");
ch = [Link]();
switch (ch)
{
case 1:
[Link]("Enter value of r: ");
r = [Link]();
Page |4

area = 3.14 * r * r;
[Link]("The value of Area: " + area);
break;
case 2:
[Link]("Enter value of l: ");
l = [Link]();
[Link]("Enter value of b: ");
b = [Link]();
area = l * b;
[Link]("The Area of Rectangle: " + area);
break;
case 3:
[Link]("Enter value of base: ");
base = [Link]();
[Link]("Enter value of height: ");
height = [Link]();
x = 0.5 * base * height;
[Link]("The value of the Area of triangle: " + x);
break;
default:
[Link]("Wrong choice!");
break;
}
}
}
Page |5

PROGRAM-4

WAP In Java To Convert Day Into Year,Month,Week


And Remaining Days.

import java.u l.*;


class Day
{
public sta c void main(String args[])
{
int day = 7653, y = 0, m = 0, w = 0;
int day2 = 0, day3 = 0, day4 = 0;
y = day / 365;
day2 = day % 365;
m = day2 / 30;
day3 = day2 % 30;
w = day3 / 7;
day4 = day3 % 7;
[Link]("Year = " + y);
[Link]("Month = " + m);
[Link]("Week = " + w);
[Link]("rem day = " + day4);
}
}
Page |6

PROGRAM-5

WAP in Java To Sum 3*3 Two Matrix.

public class MatrixSum

{
public sta c void main(String[] args)

int a[][] = {{1,2,3},{4,5,6},{7,8,9}};


int b[][] = {{9,8,7},{6,5,4},{3,2,1}};
int sum[][] = new int[3][3];

for(int i=0; i<3; i++)

{
for(int j=0; j<3; j++)

{
sum[i][j] = a[i][j] + b[i][j];
[Link](sum[i][j] + " ");
}
[Link]();
}
}
}

Output:

10 10 10
10 10 10
10 10 10
Page |7

PROGRAM-6

Java Function (Method) Use To Calculate Area of


Rectangle.

import java.u l.*;


class Rectangle
{
public sta c int Rect(int l, int b)
{ int area = 0; area = l * b;
return area;
}
public sta c void main(String args[])
{
Scanner sc = new Scanner([Link]);
int l, b, a = 0;
[Link]("Enter value of l = ");
l = [Link]();
[Link]("Enter value of b =
"); b = [Link](); a = Rect(l, b);
[Link]("The Area of Rectangle = " + a);
}
}
Page |8

PROGRAM-7

Non-static functions & Object Program.

import java.u l.*;


class Student
{

String name;
int age;

void display()
{
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}

public class Main {


public sta c void main(String[] args)
{

Student s1 = new Student();

[Link] = "Rahul";
[Link] = 20;

method
[Link]();
}
}
Page |9

PROGRAM-8

Life Of A Thread.

import java.u l.*;


class MyThread extends Thread
{
public void run()
{
[Link]("Thread is running...");
}
}
public class Main
{
public sta c void main(String[] args)
{
MyThread t1 = new MyThread();

[Link]("State: " + [Link]());


[Link]();
[Link]("State a er start: " + [Link]());
try
{
[Link]();
}
catch (Excep on e) {}
[Link]("State a er execu on: " + [Link]());
}
}
P a g e | 10

PROGRAM-9

Creating A Thread Using A Thread Class.

class MyThread extends Thread


{

public void run()


{
for(int i = 1; i <= 5; i++)
{
[Link]("Thread is running: " + i);
}
}
}

public class Main


{
public sta c void main(String[] args)
{

MyThread t1 = new MyThread();

[Link]();
}
}
P a g e | 11

PROGRAM-10

WAP In Java Using Try & Catch For Exception Handling.

import java.u l.*;


class Error
{
public sta c void main(String args[])
{
int a = 10;
int b = 5;
int c = 5;
int x, y; try {
x = a / (b - c);
}
catch (Arithme cExcep on e)
{
[Link]("Division by zero");
}
y = a / (b + c);
[Link]("y = " + y);
}
}

You might also like