0% found this document useful (0 votes)
34 views40 pages

Wa0002.

Uploaded by

Ahmed Zubair
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)
34 views40 pages

Wa0002.

Uploaded by

Ahmed Zubair
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/ 40

22-UCA-039

Program 1: Arithmetic Operations

Source code:

import java.util.*;
public class Program1
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter two integers to perform arithmetic progression");
System.out.print("Enter integer 1 : ");
int a = scan.nextInt();
System.out.print("Enter integer 2 : ");
int b = scan.nextInt();
System.out.println("Performing arithmetic progression");
System.out.println("Addition : "+(a+b));
System.out.println("Subtraction : "+(a-b));
System.out.println("Multiplication : "+(a*b));
System.out.println("Division : "+(a/b));
System.out.println("Modulus : "+(a%b));
}
}

Output:

Enter two integers to perform arithmetic progression


Enter integer 1 : 10
Enter integer 2 : 12
Performing arithmetic progression
Addition : 22
Subtraction : -2
Multiplication : 120
Division : 0
Modulus : 10
22-UCA-039

Program 2: Concatenation

Source code:

import java.util.*;
public class Program2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Concatenating three different strings");
System.out.print("String 1 : ");
String a = scan.nextLine();
System.out.print("String 2 : ");
String b = scan.nextLine();
System.out.print("String 3 : ");
String c = scan.nextLine();
System.out.println("The concatenated string is : "+a+" "+b+" "+c);
}
}

Output:
Concatenating three different strings
String 1 : We
String 2 : Are
String 3 : Venom
The concatenated string is : We Are Venom
22-UCA-039

Program 3: Conversion of Temperature from Fahrenheit to Celsius

Source Code:
import java.util.Scanner;
public class TemperatureConvert
{
public static void main(String[] args)
{
Scanner T = new Scanner(System.in);
System.out.print("Enter value for Fahrenheit :");
float Fahren = T.nextFloat();
System.out.println("The given Fahrenheit is : "+Fahren);
float Celsi = (Fahren-32)*5/9;
System.out.println("Fahrenheit in Celsius is : "+Celsi);
}
}

Output:

U:\cas39\Java Programming>javac FahrCels.java


U:\cas39\Java Programming>java FahrCels

Enter value for Fahrenheit :100


The given Fahrenheit is : 100.0
Fahrenheit in Celsius is : 37.77778
22-UCA-039

Program 4: Checking positive or negative or zero using if else


statement

Source Code:
import java.util.Scanner;
public class PosNegZero
{
public static void main(String[] args)
{
Scanner A = new Scanner(System.in);
System.out.print("Enter your integer number here : ");
int evalu = A.nextInt();
if (evalu>0)
{
System.out.println(evalu+" is a positive integer");
}
else if (evalu==0)
{
System.out.println(evalu+" is zero");
}
else
{
System.out.println(evalu+" is a negative integer");
}

}
}

Output:
U:\cas39\Java Programming>java PosNegZero
Enter your integer number here : 45
45 is a positive integer
22-UCA-039

Program 5: Count of odd and even in the given range

Source code:
import java.util.Scanner;
public class CountOddEven2
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the start of the range : ");
int start = scanner.nextInt();
System.out.print("Enter the end of the range : ");
int end = scanner.nextInt();

int oddCount = 0;
int evenCount = 0;
for (int i = start; i<=end; i++)
{
if (i%2==0)
{
evenCount++;
}
else
oddCount++;
}
System.out.println("Count of even numbers in the given range is : "
+evenCount);
System.out.println("Count of odd numbers in the given range is : "
+oddCount);
}
}

Output:

U:\cas39\Java Programming>java CountOddEven2


Enter the start of the range : 0
Enter the end of the range : 10
Count of even numbers in the given range is : 6
Count of odd numbers in the given range is : 5
22-UCA-039

Program 6: Multiples of 11

Source code:
/* multiples of 11 using
while, do while and for loop */

