NPTEL Programming In Java Programming Assignment Week-6 Sept-2023 Swayam

 

Week 6 : Programming Assignment 1

Due on 2023-09-07, 23:59 IST

Complete the code segment to print the following using the concept of extending the Thread class in Java:

-----------------OUTPUT-------------------

Thread is Running.

-------------------------------------------------

Select the Language for this assignment. 
File name for this program : 
// 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();
    }  
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Thread is Running.
Thread is Running.
Passed



Week 6 : Programming Assignment 2

Due on 2023-09-07, 23:59 IST

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.

-------------------------------------------------

Select the Language for this assignment. 
File name for this program : 
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
public static void main(String[] args) {  
        Question62 ex = new Question62();  
        Thread t1= new Thread(ex);  
        t1.setName("Main Thread");
        t1.start();  
        System.out.println("Welcome to Java Week 6 New Question.");  
        t1.setName("Main Thread");
    }
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Welcome to Java Week 6 New Question.\n
Main Thread has ended.
Welcome to Java Week 6 New Question.\n
Main Thread has ended.
Passed



Week 6 : Programming Assignment 3

Due on 2023-09-07, 23:59 IST

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.

Select the Language for this assignment. 
File name for this program : 
// 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() {}
}
// Class MyThread is defined which extends class B
class MyThread extends B {
    // run() is overriden and 'NPTEL Java' is printed.
    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();
     }
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
NPTEL Java week-6 new Assignment Q3
NPTEL Java week-6 new Assignment Q3
Passed



Week 6 : Programming Assignment 4

Due on 2023-09-07, 23:59 IST

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

-------------------------------------------------

Select the Language for this assignment. 
File name for this program : 
/*
A simple class that demonstrates using the 'synchronized'
keyword so that multiple threads may send it messages.
The class stores two ints, a and b; sum() returns
their sum, and inc() increments both numbers.
<p>
The sum() and incr() methods form a "critical section" --
they can compute the wrong thing if run by multiple threads
at the same time. The sum() and inc() methods are declared
"synchronized" -- they respect the lock in the receiver object.
*/
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.
public synchronized int sum() {
return(a+b);
}
 
// Increments both a and b. (writer)
public synchronized 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.
*/
}
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Final sum:6000
Final sum:6000\n
Passed after ignoring Presentation Error



Week 6 : Programming Assignment 5

Due on 2023-09-07, 23:59 IST

Given a snippet of code, 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

-------------------------------------------------

Select the Language for this assignment. 
File name for this program : 
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...
// start the thread-1  
  t1.start();  
// set the name of thread-1
  t1.setName("Week 6 Assignment Q5");  
 
// start the thread-2  
  t2.start();  
// set the name of thread-2
  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());
 
 }  
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Name of thread 't1':Thread-0\n
Name of thread 't2':Thread-1\n
New name of thread 't1':Week 6 Assignment Q5\n
New name of thread 't2':Week 6 Assignment Q5 New
Name of thread 't1':Thread-0\n
Name of thread 't2':Thread-1\n
New name of thread 't1':Week 6 Assignment Q5\n
New name of thread 't2':Week 6 Assignment Q5 New\n
Passed after ignoring Presentation Error


No comments

File Uploading in PHP

  File Uploading in PHP PHP allow you to upload any type of a file i.e. image, binary or text files.etc..,PHP has one in built global variab...