Sunday, August 13, 2023

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.


 

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.



Program :

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 Exercise3_4{

public static void main( String[] args ) {

        Scanner sc = new Scanner(System.in);

int n1=sc.nextInt();

int n2=sc.nextInt();

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

}

}



InputOutput
Test Case 1
3 5
8
15

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.

 



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.


Program :

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{

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


InputOutput
Test Case 1
2.0 3.0 4.0
16.0
24.0

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.



 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.


Program :


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

  }

}  



InputOutput
Test Case 1
2.0  3.0
1.0  2.0
1.4142135623730951

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.


 

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.


Program :

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

           if (n==1)      //Terminal condition
            return 0;
        else if(n==2)
            return 1;
return fib(n - 1) + fib(n - 2); //Recursive call of function 
}
}


InputOutput
Test Case 1
8
13


NPTEL » Programming In Java Week-3 Quiz Solution

 


Week 3:Assignment 3


1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 

Write a Program to find the sum of all even numbers from 1 to N where the value of N is taken as input. [For example when N is 10 then the sum is 2+4+6+8+10 = 30]


 

Write a Program to find the sum of all even numbers from 1 to N where the value of N is taken as input. [For example when N is 10 then the sum is 2+4+6+8+10 = 30]


Program :

#include <stdio.h>  

void main()

{

int N, sum=0; 

scanf("%d", &N);

int i;

for (i = 1; i <= N; i++)

{

if (i % 2 == 0)

sum = sum + i;

}

printf("Sum = %d", sum);

}



InputOutput
Test Case 1
10
Sum = 30
Test Case 2
15
Sum = 56

Write a program to find the factorial of a given number using while loop.


 

Write a program to find the factorial of a given number using while loop.


Program :

#include<stdio.h>

void main()

{

    int n;

    long int fact;  

    scanf("%d",&n);

int i=1;

fact = 1;

while(i<=n)

    {

        fact*=i;

        i++;

    }

    printf("The Factorial of %d is : %ld",n,fact);

}



InputOutput
Test Case 1
5
The Factorial of 5 is : 120
Test Case 2
10
The Factorial of 10 is : 3628800

The length of three sides are taken as input. Write a C program to find whether a triangle can be formed or not. If not display “This Triangle is NOT possible.” If the triangle can be formed then check whether the triangle formed is equilateral, isosceles, scalene or a right-angled triangle. (If it is a right-angled triangle then only print Right-angle triangle do not print it as Scalene Triangle).


 

The length of three sides are taken as input. Write a C program to find whether a triangle can be formed or not. If not display “This Triangle is NOT possible.” If the triangle can be formed then check whether the triangle formed is equilateral, isosceles, scalene or a right-angled triangle. (If it is a right-angled triangle then only print Right-angle triangle do not print it as Scalene Triangle).


Program :

#include<stdio.h>

int main()

{

    int a,b,c; 

    scanf("%d %d %d",&a, &b, &c);

if(a<(b+c)&&b<(a+c)&&c<(a+b))

    {

        if(a==b&&a==c&&b==c)

        printf("Equilateral Triangle");

          else if(a==b||a==c||b==c)

          printf("Isosceles Triangle");

          else   

    if((a*a)==(b*b)+(c*c)||(b*b)==(a*a)+(c*c)||(c*c)==(a*a)+(b*b))

        printf("Right-angle Triangle");

        else if(a!=b&&a!=c&&b!=c)

        printf("Scalene Triangle");

    }

    else

    printf("Triangle is not possible");

}



InputOutput
Test Case 1
9 9 9
Equilateral Triangle
Test Case 2
5 12 13
Right-angle Triangle

Write a C Program to Find the Smallest Number among Three Numbers (integer values) using Nested IF-Else statement.


 Write a C Program to Find the Smallest Number among Three Numbers (integer values) using Nested IF-Else statement.


Program  :


#include <stdio.h>

int main()

{

    int n1, n2, n3; 

    scanf("%d %d %d", &n1, &n2, &n3);

if (n1<n2)

    {

        if(n1<n3)

            printf("%d is the smallest number.", n1);

        else

            printf("%d is the smallest number.", n3);

    }

    else

    {

        if(n2<n3)

            printf("%d is the smallest number.", n2);

        else

            printf("%d is the smallest number.", n3);

    }

}



InputOutput
Test Case 1
80 40 90
40 is the smallest number.
Test Case 2
77 88 -99
-99 is the smallest number.

NPTEL » Problem Solving Through Programming In C Week-4 Quiz Solution

 


Week 4 : Assignment 4


1 point
What is the purpose of the "if-else" statement in C?
 
 
 
 
1 point
What is the correct syntax for an "if-else" statement in C?
 
 
 
 
1 point
Which of the following is true about nested "if-else" statements?
 
 
 
 
1 point
What happens if there is no "else" part in an "if-else" statement?
 
 
 
 
1 point
Which of the following operators can be used to combine multiple conditions in an "if" statement?
 
 
 
 
1 point
 
 
 
 
1 point
If multiple conditions are used in a single "if" statement then the testing of those conditions are done
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point