import java.util.Scanner;
public class MultiplesOf11
{
public static void main(String[] args)
{
int x = 0;
Scanner obj = new Scanner(System.in);
System.out.println("Welcome User, let\'s print the multiples of 11!");
System.out.print("Enter the start of the range : ");
int start = obj.nextInt();
System.out.print("Enter the end of the range : ");
int end = obj.nextInt();

System.out.println("WHILE LOOP");
while(x<=end)
{
if (x%11==0 && x!=0)
{
System.out.print(x+"\t");
}
x++;
}

System.out.println("\n*DO WHILE LOOP*");


x=start;
do
{
if (x % 11 == 0)
{
System.out.print(x + "\t");
}
x++;
}
while (x <= end);

System.out.println("\n*FOR LOOP*");
for (int i = start; i <= end; i++)
{
if (i%11==0)
System.out.print(i + "\t");
}

}
22-UCA-039

Output:

U:\cas39\Java Programming>java MultiplesOf11


Welcome User, let’s print the multiples of 11!
Enter the start of the range : 1
Enter the end of the range : 121
WHILE LOOP
11 22 33 44 55 66 77 88 99 110 121
*DO WHILE LOOP*
11 22 33 44 55 66 77 88 99 110 121
*FOR LOOP*
11 22 33 44 55 66 77 88 99 110 121
22-UCA-039

Program 7: Converting single digit numbers to words


(SWITCH CASE)

Source code:

//Converting single digit numbers to words

import java.util.Scanner;
public class Program7
{
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
System.out.print("Enter a single digit positive number : ");
int a = obj.nextInt();

switch (a)
{
default:
System.out.println("Error!, please enter a single digit positive number");
break;
case 0:
System.out.println("Zero");
break;
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
case 3:
System.out.println("Three");
break;
case 4:
System.out.println("Four");
break;
case 5:
System.out.println("Five");
break;
case 6:
System.out.println("Six");
break;
case 7:
System.out.println("Seven");
break;
case 8:
System.out.println("Eight");
break;
case 9:
22-UCA-039

System.out.println("Nine");
break;
}

}
}

Output:

U:\cas39\Java Programming>java Program7


Enter a single digit positive number : 3
Three
22-UCA-039

Program 8: CHECKING ARMSTRONG NUMBER (METHODS)

Source code:
import java.util.Scanner;
public class Program8
{
static void Arm_Num(int x)
{
int y=x, a=0;
while (y!=0)
{
int r = y%10;
a = a+(r*r*r);
y = y/10;
}
if(a==x)
{
System.out.println("The given number is an Armstrong number");
}
else
{
System.out.println("The given number is not an Armstrong number");
}
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter a number to chech it is an Armstrong number or
not : ");
int x = s.nextInt();
Arm_Num(x);
}
}

Output:

U:\cas39\Java Programming\Observation Programs>java Program8


Enter a number to check it is an Armstrong number or not : 153
The given number is an Armstrong number
22-UCA-039

Program 9: Printing Fibonacci series (Methods)

Source code:

//Printing Fibonacci Series


import java.util.Scanner;
public class Program9
{
static void fib_print(int x)
{
int a=0,b=1;
if (x==0)
{
System.out.println(x);
}
else
{
System.out.print(0+" ");
}
for(int i=1; i<=x; i++)
{
int num=a+b;
if (num>x)
{
break;
}
System.out.print(num+" ");
a=b;
b=num;
}
}
public static void main(String[] args)
{
System.out.print("Enter the number : ");
Scanner s = new Scanner(System.in);
int x = s.nextInt();
fib_print(x);
}
}

Output:

U:\cas39\Java Programming\Observation Programs>java Program9


Enter the number : 5702887
0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
10946 17711 28657 46368 75025 121393 196418 317811 514229 832040
1346269 2178309 3524578 5702887
22-UCA-039

Program 10: USING METHODS

Source code:
import java.util.Scanner;
public class Program10
{
static int isOdd(int a)
{
if (a%2!=0)
{
return 1;
}
else
{
return 0;
}
}
static int isMultiple5(int a)
{
if (a%5==0)
{
return 1;
}
else
{
return 0;
}
}
static int Add(int a, int b)
{
return a+b;
}
static boolean inRange(int a, int b, int c)
{
if (b<=a && a<=c)
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter number to be checked odd : ");
int a = s.nextInt();
System.out.println(isOdd(a));
22-UCA-039

System.out.print("Enter number to be checked for multiples of 5 : ");


int b = s.nextInt();
System.out.println(isMultiple5(b));

System.out.println("Enter number to be added");


System.out.print("Enter number 1 : ");
int c = s.nextInt();
System.out.print("Enter number 2 : ");
int d = s.nextInt();
System.out.println(Add(c,d));

System.out.println("Enter number to check range");


System.out.print("Enter number to check if the number is in the range : ");
int e = s.nextInt();
System.out.print("Enter start of the range : ");
int f = s.nextInt();
System.out.print("Enter end of the range : ");
int g = s.nextInt();
System.out.println(inRange(e,f,g));
}
}

