Week 08 : Programming Assignments - NPTEL >> Programming In Java - 2025
Week 08 : Programming Assignment 1
Solution :
public class W08_P1 {
// Create a class that extends Thread
static class MyThread extends Thread {
@Override
public void run() {
// Answer
System.out.print("Thread is running");
//End
}
}
public static void main(String[] args) {
// Create object of MyThread
MyThread t = new MyThread();
// Start the thread
t.start();
}
}
Week 08 : Programming Assignment 2
Solution :
public class W08_P2 {
// Create a class that implements Runnable interface
static class MyRunnable implements Runnable {
@Override
public void run() {
// Answer
System.out.print("Runnable thread is running");
//End
}
}
public static void main(String[] args) {
// Create object of MyRunnable
MyRunnable r = new MyRunnable();
// Create Thread using Runnable object
Thread t = new Thread(r);
// Start the thread
t.start();
}
}
Week 08 : Programming Assignment 3
Solution :
public class W08_P3 {
// Create a class that extends Thread
static class MyThread extends Thread {
@Override
public void run() {
// Answer
System.out.println("Thread is running");
// End
}
}
public static void main(String[] args) {
// Create thread object
MyThread t = new MyThread();
// Thread is created but not started yet
System.out.println("Thread state before start");
// Start thread
t.start();
// Thread has started running
System.out.println("Thread state after start");
try {
// Wait for thread to finish
t.join();
} catch (InterruptedException e) {
// Not needed for beginners, but required to handle possible interruptions
}
// Thread has finished
System.out.println("Thread state after completion");
}
}
Week 08 : Programming Assignment 4
Solution :
public class W08_P4 {
// Thread class
static class MyThread extends Thread {
@Override
public void run() {
// No output here to keep portal testing consistent
}
}
public static void main(String[] args) {
MyThread t = new MyThread();
// Set thread priority
t.setPriority(8);
// Start thread
t.start();
// ANSWER
System.out.print("Thread priority is: " + t.getPriority());
// END
}
}
Week 08 : Programming Assignment 5
Solution :
public class W08_P5 {
// Shared class with a number
static class Counter {
int count = 0;
// Synchronized method to safely increase number
public synchronized void increment() {
count++;
}
}
// Thread class to run increment
static class MyThread extends Thread {
Counter c;
MyThread(Counter c) {
this.c = c;
}
@Override
public void run() {
//ANSWER
for(int i =1 ; i<=1000 ; i++)
{
c.increment();
}
//END
}
}
public static void main(String[] args) {
Counter c = new Counter();
// Create two threads
MyThread t1 = new MyThread(c);
MyThread t2 = new MyThread(c);
// Start both threads
t1.start();
t2.start();
try {
// Wait for both threads to finish
t1.join();
t2.join();
} catch (InterruptedException e) {
}
// Print final count
System.out.println("Final count is: " + c.count);
}
}
public class W08_P2 {
// Create a class that implements Runnable interface
static class MyRunnable implements Runnable {
@Override
public void run() {
// Answer
System.out.print("Runnable thread is running");
//End
}
}
public static void main(String[] args) {
// Create object of MyRunnable
MyRunnable r = new MyRunnable();
// Create Thread using Runnable object
Thread t = new Thread(r);
// Start the thread
t.start();
}
}
Solution :
public class W08_P3 {
// Create a class that extends Thread
static class MyThread extends Thread {
@Override
public void run() {
// Answer
System.out.println("Thread is running");
// End
}
}
public static void main(String[] args) {
// Create thread object
MyThread t = new MyThread();
// Thread is created but not started yet
System.out.println("Thread state before start");
// Start thread
t.start();
// Thread has started running
System.out.println("Thread state after start");
try {
// Wait for thread to finish
t.join();
} catch (InterruptedException e) {
// Not needed for beginners, but required to handle possible interruptions
}
// Thread has finished
System.out.println("Thread state after completion");
}
}
Week 08 : Programming Assignment 4
Solution :
public class W08_P4 {
// Thread class
static class MyThread extends Thread {
@Override
public void run() {
// No output here to keep portal testing consistent
}
}
public static void main(String[] args) {
MyThread t = new MyThread();
// Set thread priority
t.setPriority(8);
// Start thread
t.start();
// ANSWER
System.out.print("Thread priority is: " + t.getPriority());
// END
}
}
Week 08 : Programming Assignment 5
Solution :
public class W08_P5 {
// Shared class with a number
static class Counter {
int count = 0;
// Synchronized method to safely increase number
public synchronized void increment() {
count++;
}
}
// Thread class to run increment
static class MyThread extends Thread {
Counter c;
MyThread(Counter c) {
this.c = c;
}
@Override
public void run() {
//ANSWER
for(int i =1 ; i<=1000 ; i++)
{
c.increment();
}
//END
}
}
public static void main(String[] args) {
Counter c = new Counter();
// Create two threads
MyThread t1 = new MyThread(c);
MyThread t2 = new MyThread(c);
// Start both threads
t1.start();
t2.start();
try {
// Wait for both threads to finish
t1.join();
t2.join();
} catch (InterruptedException e) {
}
// Print final count
System.out.println("Final count is: " + c.count);
}
}
public class W08_P4 {
// Thread class
static class MyThread extends Thread {
@Override
public void run() {
// No output here to keep portal testing consistent
}
}
public static void main(String[] args) {
MyThread t = new MyThread();
// Set thread priority
t.setPriority(8);
// Start thread
t.start();
// ANSWER
System.out.print("Thread priority is: " + t.getPriority());
// END
}
}
Solution :
public class W08_P5 {
// Shared class with a number
static class Counter {
int count = 0;
// Synchronized method to safely increase number
public synchronized void increment() {
count++;
}
}
// Thread class to run increment
static class MyThread extends Thread {
Counter c;
MyThread(Counter c) {
this.c = c;
}
@Override
public void run() {
//ANSWER
for(int i =1 ; i<=1000 ; i++)
{
c.increment();
}
//END
}
}
public static void main(String[] args) {
Counter c = new Counter();
// Create two threads
MyThread t1 = new MyThread(c);
MyThread t2 = new MyThread(c);
// Start both threads
t1.start();
t2.start();
try {
// Wait for both threads to finish
t1.join();
t2.join();
} catch (InterruptedException e) {
}
// Print final count
System.out.println("Final count is: " + c.count);
}
}