Programming In Java Week 5: Programming Assignments Jan-Jun 2022

 NPTEL PROGRAMMING ASSIGNMENTS

Week 5 : Programming Assignment 1

Due Date of Submission 2022-03-03, 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.


import java.util.Scanner;
interface Number {
    int findSqr(int i);  // Returns the square of n
}

 

 

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)); 
    } 
}




Week 5 : Programming Assignment 2

Due Date of Submission 2022-03-03, 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).


import java.util.Scanner;
interface GCD {
    public int findGCD(int n1,int 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)); 
    } 
}




Week 5 : Programming Assignment 3

Due Date of Submission 2022-03-03, 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.


import java.util.Scanner;
  public class Question5_3 {
  public static void main(String[] args) { 
      int a, b;
      Scanner input = new Scanner(System.in);



 

 



Week 5 : Programming Assignment 4

Due Date of Submission 2022-03-03, 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.

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;


 

 



Week 5 : Programming Assignment 5

Due Date of Submission 2022-03-03, 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“.


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; 


 

 


Post a Comment (0)
Previous Post Next Post