Programming in Java | NPTEL | Week 3 Programming Assignments Solutions

This article will help you with the answers of Programming in Java NPTEL 2022 Week 3 Programming Assignment Solutions.

Programming in Java NPTEL Week 3 Programming Assignment 1 Solutions

This program is related to the generation of Fibonacci numbers.
For example: 0,1, 1,2, 3,5, 8, 13,… is a Fibonacci sequence where 13 is the 8th Fibonacci number.

import java.util.Scanner;
public class Fibonacci { 

public static void main(String args[]){ 
	Scanner sc = new Scanner(;
	int n=sc.nextInt();
//Template code:
static int fib(int n) { 

//complete the code segment to find the nth Fibonacci number in the Fibonacci sequence and return the value. Write the function recursively.
  return 0;
else if(n==2)
  return 1;
return fib(n-1) + fib(n-2);


Programming Assignment 2 Solution

Define a class Point with two fields x and y each of type double. Also, define a method distance(Point p1, Point p2) to calculate the distance between points p1 and p2 and return the value in double.
Complete the code segment given below. Use Math.sqrt( ) to calculate the square root.

import java.util.Scanner;

public class Circle extends Point{
public static void main(String[] args) {

	 Scanner sc = new Scanner(;
	 Point p1=new Point();
	 Point p2=new Point();
	 Circle c1=new Circle();


//Complete the code segment to define a class Point with parameter x,y and method distance()for calculating distance between two points.
//Note: Pass objectsof type class Point as argument in distance() method. 
class Point{
  double x;
  double y;
  public static void distance(Point p1, Point p2) {
    double d;
    d = Math.sqrt((p2.x - p1.x)*(p2.x - p1.x) + (p2.y - p1.y)*(p2.y - p1.y));

Programming Assignment 3 Solution

A class Shape is defined with two overloading constructors in it. Another class Test1 is partially defined which inherits the class Shape. The class Test1 should include two overloading constructors as appropriate for some object instantiation shown in main() method. You should define the constructors using the super class constructors. Also, override the method calculate( ) in Test1 to calculate the volume of a Shape.

import java.util.Scanner;
class Shape{
double length, breadth;
Shape(double l, double b){ //Constructor to initialize a Shape object  
length = l;
breadth= b;
Shape(double len){    //Constructor to initialize another Shape object  
length = breadth = len;
double calculate(){// To calculate the area of a shape object
return length * breadth ;
public class Test1 extends Shape{    

//Create a derived class constructor which can call the one parametrized constructor of the base class
//Create a derived class constructor which can call the two parametrized constructor of the base class
//Override the method calculate() in the derived class to find the volume of a shape instead of finding the area of a shape

double height;
Test1(double length, double h) {
  height = h;
Test1(double length, double breadth, double h) {
  super(length, breadth);
  height = h;
double calculate() {
  return length*breadth*height;

public static void main(String args[]){
	Scanner sc = new Scanner(;//Create an object to read                                                               
	double l=sc.nextDouble(); //Read length
	double b=sc.nextDouble(); //Read breadth	
	double h=sc.nextDouble(); //Read height
	Test1 myshape1 = new Test1(l,h);
	Test1 myshape2 = new Test1(l,b,h);
	double volume1;
	double volume2;
	volume1 = myshape1.calculate();

Programming Assignment 4 Solution

This program to exercise the call of static and non-static methods. A partial code is given defining two methods, namely sum( ) and multiply ( ). You have to call these methods to find the sum and product of two numbers. Complete the code segment as instructed.  

import java.util.Scanner;
class QuestionScope {
int sum(int a, int b){ //non-static method
        return a + b;
static int multiply(int a, int b){ //static method
        return a * b;
public class Test3{
public static void main( String[] args ) {
        Scanner sc = new Scanner(;
		int n1=sc.nextInt();
		int n2=sc.nextInt();

//Called the method sum() to find the sum of two numbers.
//Called the method multiply() to find the product of two numbers.
QuestionScope st = new QuestionScope();
int result1 = st.sum(n1, n2);
int result2 = QuestionScope.multiply(n1, n2);



Week 3: Programming Assignment 5 Solution

Complete the code segment to swap two numbers using call by object reference.

import java.util.Scanner;
class Question {  //Define a class Question with two elements e1 and e2.
  Scanner sc = new Scanner(;
int e1 = sc.nextInt();  //Read e1
int e2 = sc.nextInt();  //Read e2
public class Question3 {

// Define static method swap()to swap the values of e1 and e2 of class Question.

static void swap(Question obj) {
  int temp;
  temp = obj.e1;
  obj.e1 = obj.e2;
  obj.e2 = temp;

public static void main(String[] args) {
//Create an object of class Question
	Question t = new Question ();
  //Call the method swap()

System.out.println(t.e1+" "+t.e2);    


The above question set contains all the correct answers. But in any case, you find any typographical, grammatical or any other error in our site then kindly inform us. Don’t forget to provide the appropriate URL along with error description. So that we can easily correct it.

Thanks in advance.

For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.

Leave a Comment

Your email address will not be published. Required fields are marked *