0% found this document useful (0 votes)
33 views37 pages

Java Programs 38

Uploaded by

ANJALI MODI
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)
33 views37 pages

Java Programs 38

Uploaded by

ANJALI MODI
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

/***********************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name: Addition of two numbers

***********************************/

class Add

public static void main(String args[])

int x,y,z;

x=56;

y=44;

z=x+y;

[Link](z);

OUTPUT :
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Subtraction of two numbers

***********************************/

class Sub

public static void main(String args[])

int x,y,z;

x=25;

y=15;

z=x-y;

[Link](z);

OUTPUT :
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Multiplication of two numbers

***********************************/

class multi

public static void main(String args[])

int x,y,z;

x=56;

y=44;

z=x*y;

[Link](z);

OUTPUT :
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Division of two numbers

***********************************/

class Div

public static void main(String args[])

int x,y,z;

x=99;

y=3;

z=x/y;

[Link](z);

OUTPUT :
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Performing Arithmatic operations

***********************************/

class Arith

public static void main(String args[])

int x,y,a,b,c,d;

x=99;

y=3;

a=99+3;

b=99-3;

c=99*3;

d=99/3;

[Link](a);

[Link](b);

[Link](c);

[Link](d);

OUTPUT :
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Area of Circle

***********************************/

class Area

public static void main(String args[])

int r;

double pi,a;

r=2;

pi=3.14;

a=pi*r*r;

[Link](a);

OUTPUT :
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Equation of straight lines

***********************************/

class equation

public static void main(String args[])

double y,m,x,c;

m=2;

x=3;

c=1;

y=m*x+c;

[Link](y);

OUTPUT :
/*************************************
Name-Bhuvi Jain
Batch-A1
Enrollment No.- 220158
Aim- To create a menu
**************************************/
class Menu
{
public static void main(String args[])
{
int v=2,w=1,x=4,y=3,z=1;
[Link]("Hostel Facilities");
switch(v)
{
case 1:[Link]("very poor");
break;
case 2:[Link]("poor");
break;
case 3:[Link]("good");
break;
case 4:[Link]("excellent");
break;
default:[Link]("wrong no. selected");
}
[Link]("Mess Facilities");
switch(w)
{
case 1:[Link]("very poor");
break;
case 2:[Link]("poor");
break;
case 3:[Link]("good");
break;
case 4:[Link]("excellent");
break;
default:[Link]("wrong no. selected");
}
[Link]("Hospital Facilities");
switch(x)
{
case 1:[Link]("very poor");
break;
case 2:[Link]("poor");
break;
case 3:[Link]("good");
break;
case 4:[Link]("excellent");
break;
default:[Link]("wrong no. selected");
}
[Link]("Lab Facilities");
switch(y)
{
case 1:[Link]("very poor");
break;
case 2:[Link]("poor");
break;
case 3:[Link]("good");
break;
case 4:[Link]("excellent");
break;
default:[Link]("wrong no. selected");
}
[Link]("Shuttle Facilities");
switch(z)
{
case 1:[Link]("very poor");
break;
case 2:[Link]("poor");
break;
case 3:[Link]("good");
break;
case 4:[Link]("excellent");
break;
default:[Link]("wrong no. selected");
}
}
}
OUTPUT :
/*************************************
Name-Bhuvi Jain
Batch-A1
Enrollment No.- 220158

Aim- To print grades A,B,C,D,E for given marks

**************************************/

