0% found this document useful (0 votes)
2 views11 pages

Uveshjava

This document is an assignment for Java Programming-II from Narmada College of Science and Commerce, detailing practical tasks for students. It includes various Java programming exercises such as creating threads, printing even numbers, and managing thread properties. The assignment is intended for the academic year 2024-25 and includes student and teacher information along with code examples and outputs for each task.

Uploaded by

uveshgata
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 views11 pages

Uveshjava

This document is an assignment for Java Programming-II from Narmada College of Science and Commerce, detailing practical tasks for students. It includes various Java programming exercises such as creating threads, printing even numbers, and managing thread properties. The assignment is intended for the academic year 2024-25 and includes student and teacher information along with code examples and outputs for each task.

Uploaded by

uveshgata
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/ 11

NARMADA COLLEGE OF SCIENCE AND COMMERCE

Zadeshwar, Bharuch, Gujarat 392001.

JAVA PROGRAMMING-II
Assingment-1(Practical)

Name : Gata Mohammaduvesh


Roll No : CS-3006
Subject : Java Programming-II
Semester : 6th
Year : 2024-25
Teacher’s : Komal Dave
Name
H.O.D. : A.D. Prabha Kumari

Teacher’s Sign H.O.D. Sign


______________ __________________
NARMADA COLLEGE OF SCIENCE AND COMMERCE
ZADESHWAR BHARUCH, GUJARAT 392011
Affiliated to Veer Narmad South Gujarat University, Surat
_______________________________________________________________

CERTIFICATE
This is to certify that Mr./Miss________________________
Student of
T.Y. B. Sc (Computer Science) Sem-6, Exam
No_______________
Has satisfactorily completed his/her practical work in
Subject ___________________
At Narmada college of science and commerce Bharuch
for the
Academic year 2024-25.

______________ ___________________
Teacher’s Sign (A.D. Prabhakumari)
H.O.D. of Computer
Science
NCSC, Zadeshwar,
Bharuch.

Date___/___/___
INDEX

SR.NO PRACTICAL SUBMISSIO TEACHER


N DATE SIGN
1 ASSINGMENT-1
Q1. Write a Java Program to Create a thread using thread class
and runnable interface.
Code: - #Using Thread Class
class cl0 extends Thread
{
public void run ()
{
System.out.println("Thread Created");
}
public static void main (String args [])
{
cl2 t1=new cl2();
t1. Start ();
}}
Output: -
C:\sem-6 Java\Assingment-1>javac q1.java
C:\sem-6 Java\Assingment-1>java cl0
Thread Created
Code: - #Using Runnable Interface
class animal implements Runnable
{
public void run ()
{
System.out.println("Runnable Interface");
}
public static void main (String args [])
{ animal a1=new animal ();
Thread t1=new Thread(a1);
t1.start ();
}}
Output: -
C:\sem-6 Java\Assingment-1>javac q1.java
C:\sem-6 Java\Assingment-1>java animal
Runnable Interface
Q2. Write a java program to print even number every 5 seconds.
Code: -
class cl1 extends Thread
{
public void run ()
{
For (int i=1; i<=50; i++)
{
try
{
int tm1=1000;
cl1.sleep(tm1);
if(i%2==0)
{
System.out.println("Even Number="+i);
}}
Catch (Exception e)
{
System.out.println("Error is: "+e);
}}}
public static void main (String args [])
{
cl1 t1=new cl1();
t1. start ();
}}
Output: -
C:\sem-6 Java\Assingment-1>javac q2.java
C:\sem-6 Java\Assingment-1>java cl1
Even Number=2
Even Number=4
Even Number=6
Even Number=8
Even Number=10
Even Number=12
Even Number=14
Even Number=16
Even Number=18
Even Number=20
Even Number=22
Even Number=24
Even Number=26
Even Number=28
Even Number=30
Even Number=32
Even Number=34
Even Number=36
Even Number=38
Even Number=40
Even Number=42
Even Number=44
Even Number=46
Even Number=48
Even Number=50
Q3. Write a java program to set and get current thread name and
also retrive thread id, priority and state.
Code: -
class thr1 implements Runnable
{
public void run ()
{
System.out.println("Thread is runnable");
}
public static void main (String args [])
{
thr1 ob1=new thr1();
Thread t1=new Thread(ob1,"Thread0");
String s1=t1.getName();
t1. start ();
System.out.println("Thread Name is:"+s1);
System.out.println("Thread Priority is:"+t1.getPriority());
System.out.println("Thread Id is:"+t1.getId());
System.out.println("Thread State is:"+t1.getState());
}}

Output: -
C:\sem-6 Java\Assingment-1>javac q3.java
C:\sem-6 Java\Assingment-1>java thr1
Thread is runnable
Thread Name is: Thread0
Thread Priority is:5
Thread Id is:19
Thread State is: TERMINATED
Q4. Write a java program to set and get priority of user created
thread.
Code: -
class thr2 implements Runnable
{
public void run ()
{
System.out.println("Thread is runnable");
}
public static void main (String args [])
{
thr2 ob0=new thr2();
Thread t1=new Thread(ob0,"Thread01");
String s1=t1.getName();
t1. Start ();
System.out.println("Thread Name is:"+s1);
t1. setPriority (Thread.MAX_PRIORITY);
System.out.println("Thread setPriority is:"+t1.getPriority());
}}
Output: -
C:\sem-6 Java\Assingment-1>javac q4.java
C:\sem-6 Java\Assingment-1>java thr2
Thread is runnable
Thread Name is: Thread01
Thread setPriority is:10
Q.5 Write a java program to print good morning and welcome
continues on the screen in java using thread.
Code: -
class c1 extends Thread
{
public void run ()
{
while(true)
{
System.out.println("Good Morning!");
System.out.println("Welcome!");
try
{
Thread.sleep(2000);
}
Catch (Exception e)
{
System.out.println("Error is "+e);
}}}
public static void main (String args [])
{
c1 t1=new c1();
t1. start ();
}}
Output: -
C:\sem-6 Java\Assingment-1>javac q5.java
C:\sem-6 Java\Assingment-1>java c1
Good Morning!
Welcome!
Good Morning!
Welcome!
Q.6 Write a java program to check the thread is daemon thread
or not set anyone user thread to daemon thread.
Code: -
class cl2 extends Thread
{
public void run ()
{
If (cl2.currentThread(). isDaemon())
{
System.out.println("Deamon Thread Created");
}
else
{
System.out.println("Normal Thread Created");
}}
public static void main (String args [])
{
cl2 t1=new cl2();
cl2 t2=new cl2();
t1.setDaemon(true);
t1.start();
t2.start();
}}
Output: -
C:\sem-6 Java\Assingment-1>javac q6.java
C:\sem-6 Java\Assingment-1>java cl2
Deamon Thread Created
Normal Thread Created

You might also like