JAVA Week 3 NPTEL answers

  Java Week 3:Q1 To the generation of Fibonacci numbers.

import java.util.Scanner; //This package for reading input
public class Fibonacci { 
    
    public static void main(String args[]) { 
	 Scanner sc = new Scanner(System.in);
	 int n=sc.nextInt(); //Read an integer
        System.out.println(fib(n)); //Generate and print the n-th Fibonacci                
                                     //number
    } 
 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.
		int j=n-1;
		if( j == 0 )
		{
			return 0;
		}
		else if( j == 1 )
		{ 
			return 1;
 		}
		else 
		{
			return (fib(n-2)+fib(n-1));
		}
	}
}

Java Week 3:Q2 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(System.in);
	 Point c=new Point(); //Create a point center
	 c.x=sc.nextDouble();
	 c.y=sc.nextDouble();
	 Point p=new Point();  //Create a point on circumference
	 p.x=sc.nextDouble();
	 p.y=sc.nextDouble(); 
	 Circle c1=new Circle(); //Create an object of class Circle
	 c1.distance(c,p);  //Calcualte radius of the circle
	
  }
 
}
//Complete the code segment to define a class Point with variable x,y and method distance() that calculates the distance between two points.
//Note: Pass objects of type class Point as argument in distance() method. 
class Point {
	double x,y;
	double distance(Point p1, Point p2)
	{
		double result;
		result = Math.sqrt(Math.pow((p2.x - p1.x),2) + Math.pow((p2.y - p1.y),2));
 		System.out.print(result);
		return result;		
	}
}

Java Week 3:Q3 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
double height;
Test1(double l,double h)
{
 super(l);
 this.length = l;
 this.height = h;
}
//Create a derived class constructor which can call the two parametrized constructor of the base class
Test1(double l,double b,double h)
{
  super(l,b);
  this.length=l;
  this.breadth=b;
  this.height=h;
}
//Override the method calculate() in the derived class to find the volume of a shape instead of finding the area of a shape
double calculate()
{
 return length*breadth*height; 
}




public static void main(String args[]){
	Scanner sc = new Scanner(System.in);//Create an object to read input                                                            
	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();
	volume2=myshape2.calculate();
	System.out.println(volume1);
	System.out.println(volume2);
	}
}

Java Week 3:Q4 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.

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(System.in);
		int n1=sc.nextInt();
		int n2=sc.nextInt();
QuestionScope q = new QuestionScope();
//Called the method sum() to find the sum of two numbers.
int res_sum,res_mult;
res_sum= q.sum(n1,n2);
//Called the method multiply() to find the product of two numbers.
res_mult= q.multiply(n1,n2);

System.out.println(res_sum);
System.out.print(res_mult);



 }  
}  

Java Week 3:Q5 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(System.in);
  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 int temp;
static void swap(Question q)
{
	int temp;
	temp = q.e1;
	q.e1 = q.e2;
	q.e2 = temp;
}

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

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

Comments