Output:

U:\cas39\Java Programming\Observation Programs>java Program10


Enter number to be checked odd : 2
0
Enter number to be checked for multiples of 5 : 120
1
Enter number to be added
Enter number 1 : 2
Enter number 2 : 5
7
Enter number to check range
Enter number to check if the number is in the range : 2
Enter start of the range : 4
Enter end of the range : 6
false
22-UCA-039

Program 11: Initialization of Arrays

Source code:
import java.util.Scanner;
public class Program11
{
public static void main(String[] args)
{
int[] nos = {1,2,3,4,5};
String[] vehicles = {"Cycle","Car","Bike","Truck"};
float[] temp = {28.2f,30.1f,33.5f,66.7f};

for (int i=0; i<nos.length;i++)


{
System.out.println(nos[i]);
}
System.out.println(" ");
for (int i=0; i<vehicles.length;i++)
{
System.out.println(vehicles[i]);
}
System.out.println(" ");
for (int i=0; i<temp.length;i++)
{
System.out.println(temp[i]);
}
}
}

Output:
U:\cas39\Java Programming\Observation Programs>java Program11
1
2
3
4
5

Cycle
Car
Bike
Truck

28.2
30.1
33.5
66.7
22-UCA-039
22-UCA-039

Program 12: METHOD OVERLOADING


12. Develop a program to define a method sum
with the following signatures:
int sum(int,int);
float sum(float, float);
double sum(double, double);

Source code:
import java.util.Scanner;
public class Program12
{
static int sum(int a, int b)
{
return a+b;
}

static float sum(float x, float y)


{
return x+y;
}

static double sum(double m, double n)


{
return m+n;
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Enter int numbers");
System.out.print("Enter int number 1 : ");
int a = s.nextInt();
System.out.print("Enter int number 2 : ");
int b = s.nextInt();

System.out.println("Enter float numbers");


System.out.print("Enter float number 1 : ");
float x = s.nextFloat();
System.out.print("Enter float number 2 : ");
float y = s.nextFloat();

System.out.println("Enter double numbers");


System.out.print("Enter double number 1 : ");
double m = s.nextDouble();
System.out.print("Enter double number 2 : ");
double n = s.nextDouble();

System.out.println(sum(a,b));
System.out.println(sum(x,y));
22-UCA-039

System.out.println(sum(m,n));
}
}

Output:
Enter int numbers
Enter int number 1 : 344
Enter int number 2 : 342
Enter float numbers
Enter float number 1 : 3453.34
Enter float number 2 : 4534.23
Enter double numbers
Enter double number 1 : 23423.2342
Enter double number 2 : 23423.4545
686
7987.5703
46846.6887
22-UCA-039

Program 13: CLASSES AND OBJECTS

Source code:
// Program 13 - CLASSES AND OBJECTS - 2
import java.util.Scanner;
public class Employee
{
String emp_name;
int emp_id, emp_basicpay, emp_hra;
double emp_pf, emp_netsal;
public void accept(String name, int id, int basicpay, int hra)
{
emp_name = name;
emp_id = id;
emp_basicpay = basicpay;
emp_hra = hra;
}
private double calcpf()
{
emp_pf = 0.12 * emp_basicpay;
return emp_pf;
}
public double calcnet()
{
emp_netsal = (emp_basicpay + emp_hra) - emp_pf;
return emp_netsal;
}
public void display()
{
System.out.println("Employee name : "+emp_name);
System.out.println("ID : "+emp_id);
System.out.println("Basic pay : "+emp_basicpay);
System.out.println("HRA : "+emp_hra);
System.out.println("Provident fund : "+emp_pf);
System.out.println("Net salary : "+emp_netsal);
}

public static void main(String[] args)


{
Scanner obj = new Scanner(System.in);
System.out.print("Enter the name of the employee : ");
String name = obj.nextLine();
System.out.print("Enter id : ");
int id = obj.nextInt();
System.out.print("Basic pay : ");
int basicpay = obj.nextInt();
System.out.print("HRA : ");
int hra = obj.nextInt();
22-UCA-039

Employee obj2 = new Employee();


obj2.accept(name,id,basicpay,hra);
obj2.calcpf();
obj2.calcnet();
obj2.display();
}
}

