NPTEL Programming In Java Programming Assignment Week-5 August-2023 Swayam

 

Week 5 : Programming Assignment 1

Due on 2023-08-31, 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.
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 {
    //Define a method to find the square of a number
     int i, square;
     public int findSqr(int i) {
         square=i*i;
            return square;       
      }
}
public class Question5_1{ 
        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)); 
    } 
}
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
5
25
25
Passed



Week 5 : Programming Assignment 2

Due on 2023-08-31, 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) other than positive number(s).
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 {
    int n1,n2;
        
    //Create a method to calculate GCD
    public int findGCD(int n1, int n2){
        if(n1==0&& n2==0) {
            return -1;
        }
        else if(n2 == 0){
            return n1;
        }
        
        else {
            return findGCD(n2, n1%n2);
        }
        }
 }
public class Question5_2{ 
        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)); 
    } 
}
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
40 60
20
20
Passed
Test Case 2
2 0
2
2
Passed
Test Case 3
-1 -1
-1
-1
Passed



Week 5 : Programming Assignment 3

Due on 2023-08-31, 23:59 IST
Complete the code segment to catch the ArithmeticException in the following, if any. On the occurrence of such an exception, your program should print “Exception caught: Division by zero.” If there is no such exception, it will print the result of division operation on two integer values.
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
  
  public class Question5_3 {
  public static void main(String[] args) { 
      int a, b;
      Scanner input = new Scanner(System.in);
       int result;  
       a = input.nextInt();
       b = input.nextInt();
  
      // try block to divide two numbers and display the result
         try {
              result = a/b;
              System.out.print(result);
             }
          // catch block to catch the ArithmeticException
          catch (ArithmeticException e) {
             System.out.print("Exception caught: Division by zero.");
          }
}
}
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
4 0
Exception caught: Division by zero.
Exception caught: Division by zero.
Passed
Test Case 2
10 3
3
3
Passed



Week 5 : Programming Assignment 4

Due on 2023-08-31, 23:59 IST
In the following program, an array of integer data to be initialized. During the initialization, if a user enters a value other than integer value, then it will throw InputMismatchException exception. On the occurrence of such an exception, your program should print “You entered bad data.” If there is no such exception it will print the total sum of the array.
Select the Language for this assignment. 
File name for this program : 
//Prefixed Fixed Code:
import java.util.*;
public class Question5_4{
  public static void main(String[] args) {
      Scanner sc = new Scanner(System.in); 
      int length = sc.nextInt(); 
      // create an array to save user input 
      int[] name = new int[length];
      int sum=0;//save the total sum of the array.
try{
       for(int i=0;i<length;i++){  
          int userInput=sc.nextInt();
          name[i] = userInput;
          sum=sum+name[i]; 
          } 
        System.out.print(sum);
        }
       catch(InputMismatchException e) {
        System.out.print("You entered bad data.");
        }
}
}
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
3
5 2 1
8
8
Passed
Test Case 2
2
1 1.0
You entered bad data.
You entered bad data.
Passed



Week 5 : Programming Assignment 5

Due on 2023-08-31, 23:59 IST

In the following program, there may be multiple exceptions. You have to complete the code using only one try-catch block to handle all the possible exceptions.

For example, if user’s input is 1, then it will throw and catch java.lang.NullPointerException“.

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
public class Question5_5{
    public static void main (String   args[ ] ) {
           Scanner scan = new Scanner(System.in);
            int i = scan.nextInt();
        int j;
try {
             switch (i) {
            case 0 : 
            int zero = 0; 
            j = 92/ zero;       
            break;
            case 1 : 
            int b[ ] = null; 
            j = b[0] ;  
            break;
               default:
               System.out.print("No exception");
            }       
          }
            // catch block          
        catch (Exception e) {       
           System.out.print(e) ;
        }
}
}
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
5
No exception
No exception
Passed
Test Case 2
0
java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
Passed
Test Case 3
1
java.lang.NullPointerException
java.lang.NullPointerException
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...