NPTEL » Programming In Java Week 3 : Programming Assignment 5 Aug 2022

 

Week 3 : Programming Assignment 5

Due on 2022-08-18, 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 {

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


    public static void swap(Question t)

    {

    int temp = t.e1;

        t.e1 = t.e2;

        t.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);

    }

}






netaji gandi Tuesday, August 16, 2022
NPTEL » Programming In Java Week 3 : Programming Assignment 4 Aug 2022

 

Week 3 : Programming Assignment 4

Due on 2022-08-18, 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();


        //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(); // Create an object to call non-static method

        int result1=st.sum(n1,n2); // Call the method

        int result2=QuestionScope.multiply(n1,n2); // Create an object to call static method


        System.out.println(result1);

        System.out.print(result2);

    }

}












netaji gandi
NPTEL » Programming In Java Week 3 : Programming Assignment 3 Aug 2022

 

Week 3 : Programming Assignment 3

Due on 2022-08-18, 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{


    //Template code:

double height;

Test1(double length,double h) {

    //base class constructor with one parameter is called

super(length);

height=h;

}


Test1(double length,double breadth,double h) {

    //base class constructor having two argument is called

super(length,breadth);

height=h;

}


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);

}

}




netaji gandi
NPTEL » Programming In Java Week 3 : Programming Assignment 2 Aug 2022

 

Week 3 : Programming Assignment 2

Due on 2022-08-18, 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);


  }


}


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));

  System.out.print(d);

    }

}



netaji gandi
NPTEL » Programming In Java Week 3 : Programming Assignment 1 Aug 2022

 

Week 3 : Programming Assignment 1

Due on 2022-08-18, 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) {

    if (n==1)      //Terminal condition

    return 0;

    else if(n==2)

    return 1;

    return fib(n - 1) + fib(n - 2); //Recursive call of function

    }

}







netaji gandi
NPTEL » Programming In Java Week 3:Assignment 3 Aug 2022

 

Week 3:Assignment 3




Due date: 2022-08-17, 23:59 IST.
Assignment not submitted
1 point
 
 
 
 
1 point
 
 
 
 Ans:a
1 point
 
 
 
 Ans: C
1 point
 
 
 
 Ans: b
1 point
 
 
 
 Ans: C
1 point
 
 
 
 Ans: d
1 point
 
 
 
 Ans: d
1 point
 
 
 
 Ans: C
1 point
 
 
 
 Ans: b
1 point
 
 
 
 Ans: a

netaji gandi
WEEK-3 Programming, Data Structures And Algorithms Using Python Aug 2022

  Week 3 Programming Assignment



Write three Python functions as specified below. Paste the text for all three functions together into the submission window. Your function will be called automatically with various inputs and should return values as specified. Do not write commands to read any input or print any output.

  • You may define additional auxiliary functions as needed.
  • In all cases you may assume that the value passed to the function is of the expected type, so your function does not have to check for malformed inputs.
  • For each function, there are normally some public test cases and some (hidden) private test cases.
  • "Compile and run" will evaluate your submission against the public test cases.
  • "Submit" will evaluate your submission against the hidden private test cases. There are 15 private test cases, with equal weightage. You will get feedback about which private test cases pass or fail, though you cannot see the actual test cases.
  • Ignore warnings about "Presentation errors".

  1. Write a function contracting(l) that takes as input a list of integer l and returns True if the absolute difference between each adjacent pair of elements strictly decreases.

    Here are some examples of how your function should work.

      >>> contracting([9,2,7,3,1])
      True
    
      >>> contracting([-2,3,7,2,-1]) 
      False
    
      >>> contracting([10,7,4,1])
      False
    
  2. In a list of integers l, the neighbours of l[i] are l[i-1] and l[i+1]l[i] is a hill if it is strictly greater than its neighbours and a valley if it is strictly less than its neighbours.

    Write a function counthv(l) that takes as input a list of integers l and returns a list [hc,vc] where hc is the number of hills in l and vc is the number of valleys in l.

    Here are some examples to show how your function should work.

     
    >>> counthv([1,2,1,2,3,2,1])
    [2, 1]
    
    >>> counthv([1,2,3,1])
    [1, 0]
    
    >>> counthv([3,1,2,3])
    [0, 1]
    
    
  3. A square n×n matrix of integers can be written in Python as a list with n elements, where each element is in turn a list of n integers, representing a row of the matrix. For instance, the matrix

      1  2  3
      4  5  6
      7  8  9
    

    would be represented as [[1,2,3], [4,5,6], [7,8,9]].

    Write a function leftrotate(m) that takes a list representation m of a square matrix as input, and returns the matrix obtained by rotating the original matrix counterclockwize by 90 degrees. For instance, if we rotate the matrix above, we get

      3  6  9
      2  5  8    
      1  4  7
    

    Your function should not modify the argument m provided to the function rotate().

    Here are some examples of how your function should work.

     
      >>> leftrotate([[1,2],[3,4]])
      [[2, 4], [1, 3]]
    
      >>> leftrotate([[1,2,3],[4,5,6],[7,8,9]])
      [[3, 6, 9], [2, 5, 8], [1, 4, 7]]
    
      >>> leftrotate([[1,1,1],[2,2,2],[3,3,3]])
      [[1, 2, 3], [1, 2, 3], [1, 2, 3]]


Program:

    

def contracting(l):
  for i in range(len(l)-3):
    a=abs(l[i+2]-l[i+1])
    b=abs(l[i+1]-l[i])
    if a<b:
      continue
    else:
      return False
  return True  
def counthv(l):
  if len(l)>2:
    hc,vc=0,0
    for i in range(1,len(l)-1):
      if l[i]>l[i-1] and l[i]>l[i+1]:
        hc+=1
      elif l[i]<l[i-1] and l[i]<l[i+1]:
        vc+=1
  return ([hc,vc]) 
def leftrotate(l):
  n=[]
  for i in range(len(l)-1,-1,-1):
    m=[]
    for j in range(len(l)):
      m.append(l[j][i])
    n.append(m)
  return n  

                  

netaji gandi

OBJECT ORIENTED PROGRAMMING USING JAVA LAB

Java Programming Lab Experiments VR-23 2025-26 Java Programming Lab Experiments Experiment 1: Fibonacci Sequence The F...