class Grades{

public static void main(String args[]){

double marks=65;

if(marks<100 && marks>=90)

{[Link]("A");

else if(marks>=80 && marks<90)

{[Link]("B");

else if(marks<80 && marks>=70)

{[Link]("C");

else if(marks<70 && marks>=60)

{[Link]("D");

else

{[Link]("E");

OUTPUT:
/***********************************

NAME:Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Adding two times where each time is described with three variables hour minute and
second.

***********************************/

class Timetest2

int h,m,s;

void inpt(int a,int b,int c)

h=a;

m=b;

s=c;

void outpt()

[Link]("h="+h+"m="+m+"s="+s);

void timeadd(Timetest2 t1,Timetest2 t2)

h=t1.h+t2.h;

m=t1.m+t2.m;

s=t1.s+t2.s;

if(s>59)

s=s-60;

m=m+1;

if(m>59)
{

m=m-60;

h=h+1;

class Test2

public static void main(String args[])

Timetest2 O1=new Timetest2();

Timetest2 O2=new Timetest2();

Timetest2 O3=new Timetest2();

[Link](17,18,23);

[Link](10,58,10);

[Link](O1,O2);

[Link]();

OUTPUT :
/***********************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name:Adding two distances where each distance is described with three variables
m,cm,mm.

***********************************/

class Timetest3

int m,cm,mm;

void inpt(int a,int b,int c)1

m=a;

cm=b;

mm=c;

void outpt()

[Link]("m="+m+"cm="+cm+"mm="+mm);

void timeadd(Timetest3 t1,Timetest3 t2)

m=t1.m+t2.m;

cm=[Link]+[Link];

mm=[Link]+[Link];

if(mm>9)

mm=mm-10;

cm=cm+1;

if(cm>99)
{

cm=cm-100;

m=m+1;

class Test3

public static void main(String args[])

Timetest3 O1=new Timetest3();

Timetest3 O2=new Timetest3();

Timetest3 O3=new Timetest3();

[Link](17,80,30);

[Link](10,58,10);

[Link](O1,O2);

[Link]();

}
OUTPUT:
/***********************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name : Write a class to do the double of 1-D array and test it using main function.

***********************************/

CODE :

import [Link].*;

class double1Dmain

public static void main(String args[])

double1D t1= new double1D();

[Link]();

[Link]();

[Link]();

class double1D

int a[];

double1D()

a=new int[5];

void input()

[Link]("enter the array elements");

Scanner sc=new Scanner([Link]);

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

{
a[i]=[Link]();

void output()

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

[Link](a[i]);

void proc()

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

a[i]=2*a[i];

OUTPUT :
/***********************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name : Write a class to do the sum of 1-D array and using objects and classes.

***********************************/

Code:
import [Link].*;

class sum1Dmain

public static void main(String args[])

sum1D t1= new sum1D();

[Link]();

[Link]();

[Link]();

class sum1D

int a[];

int sum=0;

sum1D()

a=new int[5];

void input()

[Link]("enter the array elements");

Scanner sc=new Scanner([Link]);

for(int i=0;i<5;i++)
{

a[i]=[Link]();

void output()

[Link](sum);

void proc()

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

sum=sum+a[i];

OUTPUT :
/***********************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name : Write a class to do the reverse of 1-D array using 2nd array.

***********************************/

Code :

import [Link].*;

class ReverseMain {

public static void main(String[] args) {

Reverse t1 = new Reverse();

[Link]();

[Link]();

[Link]();

class Reverse

int a[];

int b[];

Reverse(){

a=new int[5];

b=new int[5];

void input()

[Link]("enter the elements:");

Scanner sc= new Scanner([Link]);

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

a[i]=[Link]();
}

void output(){

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

[Link](b[i]+" ");

void proc()

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

b[i]=a[4-i];

OUTPUT :
/***********************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name : Program to do the sum of two matrices of size 3x3 using object and classes

***********************************/

Code:

import [Link].*;

class Matrixmain {

public static void main(String[] args)

Matrix t1 = new Matrix();

[Link]();

[Link]();

[Link]();

class Matrix {

int a[][];

int b[][];

int c[][];

Matrix() {

a = new int[3][3];

b = new int[3][3];

c = new int[3][3];

void input() {

[Link]("Enter the array elements:");

Scanner sc = new Scanner([Link]);

[Link]("Enter the elements of array a:");

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


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

a[i][j] = [Link](); } }

[Link]("Enter the elements of array b:");

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

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

b[i][j] = [Link](); } }

void output() {

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

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

{[Link](c[i][j]+" ");

[Link](); } }

void proc() {

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

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

c[i][j]= a[i][j]+b[i][j];

}}

OUTPUT :
/*******************************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name : Wite a program using this() keyword.

*********************************************/

Code :

import [Link].*;

class Test

int x,y;

Test()

Test(int x,int y)

this.x=x;

this.y=y;

void output()

[Link]("value of x : " + x);

[Link]("value of y : " + y);

public class Testmain {

public static void main(String args[])

Test t1=new Test(5,7);

[Link](); }

}
OUTPUT :
/*******************************************

NAME: Bhuvi Jain

CLASS:2nd year Sec A

Enroll. NO.:220158

Program Name : Wite a program using Super keyword.

*********************************************/

Code :
class Vehicle{
String name="Bus";}
class Bus extends Vehicle{
String name="School Bus";
void printName(){
[Link]("Class Bus:");
[Link](name);
[Link]("Class Vehicle:");
[Link]([Link]);}}
public class TestSuper1 {
public static void main(String[] args) {
// TODO code application logic here
Bus d=new Bus();
[Link]();
}}
OUTPUT:
/***********************************
NAME:Bhuvi Jain
CLASS:2nd year Sec A
Enroll. NO.:220158
Program Name:Adding two times where each time is described with two variables hour and
minute
***********************************/
class Timetest2
{
int h,m;
void inpt(int a,int b)
{
h=a;
m=b;
}
void outpt()
{
[Link]("h="+h+"m="+m);
}
void timeadd(Timetest2 t1,Timetest2 t2)
{
h=t1.h+t2.h;
m=t1.m+t2.m;
if(m>59)
{
m=m-60;
h=h+1;
}
}
}
class Test2
{
public static void main(String args[])
{
Timetest2 O1=new Timetest2();
Timetest2 O2=new Timetest2();
Timetest2 O3=new Timetest2();
[Link](17,18)
[Link](10,58);
[Link](O1,O2);
[Link]();
}
}
OUTPUT :
/**********************************************
NAME: Bhuvi Jain
CLASS:2nd year Sec A
Enroll. NO.:220158
Program Name: Create a package for printing Hello World
************************************************/
Creating package
package xyz;
public class Hello
{
public void printhello()
{
[Link]("Hello World");
}
}
Importing Package
import xyz .*;
class Test
{
public static void main(String args[])
{
Hello t1= new Hello();
[Link]();
}
}
/************************************************************************
NAME: Bhuvi Jain
CLASS:2nd year Sec A
Enroll. No.:220158
Program Name: Create package for any 5 created programs and test function and methods
**************************************************************************/
Creating Package
(i)package calculation;
public class Add
{
public void Add()

{
int x,y,z;
x=56;
y=44;
z=x+y;
[Link](z);
}
}
(ii) package calculation;
public class Area
{
public void Area()
{
int r;
double pi,a;
r=2;
pi=3.14;
a=pi*r*r;
[Link](a);
}
}
(iii) package calculation;
public class Div
{
public void Div()
{
int x,y,z;
x=99;
y=3;
z=x/y;
[Link](z);
}
}
(iv) package calculation;
public class multi
{
public void multi()
{
int x,y,z;
x=56;
y=44;
z=x*y;
[Link](z);
}
}
(v) package calculation;
public class Sub
{
public void Sub()
{
int x,y,z;
x=25;
y=15;
z=x-y;
[Link](z);
}
}
Importing Package
import calculation.*;
class arithmetic
{
public static void main(String args[])
{
Add t1=new Add();
Area t2=new Area();
Div t3=new Div();
multi t4 =new multi();
Sub t5=new Sub();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
/****************************
Name: Bhuvi Jain
CLASS:2nd year Sec A
Enroll. NO.:220158
Program Name: MultiThreading
*****************************/
class test1 extends Thread{
public void run(){
for(int i=0;i<5;i++){
[Link]("fun1->"+i);
}}
}
class test2 extends Thread{
public void run(){
for(int i=0;i<5;i++){
[Link]("fun2->"+i);
}}
}
class Test{
public static void main(String[] args){
test1 t1=new test1();
test2 t2=new test2();
[Link]();
[Link]();
}}
OUTPUT:
/**************************************************************************
Name: Bhuvi Jain
CLASS:2nd year Sec A
Enroll. NO.:220158
Program Name: To set the priority of threads by different values and analyze the output.
**************************************************************************/
class test1 extends Thread{
public void run(){
for(int i=0;i<5;i++){
[Link]("fun1->"+i);
} }}
class test2 extends Thread{
public void run(){
for(int i=0;i<5;i++){
[Link]("fun2->"+i);
} }}
class Test{
public static void main(String[] args){
test1 t1=new test1();
test2 t2=new test2();
[Link](5);
[Link](6);
[Link]();
[Link]();}}
OUTPUT
/**************************************************************************
Name: Bhuvi Jain
CLASS:2nd year Sec A
Enroll. NO.:220158
Program Name: Write a program to test different methods of thread.
isalive(),join(),sleep(), setname(),getname()

**************************************************************************/
Code:
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]([Link]().getName() + ": " + i);
try {
[Link](500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
[Link](e);
}
}
}
}
public class ThreadMethodsDemo {
public static void main(String[] args) {
MyThread thread1 = new MyThread();
[Link]("Thread 1 is alive before start: " + [Link]()); // false
[Link]();
[Link]("Thread 1 is alive after start: " + [Link]()); // true
MyThread thread2 = new MyThread();
[Link]("Main thread starts thread2 and waits for it to finish...");
[Link]();
try {
[Link](); // Main thread waits for thread2 to finish
} catch (InterruptedException e) {
[Link](e);
}
[Link]("Main thread resumes after thread2 finishes.");
[Link]("Main thread sleeping for 2 seconds...");
try {
[Link](2000); // Main thread sleeps for 2 seconds
} catch (InterruptedException e) {
[Link](e);
}
[Link]("Main thread woke up after sleeping.");
MyThread thread3 = new MyThread();
[Link]("CustomThreadName");
[Link]("Thread 3 name: " + [Link]());
}
}
OUTPUT:

You might also like