0% found this document useful (0 votes)
2 views

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)
2 views

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
You are on page 1/ 37

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

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;

System.out.println(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;

System.out.println(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;

System.out.println(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;

System.out.println(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;

System.out.println(a);

System.out.println(b);

System.out.println(c);

System.out.println(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;

System.out.println(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;

System.out.println(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;
System.out.println("Hostel Facilities");
switch(v)
{
case 1:System.out.println("very poor");
break;
case 2:System.out.println("poor");
break;
case 3:System.out.println("good");
break;
case 4:System.out.println("excellent");
break;
default:System.out.println("wrong no. selected");
}
System.out.println("Mess Facilities");
switch(w)
{
case 1:System.out.println("very poor");
break;
case 2:System.out.println("poor");
break;
case 3:System.out.println("good");
break;
case 4:System.out.println("excellent");
break;
default:System.out.println("wrong no. selected");
}
System.out.println("Hospital Facilities");
switch(x)
{
case 1:System.out.println("very poor");
break;
case 2:System.out.println("poor");
break;
case 3:System.out.println("good");
break;
case 4:System.out.println("excellent");
break;
default:System.out.println("wrong no. selected");
}
System.out.println("Lab Facilities");
switch(y)
{
case 1:System.out.println("very poor");
break;
case 2:System.out.println("poor");
break;
case 3:System.out.println("good");
break;
case 4:System.out.println("excellent");
break;
default:System.out.println("wrong no. selected");
}
System.out.println("Shuttle Facilities");
switch(z)
{
case 1:System.out.println("very poor");
break;
case 2:System.out.println("poor");
break;
case 3:System.out.println("good");
break;
case 4:System.out.println("excellent");
break;
default:System.out.println("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)

{System.out.println("A");

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

{System.out.println("B");

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

{System.out.println("C");

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

{System.out.println("D");

else

{System.out.println("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()

System.out.println("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();

O1.inpt(17,18,23);

O2.inpt(10,58,10);

O3.timeadd(O1,O2);

O3.outpt();

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

System.out.println("m="+m+"cm="+cm+"mm="+mm);

void timeadd(Timetest3 t1,Timetest3 t2)

m=t1.m+t2.m;

cm=t1.cm+t2.cm;

mm=t1.mm+t2.mm;

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

O1.inpt(17,80,30);

O2.inpt(10,58,10);

O3.timeadd(O1,O2);

O3.outpt();

}
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 java.util.*;

class double1Dmain

public static void main(String args[])

double1D t1= new double1D();

t1.input();

t1.proc();

t1.output();

class double1D

int a[];

double1D()

a=new int[5];

void input()

System.out.println("enter the array elements");

Scanner sc=new Scanner(System.in);

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

{
a[i]=sc.nextInt();

void output()

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

System.out.println(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 java.util.*;

class sum1Dmain

public static void main(String args[])

sum1D t1= new sum1D();

t1.input();

t1.proc();

t1.output();

class sum1D

int a[];

int sum=0;

sum1D()

a=new int[5];

void input()

System.out.println("enter the array elements");

Scanner sc=new Scanner(System.in);

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

a[i]=sc.nextInt();

void output()

System.out.println(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 java.util.*;

class ReverseMain {

public static void main(String[] args) {

Reverse t1 = new Reverse();

t1.input();

t1.proc();

t1.output();

class Reverse

int a[];

int b[];

Reverse(){

a=new int[5];

b=new int[5];

void input()

System.out.println("enter the elements:");

Scanner sc= new Scanner(System.in);

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

a[i]=sc.nextInt();
}

void output(){

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

System.out.print(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 java.util.*;

class Matrixmain {

public static void main(String[] args)

Matrix t1 = new Matrix();

t1.input();

t1.proc();

t1.output();

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

System.out.println("Enter the array elements:");

Scanner sc = new Scanner(System.in);

System.out.println("Enter the elements of array a:");

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


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

a[i][j] = sc.nextInt(); } }

System.out.println("Enter the elements of array b:");

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

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

b[i][j] = sc.nextInt(); } }

void output() {

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

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

{System.out.print(c[i][j]+" ");

System.out.println(); } }

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

class Test

int x,y;

Test()

Test(int x,int y)

this.x=x;

this.y=y;

void output()

System.out.println("value of x : " + x);

System.out.println("value of y : " + y);

public class Testmain {

public static void main(String args[])

Test t1=new Test(5,7);

t1.output(); }

}
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(){
System.out.println("Class Bus:");
System.out.println(name);
System.out.println("Class Vehicle:");
System.out.println(super.name);}}
public class TestSuper1 {
public static void main(String[] args) {
// TODO code application logic here
Bus d=new Bus();
d.printName();
}}
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()
{
System.out.println("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();
O1.inpt(17,18)
O2.inpt(10,58);
O3.timeadd(O1,O2);
O3.outpt();
}
}
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()
{
System.out.println("Hello World");
}
}
Importing Package
import xyz .*;
class Test
{
public static void main(String args[])
{
Hello t1= new Hello();
t1.printhello();
}
}
/************************************************************************
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;
System.out.println(z);
}
}
(ii) package calculation;
public class Area
{
public void Area()
{
int r;
double pi,a;
r=2;
pi=3.14;
a=pi*r*r;
System.out.println(a);
}
}
(iii) package calculation;
public class Div
{
public void Div()
{
int x,y,z;
x=99;
y=3;
z=x/y;
System.out.println(z);
}
}
(iv) package calculation;
public class multi
{
public void multi()
{
int x,y,z;
x=56;
y=44;
z=x*y;
System.out.println(z);
}
}
(v) package calculation;
public class Sub
{
public void Sub()
{
int x,y,z;
x=25;
y=15;
z=x-y;
System.out.println(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();
t1.Add();
t2.Area();
t3.Div();
t4.multi();
t5.Sub();
}
}
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++){
System.out.println("fun1->"+i);
}}
}
class test2 extends Thread{
public void run(){
for(int i=0;i<5;i++){
System.out.println("fun2->"+i);
}}
}
class Test{
public static void main(String[] args){
test1 t1=new test1();
test2 t2=new test2();
t1.start();
t2.start();
}}
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++){
System.out.println("fun1->"+i);
} }}
class test2 extends Thread{
public void run(){
for(int i=0;i<5;i++){
System.out.println("fun2->"+i);
} }}
class Test{
public static void main(String[] args){
test1 t1=new test1();
test2 t2=new test2();
t1.setPriority(5);
t2.setPriority(6);
t1.start();
t2.start();}}
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++) {
System.out.println(Thread.currentThread().getName() + ": " + i);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class ThreadMethodsDemo {
public static void main(String[] args) {
MyThread thread1 = new MyThread();
System.out.println("Thread 1 is alive before start: " + thread1.isAlive()); // false
thread1.start();
System.out.println("Thread 1 is alive after start: " + thread1.isAlive()); // true
MyThread thread2 = new MyThread();
System.out.println("Main thread starts thread2 and waits for it to finish...");
thread2.start();
try {
thread2.join(); // Main thread waits for thread2 to finish
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("Main thread resumes after thread2 finishes.");
System.out.println("Main thread sleeping for 2 seconds...");
try {
Thread.sleep(2000); // Main thread sleeps for 2 seconds
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("Main thread woke up after sleeping.");
MyThread thread3 = new MyThread();
thread3.setName("CustomThreadName");
System.out.println("Thread 3 name: " + thread3.getName());
}
}
OUTPUT:

You might also like