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

 NPTEL PROGRAMMING ASSIGNMENTS

Week 3 : Programming Assignment 1

Due Date of Submission 2022-02-17, 23:59 IST

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.

A partial code is given and you have to complete the code as per the instruction given below.


import java.util.Scanner;
public class Fibonacci { 
public static void main(String args[]){ 
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
System.out.println(fib(n)); 

//Template code:
static int fib(int n) { 


 

 



Week 3 : Programming Assignment 2

Due Date of Submission 2022-02-17, 23:59 IST

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 p1=new Point();
p1.x=sc.nextDouble();
p1.y=sc.nextDouble();
Point p2=new Point();
p2.x=sc.nextDouble();
p2.y=sc.nextDouble(); 
Circle c1=new Circle();
c1.distance(p1,p2);

  }
}

 

 



Week 3 : Programming Assignment 3

Due Date of Submission 2022-02-17, 23:59 IST

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{    


 

 



Week 3 : Programming Assignment 4

Due Date of Submission 2022-02-17, 23:59 IST

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(System.in);
int n1=sc.nextInt();
int n2=sc.nextInt();


 

 



Week 3 : Programming Assignment 5

Due Date of Submission 2022-02-17, 23:59 IST

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(System.in);

int e1 = sc.nextInt();  //Read e1

int e2 = sc.nextInt();  //Read e2

}

public class Question3 {


 

 


Post a Comment (0)
Previous Post Next Post