Monday, October 5, 2020

NPTEL PROGRAMMING IN JAVA WEEK 3 ANSWERS (JUL-DEC 2020)


QUESTION 1:

if (n<=1) 
            return (1-n); 
        return fib(n - 1) + fib(n - 2); 

QUESTION 2:

class Point{
   double x,y;
  
   public double distance(Point p1, Point p2)
                {
                  
                  double root=(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
                  root=Math.sqrt(root);
                  System.out.print(root);
                  return root;
                }
}

QUESTION 3:


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

QUESTION 4:


QuestionScope a= new QuestionScope();
System.out.println(a.sum(n1,n2));
System.out.print(QuestionScope.multiply(n1,n2));

QUESTION 5:


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

0 Comments:

Post a Comment