NPTEL Programming In Java Week 4 Programming Assignment Answers Solution | 2024 July | Swayam

 



Week 04 : Programming Assignment 1

Due on 2024-08-22, 23:59 IST

Complete the code segment to swap two numbers using call by object reference.

Your last recorded submission was on 2024-08-13, 08:27 IST
Select the Language for this assignment. 
File name for this program : 
//Prefixed Fixed code
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 W04_P1 {
// 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) {
    Question t = new Question();
    System.out.println("Before Swap: " + t.e1 + " " + t.e2);
    swap(t);
    System.out.print("After Swap: " + t.e1 + " " + t.e2);
  }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5 10
Before Swap: 5 10\n
After Swap: 10 5
Before Swap: 5 10\n
After Swap: 10 5
Passed



Week 04 : Programming Assignment 2

Due on 2024-08-22, 23:59 IST

1 - Problem Statement:      

Define a class Point with members

§ private double x;

§ private double y;

and methods:

§ public Point(double x, double y){}  // Constructor to create a new point?

§ public double slope(Point p2){} // Function to return the slope of the line formed from current Point and another Point
(Assume that input will always be chosen so that slope will never be infinite)

Your last recorded submission was on 2024-08-13, 08:46 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
//Complete the code segment to define a class Point with parameter x,y and method skope() for calculating slope between two points.
// Note: Pass objectsof type class Point as argument in slope() method.
class Point{
  private double x;
  private double y;
  
  public Point(double x, double y){
    this.x = x;
    this.y = y;
  }
  public double slope(Point p2){
    return ((this.y - p2.y) / (this.x - p2.x));
  }
}
public class W04_P2{         
   public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double x1 = sc.nextDouble();
        double y1 = sc.nextDouble();
        double x2 = sc.nextDouble();
        double y2 = sc.nextDouble();
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        
        System.out.print("Slope: "+p1.slope(p2));
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
0 0
1 1
Slope: 1.0
Slope: 1.0
Passed


Week 04 : Programming Assignment 3

Due on 2024-08-22, 23:59 IST

This program to exercise the create static and non-static methods. A partial code is given, you have to define two methods, namely sum( ) and multiply( ). These methods have been called to find the sum and product of two numbers. Complete the code segment as instructed.  

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
class QuestionScope {
//Create the method sum() to find the sum of two numbers.
//Create the static method multiply() to find the product of two numbers.
public int sum(int n1, int n2) {
  return n1+n2;
}
public static int multiply(int n1, int n2) {
  return n1*n2;
}
}
 
public class W04_P3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n1 = sc.nextInt();
        int n2 = sc.nextInt();
        int sum = 0, prod = 0;
        QuestionScope st = new QuestionScope(); // Create an object to call non-
                                                // static method
        sum = st.sum(n1, n2); // Call the method
        prod = QuestionScope.multiply(n1, n2);  // Create an object to call
                                                 // static method
        System.out.println("Sum: "+sum);
        System.out.print("Product: "+prod);
        sc.close();
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5 10
Sum: 15\n
Product: 50
Sum: 15\n
Product: 50
Passed



Week 04 : Programming Assignment 4

Due on 2024-08-22, 23:59 IST

The program in this assignment is attempted to print the following output:

 

-----------------OUTPUT-------------------

This is large

This is medium

This is small

This is extra-large

-------------------------------------------------

 

However, the code is intentionally with some bugs in it. Debug the code to execute the program successfully.

Select the Language for this assignment. 
File name for this program : 
interface ExtraLarge{
    String extra = "This is extra-large";
    //public static void display(); //commented function declaration
}
 
class Large {
    public void Print() {
        System.out.println("This is large"); //semi-colon here
    }
}
 
class Medium extends Large {
    public void Print() {
          super.Print();  //removed 'super.' from here
        System.out.println("This is medium");
    }
}
class Small extends Medium {
    public void Print() {
        super.Print(); //removed semi-colon from here
        System.out.println("This is small");
    //removed a curly bracket from here
  }
}
class W04_P4 implements ExtraLarge{
    public static void main(String[] args) {
        Small s = new Small();
        s.Print();
        W04_P4 q = new W04_P4();
        q.display();
    }
    public void display(){
        System.out.print(extra);
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
This is large\n
This is medium\n
This is small\n
This is extra-large
This is large\n
This is medium\n
This is small\n
This is extra-large
Passed



Week 04 : Programming Assignment 5

Due on 2024-08-22, 23:59 IST

Consider First n even numbers starting from zero(0).Complete the code segment to calculate sum of  all the numbers divisible by 3 from 0 to n. Print the sum.
Example:

 

Input: n = 5

 

-------

0 2 4 6 8

Even number divisible by 3:0 6

sum:6

Select the Language for this assignment. 
File name for this program : 
// Prefixed Fixed Code:
import java.util.Scanner;
 
public class W04_P5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
// Use for or while loop to sum first n positive even numbers starting from 0 which are
// divisible by 3.
for(int i=0; i<2*n; i=i+2)
  if(i%3==0)
    sum+=i;
System.out.print("Sum: "+sum);// Suffixed Fixed Code:
        sc.close();
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
6
Sum: 6
Sum: 6
Passed


No comments

VR23 Web Technologies Lab MCA II Year I Semester Experiment 7:

  Aim: PHP script to a) Find the length of a string. b) Count no of words in a string. c) Reverse a string. d) Search for a specific string....