Output:
Enter the name of the employee : Lee jinhyuk
Enter id : 1001
Basic pay : 10000000
HRA : 900000
Employee name : Lee jinhyuk
ID : 1001
Basic pay : 10000000
HRA : 900000
Provident fund : 1200000.0
Net salary : 9700000.0
22-UCA-039

Program 14: CLASSES AND OBJECTS

Source code:

// Program 14 - Classes and objects - student


import java.util.Scanner;
public class Student
{
int stuid, mark1, mark2, mark3;
String stuname;
int total_marks, avg_marks;
public void accept(String name, int id, int m1, int m2, int m3) {
stuname = name;
stuid = id;
mark1 = m1;
mark2 = m2;
mark3 = m3;
}
public int calctotal(){
total_marks = mark1+mark2+mark3;
return total_marks;
}
public int calcavg()
{
avg_marks = total_marks/3;
return avg_marks;
}
public void display()
{
System.out.println("Student name : "+stuname);
System.out.println("Student ID : "+stuid);
System.out.println("mark 1 : "+mark1);
22-UCA-039

System.out.println("mark 2 : "+mark2);
System.out.println("mark 3 : "+mark3);
System.out.println("Total marks : "+total_marks);
System.out.println("Average marks : "+avg_marks);
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter student name : ");
String name = s.nextLine();
System.out.print("Enter student id : ");
int id = s.nextInt();
System.out.println("Enter three marks");
System.out.print("Mark 1 : ");
int m1 = s.nextInt();
System.out.print("Mark 2 : ");
int m2 = s.nextInt();
System.out.print("Mark 3 : ");
int m3 = s.nextInt();
Student obj = new Student();
obj.accept(name,id,m1,m2,m3);
obj.calctotal();
obj.calcavg();
obj.display();
}
}

Output:
Enter student name : Jin Sung
Enter student id : 1021
Enter three marks
Mark 1 : 100
Mark 2 : 99
Mark 3 : 100
Student name : Jin Sung
Student ID : 1021
mark 1 : 100
mark 2 : 99
mark 3 : 100
Total marks : 299
Average marks : 99
22-UCA-039

Program 15: TYPES OF CONSTRUCTORS

Source code:
// Program 15 - Types of Constructors
class cons
{
int x,y;
public cons()
{
x = 100;
y = 30;
}
public cons(int a, int b)
{
22-UCA-039

x = a;
y = b;
}
public static void main(String[] args)
{
cons obj = new cons();
System.out.println("Value of x and y is : "+obj.x+" "+obj.y);
cons obj2 = new cons(17,27);
System.out.println("Value of x and y : "+obj2.x+" "+obj2.y);
}
}

Output:
U:\cas39\Java Programming\Observation Programs>java cons
Value of x and y is : 100 30
Value of x and y : 17 27

Program 16: Single Inheritance

Source code:

import java.util.Scanner;
class Person
22-UCA-039

{
Scanner s = new Scanner(System.in);
String fname, lname;
public void getFirstName()
{
System.out.print("Enter first name : ");
fname = s.nextLine();
}
public void getLastName()
{
System.out.print("Enter last name : ");
lname = s.nextLine();
}
}

class employee extends Person


{
String jobtitle;
public void getJobTitle()
{
System.out.print("Enter occupation/job title : ");
jobtitle = s.nextLine();
}
}

class Program16
{
public static void main(String[] args)
{
employee obj = new employee();
obj.getFirstName();
obj.getLastName();
obj.getJobTitle();
System.out.println("Printing all the collected information...");
System.out.println("Full name : "+obj.fname+" "+obj.lname);
System.out.println("Job title : "+obj.jobtitle);
}
}

Output:

Enter first name : John


