Java Programs 38
Java Programs 38
Enroll. NO.:220158
***********************************/
class Add
int x,y,z;
x=56;
y=44;
z=x+y;
System.out.println(z);
OUTPUT :
/***********************************
NAME:Bhuvi Jain
Enroll. NO.:220158
***********************************/
class Sub
int x,y,z;
x=25;
y=15;
z=x-y;
System.out.println(z);
OUTPUT :
/***********************************
NAME:Bhuvi Jain
Enroll. NO.:220158
***********************************/
class multi
int x,y,z;
x=56;
y=44;
z=x*y;
System.out.println(z);
OUTPUT :
/***********************************
NAME:Bhuvi Jain
Enroll. NO.:220158
***********************************/
class Div
int x,y,z;
x=99;
y=3;
z=x/y;
System.out.println(z);
OUTPUT :
/***********************************
NAME:Bhuvi Jain
Enroll. NO.:220158
***********************************/
class Arith
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
Enroll. NO.:220158
***********************************/
class Area
int r;
double pi,a;
r=2;
pi=3.14;
a=pi*r*r;
System.out.println(a);
OUTPUT :
/***********************************
NAME:Bhuvi Jain
Enroll. NO.:220158
***********************************/
class equation
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
**************************************/
class Grades{
double marks=65;
{System.out.println("A");
{System.out.println("B");
{System.out.println("C");
{System.out.println("D");
else
{System.out.println("E");
OUTPUT:
/***********************************
NAME:Bhuvi Jain
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;
h=a;
m=b;
s=c;
void outpt()
System.out.println("h="+h+"m="+m+"s="+s);
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
O1.inpt(17,18,23);
O2.inpt(10,58,10);
O3.timeadd(O1,O2);
O3.outpt();
OUTPUT :
/***********************************
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;
m=a;
cm=b;
mm=c;
void outpt()
System.out.println("m="+m+"cm="+cm+"mm="+mm);
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
O1.inpt(17,80,30);
O2.inpt(10,58,10);
O3.timeadd(O1,O2);
O3.outpt();
}
OUTPUT:
/***********************************
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
t1.input();
t1.proc();
t1.output();
class double1D
int a[];
double1D()
a=new int[5];
void input()
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 :
/***********************************
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
t1.input();
t1.proc();
t1.output();
class sum1D
int a[];
int sum=0;
sum1D()
a=new int[5];
void input()
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 :
/***********************************
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 {
t1.input();
t1.proc();
t1.output();
class Reverse
int a[];
int b[];
Reverse(){
a=new int[5];
b=new int[5];
void input()
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 :
/***********************************
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 {
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() {
a[i][j] = sc.nextInt(); } }
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 j=0;j<3;j++)
c[i][j]= a[i][j]+b[i][j];
}}
OUTPUT :
/*******************************************
Enroll. NO.:220158
*********************************************/
Code :
import java.util.*;
class Test
int x,y;
Test()
Test(int x,int y)
this.x=x;
this.y=y;
void output()
t1.output(); }
}
OUTPUT :
/*******************************************
Enroll. NO.:220158
*********************************************/
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: