NPTEL Programming in Java Sep 2024 Week 6 Programming Assignment 1 2 3 4 5

 




Week 06 : Programming Assignment 1

Due on 2024-09-05, 23:59 IST
An interface Number is defined in the following program. 
You have to declare a class A, which will implement the interface Number.
Note that the method findSqr(n) will return the square of the number n.
Your last recorded submission was on 2024-08-26, 17:49 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
interface Number {
    int findSqr(int i);  // Returns the square of n
}
//Create a class A which implements the interface Number.
class A implements Number{
  public int findSqr(int i){
    return i*i;
  }
}
public class W06_P1{ 
        public static void main (String[] args){ 
          A a = new A();   //Create an object of class A
           // Read a number from the keyboard
           Scanner sc = new Scanner(System.in);  
           int i = sc.nextInt();
           System.out.print(a.findSqr(i)); 
    } 
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
12
144
144
Passed




Week 06 : Programming Assignment 2

Due on 2024-09-05, 23:59 IST
This program is to find the GCD (greatest common divisor) of two integers writing a recursive function findGCD(n1,n2).
Your function should return -1, if the argument(s) is(are) negative number(s).
Your last recorded submission was on 2024-08-26, 18:10 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
interface GCD {
    public int findGCD(int n1,int n2);
}
//Create a class B, which implements the interface GCD
class B implements GCD{
  public int findGCD(int n1,int n2){
    if ( n1 < 0 || n2 < 0 )
      return(-1);
    else if ( n2 == 0 )
      return n1;
    else if ( n1 > n2 )
      return findGCD(n2,n1%n2);
    else
      return findGCD(n1,n2%n1);
      }
}
public class W06_P2{ 
        public static void main (String[] args){ 
          B a = new B();   //Create an object of class B
            // Read two numbers from the keyboard
            Scanner sc = new Scanner(System.in);  
            int p1 = sc.nextInt();
           int p2 = sc.nextInt();
            System.out.print(a.findGCD(p1,p2)); 
    } 
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
20 30
10
10
Passed



Week 06 : Programming Assignment 3

Due on 2024-09-05, 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.
-------------------------------------------------
Your last recorded submission was on 2024-08-26, 18:15 IST
Select the Language for this assignment. 
File name for this program : 
// Write the appropriate code to extend the Thread class to complete the class W06_P3.
class W06_P3 extends Thread{
  public void run(){
        System.out.print("Thread is Running.");
    }
public static void main(String args[]){  
 
        // Creating object of thread class
        W06_P3 thread=new W06_P3();  
 
                // Start the thread
        thread.start();
    }  
}
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 06 : Programming Assignment 4

Due on 2024-09-05, 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 some numbers. Do the necessary use of 'synchronized' keyword, so that, the program prints the output in the following order:  
-----------------OUTPUT------------------- (this line should not to be printed)
5
10
15
20
25
100
200
300
400
500
-------------------------------------------------(this line should not to be printed)


(Remember to match the output given exactly, including the spaces and new lines)
(passed with presentation error means you will get full marks)
Your last recorded submission was on 2024-08-26, 18:18 IST
Select the Language for this assignment. 
File name for this program : 
class Execute{
// fix the code below to get the desired output
synchronized void print(int n){
   for(int i=1;i<=5;i++){  
     System.out.println(n*i);  
     try{  
      Thread.sleep(400);  
     }catch(Exception e){
        System.out.println(e);
     }  
   }
 }
} // Ending Execute class
 
class Thread1 extends Thread{  
    Execute t;  
    Thread1(Execute t){  
        this.t=t;  
    }  
    public void run(){  
        t.print(5);  
    } 
}  
 
class Thread2 extends Thread{  
    Execute t;  
    Thread2(Execute t){  
        this.t=t;  
    }  
    public void run(){  
        t.print(100);  
    }  
}  
  
public class W06_P4{  
    public static void main(String args[]){  
        Execute obj = new Execute();//only one object  
        Thread1 t1=new Thread1(obj);  
        Thread2 t2=new Thread2(obj);  
        t1.start();  
        t2.start();  
    }  
}
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 06 : Programming Assignment 5

Due on 2024-09-05, 23:59 IST
Add necessary codes to print the following:

-----------------OUTPUT-------------------
Name of thread 't':Thread-0
New name of thread 't':NPTEL
Thread is running.
-------------------------------------------------
Hint: Use the setName() function
Your last recorded submission was on 2024-08-26, 18:21 IST
Select the Language for this assignment. 
File name for this program : 
class W06_P5 extends Thread{  
  public void run(){  
    System.out.print("Thread is running.");  
  }  
 public static void main(String args[]){  
    W06_P5 t=new W06_P5();  
    System.out.println("Name of thread 't':"+ t.getName());
// Write the necessary code below...
t.setName("NPTEL");
t.start();
System.out.println("New name of thread 't':"+ t.getName());  
 }  
}
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed





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...