Week 05 : Programming Assignments - NPTEL >> Programming In Java - 2025
Week 05 : Programming Assignment 1
Solution :
import java.util.Scanner;
interface Number {
int findSqr(int i); // Returns the square of n
}
// Answer
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;
}
}
//End
public class W05_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));
}
}
Week 05 : Programming Assignment 2
Solution :
import java.util.Scanner;
interface GCD {
public int findGCD(int n1,int n2);
}
// Answer
class B implements GCD {
// Create a method to calculate GCD
public int findGCD(int n1, int n2) {
if (n1 == 0 && n2 == 0) {
return 0;
} else if (n2 == 0) {
return n1;
} else {
return findGCD(n2, n1 % n2);
}
}
}
//End
public class W05_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));
}
}
Week 05 : Programming Assignment 3
Solution :
import java.util.Scanner;
public class W05_P3 {
public static void main(String[] args) {
int a, b;
Scanner input = new Scanner(System.in);
// Read any two values for a and b
int result;
a = input.nextInt();
b = input.nextInt();
// Answer
//Get the result of a/b;
//# 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.");
}
// End
}
}
Week 05 : Programming Assignment 4
Solution :
//Prefixed Fixed Code:
import java.util.Scanner;
import java.util.InputMismatchException;
public class W05_P4 {
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.
// ANSWER
/*
Define try-catch block to save user input in the array "name"
If there is an exception then catch the exception otherwise print the total sum of the array.
*/
try {
for (int i =0; i < length; i++)
{
name[i] = sc.nextInt();
sum = sum + name[i];
}
System.out.print(sum);
}
catch(InputMismatchException e) {
System.out.print("You entered bad data.");
}
// END
}
}
Week 05 : Programming Assignment 5
Solution :
import java.util.Scanner;
public class W05_P5{
public static void main (String args[ ] ) {
Scanner scan = new Scanner(System.in);
int i=scan.nextInt();
int j;
//ANSWER
// Put the following code under try-catch block to handle exceptions
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 (NullPointerException ne)
{
System.out.print("java.lang.NullPointerException");
}
catch (ArithmeticException ae)
{
System.out.print("java.lang.ArithmeticException: / by zero");
}
//END
}
}
import java.util.Scanner;
interface GCD {
public int findGCD(int n1,int n2);
}
// Answer
class B implements GCD {
// Create a method to calculate GCD
public int findGCD(int n1, int n2) {
if (n1 == 0 && n2 == 0) {
return 0;
} else if (n2 == 0) {
return n1;
} else {
return findGCD(n2, n1 % n2);
}
}
}
//End
public class W05_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));
}
}
Solution :
import java.util.Scanner;
public class W05_P3 {
public static void main(String[] args) {
int a, b;
Scanner input = new Scanner(System.in);
// Read any two values for a and b
int result;
a = input.nextInt();
b = input.nextInt();
// Answer
//Get the result of a/b;
//# 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.");
}
// End
}
}
Week 05 : Programming Assignment 4
Solution :
//Prefixed Fixed Code:
import java.util.Scanner;
import java.util.InputMismatchException;
public class W05_P4 {
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.
// ANSWER
/*
Define try-catch block to save user input in the array "name"
If there is an exception then catch the exception otherwise print the total sum of the array.
*/
try {
for (int i =0; i < length; i++)
{
name[i] = sc.nextInt();
sum = sum + name[i];
}
System.out.print(sum);
}
catch(InputMismatchException e) {
System.out.print("You entered bad data.");
}
// END
}
}
Week 05 : Programming Assignment 5
Solution :
import java.util.Scanner;
public class W05_P5{
public static void main (String args[ ] ) {
Scanner scan = new Scanner(System.in);
int i=scan.nextInt();
int j;
//ANSWER
// Put the following code under try-catch block to handle exceptions
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 (NullPointerException ne)
{
System.out.print("java.lang.NullPointerException");
}
catch (ArithmeticException ae)
{
System.out.print("java.lang.ArithmeticException: / by zero");
}
//END
}
}
//Prefixed Fixed Code:
import java.util.Scanner;
import java.util.InputMismatchException;
public class W05_P4 {
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.
// ANSWER
/*
Define try-catch block to save user input in the array "name"
If there is an exception then catch the exception otherwise print the total sum of the array.
*/
try {
for (int i =0; i < length; i++)
{
name[i] = sc.nextInt();
sum = sum + name[i];
}
System.out.print(sum);
}
catch(InputMismatchException e) {
System.out.print("You entered bad data.");
}
// END
}
}
Solution :
import java.util.Scanner;
public class W05_P5{
public static void main (String args[ ] ) {
Scanner scan = new Scanner(System.in);
int i=scan.nextInt();
int j;
//ANSWER
// Put the following code under try-catch block to handle exceptions
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 (NullPointerException ne)
{
System.out.print("java.lang.NullPointerException");
}
catch (ArithmeticException ae)
{
System.out.print("java.lang.ArithmeticException: / by zero");
}
//END
}
}