Enter last name : Snow
Enter occupation/job title : Ranger
Printing all the collected information...
Full name : John Snow
Job title : Ranger

Program 17: Abstract Class


22-UCA-039

Source code:
abstract class Testabs
{
abstract void m1();
abstract void m2();
public void m3()
{
System.out.println("Method 3");
}
public void m4()
{
System.out.println("Method 4");
}
}

class Test extends Testabs


{
public void m1()
{
System.out.println("Defining method 1 in class Test");
}
public void m2()
{
System.out.println("Defining method 2 in class Test");
}
}

class Program17
{
public static void main(String[] args)
{
Test obj = new Test();
obj.m1();
obj.m2();
obj.m3();
obj.m4();
}
}

Output:

Defining method 1 in class Test


Defining method 2 in class Test
Method 3
Method 4
22-UCA-039
22-UCA-039

Program 18: MULTIPLE INHERITANCE THROUGH INTERFACES

Source Code:

import java.util.*;
interface inter1
{
void GetStudentNames();
}

interface inter2
{
void DisplayStudentNames();
}

class Impletest implements inter1, inter2


{
String a,b,c;
public void GetStudentNames()
{
Scanner QT = new Scanner(System.in);
System.out.println("Enter three student names...");
System.out.print("Student 1 : ");
a = QT.nextLine();
System.out.print("Student 2 : ");
b = QT.nextLine();
System.out.print("Student 3 : ");
c = QT.nextLine();
}

public void DisplayStudentNames()


{
System.out.println("Displaying all the student names : ");
System.out.println("Student "+a);
System.out.println("Student "+b);
System.out.println("Student "+c);
}
}

class Program18
{
public static void main(String[] args)
{
Impletest obj = new Impletest();
obj.GetStudentNames();
obj.DisplayStudentNames();
}
}
22-UCA-039

Output:

U:\cas39\Java Programming\Observation Programs>java Program18


Enter three student names...
Student 1 : Elliot
Student 2 : Tyrell
Student 3 : Robot
Displaying all the student names :
Student Elliot
Student Tyrell
Student Robot
22-UCA-039

Program 19: METHOD OVERRIDING

Source Code:

class Game
{
void Stats()
{
System.out.println("Health points");
}
}

class Game1 extends Game


{
void Stats()
{
System.out.println("Magic points");
}
}

class Game2 extends Game


{
void Stats()
{
System.out.println("Experience points");
}
}

public class Program19


{
public static void main(String[] args)
{
System.out.println("Character stats\n");
Game obj1 = new Game();
Game1 obj2 = new Game1();
Game2 obj3 = new Game2();
obj1.Stats();
obj2.Stats();
obj3.Stats();
}
}

Output:

U:\cas39\Java Programming\Observation Programs>java Program19


Character stats
22-UCA-039

Health points
Magic points
Experience points
22-UCA-039

Program 20: PACKAGES

Source Code:

