Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

NPTEL Programming In Java Programming Assignment July-2025 Swayam

NPTEL » Programming In Java



  Please scroll down for latest Programs. ðŸ‘‡  



Week 01 : Programming Assignment 1

Due on 2025-08-07, 23:59 IST

Write a Java program to check if a given integer is “Positive” or “Negative”.

(0 (Zero) should be considered positive by this program.)

 

NOTE:

The code you see is not complete.

Your task is to complete the code as per the question.
Think of it like a programming puzzle.

 

(Remember to match the output given exactly, including the spaces and new lines)

(Passed with presentation error means you will get full marks)

Your last recorded submission was on 2025-07-24, 10:13 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W01_P1 {
4
    public static void main(String[] args) {
5
        Scanner in = new Scanner(System.in);
6
        int number = in.nextInt();
7
// Check if the number is Positive or Negative and print accordingly
8
if (number >= 0) {
9
  System.out.print("Positive");
10
} else {
11
  System.out.print("Negative");
12
}
0
in.close();
1
    }
2
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4
Positive
Positive
Passed
Test Case 2
-3
Negative
Negative
Passed



Week 01 : Programming Assignment 2

Due on 2025-08-07, 23:59 IST

Write a Java program to calculate the volume of a cylinder given its radius and height.

Formula:

=2ℎ

You can use Math.PI for the computation.

NOTE:

The code you see is not complete.

Your task is to complete the code as per the question.
Think of it like a programming puzzle.

(This question can be solved in just one line of code)


Your last recorded submission was on 2025-07-24, 10:15 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W01_P2 {
4
    public static void main(String[] args) {
5
        Scanner in = new Scanner(System.in);
6
        double radius = in.nextDouble();
7
        double height = in.nextDouble();
8
// Calculate the volume
9
double volume = Math.PI * radius * radius * height;
0
// Display the result
1
    System.out.printf("Volume is: %.2f", volume);
2
    in.close();
3
  }
4
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
3.5
5.0
Volume is: 192.42
Volume is: 192.42
Passed



Week 01 : Programming Assignment 3

Due on 2025-08-07, 23:59 IST

Write a Java program to print the multiplication table of a given number up to 4.

 

NOTE:

Print EXACTLY as shown in the sample output.

DO NOT MISS a single space otherwise you will not be scored.

(Remember to match the output given exactly, including the spaces and new lines)

(passed with presentation error means you will get full marks)

Your last recorded submission was on 2025-07-24, 10:16 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W01_P3 {
4
    public static void main(String[] args) {
5
        Scanner in = new Scanner(System.in);
6
        int number = in.nextInt();
7
// Print the multiplication table of number up to 5
8
for (int i = 1; i <= 4; i++)
9
  System.out.printf("%d x %d = %d\n", number, i, number * i);
0
in.close();
1
    }
2
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
5 x 1 = 5\n
5 x 2 = 10\n
5 x 3 = 15\n
5 x 4 = 20
5 x 1 = 5\n
5 x 2 = 10\n
5 x 3 = 15\n
5 x 4 = 20\n
Passed after ignoring Presentation Error



Week 01 : Programming Assignment 4

Due on 2025-08-07, 23:59 IST

Complete the code fragment that reads two integer inputs from keyboard and compute the quotient and remainder.

Your last recorded submission was on 2025-07-24, 10:18 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class W01_P4{
3
       public static void main(String[] args) {
4
       Scanner sc = new Scanner(System.in);
5
       int x=sc.nextInt();
6
       int y=sc.nextInt();
7
//code for quotient and remainder
8
if (y == 0) {
9
  System.out.println("Error: Division by zero is not allowed.");
10
}
11
else {
12
  int quotient = x / y;
13
  int remainder = x % y;
14
  System.out.println("The Quotient is = " + quotient);
15
  System.out.print("The Remainder is = " + remainder);
16
}
0
sc.close();  
1
  }
2
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
556
9
The Quotient is = 61\n
The Remainder is = 7
The Quotient is = 61\n
The Remainder is = 7
Passed



Week 01 : Programming Assignment 5

Due on 2025-08-07, 23:59 IST

Write a Java program to print the area and perimeter of a rectangle.

Your last recorded submission was on 2025-07-24, 10:19 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class W01_P5 { 
3
   public static void main(String[] strings) {
4
       double width ;
5
       double height;
6
       Scanner in = new Scanner(System.in);
7
       width = in.nextDouble();
8
       height = in.nextDouble();
9
// Calculate the perimeter of the rectangle
10
double perimeter = 2 * ( height + width ) ;
11
// Calculate the area of the rectangle
12
double area = height * width;
0
// Print the calculated perimeter using placeholders for values
1
       System.out.printf("Perimeter is 2*(%.1f + %.1f) = %.2f\n", height, width, perimeter);
2
3
// Print the calculated area using placeholders for values
4
       System.out.printf("Area is %.1f * %.1f = %.2f", width, height, area);    
5
   }
6
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5.6 8.5
Perimeter is 2*(8.5 + 5.6) = 28.20\n
Area is 5.6 * 8.5 = 47.60
Perimeter is 2*(8.5 + 5.6) = 28.20\n
Area is 5.6 * 8.5 = 47.60
Passed




W02 Programming Assignments 1

Due on 2025-08-07, 23:59 IST

Write a Java program to calculate the area of a rectangle.

The formula for area is:
Area = length × width

You are required to read the length and width from the user, compute the area, and print the result.

This task helps you practice using variables, arithmetic operations, and printing output in Java.

Your last recorded submission was on 2025-08-01, 16:57 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W02_P1 {
4
    public static void main(String[] args) {
5
        Scanner sc = new Scanner(System.in);
6
7
        // Read length and width of the rectangle
8
        int length = sc.nextInt();
9
        int width = sc.nextInt();
10
// ================================================
11
        // NOTE TO STUDENTS:
12
        // This is a simple beginner-level task.
13
        // Your role is to calculate the area using the given length and width.
14
        // Complete the line below using the correct formula.
15
        // ================================================
16
17
        // TODO: Calculate area of the rectangle
18
19
        /*
20
         Hint:
21
         - Multiply length and width to get the area
22
         - Store the result in a variable called 'area'
23
         */
24
int area = length * width;
0
// Print the area
1
        System.out.print("Area is: " + area);
2
3
        sc.close();
4
    }
5
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
10
Area is: 50
Area is: 50
Passed



W02 Programming Assignments 2

Due on 2025-08-07, 23:59 IST

Problem Statement

Write a Java program to calculate the perimeter of a rectangle.

The formula for perimeter is:
Perimeter = 2 multiplied by (length + width)

You are required to read the length and width as integers from the user, compute the perimeter, and print the result.

This problem helps in practicing arithmetic operations and output printing in Java.

Your last recorded submission was on 2025-08-01, 16:59 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W02_P2 {
4
    public static void main(String[] args) {
5
        Scanner sc = new Scanner(System.in);
6
7
        // Read length and width of the rectangle
8
        int length = sc.nextInt();
9
        int width = sc.nextInt();
10
// Complete the code to calculate the perimeter of the rectangle
11
        // TODO: Calculate the perimeter using the correct formula
12
        /*
13
         Hint:
14
         The formula is: perimeter = 2 multiplied by (length + width)
15
         */
16
int perimeter = 2 * (length + width);
0
System.out.println("Perimeter is: " + perimeter);
1
2
        sc.close();
3
    }
4
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4  
6
Perimeter is: 20
Perimeter is: 20\n
Passed after ignoring Presentation Error



W02 Programming Assignments 3

Due on 2025-08-07, 23:59 IST

Finding the Maximum Element in an Array


Problem Statement

What is the Maximum Element?
In an array of numbers, the maximum is the largest number among all elements.

In this assignment:

  • You will read n numbers from the user

  • Store them in an array

  • Find the largest number among them

  • Print the maximum number

This task helps you apply loops and arrays together to solve a real logic-based problem.

Your last recorded submission was on 2025-08-01, 17:00 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W02_P3 {
4
    public static void main(String[] args) {
5
        Scanner sc = new Scanner(System.in);
6
7
        int n = sc.nextInt();
8
        int[] arr = new int[n];
9
10
        // Read n numbers into array
11
        for (int i = 0; i < n; i++) {
12
            arr[i] = sc.nextInt();
13
        }
14
15
        int max = arr[0];  // Assume first element is maximum
16
// TODO: Use a loop to find maximum element
17
        /*
18
         Hint:
19
         Start loop from index 1 to n - 1
20
         Compare each element with max
21
         If element is greater, update max
22
         */
23
for (int i = 1; i < n; i++) {
24
  if (arr[i] > max) {
25
    max = arr[i];
26
  }
27
}
0
System.out.println("Maximum is: " + max);
1
2
        sc.close();
3
    }
4
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5  
15 42 9 28 37
Maximum is: 42
Maximum is: 42\n
Passed after ignoring Presentation Error



W02 Programming Assignments 4

Due on 2025-08-07, 23:59 IST

Create a Class and Access Its Member Variable


Problem Statement

In this task, you will practice creating and using a class in Java.

You need to:

  1. Create a class called Rectangle

  2. Declare two integer member variables length and width

  3. In the main method, create an object of the Rectangle class, assign values to length and width, and print their sum

This problem helps you understand how to define a class, create objects, and access class members in Java.

Your last recorded submission was on 2025-08-01, 17:01 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W02_P4 {
4
5
    // Declare a class named Rectangle
6
    static class Rectangle {
7
        int length;
8
        int width;
9
    }
10
11
    public static void main(String[] args) {
12
        Scanner sc = new Scanner(System.in);
13
14
        // Read length and width
15
        int l = sc.nextInt();
16
        int w = sc.nextInt();
17
18
        // Create an object of the Rectangle class
19
        Rectangle rect = new Rectangle();
20
21
        // Assign values to the object's member variables
22
        rect.length = l;
23
        rect.width = w;
24
// Complete the code to print the sum of length and width
25
        // TODO: Print the sum using rect.length and rect.width
26
        /*
27
         Hint:
28
         Use: rect.length + rect.width to get the sum
29
         Print the result using System.out.println
30
         tip--System.out.println("Sum of length and width is: " +.....)
31
         */
32
System.out.println("Sum of length and width is: " + (rect.length + rect.width));
0
sc.close();
1
    }
2
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5  
10
Sum of length and width is: 15
Sum of length and width is: 15\n
Passed after ignoring Presentation Error



W02 Programming Assignments 5

Due on 2025-08-07, 23:59 IST

Working with Multiple Classes, Constructors, and the this Keyword


Problem Statement

In this task, you will learn how to:

  • Declare multiple classes in the same Java program

  • Use constructors to initialize values

  • Apply the this keyword to refer to instance variables

What you need to do:

  1. Declare a class called Circle with one member variable radius

  2. Write a constructor for Circle that takes radius as a parameter and assigns it using the this keyword

  3. In the main method, create an object of Circle and print its radius

This task helps understand how classes work together and how constructors and the this keyword are used for clarity.


Your last recorded submission was on 2025-08-01, 17:02 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W02_P5 {
4
5
    // Declare a separate class named Circle
6
    static class Circle {
7
8
        int radius;
9
// TODO: Write a constructor that takes radius as parameter
10
        // Use the 'this' keyword to assign the value to the member variable
11
        /*
12
         Hint:
13
         The constructor name should be Circle
14
         Use: this.radius = radius;
15
         */
16
public Circle(int radius) {
17
  this.radius = radius;
18
}
0
}
1
2
    public static void main(String[] args) {
3
        Scanner sc = new Scanner(System.in);
4
5
        // Read radius value from user
6
        int r = sc.nextInt();
7
8
        // Create an object of Circle class using constructor
9
        Circle c = new Circle(r);
10
11
        // Print the radius using object member
12
        System.out.println("Radius of the circle is: " + c.radius);
13
14
        sc.close();
15
    }
16
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
7
Radius of the circle is: 7
Radius of the circle is: 7\n
Passed after ignoring Presentation Error





W06 Programming Assignments 1

Due on 2025-09-04, 23:59 IST

Safe Division with Run-time Error Handling


Problem Statement

In Java, some operations can cause run-time errors, for example dividing a number by zero.
We can use a try-catch block to handle such errors and avoid program crashes.

Task:

  • Read two integers from the user

  • Divide the first number by the second inside a try-catch block

  • If the second number is zero, print "Cannot divide by zero"

  • Otherwise, print the result

This task introduces basic run-time error handling in a safe and controlled way.

Your last recorded submission was on 2025-08-30, 17:15 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W06_P1 {
4
    public static void main(String[] args) {
5
        Scanner sc = new Scanner(System.in);
6
7
        // Read two integers
8
        int num1 = sc.nextInt();
9
        int num2 = sc.nextInt();
10
11
        // Use try-catch to handle possible run-time error
12
        try {
13
int result = num1 / num2;
14
System.out.println("Result is: " + result);
0
} catch (ArithmeticException e) {
1
            // Print safe message if division by zero occurs
2
            System.out.println("Cannot divide by zero");
3
        }
4
5
        sc.close();
6
    }
7
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
10  
0
Cannot divide by zero
Cannot divide by zero\n
Passed after ignoring Presentation Error
Test Case 2
10  
2
Result is: 5
Result is: 5\n
Passed after ignoring Presentation Error



W06 Programming Assignments 2

Due on 2025-09-04, 23:59 IST

Programming Assignment: Nested try-catch Block


Problem Statement

In Java, nested try-catch blocks allow handling multiple levels of errors separately.
You can place one try-catch block inside another to handle different types of errors in different places.

Programming Assignment:

  • Read two integers from the user

  • Inside an outer try-catch block, perform the following:

    • Inside a nested try block, divide the first number by the second

    • If division by zero occurs, handle it with the inner catch block

  • In the outer catch block, handle any other unexpected errors

  • Print appropriate messages for each scenario

This programming assignment introduces nested try-catch structure.

Your last recorded submission was on 2025-08-30, 17:17 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W06_P2 {
4
    public static void main(String[] args) {
5
        Scanner sc = new Scanner(System.in);
6
7
        // Read two integers
8
        int num1 = sc.nextInt();
9
        int num2 = sc.nextInt();
10
11
        // Outer try-catch block
12
        try {
13
14
            // Inner try-catch block for division operation
15
            try {
16
int result = num1 / num2;
17
System.out.println("Division successful");
18
System.out.println("Result is: " + result);
0
} catch (ArithmeticException e) {
1
                System.out.println("Cannot divide by zero");
2
            }
3
4
        } catch (Exception e) {
5
            // Handles other unexpected errors
6
            System.out.println("An unexpected error occurred");
7
        }
8
9
        sc.close();
10
    }
11
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
10  
2
Division successful\n
Result is: 5
Division successful\n
Result is: 5\n
Passed after ignoring Presentation Error



W06 Programming Assignments 3

Due on 2025-09-04, 23:59 IST

Programming Assignment: try Block with Multiple catch Blocks


Problem Statement

In Java, a try block can be followed by multiple catch blocks to handle different types of errors separately.

This improves error handling by allowing specific actions for different exceptions.

Key Concepts:

  • The first matching catch block handles the error

  • Catch blocks are written in order from most specific to general

Programming Assignment:

  • Read two integers from the user

  • Inside a try block, divide the first number by the second

  • Handle ArithmeticException separately to detect division by zero

  • Handle any other general errors using another catch block

  • Print suitable messages based on the type of error

This demonstrates structured error handling with multiple catch blocks.

Your last recorded submission was on 2025-08-30, 17:19 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W06_P3 {
4
    public static void main(String[] args) {
5
        Scanner sc = new Scanner(System.in);
6
7
        // Read two integers
8
        int num1 = sc.nextInt();
9
        int num2 = sc.nextInt();
10
11
        // Try block with multiple catch blocks
12
        try {
13
// TODO: Perform division and print result if successful
14
            int result = num1 / num2;
15
            System.out.println("Division successful");
16
            System.out.println("Result is: " + result);
0
} catch (ArithmeticException e) {
1
            // Handles division by zero error
2
            System.out.println("Cannot divide by zero");
3
        } catch (Exception e) {
4
            // Handles other general errors
5
            System.out.println("An unexpected error occurred");
6
        }
7
8
        sc.close();
9
    }
10
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
20  
4
Division successful\n
Result is: 5
Division successful\n
Result is: 5\n
Passed after ignoring Presentation Error



W06 Programming Assignments 4

Due on 2025-09-04, 23:59 IST

Programming Assignment: Using finally in try-catch Block


Problem Statement

In Java, the finally block is a special part of error handling.

What is finally?

  • The code inside a finally block always runs, whether there is an error or not

  • It is usually used to close resources like files, database connections, or simply to show a message

Programming Assignment:

  • Read two integers from the user

  • Inside a try block, divide the first number by the second

  • If division by zero occurs, show an error message using catch block

  • Use a finally block to print "Program Ended" no matter what happens

This helps you understand how finally block always runs in a program.

Your last recorded submission was on 2025-08-30, 17:21 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W06_P4 {
4
    public static void main(String[] args) {
5
        Scanner sc = new Scanner(System.in);
6
7
        // Read two integers
8
        int num1 = sc.nextInt();
9
        int num2 = sc.nextInt();
10
11
        // try-catch-finally structure
12
        try {
13
// TODO: Perform division and print result
14
            int result = num1 / num2;
15
            System.out.println("Result is: " + result);
0
} catch (ArithmeticException e) {
1
            System.out.println("Cannot divide by zero");
2
        } finally {
3
            // Print final message, runs always
4
            System.out.println("Program Ended");
5
        }
6
7
        sc.close();
8
    }
9
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
15  
3
Result is: 5\n
Program Ended
Result is: 5\n
Program Ended\n
Passed after ignoring Presentation Error



W06 Programming Assignments 5

Due on 2025-09-04, 23:59 IST

Programming Assignment: Using throws Statement for Error Handling


Problem Statement

In Java, the throws keyword is used when a method might cause an error, but the method itself does not handle it.
Instead, it passes the responsibility to the caller of the method.

Why use throws?

  • Some methods may cause errors called "checked exceptions"

  • Instead of handling the error inside the method, we declare throws to inform the caller

Programming Assignment:

  • Create a method called calculateSquareRoot

  • The method reads a number and returns its square root

  • If the number is negative, it throws an Exception

  • In the main method, use a try-catch block to handle the error

This demonstrates how to use throws and handle errors safely in the caller method.

Your last recorded submission was on 2025-08-30, 17:23 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W06_P5 {
4
5
    // Method to calculate square root, may throw Exception
6
    public static double calculateSquareRoot(double num) throws Exception {
7
// TODO: Throw Exception if number is negative
8
        if (num < 0) {
9
            throw new Exception("Number cannot be negative");
10
        }
11
        return Math.sqrt(num);
0
}
1
2
    public static void main(String[] args) {
3
        Scanner sc = new Scanner(System.in);
4
5
        double number = sc.nextDouble();
6
7
        try {
8
            double result = calculateSquareRoot(number);
9
            System.out.println("Square root is: " + result);
10
        } catch (Exception e) {
11
            System.out.println("Cannot calculate square root of negative number");
12
        }
13
14
        sc.close();
15
    }
16
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
16
Square root is: 4.0
Square root is: 4.0\n
Passed after ignoring Presentation Error






W08 Programming Assignments 1

Due on 2025-09-18, 23:59 IST

Creating a Thread using Thread Class


Problem Statement

In Java, you can run multiple tasks at the same time using Multithreading.
The simplest way to create a thread is by extending the built-in Thread class.

What is a Thread?

  • A thread is a small unit of a program that runs independently

  • Multiple threads can run in parallel, improving efficiency

Programming Assignment:

  • Create a class called MyThread that extends Thread

  • In its run() method, print "Thread is running"

  • In the main method, create an object of MyThread and start the thread

This helps you understand the basic way to create and start a thread in Java.

Your last recorded submission was on 2025-09-15, 21:28 IST
Select the Language for this assignment. 
File name for this program : 
1
public class W08_P1 {
2
3
    // Create a class that extends Thread
4
    static class MyThread extends Thread {
5
6
        @Override
7
        public void run() {
8
System.out.print("Thread is running");
0
}
1
    }
2
3
    public static void main(String[] args) {
4
5
        // Create object of MyThread
6
        MyThread t = new MyThread();
7
8
        // Start the thread
9
        t.start();
10
    }
11
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Thread is running
Thread is running
Passed



W08 Programming Assignments 2

Due on 2025-09-18, 23:59 IST

Creating a Thread using Runnable Interface


Problem Statement

In Java, another common way to create threads is by implementing the Runnable interface.

What is Runnable?

  • Runnable is an interface with a single method called run()

  • You can pass a Runnable object to a Thread and start the thread

Why use Runnable?

  • It allows your class to extend another class, as Java supports single inheritance

  • It provides flexibility in thread creation

Programming Assignment:

  • Create a class called MyRunnable that implements Runnable

  • In its run() method, print "Runnable thread is running"

  • In the main method, create a Thread object using MyRunnable and start the thread

This demonstrates thread creation using the Runnable interface.

Your last recorded submission was on 2025-09-15, 21:29 IST
Select the Language for this assignment. 
File name for this program : 
1
public class W08_P2 {
2
3
    // Create a class that implements Runnable interface
4
    static class MyRunnable implements Runnable {
5
6
        @Override
7
        public void run() {
8
System.out.print("Runnable thread is running");
0
}
1
    }
2
3
    public static void main(String[] args) {
4
5
        // Create object of MyRunnable
6
        MyRunnable r = new MyRunnable();
7
8
        // Create Thread using Runnable object
9
        Thread t = new Thread(r);
10
11
        // Start the thread
12
        t.start();
13
    }
14
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Runnable thread is running
Runnable thread is running
Passed



W08 Programming Assignments 3

Due on 2025-09-18, 23:59 IST

Programming Assignment: Understanding Basic Thread States in Java


Problem Statement

When a thread runs in Java, it moves through different stages called states.

What are Thread States?
Think of a thread like a person:

  • It starts in one state

  • Moves to another as work happens

  • Finally, it finishes

For beginners, focus on these three simple states:

  1. New – The thread is created but not started yet

  2. Running – The thread is doing its work

  3. Terminated – The thread has finished its work

Programming Assignment:

  • Create a class called MyThread that extends Thread

  • Inside its run() method, print "Thread is running"

  • In the main method:

    • Create a MyThread object

    • Print "Thread state before start"

    • Start the thread

    • Print "Thread state after start"

    • Wait for thread to finish using join()

    • Print "Thread state after completion"

This shows how thread state changes as the thread runs.

Your last recorded submission was on 2025-09-15, 21:32 IST
Select the Language for this assignment. 
File name for this program : 
1
public class W08_P3 {
2
3
    // Create a class that extends Thread
4
    static class MyThread extends Thread {
5
6
        @Override
7
        public void run() {
8
System.out.println("Thread is running");
0
}
1
    }
2
3
    public static void main(String[] args) {
4
5
        // Create thread object
6
        MyThread t = new MyThread();
7
8
        // Thread is created but not started yet
9
        System.out.println("Thread state before start");
10
11
        // Start thread
12
        t.start();
13
14
        // Thread has started running
15
        System.out.println("Thread state after start");
16
17
        try {
18
            // Wait for thread to finish
19
            t.join();
20
        } catch (InterruptedException e) {
21
            // Not needed for beginners, but required to handle possible interruptions
22
        }
23
24
        // Thread has finished
25
        System.out.println("Thread state after completion");
26
    }
27
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Thread state before start\n
Thread state after start\n
Thread is running\n
Thread state after completion
Thread state before start\n
Thread state after start\n
Thread is running\n
Thread state after completion\n
Passed after ignoring Presentation Error



W08 Programming Assignments 4

Due on 2025-09-18, 23:59 IST

Understanding Thread Priority in Java


Problem Statement

In Java, each thread has a priority, a number from 1 (lowest) to 10 (highest).
Priority suggests how important a thread is, though actual scheduling depends on the system.

Programming Assignment:

  • Create a class MyThread that extends Thread

  • In the main method:

    • Create a MyThread object

    • Set its priority to 8

    • Start the thread

    • Print the thread's priority after setting

No output should come from the thread's run() method to avoid output mismatch.

Your last recorded submission was on 2025-09-15, 21:38 IST
Select the Language for this assignment. 
File name for this program : 
1
public class W08_P4 {
2
3
    // Thread class
4
    static class MyThread extends Thread {
5
6
        @Override
7
        public void run() {
8
            // No output here to keep portal testing consistent
9
        }
10
    }
11
12
    public static void main(String[] args) {
13
14
        MyThread t = new MyThread();
15
16
        // Set thread priority
17
        t.setPriority(8);
18
19
        // Start thread
20
        t.start();
21
System.out.print("Thread priority is: " + t.getPriority());
0
}
1
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Thread priority is: 8
Thread priority is: 8
Passed



W08 Programming Assignments 5

Due on 2025-09-18, 23:59 IST

Programming Assignment: Introduction to Thread Synchronization


Problem Statement

What is a Thread?
Imagine your computer doing many tasks at once — for example:

  • Playing music

  • Downloading files

  • Browsing the internet

In Java, each small task that runs independently is called a Thread.
Threads help programs run faster by working at the same time.

Why Synchronization?
When multiple threads work on the same thing together, they may interfere with each other.
For example:

  • Two threads try to update the same number at the same time

  • The final result may be wrong

What is Synchronization?

  • It is like putting a lock

  • Only one thread can work on the shared thing at a time

  • This prevents problems caused by threads disturbing each other


Programming Assignment:

  • Create a class Counter with a number count starting from 0

  • Write a method increment() to increase the number by 1, using synchronized keyword

  • Create a thread class called MyThread that runs the increment() method 1000 times

  • In main, run two threads to increase the number

  • After both threads finish, print the final count

This shows how to use synchronization to avoid problems when multiple threads share data.

Your last recorded submission was on 2025-09-15, 21:40 IST
Select the Language for this assignment. 
File name for this program : 
1
public class W08_P5 {
2
3
    // Shared class with a number
4
    static class Counter {
5
        int count = 0;
6
7
        // Synchronized method to safely increase number
8
        public synchronized void increment() {
9
            count++;
10
        }
11
    }
12
13
    // Thread class to run increment
14
    static class MyThread extends Thread {
15
        Counter c;
16
17
        MyThread(Counter c) {
18
            this.c = c;
19
        }
20
21
        @Override
22
        public void run() {
23
for (int i = 0; i < 1000; i++) {
24
                c.increment();
25
            }
0
}
1
    }
2
3
    public static void main(String[] args) {
4
5
        Counter c = new Counter();
6
7
        // Create two threads
8
        MyThread t1 = new MyThread(c);
9
        MyThread t2 = new MyThread(c);
10
11
        // Start both threads
12
        t1.start();
13
        t2.start();
14
15
        try {
16
            // Wait for both threads to finish
17
            t1.join();
18
            t2.join();
19
        } catch (InterruptedException e) {
20
        }
21
22
        // Print final count
23
        System.out.println("Final count is: " + c.count);
24
    }
25
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Final count is: 2000
Final count is: 2000\n
Passed after ignoring Presentation Error





Week 09 : Programming Assignment 1

Due on 2025-09-25, 23:59 IST

Write suitable code to develop a 2D Flip-Flop Array with dimension 5 × 5, which replaces all input elements with values 0 by 1 and 1 by 0. An example is shown below:

INPUT:
               00001
               00001
               00001
               00001
               00001

OUTPUT:
               11110
               11110
               11110
               11110
               11110

Note the following points carefully
:
1. Here, the input must contain only 0 and 1.

2. The input and output array size must be of dimension 5 × 5.
3. Flip-Flop: If 0 then 1 and vice-versa.

Your last recorded submission was on 2025-09-20, 13:54 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class W09_P1{
3
    public static void main(String args[]){
4
        Scanner sc = new Scanner(System.in);
5
        int[][] matrix = new int[5][5];
6
        for (int i = 0; i < 5; i++) {
7
            String row = sc.next();
8
            for (int j = 0; j < 5; j++) {
9
                matrix[i][j] = Character.getNumericValue(row.charAt(j));
10
            }
11
        }
12
        for (int i = 0; i < 5; i++) {
13
            for (int j = 0; j < 5; j++) {
14
                if (matrix[i][j] == 0) {
15
                    matrix[i][j] = 1;
16
                }
17
                else {
18
                    matrix[i][j] = 0;
19
                }
20
            }
21
        }
22
        for (int i = 0; i < 5; i++) {
23
            for (int j = 0; j < 5; j++) {
24
                System.out.print(matrix[i][j]);
25
            }
26
            System.out.println();
27
        }
0
} // The main() ends here
1
} // The main class ends here
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
00001
00001
00001
00001
00001
11110\n
11110\n
11110\n
11110\n
11110
11110\n
11110\n
11110\n
11110\n
11110\n
Passed after ignoring Presentation Error



Week 09 : Programming Assignment 2

Due on 2025-09-25, 23:59 IST

Complete the code to develop a BASIC CALCULATOR that can perform operations like AdditionSubtractionMultiplication and Division.

Note the following points carefully
:
1. Use only 
double datatype to store calculated numeric values.
2. Assume input to be of 
integer datatype.
3. The output should be rounded using 
Math.round() method.
4. Take care of the spaces during formatting output (e.g., single space each before and after =).
5. The calculator should be able to perform required operations on a minimum of two operands as shown in the below example:


Input:
                       5+6 

Output:
                       5+6 = 11


Your last recorded submission was on 2025-09-20, 13:57 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class W09_P2{
3
    public static void main(String args[]){
4
        Scanner sc = new Scanner(System.in);
5
        String input = sc.nextLine(); // Read as string, e.g., 5+6
6
String[] parts;
7
        char operator = ' ';
8
        double result = 0.0;
9
        if (input.contains("+")) {
10
            parts = input.split("\\+");
11
            operator = '+';
12
        } else if (input.contains("-")) {
13
            parts = input.split("-");
14
            operator = '-';
15
        } else if (input.contains("*")) {
16
            parts = input.split("\\*");
17
            operator = '*';
18
        } else if (input.contains("/")) {
19
            parts = input.split("/");
20
            operator = '/';
21
        } else {
22
            return;
23
        }
24
        int operand1 = Integer.parseInt(parts[0].trim());
25
        int operand2 = Integer.parseInt(parts[1].trim());
26
        switch (operator) {
27
            case '+':
28
                result = (double) operand1 + operand2;
29
                break;
30
            case '-':
31
                result = (double) operand1 - operand2;
32
                break;
33
            case '*':
34
                result = (double) operand1 * operand2;
35
                break;
36
            case '/':
37
                if (operand2 != 0) {
38
                    result = (double) operand1 / operand2;
39
                }
40
                break;
41
        }
42
        long roundedResult = Math.round(result);
43
        System.out.println(input + " = " + roundedResult);
0
} // The main() method ends here
1
} // The main class ends here
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5+6
5+6 = 11
5+6 = 11\n
Passed after ignoring Presentation Error



Week 09 : Programming Assignment 3

Due on 2025-09-25, 23:59 IST

Write a Java program that utilizes multithreading to calculate and print the squares of numbers from a specified begin to a specified end.

The main method is already created.

You need to design a SquareThread class that has two members,

§ int begin;

§ int end;

Each thread should sequentially print the squares of numbers from begin to end (both inclusive).

The same code will be used to create another thread that prints the sqaure of numbers from end to begin in reverse order.

(if begin is greater than end, print the square of each number in reverse order first)

The main method will first call SquareThread with begin and end and then in reverse order.

The class you create should be able to handle such case and print as required in the correct order.

HINT: use the keyword `synchronized` in the run method.


Your last recorded submission was on 2025-09-20, 14:01 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
class SquareThread extends Thread {
4
    private int begin;
5
    private int end;
6
    private static final Object lock = new Object();
7
    public SquareThread(int begin, int end) {
8
        this.begin = begin;
9
        this.end = end;
10
    }
11
    public void run() {
12
        synchronized (SquareThread.lock) {
13
            if (begin > end) {
14
                for (int i = begin; i >= end; i--) {
15
                    System.out.println(i * i);
16
                }
17
            }
18
            else {
19
                for (int i = begin; i <= end; i++) {
20
                    System.out.println(i * i);
21
                }
22
            }
23
        }
24
    }
0
}
1
2
public class W09_P3 {
3
    public static void main(String args[]) {
4
        Scanner scanner = new Scanner(System.in);
5
        //System.out.print("Enter the begin for square calculation: ");
6
        int begin = scanner.nextInt();
7
        //System.out.print("Enter the end for square calculation: ");
8
        int end = scanner.nextInt();
9
        scanner.close();
10
11
        SquareThread thread1 = new SquareThread(begin, end);
12
        SquareThread thread2 = new SquareThread(end, begin);
13
14
        thread1.start();
15
        thread2.start();
16
    }
17
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1
5
1\n
4\n
9\n
16\n
25\n
25\n
16\n
9\n
4\n
1
1\n
4\n
9\n
16\n
25\n
25\n
16\n
9\n
4\n
1\n
Passed after ignoring Presentation Error
Test Case 2
9
6
81\n
64\n
49\n
36\n
36\n
49\n
64\n
81
81\n
64\n
49\n
36\n
36\n
49\n
64\n
81\n
Passed after ignoring Presentation Error



Week 09 : Programming Assignment 4

Due on 2025-09-25, 23:59 IST

Complete the code segment to catch the exception in the following, if any.

On the occurrence of such an exception, your program should print

“Please enter valid data” .

If there is no such exception, it will print the square of the number entered.


Your last recorded submission was on 2025-09-20, 14:12 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.io.*;  
2
class W09_P4{  
3
        public static void main(String args[]){
4
        try{
5
                InputStreamReader r=new InputStreamReader(System.in);  
6
                BufferedReader br=new BufferedReader(r);  
7
                String number=br.readLine();  
8
                int x = Integer.parseInt(number);
9
                System.out.println(x*x);
10
            } catch (Exception e) {
11
                System.out.println("Please enter valid data");
12
            }
0
} // The main() ends here
1
} // The main class ends here
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
4
4\n
Passed after ignoring Presentation Error
Test Case 2
p
Please enter valid data
Please enter valid data\n
Passed after ignoring Presentation Error



Week 09 : Programming Assignment 5

Due on 2025-09-25, 23:59 IST

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 distance(Point p2){} // Function to return the distance of this Point from another Point


Your last recorded submission was on 2025-09-20, 14:04 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W09_P5{
4
            
5
    public static void main(String[] args) {
6
7
        Scanner sc = new Scanner(System.in);
8
        double x1 = sc.nextDouble();
9
        double y1 = sc.nextDouble();
10
        double x2 = sc.nextDouble();
11
        double y2 = sc.nextDouble();
12
        Point p1 = new Point(x1, y1);
13
        Point p2 = new Point(x2, y2);
14
        
15
        System.out.print(p1.distance(p2));
16
    }
17
18
}
19
class Point{
20
    private double x;
21
    private double y;
22
    public Point(double x, double y){
23
        this.x = x;
24
        this.y = y;
25
    }
26
    public double distance(Point p2){
27
        double dist;
28
        dist = Math.sqrt(Math.pow((p2.x-x),2) + Math.pow((p2.y-y),2));
29
        return dist;
30
    }
31
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
0 0
1 1
1.4142135623730951
1.4142135623730951
Passed
Test Case 2
0 0
0 5
5.0
5.0
Passed





W10 Programming Assignments 1

Due on 2025-10-02, 23:59 IST

Introduction to JDBC and Required Imports


Problem Statement

What is JDBC?
JDBC means Java Database Connectivity, which allows Java programs to work with databases.

Before writing database programs, you must import the correct packages. Without proper imports, the code will not compile.

Programming Assignment:

  • Import the necessary JDBC packages so the program can compile

  • You only need to complete the import section

  • The output "import successful" is printed automatically

This helps you practice the correct way to prepare Java programs for database work.

Your last recorded submission was on 2025-09-25, 13:44 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.sql.Connection;
2
import java.sql.DriverManager;
0
public class W10_P1 {
1
    public static void main(String[] args) {
2
3
        // The below code requires correct imports to compile
4
        Connection con = null;
5
        DriverManager dm = null;
6
7
        // Output statement, for portal testing only
8
        System.out.println("import successful");
9
    }
10
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
import successful
import successful\n
Passed after ignoring Presentation Error



W10 Programming Assignments 2

Due on 2025-10-02, 23:59 IST

 Writing a JDBC Connection String


Problem Statement

What is a JDBC Connection String?
To connect a Java program to a database, we use a special sentence called a connection string.
It tells Java:

  • Which database to use

  • Where the database is located

In this assignment, you will practice writing the correct JDBC connection string for SQLite database.

Programming Assignment:

  • Write the correct connection string for an SQLite database called test.db

  • The rest of the program prints "connection string ready" if your string is correct

This helps beginners practice writing JDBC connection strings safely, without actual database access.

Your last recorded submission was on 2025-09-25, 13:54 IST
Select the Language for this assignment. 
File name for this program : 
1
public class W10_P2 {
2
    public static void main(String[] args) {
3
// TODO: Complete the connection string for SQLite database
4
5
        /*
6
         Hint:
7
         JDBC connection string for SQLite starts with "jdbc:sqlite:"
8
         The database file is "test.db"
9
         */
10
   String url = "jdbc:sqlite:test.db";
0
// Portal test output
1
        if (url.equals("jdbc:sqlite:test.db")) {
2
            System.out.println("connection string ready");
3
        } else {
4
            System.out.println("incorrect connection string");
5
        }
6
    }
7
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
connection string ready
connection string ready\n
Passed after ignoring Presentation Error



W10 Programming Assignments 3

Due on 2025-10-02, 23:59 IST

Writing a Simple SQL Insert Statement


Problem Statement

What is an SQL Insert Statement?
When working with databases, we use the INSERT command to add new records (data) into a table.

In this assignment:

  • Imagine there is a table called students with two columns: id and name

  • You will practice writing the correct SQL insert statement as a text string

  • No actual database operation is performed, only the string is checked

Programming Assignment:

  • Complete the SQL insert statement to add a student with id 1 and name 'Alice'

  • The rest of the code checks your string and prints "insert statement ready" if correct

This task helps beginners practice writing SQL commands safely.

Your last recorded submission was on 2025-09-25, 14:09 IST
Select the Language for this assignment. 
File name for this program : 
1
public class W10_P3 {
2
    public static void main(String[] args) {
3
String sql = "INSERT INTO students (id, name) VALUES (1, 'Alice');";
0
// Portal test output
1
        if (sql.equals("INSERT INTO students (id, name) VALUES (1, 'Alice');")) {
2
            System.out.println("insert statement ready");
3
        } else {
4
            System.out.println("incorrect statement");
5
        }
6
    }
7
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
insert statement ready
insert statement ready\n
Passed after ignoring Presentation Error



W10 Programming Assignments 4

Due on 2025-10-02, 23:59 IST

Writing a Simple SQL SELECT Statement 


Problem Statement

What is a SQL SELECT Statement?
The SELECT statement is used to get data from a database table.
You can choose to fetch all columns or specific columns.

In this assignment:

  • Imagine a table called students with two columns: id and name

  • You will practice writing a SQL statement to fetch all columns for all students

Programming Assignment:

  • Write a SQL SELECT statement as a text string to get all data from students table

  • The rest of the program checks your string and prints "select statement ready" if correct

This task helps beginners practice safe SQL reading commands.

Your last recorded submission was on 2025-09-25, 14:12 IST
Select the Language for this assignment. 
File name for this program : 
1
public class W10_P4 {
2
    public static void main(String[] args) {
3
String sql = "SELECT * FROM students;";
0
if (sql.equals("SELECT * FROM students;")) {
1
            System.out.println("select statement ready");
2
        } else {
3
            System.out.println("incorrect statement");
4
        }
5
    }
6
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
select statement ready
select statement ready\n
Passed after ignoring Presentation Error



W10 Programming Assignments 5

Due on 2025-10-02, 23:59 IST

Writing a Simple SQL UPDATE Statement 

Problem Statement

What is an SQL UPDATE Statement?
The UPDATE command is used to change existing data in a database table.

In this assignment:

  • Imagine a table called students with two columns: id and name

  • You will write a SQL statement to update the name of the student with id 1 to 'Bob'

Programming Assignment:

  • Write the correct SQL UPDATE statement as a text string

  • The rest of the code checks your string and prints "update statement ready" if correct

This helps beginners practice safe SQL update syntax.



Your last recorded submission was on 2025-09-25, 14:21 IST
Select the Language for this assignment. 
File name for this program : 
1
public class W10_P5 {
2
    public static void main(String[] args) {
3
String sql = "UPDATE students SET name = 'Bob' WHERE id = 1;";
0
// Portal test output
1
        if (sql.equals("UPDATE students SET name = 'Bob' WHERE id = 1;")) {
2
            System.out.println("update statement ready");
3
        } else {
4
            System.out.println("incorrect statement");
5
        }
6
    }
7
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
update statement ready
update statement ready\n
Passed after ignoring Presentation Error





Week 11 : Programming Assignment 1

Due on 2025-10-09, 23:59 IST

The following code needs some package to work properly.

Write appropriate code to

§ import the required package(s) in order to make the program compile and execute successfully.
(Ignore the hidden code)

Your last recorded submission was on 2025-10-01, 20:02 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
import java.sql.*; // Import JDBC packages
3
import static java.sql.DriverManager.*; // Static import for DriverManager methods
0
public class W11_P1 {
1
    public static void main(String args[]) {
2
        try {
3
            Connection conn = null;
4
            Statement stmt = null;
5
            String DB_URL = "jdbc:sqlite:/tempfs/db";
6
            System.setProperty("org.sqlite.tmpdir", "/tempfs");
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
0
} catch (Exception e) {
1
            System.out.println(e);
2
        }
3
    }
4
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
0
true
true
Passed



Week 11 : Programming Assignment 2

Due on 2025-10-09, 23:59 IST

Write the JDBC codes needed to create a Connection interface using the DriverManager class and the variable DB_URL.  Check whether the connection is successful using 'isValid(timeout)' method to generate the output, which is either 'true' or 'false'.

 

Note the following points carefully:

§ Name the connection object as conn only.

§ Use timeout value as 1.

(Ignore hidden code)

Your last recorded submission was on 2025-10-01, 20:04 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.sql.*;
2
import java.util.Scanner;
3
public class W11_P2 {
4
    public static void main(String args[]) {
5
        try {
6
              Connection conn = null;
7
              Statement stmt = null;
8
              String DB_URL = "jdbc:sqlite:/tempfs/db";
9
              System.setProperty("org.sqlite.tmpdir", "/tempfs");
10
// Open a connection and check connection status
11
              conn = DriverManager.getConnection(DB_URL);
12
              boolean status = conn.isValid(1);
13
              System.out.print(status);
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
0
conn.close();
1
        } catch (Exception e) {
2
            System.out.println(e);
3
        }
4
    }
5
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
0
true
true
Passed



Week 11 : Programming Assignment 3

Due on 2025-10-09, 23:59 IST

Due to some mistakes in the below code, the code is not compiled/executable.

Modify and debug the JDBC code to make it execute successfully.

Your last recorded submission was on 2025-10-01, 20:05 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.sql.*;
2
import java.util.Scanner;
3
4
public class W11_P3 {
5
    public static void main(String args[]) {
6
        try {
7
              Connection conn = null;
8
              Statement stmt = null;
9
              String DB_URL = "jdbc:sqlite:/tempfs/db";
10
              System.setProperty("org.sqlite.tmpdir", "/tempfs");
11
              conn = DriverManager.getConnection(DB_URL);
12
              conn.close();
13
              System.out.print(conn.isClosed());
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
0
} catch (Exception e) {
1
            System.out.println(e);
2
        }
3
    }
4
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
0
true
true
Passed



Week 11 : Programming Assignment 4

Due on 2025-10-09, 23:59 IST

Complete the code segment to create a new table named ‘STUDENTS’ in SQL database using the following information.

Column

UID

Name

Roll

Age

Type

Integer

Varchar (45)

Varchar (12)

Integer

Your last recorded submission was on 2025-10-01, 20:06 IST
Select the Language for this assignment. 
File name for this program : 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
// The statement containing SQL command to create table "STUDENTS"
3
String sql = "CREATE TABLE STUDENTS " +
4
             "(UID INT, " +
5
             "Name VARCHAR(45), " +
6
             "Roll VARCHAR(12), " +
7
             "Age INT)";
8
9
// Execute the statement containing SQL command below this comment
10
stmt.executeUpdate(sql);
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
0
}
1
       catch(Exception e){ System.out.println(e);}  
2
    }
3
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1
No. of columns : 4\n
Column 1 Name: UID\n
Column 1 Type : INT\n
Column 2 Name: Name\n
Column 2 Type : VARCHAR\n
Column 3 Name: Roll\n
Column 3 Type : VARCHAR\n
Column 4 Name: Age\n
Column 5 Type : INT
No. of columns : 4\n
Column 1 Name: UID\n
Column 1 Type : INT\n
Column 2 Name: Name\n
Column 2 Type : VARCHAR\n
Column 3 Name: Roll\n
Column 3 Type : VARCHAR\n
Column 4 Name: Age\n
Column 5 Type : INT
Passed



Week 11 : Programming Assignment 5

Due on 2025-10-09, 23:59 IST

Complete the code segment to rename an already created table named ‘STUDENTS’  into ‘GRADUATES’.

Your last recorded submission was on 2025-10-01, 20:07 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.sql.*;
2
import java.lang.*;
3
public class W11_P5 {
4
    public static void main(String args[]) {
5
        try {
6
              Connection conn = null;
7
              Statement stmt = null;
8
              String DB_URL = "jdbc:sqlite:/tempfs/db";
9
              System.setProperty("org.sqlite.tmpdir", "/tempfs");
10
              // Open a connection
11
              conn = DriverManager.getConnection(DB_URL);
12
              stmt = conn.createStatement();
13
~~~THERE IS SOME INVISIBLE CODE HERE~~~
14
// Write the SQL command to rename a table
15
String sql = "ALTER TABLE STUDENTS RENAME TO GRADUATES";
16
17
// Execute the SQL command
18
stmt.executeUpdate(sql);
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
0
}   catch(Exception e){ System.out.println(e);}  
1
    }
2
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1
TABLE NAME = GRADUATES
TABLE NAME = GRADUATES
Passed





W12 Programming Assignments 1

Due on 2025-10-16, 23:59 IST

 Parameterized Constructors in Java


Problem Statement

What is a Constructor?
A constructor is a special method in Java used to create an object.
It looks like a method, but:

  • Its name is the same as the class name

  • It does not have a return type

What is a Parameterized Constructor?
Sometimes, when creating an object, we want to give it some initial values.
We can do this by passing values (parameters) to the constructor.

Example:
If we have a Student class with a name and roll number, we can set these values using a constructor.


Programming Assignment

In this task:

  • Create a class called Student

  • Add two variables: name (String) and roll (int)

  • Write a parameterized constructor to assign values to these variables

  • In the main method, create a Student object by passing name and roll number

  • Print the details

This helps you understand how to use constructors to create objects with values, a common topic in interviews.

Your last recorded submission was on 2025-10-06, 21:44 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W12_P1 {
4
5
    // Declare Student class
6
    static class Student {
7
        String name;
8
        int roll;
9
public Student(String name, int roll) {
10
            this.name = name;
11
            this.roll = roll;
12
        }
0
}
1
2
    public static void main(String[] args) {
3
        Scanner sc = new Scanner(System.in);
4
5
        String name = sc.nextLine();
6
        int roll = sc.nextInt();
7
8
        // Create object of Student class with given values
9
        Student s = new Student(name, roll);
10
11
        // Print student details
12
        System.out.println("Student Name: " + s.name);
13
        System.out.println("Roll Number: " + s.roll);
14
15
        sc.close();
16
    }
17
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
Deepak
101
Student Name: Deepak\n
Roll Number: 101
Student Name: Deepak\n
Roll Number: 101\n
Passed after ignoring Presentation Error



W12 Programming Assignments 2

Due on 2025-10-16, 23:59 IST

Understanding Method Overloading in Java


Problem Statement

What is Method Overloading?
In Java, you can create multiple methods with the same name but different parameters. This is called Method Overloading.

Why is it useful?

  • Makes code cleaner

  • Allows methods to perform similar tasks with different types of input

This is a very common Java interview topic to test your understanding of functions and code flexibility.


Programming Assignment

In this task:

  • Create a class called Calculator

  • Overload a method called add in two ways:

    1. A method that adds two integers

    2. A method that adds three integers

  • In the main method, call both versions of add and print the results

This helps you practice method overloading, which is essential for interviews and real-world coding.

Your last recorded submission was on 2025-10-06, 21:45 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W12_P2 {
4
5
    // Calculator class with overloaded add methods
6
    static class Calculator {
7
8
        public int add(int a, int b) {
9
            return a + b;
10
        }
11
public int add(int a, int b, int c) {
12
            return a + b + c;
13
        }
0
}
1
2
    public static void main(String[] args) {
3
        Scanner sc = new Scanner(System.in);
4
5
        int a = sc.nextInt();
6
        int b = sc.nextInt();
7
8
        int x = sc.nextInt();
9
        int y = sc.nextInt();
10
        int z = sc.nextInt();
11
12
        Calculator c = new Calculator();
13
14
        int sumTwo = c.add(a, b);
15
        System.out.println("Sum of two numbers: " + sumTwo);
16
17
        int sumThree = c.add(x, y, z);
18
        System.out.println("Sum of three numbers: " + sumThree);
19
20
        sc.close();
21
    }
22
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5  
10  
2  
3  
4
Sum of two numbers: 15\n
Sum of three numbers: 9
Sum of two numbers: 15\n
Sum of three numbers: 9\n
Passed after ignoring Presentation Error



W12 Programming Assignments 3

Due on 2025-10-16, 23:59 IST

If-Else with Nested Conditions in Java


Problem Statement

What is If-Else?
In Java, if-else statements let your program make decisions.

  • if checks a condition

  • If true, some code runs

  • If false, another block of code can run

What is a Nested If?

  • You can place one if statement inside another

  • This allows checking more than one condition

Why is this important?

  • Decision-making is one of the most common coding tasks

  • Almost every programming interview includes if-else logic


Programming Assignment

In this task:

  • Read an integer from the user

  • If the number is greater than 0: print "Positive Number"

  • If the number is less than 0: print "Negative Number"

  • If the number is exactly 0: print "Zero"

This helps you practice nested if-else, a very important concept

Your last recorded submission was on 2025-10-06, 21:47 IST
Select the Language for this assignment. 
File name for this program : 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
        if (num > 0) {
3
            System.out.println("Positive Number");
4
        } else {
5
            if (num < 0) {
6
                System.out.println("Negative Number");
7
            } else {
8
                System.out.println("Zero");
9
            }
10
        }
0
sc.close();
1
    }
2
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
7
Positive Number
Positive Number\n
Passed after ignoring Presentation Error
Test Case 2
0
Zero
Zero\n
Passed after ignoring Presentation Error



W12 Programming Assignments 4

Due on 2025-10-16, 23:59 IST

Using a Loop to Calculate Sum of Natural Numbers


Problem Statement

What is a Loop?
In Java, loops allow a block of code to run multiple times automatically.
for loop runs when you know how many times to repeat the task.

In this assignment:

  • You will calculate the sum of first n natural numbers

  • Natural numbers are: 1, 2, 3, 4, ... up to n

Programming Assignment:

  • Read an integer n from the user

  • Use a loop to add numbers from 1 to n

  • Print the final sum

This helps practice how loops work and how to perform repeated addition.

Your last recorded submission was on 2025-10-06, 21:48 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W12_P4 {
4
    public static void main(String[] args) {
5
        Scanner sc = new Scanner(System.in);
6
7
        int n = sc.nextInt();
8
        int sum = 0;
9
        for (int i = 1; i <= n; i++) {
10
            sum += i;
11
        }
0
System.out.println("Sum is: " + sum);
1
2
        sc.close();
3
    }
4
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
Sum is: 15
Sum is: 15\n
Passed after ignoring Presentation Error



W12 Programming Assignments 5

Due on 2025-10-16, 23:59 IST

Array, Loop, and Condition Task


Problem Statement

In this task, you will apply multiple basic programming concepts together:

✅ You will read n numbers and store them in an array
✅ You will calculate the sum of all positive numbers
✅ You will count how many negative numbers are present
✅ Finally, you will print both results

This combines:

  • Arrays (storing multiple values)

  • Loops (processing values)

  • If-else conditions (deciding based on positive or negative)

This type of task helps in applying core concepts together in a structured program.


Your last recorded submission was on 2025-10-06, 21:49 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W12_P5 {
4
    public static void main(String[] args) {
5
        Scanner sc = new Scanner(System.in);
6
7
        int n = sc.nextInt();
8
        int[] arr = new int[n];
9
10
        // Read n numbers into array
11
        for (int i = 0; i < n; i++) {
12
            arr[i] = sc.nextInt();
13
        }
14
15
        int sum = 0;
16
        int negativeCount = 0;
17
        for (int number : arr) {
18
            if (number > 0) {
19
                sum += number;
20
            } else if (number < 0) {
21
                negativeCount++;
22
            }
23
        }
0
System.out.println("Sum of positive numbers: " + sum);
1
        System.out.println("Count of negative numbers: " + negativeCount);
2
3
        sc.close();
4
    }
5
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
3 -2 7 -8 10
Sum of positive numbers: 20\n
Count of negative numbers: 2
Sum of positive numbers: 20\n
Count of negative numbers: 2\n
Passed after ignoring Presentation Error

































No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.