Programming in Java | NPTEL 2022 | Week 6 Programming Assignments Solutions

This article will help you with the answers of Programming in Java NPTEL 2022 Week 6 Programming Assignment Solutions.

Programming in Java NPTEL Week 6 Programming Assignment 1 Solutions

Complete the code segment to print the following using the concept of extending the Thread class in Java:
—————–OUTPUT——————-
Thread is Running.
————————————————-

// Write the appropriate code to extend the Thread class to complete the class Question61.
public class Question61 extends Thread{ 

        public void run(){
               System.out.print("Thread is Running.");
        }

	public static void main(String args[]){  

		// Creating object of thread class
		Question61 thread=new Question61();  

                // Start the thread
		thread.start();
	}  
}

Programming Assignment 2 Solution

In the following program, a thread class Question62 is created using the Runnable interface Complete the main() to create a thread object of the class Question62 and run the thread. It should print the output as given below.
—————–OUTPUT——————-
Welcome to Java Week 6 New Question.
Main Thread has ended.
————————————————-

public class Question62 implements Runnable {  
  
    @Override  
    public void run() {  
        System.out.print(Thread.currentThread().getName()+" has ended.");  
    }

// Create main() method and appropriate statements in it

// Create main() method and appropriate statements in it

public static void main(String[] args) {
  Question62 obj = new Question62();
  Thread t1 = new Thread(obj);
  t1.setName("Main Thread");
  t1.start();
  System.out.println("Welcome to Java Week 6 New Question.");
  t1.setName("Main Thread");
}
}

Programming Assignment 3 Solution

A part of the Java program is given, which can be completed in many ways, for example using the concept of thread, etc.  Follow the given code and complete the program so that your program prints the message “NPTEL Java week-6 new Assignment Q3”. Your program should utilize the given interface/ class.

// Interface A is defined with an abstract method run()
interface A {
	public abstract void run();
}

// Class B is defined which implements A and an empty implementation of run()
class B implements A {
	public void run() {}
}

// Create a class named MyThread and extend/implement the required class/interface

class MyThread extends B {
  
  // Define a method in MyThread class to print the output
  public void run() {
    System.out.print("NPTEL Java week-6 new Assignment Q3");
  }
}


// Main class Question is defined
public class Question63 {
     public static void main(String[] args) {
		 // An object of MyThread class is created
         MyThread t = new MyThread();
		 // run() of class MyThread is called
         t.run();
     }
}

Week 6: Programming Assignment 4 Solution

Execution of two or more threads occurs in a random order. The keyword ‘synchronized’ in Java is used to control the execution of thread in a strict sequence. In the following, the program is expected to print the output as given below. Do the necessary use of ‘synchronized’ keyword, so that, the program prints the Final sum as given below:  
—————–OUTPUT——————-
Final sum:6000
————————————————-

class Pair {
private int a, b;
public Pair() {
a = 0;
b = 0;
}

// Returns the sum of a and b. (reader)
// Should always return an even number.
synchronized public int sum() {
return(a+b);
}

// Increments both a and b. (writer)
synchronized public void inc() {
a++;
b++;
}
}

/*
A simple worker subclass of Thread.
In its run(), sends 1000 inc() messages
to its Pair object.
(1000 may not be big enough to exhibit the bug on uniprocessor --
hardware more like 1000000 may be required).
*/
public class PairWorker extends Thread {
public final int COUNT = 1000;
private Pair pair;
// Ctor takes a pointer to the pair we use
public PairWorker(Pair pair) {
this.pair = pair;
}
// Send many inc() messages to our pair
public void run() {
for (int i=0; i<COUNT; i++) {
pair.inc();
}
}

/*
Test main -- Create a Pair and 3 workers.
Start the 3 workers -- they do their run() --
and wait for the workers to finish.
*/
public static void main(String args[]) {
Pair pair = new Pair();
PairWorker w1 = new PairWorker(pair);
PairWorker w2 = new PairWorker(pair);
PairWorker w3 = new PairWorker(pair);
w1.start();
w2.start();
w3.start();
// the 3 workers are running
// all sending messages to the same object
// we block until the workers complete
try {
w1.join();
w2.join();
w3.join();
}
catch (InterruptedException ignored) {}


System.out.println("Final sum:" + pair.sum()); // should be 6000
/*
If sum()/inc() were not synchronized, the result would
be 6000 in some cases, and other times random values
like 5979 due to the writer/writer conflicts of multiple
threads trying to execute inc() on an object at the same time.
*/
}
}

Week 6: Programming Assignment 5 Solution

Add necessary codes to print the following:
—————–OUTPUT——————-
Name of thread ‘t1’:Thread-0
Name of thread ‘t2’:Thread-1
New name of thread ‘t1’:Week 6 Assignment Q5
New name of thread ‘t2’:Week 6 Assignment Q5 New
————————————————-

public class Question65 extends Thread{  
  public void run(){  
      
  }  
 public static void main(String args[]){  
    Question65 t1=new Question65();  
    System.out.println("Name of thread 't1':"+ t1.getName());  

Question65 t2=new Question65();  
    System.out.println("Name of thread 't2':"+ t2.getName());

// Write the necessary code below...

t1.setName("Week 6 Assignment Q5");
t2.setName("Week 6 Assignment Q5 New");

   System.out.println("New name of thread 't1':"+ t1.getName()); 
   System.out.println("New name of thread 't2':"+ t2.getName());
 
 }  
}

For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.

Leave a Comment

Your email address will not be published. Required fields are marked *