// Program 20 - PACKAGES
package MyPackageIsBIG;
import java.util.*;
public class MyMethods
{
static void drawEqualTo()
{

System.out.println("==================================
=================");
}
static void EnterName()
{
String a,b;
Scanner BigD = new Scanner(System.in);
System.out.println("Enter your name");
System.out.print("Enter first name : ");
a = BigD.nextLine();
System.out.print("Enter last name : ");
b = BigD.nextLine();
System.out.println("Welcome, "+a+" "+b);
}
public static void main(String[] args)
{
drawEqualTo();
EnterName();
drawEqualTo();
}
}

Output:

U:\cas39\Java Programming\Observation Programs>javac MyMethods.java

U:\cas39\Java Programming\Observation Programs>javac -d .


MyMethods.java

U:\cas39\Java Programming\Observation Programs>java


MyPackageIsBIG.MyMethods
==============================================
=====
Enter your name
Enter first name : Elliot
Enter last name : Alderson
22-UCA-039

Welcome, Elliot Alderson


==============================================
=====
22-UCA-039

Program 21: USING PACKAGES IN DIFFERENT CLASSES

Source Code:

PACKAGE CODE:
package testpack;
public class mypack
{
int a=10;
int b=5;

public void addition()


{
int c=a+b;
System.out.println(c);
}
public void divide()
{
int d=a/b;
System.out.println(d);
}
public static void main(String[] args)
{
mypack obj=new mypack();
System.out.println("inside package:");
obj.subtraction();
obj.divide();

}
}

SOURCE CODE:
import testpack.*;
public class Ex_21
{
public static void main(String[] args)
{
testpack.mypack.addition();
testpack.mypack.divide();
}
}

OUTPUT:

U:\cas39\java>javac mypack.java

U:\cas39\java>javac -d . mypack.java

U:\cas39\java>java testpack.mypack
22-UCA-039

inside package:
15
2
22-UCA-039

Program 22: EXCEPTION HANDLING

Source Code:

public class Program23


{
public static void main(String[] args)
{
try
{
int[] nos={10,14,20};
System.out.println(nos[5]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("given index is out of bounds");
}
}
}

OUTPUT:

U:\cas39\java>javac Program23.java
U:\cas39\java>java Program23
given index is out of bounds
22-UCA-039

Program 23: EXCEPTION HANDLING

Source Code:

import java.util.*;
class myerror extends Exception
{
public myerror(String message)
{
super(message);
}
}
class test
{
public void display() throws myerror{
throw new myerror("HELP ME, SOMEONE");
}
}
class error
{
public static void main(String[] args)
{
try
{
test obj=new test();
obj.display();
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NUM TO DIVIDE:");
int num1=sc.nextInt();
System.out.println("ENTER THE NUM TO DIVIDE:");
int num2=sc.nextInt();
System.out.println("ANSWER:"+num1/num2);
}
catch(ArithmeticException a)
{
System.out.println("ERROR, CAN'T ABLE TO DIVIDE");
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println("END OF THE WORLD");
}
}
}
22-UCA-039

OUTPUT:

U:\cas39\Java Programming\Observation Programs>java Program23


MyException: This is an user defined exception
22-UCA-039

Program 24: MULTI THREADING

Source Code:

class multcls extends Thread


{
public void run()
{
try{
System.out.println("Thread "+this.currentThread().getName()+" is running");
System.out.println("Thread Priority"+this.currentThread().getPriority()+" is
running");
}
catch(Exception e){
System.out.println("Exception caught");
}
}
}
class multint implements Runnable
{
public void run()
{
try{
System.out.println("Thread "+Thread.currentThread().getName()+" is
running");
System.out.println("Thread Priority"+Thread.currentThread().getPriority()+"
is running");
}
catch(Exception e){
System.out.println("Exception caught");
}
}
}
public class threadeg
{
public static void main(String[] args)
{
int n=5;
for(int i=0;i<n;i++)
{
multcls demo=new multcls();
Thread obj=new Thread(new multint());
obj.start();
demo.start();
}
}
}
22-UCA-039

Output:

Thread Thread-1 is running


Thread Thread-3 is running
Thread Thread-0 is running
Thread Thread-8 is running
Thread Priority5 is running
Thread Thread-9 is running
Thread Thread-6 is running
Thread Thread-7 is running
Thread Thread-4 is running
Thread Thread-5 is running
Thread Priority5 is running
Thread Thread-2 is running
Thread Priority5 is running
Thread Priority5 is running
Thread Priority5 is running
Thread Priority5 is running
Thread Priority5 is running
Thread Priority5 is running
Thread Priority5 is running
Thread Priority5 is running
22-UCA-039

Program 25: MULTI THREADING WITH PRIORITIES

Source Code:

class Multithread extends Thread


{
public void run()
{
System.out.println("Running thread name :
"+this.currentThread().getName());
System.out.println("Running thread priority :
"+this.currentThread().getPriority());
}
}

public class Multithreading


{
public static void main(String[] args)
{
Multithread multiThread1=new Multithread();
multiThread1.setName("first thread");
multiThread1.setPriority(Thread.MIN_PRIORITY);
Multithread multiThread2=new Multithread();
multiThread2.setName("second thread");
multiThread2.setPriority(Thread.MAX_PRIORITY);
Multithread multiThread3=new Multithread();
multiThread3.setName("third thread");
multiThread1.start();
multiThread2.start();
multiThread3.start();
}
}

OUTPUT:

Running thread name : second thread


Running thread name : third thread
Running thread name : first thread
Running thread priority : 5
Running thread priority : 10
Running thread priority : 1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy