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-2023 Swayam

NPTEL Programming In Java Programming Assignment July-2023 Swayam



 Programming In Java           July 2023 NPTEL course


  Please scroll down for latest Programs. ðŸ‘‡ 


Support me : Subscribe to the YouTube Channel :  Swayam Solver



Week 1: Programming Assignment 1

Due on 2023-08-10, 23:59 IST

Complete the code segment to find the perimeter and area of a circle given a value of radius.

You should use Math.PI constant in your program. If radius is zero or less than zero then print " please enter non zero positive number ".

Your last recorded submission was on 2023-07-23, 19:28 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;  
2
public class Exercise1_1 {
3
       public static void main(String[] args) {
4
Scanner s = new Scanner(System.in); 
5
       double radius= s.nextDouble();
6
       double perimeter;
7
       double area;
8
//Calculate the perimeter
9
perimeter = 2* Math.PI* radius;
10
  //Calculate the area
11
area = Math.PI*radius*radius;
12
13
System.out.println(perimeter);
14
System.out.print(area);
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
2.0
12.566370614359172\n
12.566370614359172
12.566370614359172\n
12.566370614359172
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 1 : Programming Assignment 2

Due on 2023-08-10, 23:59 IST

Complete the code segment to find the largest among three numbers x,y, and z. You should use if-then-else construct in Java.

Your last recorded submission was on 2023-07-27, 08:18 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;  
2
public class Exercise1_2 {
3
       public static void main(String[] args) {
4
Scanner s = new Scanner(System.in); 
5
        int x = s.nextInt(); 
6
        int y = s.nextInt();
7
int z = s.nextInt();
8
int result = 0;
9
//Use if...else ladder to find the largest among 3 numbers and store the largest number in a variable called result.
10
if(x > y)
11
  if(x > z)
12
    result = x;
13
  else
14
    result = z;
15
else if( y > z )
16
    result = y;
17
  else 
18
    result =z;
19
  
20
System.out.print(result);
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
-4 -2 -5
-2
-2
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed


Week 1 : Programming Assignment 3

Due on 2023-08-10, 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

Your last recorded submission was on 2023-07-23, 19:29 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Exercise1_3 {
3
       public static void main(String[] args) {
4
       Scanner sc = new Scanner(System.in);
5
       int n=sc.nextInt();
6
      int sum=0;
7
//Use for or while loop do the operation.
8
9
for( int i = 0; i<=n*2; i+=2)
10
  if( i%3 == 0)
11
    sum+=i;
12
13
System.out.print(sum);
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
1
0
0
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed


Week 1 : Programming Assignment 4

Due on 2023-08-10, 23:59 IST

Complete the code segment to check whether the number is an Armstrong number or not.

Armstrong Number:

A positive number is called an Armstrong number if it is equal to the sum of cubes of its digits for example 153 = 1
3+53+33, 370, 371, 407, etc.

Your last recorded submission was on 2023-07-23, 19:30 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Exercise1_4 {
3
    public static void main(String[] args) {
4
       Scanner sc = new Scanner(System.in);
5
       int n=sc.nextInt();
6
           int result=0;
7
int temp=n;
8
int c=0,t; 
9
//Use while loop to check the number is Armstrong or not.
10
    while(n>0)
11
    {
12
        t=n%10;
13
        n=n/10;
14
        c=c+(t*t*t);
15
    }       
16
    if(temp==c)
17
        result=1;
18
    else
19
        result=0;
20
    //Evaluation code 
21
    System.out.print(result);
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
203
0
0
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed




Week 1 : Programming Assignment 5

Due on 2023-08-10, 23:59 IST

Complete the code segment to help Ragav , find the highest mark and average mark secured by him in "s" number of subjects.

Your last recorded submission was on 2023-07-23, 19:31 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Exercise1_5{
3
    public static void main(String[] args) {
4
     Scanner input = new Scanner(System.in);
5
         double mark_avg;
6
         int result;
7
         int i;
8
         int s;
9
      //define size of array
10
       s = input.nextInt();
11
     //The array is defined "arr" and inserted marks into it.
12
      int[] arr = new int[s];   
13
      for(i=0;i<arr.length;i++)
14
      {
15
    arr[i]=input.nextInt();
16
        }
17
//Initialize maximum element as first element of the array.  
18
   //Traverse array elements to get the current max.
19
   //Store the highest mark in the variable result.
20
   //Store average mark in avgMarks.
21
result=arr[0];
22
mark_avg=0;
23
24
for(i=0;i<arr.length;i++)
25
      {
26
  mark_avg+=arr[i];
27
  if( arr[i]> result)
28
  result = arr[i];
29
}
30
mark_avg/=s;
31
System.out.println(result);
32
System.out.print(mark_avg);
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
5
20 50 60 40 70
70\n
48.0
70\n
48.0
Passed






Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed



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


Week 2 : Programming Assignment 1

Due on 2023-08-10, 23:59 IST
Complete the code segment to call the method  print() of class Student first and then call print() method of class School.

NOTE: Don't provide any INPUT in Sample Test Cases

Your last recorded submission was on 2023-07-31, 22:42 IST
Select the Language for this assignment. 
File name for this program : 
1
// This is the class named School
2
class School { 
3
    // This is a method in class School
4
    public void print() { 
5
        System.out.println("Hi! I class SCHOOL."); 
6
    } 
7
} 
8
// This is the class named Student
9
class Student { 
10
    // This is a method in class Student
11
    public void print() { 
12
        System.out.println("Hi! I am class STUDENT"); 
13
    } 
14
}
15
16
public class Question21{ 
17
    public static void main(String args[]){
18
        // Creating object of class Student
19
        Student student = new Student();
20
21
        // Creating object of class School
22
        School school = new School();
23
24
        // Call 'print()' method of class School
25
        student.print();
26
27
        // Call 'print()' method of class Student 
28
        school.print();
29
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
Hi! I am class STUDENT\n
Hi! I class SCHOOL.
Hi! I am class STUDENT\n
Hi! I class SCHOOL.\n
Passed after ignoring Presentation Error




Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 2 : Programming Assignment 2

Due on 2023-08-10, 23:59 IST
Complete the code segment to call the method  print() of class given class Printer to print the following.

--------------------------------
Hi! I am class STUDENT
Hi! I class SCHOOL.
--------------------------------

NOTE: Don't provide any INPUT in Sample Test Cases

Your last recorded submission was on 2023-07-31, 22:47 IST
Select the Language for this assignment. 
File name for this program : 
1
// This is the class named Printer
2
class Printer { 
3
    // This are the methods in class Printer
4
    public void print() { 
5
        System.out.println("Hi! I class SCHOOL."); 
6
    } 
7
    public void print(String s) { 
8
        System.out.println(s); 
9
    } 
10
} 
11
12
public class Question22{ 
13
    public static void main(String[] args) {
14
// Create an object of class Printer
15
16
Printer p = new Printer();
17
18
// Call 'print()' methods for desired output
19
20
p.print("Hi! I am class STUDENT");
21
p.print();
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
Hi! I am class STUDENT\n
Hi! I class SCHOOL.
Hi! I am class STUDENT\n
Hi! I class SCHOOL.\n
Passed after ignoring Presentation Error




Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 2 : Programming Assignment 3

Due on 2023-08-10, 23:59 IST
Complete the code segment to call print() method of class Question by creating a method named ‘studentMethod()’.
Your last recorded submission was on 2023-07-31, 22:56 IST
Select the Language for this assignment. 
File name for this program : 
1
// This is the main class Question
2
public class Question23{ 
3
    public static void main(String[] args) { 
4
        // Object of the main class is created
5
        Question23 q = new Question23();
6
        // Print method on object of Question class is called
7
        q.studentMethod();
8
    }
9
    
10
    // 'print()' method is defined in class Question
11
    void print(Question23 object){
12
        System.out.print("Well Done!");
13
    }
14
// Define a method named 'studentMethod()' in class Question
15
    void studentMethod(){
16
// Calling a method named 'print()' in class Question23
17
        print(this);
18
    }
0
}
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
Well Done!
Well Done!
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 2 : Programming Assignment 4

Due on 2023-08-10, 23:59 IST
Complete the code segment to call default constructor first and then any other constructor in the class.
Your last recorded submission was on 2023-07-31, 23:01 IST
Select the Language for this assignment. 
File name for this program : 
1
// This is the main class Question
2
public class Question214{
3
    public static void main(String[] args){
4
        Answer a = new Answer(10,"MCQ");
5
    }
6
}
7
// This is the class Answer
8
class Answer{
9
    // This is the default constructor of the class Answer
10
    Answer(){
11
        System.out.println("You got nothing.");
12
    }
13
    // This is a parameterized constructor of the class Answer
14
    Answer(int marks, String type){
15
        //The 'this()' referene variable is able to call the default constructor of the class.
16
        this();     
17
        //Print marks and type of the question
18
        System.out.print("You got "+marks+" for an "+ type);
19
    }
20
}
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
You got nothing.\n
You got 10 for an MCQ
You got nothing.\n
You got 10 for an MCQ
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 2 : Programming Assignment 5

Due on 2023-08-10, 23:59 IST
Complete the code segment to debug / complete the program which is intended to print 'NPTEL JAVA'.
Your last recorded submission was on 2023-07-31, 23:05 IST
Select the Language for this assignment. 
File name for this program : 
1
public class Question215{ 
2
    public static void main(String[] args) {
3
  //Declare variable with name 'nptel', 'space' and 'java' and proper datatype
4
    String nptel, space, java;
5
6
  //Initialize the variables with proper input
7
    nptel = "NPTEL";
8
    space = " ";
9
    java = "JAVA";
0
System.out.print(nptel+space+java);
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
NA
NPTEL JAVA
NPTEL JAVA
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed


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


Week 3 : Programming Assignment 1

Due on 2023-08-17, 23:59 IST
This program is related to the generation of Fibonacci numbers.

For example: 0,1, 1,2, 3,5, 8, 13,… is a Fibonacci sequence where 13 is the 8th Fibonacci number.

A partial code is given and you have to complete the code as per the instruction given .
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner; //This package for reading input
2
public class Fibonacci { 
3
4
public static void main(String args[]) { 
5
     Scanner sc = new Scanner(System.in);
6
    int n=sc.nextInt(); //Read an integer
7
System.out.println(fib(n)); //Generate and print the n-th Fibonacci                
8
                                     //number
9
    } 
10
static int fib(int n) {
11
//complete the code segment to find the nth Fibonacci number in the Fibonacci sequence and return the value. Write the function recursively.
12
           if (n==1)      //Terminal condition
13
            return 0;
14
        else if(n==2)
15
            return 1;           
16
return fib(n - 1) + fib(n - 2); //Recursive call of function
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
8
13
13\n
Passed after ignoring Presentation Error




Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed


Week 3 : Programming Assignment 2

Due on 2023-08-17, 23:59 IST

Define a class Point with two fields x and y each of type double. Also, define a method distance(Point p1, Point p2) to calculate the distance between points p1 and p2 and return the value in double.

Complete the code segment given below. Use Math.sqrt( ) to calculate the square root.

Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class Circle extends Point{
4
    
5
public static void main(String[] args) {
6
7
     Scanner sc = new Scanner(System.in);
8
     Point p1=new Point();
9
     p1.x=sc.nextDouble();
10
     p1.y=sc.nextDouble();
11
     Point p2=new Point();
12
     p2.x=sc.nextDouble();
13
     p2.y=sc.nextDouble(); 
14
     Circle c1=new Circle();
15
    c1.distance(p1,p2);
16
    
17
  }
18
19
}
20
//Complete the code segment to define a class Point with parameter x,y and method distance()for calculating distance between two points.
21
//Note: Pass objectsof type class Point as argument in distance() method.
22
class Point{
23
  double x;
24
  double y;
25
26
public static void distance(Point p1,Point p2){
27
        double d;
28
      d=Math.sqrt((p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y));
29
      System.out.print(d);
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
2.0  3.0
1.0  2.0
1.4142135623730951
1.4142135623730951
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 3 : Programming Assignment 3

Due on 2023-08-17, 23:59 IST
A class Shape is defined with two overloading constructors in it. Another class Test1 is partially defined which inherits the class Shape. The class Test1 should include two overloading constructors as appropriate for some object instantiation shown in main() method. You should define the constructors using the super class constructors. Also, override the method calculate( ) in Test1 to calculate the volume of a Shape.
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
class Shape{
3
double length, breadth;
4
Shape(double l, double b){ //Constructor to initialize a Shape object  
5
length = l;
6
breadth= b;
7
}
8
Shape(double len){    //Constructor to initialize another Shape object  
9
length = breadth = len;
10
}
11
double calculate(){// To calculate the area of a shape object
12
return length * breadth ;
13
}
14
}
15
public class Test1 extends Shape{
16
//Create a derived class constructor which can call the one parametrized constructor of the base class
17
//Create a derived class constructor which can call the two parametrized constructor of the base class
18
//Override the method calculate() in the derived class to find the volume of a shape instead of finding the area of a shape
19
//Template code:
20
    double height;
21
    Test1(double length,double h) {
22
//base class constructor with one parameter is called
23
        super(length);
24
        height=h;
25
    }
26
    
27
    Test1(double length,double breadth,double h) {
28
//base class constructor having two argument is called
29
        super(length,breadth);
30
        height=h;
31
    }
32
33
    double calculate()  {
34
        return length*breadth*height;
35
    }
0
public static void main(String args[]){
1
    Scanner sc = new Scanner(System.in);//Create an object to read                                                               
2
                                          //input
3
    double l=sc.nextDouble(); //Read length
4
    double b=sc.nextDouble(); //Read breadth    
5
    double h=sc.nextDouble(); //Read height
6
    Test1 myshape1 = new Test1(l,h);
7
    Test1 myshape2 = new Test1(l,b,h);
8
    double volume1;
9
    double volume2;
10
    volume1 = myshape1.calculate();
11
    volume2=myshape2.calculate();
12
    System.out.println(volume1);
13
    System.out.println(volume2);
14
    }
15
}
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.0 3.0 4.0
16.0\n
24.0
16.0\n
24.0\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 3 : Programming Assignment 4

Due on 2023-08-17, 23:59 IST
This program to exercise the call of static and non-static methods. A partial code is given defining two methods, namely sum( ) and multiply ( ). You have to call these methods to find the sum and product of two numbers. Complete the code segment as instructed.
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
class QuestionScope {
3
int sum(int a, int b){ //non-static method
4
        return a + b;
5
    }
6
static int multiply(int a, int b){ //static method
7
        return a * b;
8
    }
9
}
10
public class Exercise3_4{
11
public static void main( String[] args ) {
12
        Scanner sc = new Scanner(System.in);
13
        int n1=sc.nextInt();
14
        int n2=sc.nextInt();
15
//Called the method sum() to find the sum of two numbers.
16
//Called the method multiply() to find the product of two numbers.
17
QuestionScope st = new QuestionScope(); // Create an object to call non-  
18
                                                //static method 
19
      int result1=st.sum(n1,n2); // Call the method
20
      int result2=QuestionScope.multiply(n1,n2);    // Create an object to call 
21
                                                //static method 
22
        
23
        System.out.println(result1);
24
        System.out.print(result2);
25
    
26
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
3 5
8\n
15
8\n
15
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 3 : Programming Assignment 5

Due on 2023-08-17, 23:59 IST
Complete the code segment to swap two numbers using call by object reference.
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
class Question {  //Define a class Question with two elements e1 and e2.
3
  Scanner sc = new Scanner(System.in);
4
int e1 = sc.nextInt();  //Read e1
5
int e2 = sc.nextInt();  //Read e2
6
}
7
public class Exercise3_5{
8
//Template code 
9
//Define static method swap()to swap the values of e1 and e2 of class T. 
10
public static void swap(Question t) {
11
int temp = t.e1;
12
    t.e1 = t.e2;
13
    t.e2 = temp;
14
  }
0
public static void main(String[] args) {
1
//Create an object of class Question
2
    Question t = new Question ();
3
  //Call the method swap()
4
swap(t);
5
6
System.out.println(t.e1+" "+t.e2);    
7
  }
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
10 20
20 10
20 10\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed


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


Week 4 : Programming Assignment 1

Due on 2023-08-24, 23:59 IST
Complete the code segment to execute the following program successfully. You should import the correct package(s) and/or class(s) to complete the code.
Select the Language for this assignment. 
File name for this program : 
1
// Import the required package(s) and/or class(es)
2
import java.util.Scanner;
3
// Import of pre-defined package java.lang and class System and all of its member
4
import static java.lang.System.*;
0
// main class Question is created
1
public class Question41{
2
  public static void main(String[] args) {
3
    // Scanner object is created
4
    Scanner scanner = new Scanner(System.in);
5
     // Read String input using scanner class
6
    String courseName = scanner.nextLine(); 
7
     // Print the scanned String
8
    out.println("Course: " + courseName); 
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
NPTEL JAVA
Course: NPTEL JAVA
Course: NPTEL JAVA\n
Passed after ignoring Presentation Error




Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 4 : Programming Assignment 2

Due on 2023-08-24, 23:59 IST
Complete the code segment to print the current year. Your code should compile successfully.

Note: 
In this program, you are not allowed to use any import statement. Use should use predefined class Calendar defined in java.util package.

Select the Language for this assignment. 
File name for this program : 
1
// The following is the declaration of the main class named Question42
2
public class Question42 { 
3
    public static void main(String args[]){
4
        int year; // integer type variable to store year
5
// Create an object of Calendar class. 
6
java.util.Calendar current;
7
               
8
               // Use getInstance() method to initialize the Calendar object.
9
               current = java.util.Calendar.getInstance();
10
        
11
        // Initialize the int variable year with the current year
12
        year = current.get(current.YEAR);
0
// Print the current Year
1
        System.out.println("Current Year: "+year);
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
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
Current Year: 2023\n
Current Month: 8
Current Year: 2023\n
Current Month: 8
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 4 : Programming Assignment 3

Due on 2023-08-24, 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 injected with some bugs. Debug the code to execute the program successfully.

Select the Language for this assignment. 
File name for this program : 

Sample solutions (Provided by instructor)

1
interface ExtraLarge{
2
    static String extra = "This is extra-large";
3
    void display();
4
}
5
6
class Large {
7
    public void Print() {
8
        System.out.println("This is large");
9
    }
10
}
11
 
12
class Medium extends Large {
13
    public void Print() {
14
        System.out.println("This is medium");
15
        super.Print();  
16
    }
17
}
18
class Small extends Medium {
19
    public void Print() {
20
        System.out.println("This is small");
21
        super.Print();  
22
    }
23
}
24
class Question43 implements ExtraLarge{
25
    public static void main(String[] args) {
26
        Small s = new Small();
27
        s.Print();
28
        Question43 q = new Question43();
29
        q.display();
30
    }
31
    public void display(){
32
        System.out.println(extra);
33
    }
34
} 
Private Test cases used for evaluationInputExpected OutputActual OutputStatus
Test Case 1
 This is small\n
This is medium\n
This is large\n
This is extra-large
This is small\n
This is medium\n
This is large\n
This is extra-large\n
Passed

The due date for submitting this assignment has passed.
1 out of 1 tests passed.
You scored 100.0/100.


Week 4 : Programming Assignment 4

Due on 2023-08-24, 23:59 IST
Complete the code segment to call the default method in the interface First and Second.
Select the Language for this assignment. 
File name for this program : 
1
interface First{ 
2
    // default method 
3
    default void show(){ 
4
        System.out.println("Default method implementation of First interface."); 
5
    } 
6
} 
7
  
8
interface Second{ 
9
    // Default method 
10
    default void show(){ 
11
        System.out.println("Default method implementation of Second interface."); 
12
    } 
13
} 
14
  
15
// Implementation class code 
16
class Question44 implements First, Second{ 
17
    // Overriding default show method 
18
    public void show(){
19
// Call show() of First interface.
20
   First.super.show();
21
22
// Call show() of Second interface.
23
   Second.super.show();
0
} 
1
  
2
    public static void main(String args[]){ 
3
        Question44 q = new Question44(); 
4
        q.show(); 
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
Default method implementation of First interface.\n
Default method implementation of Second interface.
Default method implementation of First interface.\n
Default method implementation of Second interface.\n
Passed after ignoring Presentation Error


Week 4 : Programming Assignment 5

Due on 2023-08-24, 23:59 IST
Modify the code segment to print the following output.

-----------------OUTPUT-------------------
Circle: This is Shape1
Circle: This is Shape2
-------------------------------------------------

Private Test cases used for evaluationInputExpected OutputActual OutputStatus
Test Case 1
 Circle: This is Shape2\n
Circle: This is Shape1
Circle: This is Shape2\n
Circle: This is Shape1\n
Passed

The due date for submitting this assignment has passed.
1 out of 1 tests passed.
You scored 100.0/100.

Assignment submitted on 2023-02-15, 21:26 IST
Your last recorded submission was :
1
// Interface ShapeX is created
2
interface ShapeX {
3
 public String base = "This is Shape1";
4
 public void display1();
5
}
6
7
// Interface ShapeY is created which extends ShapeX
8
interface ShapeY extends ShapeX {
9
 public String base = "This is Shape2";
10
 public void display2();
11
}
12
13
// Class ShapeG is created which implements ShapeY
14
class ShapeG implements ShapeY {
15
 public String base = "This is Shape3";
16
 //Overriding method in ShapeX interface
17
 public void display1() {
18
  System.out.println("Circle: " + ShapeX.base);
19
 }
20
 // Override method in ShapeY interface
21
  public void display2(){
22
  System.out.println("Circle: " + ShapeY.base);
23
}
24
}
25
26
// Main class Question 
27
public class Question45{
28
 public static void main(String[] args) {
29
  //Object of class shapeG is created and display methods are called.
30
  ShapeG circle = new ShapeG();
31
  circle.display2();
32
  circle.display1();
33
  
34
 }
35
}


 

Week 5 : Programming Assignment 1

Due on 2023-08-31, 23:59 IST
An interface Number is defined in the following program.  You have to declare a class A, which will implement the interface Number. Note that the method findSqr(n) will return the square of the number n.
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
interface Number {
4
    int findSqr(int i);  // Returns the square of n
5
}
6
//Create a class A which implements the interface Number.
7
class A implements Number {
8
    //Define a method to find the square of a number
9
     int i, square;
10
     public int findSqr(int i) {
11
         square=i*i;
12
            return square;       
13
      }
14
}
0
public class Question5_1{ 
1
        public static void main (String[] args){ 
2
          A a = new A();   //Create an object of class A
3
           // Read a number from the keyboard
4
           Scanner sc = new Scanner(System.in);  
5
           int i = sc.nextInt();
6
           System.out.print(a.findSqr(i)); 
7
    } 
8
}
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
25
25
Passed



Week 5 : Programming Assignment 2

Due on 2023-08-31, 23:59 IST
This program is to find the GCD (greatest common divisor) of two integers writing a recursive function findGCD(n1,n2). Your function should return -1, if the argument(s) is(are) other than positive number(s).
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
interface GCD {
4
    public int findGCD(int n1,int n2);
5
}
6
//Create a class B, which implements the interface GCD.
7
class B implements GCD {
8
    int n1,n2;
9
        
10
    //Create a method to calculate GCD
11
    public int findGCD(int n1, int n2){
12
        if(n1==0&& n2==0) {
13
            return -1;
14
        }
15
        else if(n2 == 0){
16
            return n1;
17
        }
18
        
19
        else {
20
            return findGCD(n2, n1%n2);
21
        }
22
        }
23
 }
0
public class Question5_2{ 
1
        public static void main (String[] args){ 
2
          B a = new B();   //Create an object of class B
3
            // Read two numbers from the keyboard
4
            Scanner sc = new Scanner(System.in);  
5
             int p1 = sc.nextInt();
6
             int p2 = sc.nextInt();
7
            System.out.print(a.findGCD(p1,p2)); 
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
40 60
20
20
Passed
Test Case 2
2 0
2
2
Passed
Test Case 3
-1 -1
-1
-1
Passed



Week 5 : Programming Assignment 3

Due on 2023-08-31, 23:59 IST
Complete the code segment to catch the ArithmeticException in the following, if any. On the occurrence of such an exception, your program should print “Exception caught: Division by zero.” If there is no such exception, it will print the result of division operation on two integer values.
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
  
3
  public class Question5_3 {
4
  public static void main(String[] args) { 
5
      int a, b;
6
      Scanner input = new Scanner(System.in);
7
       int result;  
8
       a = input.nextInt();
9
       b = input.nextInt();
10
  
11
      // try block to divide two numbers and display the result
12
         try {
13
              result = a/b;
14
              System.out.print(result);
15
             }
16
          // catch block to catch the ArithmeticException
17
          catch (ArithmeticException e) {
18
             System.out.print("Exception caught: Division by zero.");
19
          }
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
4 0
Exception caught: Division by zero.
Exception caught: Division by zero.
Passed
Test Case 2
10 3
3
3
Passed



Week 5 : Programming Assignment 4

Due on 2023-08-31, 23:59 IST
In the following program, an array of integer data to be initialized. During the initialization, if a user enters a value other than integer value, then it will throw InputMismatchException exception. On the occurrence of such an exception, your program should print “You entered bad data.” If there is no such exception it will print the total sum of the array.
Select the Language for this assignment. 
File name for this program : 
1
//Prefixed Fixed Code:
2
import java.util.*;
3
public class Question5_4{
4
  public static void main(String[] args) {
5
      Scanner sc = new Scanner(System.in); 
6
      int length = sc.nextInt(); 
7
      // create an array to save user input 
8
      int[] name = new int[length];
9
      int sum=0;//save the total sum of the array.
10
try{
11
       for(int i=0;i<length;i++){  
12
          int userInput=sc.nextInt();
13
          name[i] = userInput;
14
          sum=sum+name[i]; 
15
          } 
16
        System.out.print(sum);
17
        }
18
       catch(InputMismatchException e) {
19
        System.out.print("You entered bad data.");
20
        }
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
3
5 2 1
8
8
Passed
Test Case 2
2
1 1.0
You entered bad data.
You entered bad data.
Passed



Week 5 : Programming Assignment 5

Due on 2023-08-31, 23:59 IST

In the following program, there may be multiple exceptions. You have to complete the code using only one try-catch block to handle all the possible exceptions.

For example, if user’s input is 1, then it will throw and catch java.lang.NullPointerException“.

Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Question5_5{
3
    public static void main (String   args[ ] ) {
4
           Scanner scan = new Scanner(System.in);
5
            int i = scan.nextInt();
6
        int j;
7
try {
8
             switch (i) {
9
            case 0 : 
10
            int zero = 0; 
11
            j = 92/ zero;       
12
            break;
13
            case 1 : 
14
            int b[ ] = null; 
15
            j = b[0] ;  
16
            break;
17
               default:
18
               System.out.print("No exception");
19
            }       
20
          }
21
            // catch block          
22
        catch (Exception e) {       
23
           System.out.print(e) ;
24
        }
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
5
No exception
No exception
Passed
Test Case 2
0
java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
Passed
Test Case 3
1
java.lang.NullPointerException
java.lang.NullPointerException
Passed



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



Week 6 : Programming Assignment 1

Due on 2023-09-07, 23:59 IST

Complete the code segment to print the following using the concept of extending the Thread class in Java:

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

Thread is Running.

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

Select the Language for this assignment. 
File name for this program : 
1
// Write the appropriate code to extend the Thread class to complete the class Question61.
2
public class Question61 extends Thread{
3
    public void run(){
4
        System.out.print("Thread is Running.");
5
    }
0
public static void main(String args[]){  
1
2
        // Creating object of thread class
3
        Question61 thread=new Question61();  
4
5
                // Start the thread
6
        thread.start();
7
    }  
8
}
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



Week 6 : Programming Assignment 2

Due on 2023-09-07, 23:59 IST

In the following program, a thread class Question62 is created using the Runnable interface Complete the main() to create a thread object of the class Question62 and run the thread. It should print the output as given below.

 

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

Welcome to Java Week 6 New Question.

Main Thread has ended.

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

Select the Language for this assignment. 
File name for this program : 
1
public class Question62 implements Runnable {  
2
  
3
    @Override  
4
    public void run() {  
5
        System.out.print(Thread.currentThread().getName()+" has ended.");  
6
    }
7
// Create main() method and appropriate statements in it
8
public static void main(String[] args) {  
9
        Question62 ex = new Question62();  
10
        Thread t1= new Thread(ex);  
11
        t1.setName("Main Thread");
12
        t1.start();  
13
        System.out.println("Welcome to Java Week 6 New Question.");  
14
        t1.setName("Main Thread");
15
    }
0
}
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
Welcome to Java Week 6 New Question.\n
Main Thread has ended.
Welcome to Java Week 6 New Question.\n
Main Thread has ended.
Passed



Week 6 : Programming Assignment 3

Due on 2023-09-07, 23:59 IST

A part of the Java program is given, which can be completed in many ways, for example using the concept of thread, etc.  Follow the given code and complete the program so that your program prints the message "NPTEL Java week-6 new Assignment Q3". Your program should utilize the given interface/ class.

Select the Language for this assignment. 
File name for this program : 
1
// Interface A is defined with an abstract method run()
2
interface A {
3
    public abstract void run();
4
}
5
6
// Class B is defined which implements A and an empty implementation of run()
7
class B implements A {
8
    public void run() {}
9
}
10
// Class MyThread is defined which extends class B
11
class MyThread extends B {
12
    // run() is overriden and 'NPTEL Java' is printed.
13
    public void run() {
14
        System.out.print("NPTEL Java week-6 new Assignment Q3");
15
    }
16
}
0
// Main class Question is defined
1
public class Question63 {
2
     public static void main(String[] args) {
3
         // An object of MyThread class is created
4
         MyThread t = new MyThread();
5
         // run() of class MyThread is called
6
         t.run();
7
     }
8
}
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
NPTEL Java week-6 new Assignment Q3
NPTEL Java week-6 new Assignment Q3
Passed



Week 6 : Programming Assignment 4

Due on 2023-09-07, 23:59 IST

Execution of two or more threads occurs in a random order. The keyword 'synchronized' in Java is used to control the execution of thread in a strict sequence. In the following, the program is expected to print the output as given below. Do the necessary use of 'synchronized' keyword, so that, the program prints the Final sum as given below:  

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

Final sum:6000

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

Select the Language for this assignment. 
File name for this program : 
1
/*
2
A simple class that demonstrates using the 'synchronized'
3
keyword so that multiple threads may send it messages.
4
The class stores two ints, a and b; sum() returns
5
their sum, and inc() increments both numbers.
6
<p>
7
The sum() and incr() methods form a "critical section" --
8
they can compute the wrong thing if run by multiple threads
9
at the same time. The sum() and inc() methods are declared
10
"synchronized" -- they respect the lock in the receiver object.
11
*/
12
class Pair {
13
private int a, b;
14
public Pair() {
15
a = 0;
16
b = 0;
17
}
18
// Returns the sum of a and b. (reader)
19
// Should always return an even number.
20
public synchronized int sum() {
21
return(a+b);
22
}
23
24
// Increments both a and b. (writer)
25
public synchronized void inc() {
26
a++;
27
b++;
28
}
29
}
0
/*
1
A simple worker subclass of Thread.
2
In its run(), sends 1000 inc() messages
3
to its Pair object.
4
(1000 may not be big enough to exhibit the bug on uniprocessor --
5
hardware more like 1000000 may be required).
6
*/
7
public class PairWorker extends Thread {
8
public final int COUNT = 1000;
9
private Pair pair;
10
// Ctor takes a pointer to the pair we use
11
public PairWorker(Pair pair) {
12
this.pair = pair;
13
}
14
// Send many inc() messages to our pair
15
public void run() {
16
for (int i=0; i<COUNT; i++) {
17
pair.inc();
18
}
19
}
20
21
/*
22
Test main -- Create a Pair and 3 workers.
23
Start the 3 workers -- they do their run() --
24
and wait for the workers to finish.
25
*/
26
public static void main(String args[]) {
27
Pair pair = new Pair();
28
PairWorker w1 = new PairWorker(pair);
29
PairWorker w2 = new PairWorker(pair);
30
PairWorker w3 = new PairWorker(pair);
31
w1.start();
32
w2.start();
33
w3.start();
34
// the 3 workers are running
35
// all sending messages to the same object
36
// we block until the workers complete
37
try {
38
w1.join();
39
w2.join();
40
w3.join();
41
}
42
catch (InterruptedException ignored) {}
43
44
45
System.out.println("Final sum:" + pair.sum()); // should be 6000
46
/*
47
If sum()/inc() were not synchronized, the result would
48
be 6000 in some cases, and other times random values
49
like 5979 due to the writer/writer conflicts of multiple
50
threads trying to execute inc() on an object at the same time.
51
*/
52
}
53
}
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 sum:6000
Final sum:6000\n
Passed after ignoring Presentation Error



Week 6 : Programming Assignment 5

Due on 2023-09-07, 23:59 IST

Given a snippet of code, add necessary codes to print the following:

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

Name of thread 't1':Thread-0

Name of thread 't2':Thread-1

New name of thread 't1':Week 6 Assignment Q5

New name of thread 't2':Week 6 Assignment Q5 New

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

Select the Language for this assignment. 
File name for this program : 
1
public class Question65 extends Thread{  
2
  public void run(){  
3
      
4
  }  
5
 public static void main(String args[]){  
6
    Question65 t1=new Question65();  
7
    System.out.println("Name of thread 't1':"+ t1.getName());  
8
9
Question65 t2=new Question65();  
10
    System.out.println("Name of thread 't2':"+ t2.getName());
11
// Write the necessary code below...
12
// start the thread-1  
13
  t1.start();  
14
// set the name of thread-1
15
  t1.setName("Week 6 Assignment Q5");  
16
17
// start the thread-2  
18
  t2.start();  
19
// set the name of thread-2
20
  t2.setName("Week 6 Assignment Q5 New");
0
System.out.println("New name of thread 't1':"+ t1.getName()); 
1
   System.out.println("New name of thread 't2':"+ t2.getName());
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
NA
Name of thread 't1':Thread-0\n
Name of thread 't2':Thread-1\n
New name of thread 't1':Week 6 Assignment Q5\n
New name of thread 't2':Week 6 Assignment Q5 New
Name of thread 't1':Thread-0\n
Name of thread 't2':Thread-1\n
New name of thread 't1':Week 6 Assignment Q5\n
New name of thread 't2':Week 6 Assignment Q5 New\n
Passed after ignoring Presentation Error


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


Week 7 : Programming Assignment 1

Due on 2023-09-14, 23:59 IST
Complete the following code fragment to read three integer values from the keyboard and find the sum of the values. Declare a variable "sum" of type int and store the result in it.
Select the Language for this assignment. 
File name for this program : 
1
// Write the appropriate statement(s) to import the package(s) you need in your program
2
import java.util.*;
3
public class Question1{ 
4
        public static void main (String[] args){
5
6
//Write the appropriate code to read the 3 integer values and find their sum.
7
          int i,n=0,sum=0;
8
             Scanner inr = new Scanner(System.in);
9
        for (i=0;i<3;i++)
10
        {
11
            n = inr.nextInt();
12
            
13
        sum =sum+n;
14
            }
0
System.out.println(sum);
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
1
1
3
3\n
Passed after ignoring Presentation Error


Week 7 : Programming Assignment 2

Due on 2023-09-14, 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".
Select the Language for this assignment. 
File name for this program : 
1
import java.io.*;  
2
public class Question2{  
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.print(x*x);
10
    }
11
   catch (Exception e){
12
       System.out.print("Please enter valid data");
13
    }
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
2
4
4
Passed
Test Case 2
p
Please enter valid data
Please enter valid data
Passed




Week 7 : Programming Assignment 3

Due on 2023-09-14, 23:59 IST
A byte char array is initialized. You have to enter an index value"n". According to index your program will print the byte and its corresponding char value.
Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “exception occur” .If there is no such exception, it will print the required output.
Your last recorded submission was on 2023-09-04, 15:31 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.*;
2
public class Question3 {  
3
    public static void main(String[] args) { 
4
       try{ 
5
      byte barr[]={'N','P','T','E','L','-','J','A','V','A','J','A','N','-','N','O','C','C','S','E'};
6
          Scanner inr = new Scanner(System.in);
7
      int n = inr.nextInt();
8
// Complete the code to get specific indexed byte value and its corresponding char value.
9
String s2 = new String(barr,n,1);  
10
        System.out.println(barr[n]);
11
    System.out.println(s2); 
12
    }
0
catch (Exception e){
1
        System.out.println("exception occur");
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
1
80\n
P
80\n
P
Passed
Test Case 2
24
exception occur
exception occur\n
Passed after ignoring Presentation Error
Test Case 3
4
76\n
L
76\n
L
Passed


Week 7 : Programming Assignment 4

Due on 2023-09-14, 23:59 IST
The following program reads a string from the keyboard and is stored in the String variable "s1". You have to complete the program so that it should should print the number of vowels in s1 . If your input data doesn't have any vowel it will print "0".
Select the Language for this assignment. 
File name for this program : 
1
import java.io.*;
2
import java.util.*;
3
4
public class Question4{  
5
    public static void main(String[] args) { 
6
      int c=0;
7
         try{
8
            InputStreamReader r=new InputStreamReader(System.in);  
9
            BufferedReader br=new BufferedReader(r);  
10
            String s1 = br.readLine();
11
//Write your code here to count the number of vowels in the string "s1"
12
for(int i=0;i<s1.length();i++){  
13
          char s2=s1.charAt(i);
14
          if(s2=='e' || s2=='E'|| s2=='a' || s2=='A' || s2=='i' || s2=='I' || s2=='o' || s2=='O' || s2=='u' || s2=='U') 
15
         {
16
           c=c+1;
17
             }    
18
         }
0
System.out.println(c); 
1
       }
2
       catch (Exception e){
3
         System.out.println(e);
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
sam
1
1\n
Passed after ignoring Presentation Error
Test Case 2
eeE
3
3\n
Passed after ignoring Presentation Error
Test Case 3
10
0
0\n
Passed after ignoring Presentation Error


Week 7 : Programming Assignment 5

Due on 2023-09-14, 23:59 IST
A string "s1" is already initialized. You have to read the index "n"  from the keyboard. Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “exception occur” .If there is no such exception, your program should replace the char "a" at the index value "n" of the "s1" ,then it will print the modified string.
Select the Language for this assignment. 
File name for this program : 
1
import java.util.*;
2
public class Question5 {  
3
    public static void main(String[] args) { 
4
       try{ 
5
        String s1="NPTELJAVA"; 
6
            Scanner inr = new Scanner(System.in);
7
        int n = inr.nextInt();
8
            char c='a';
9
//Replace the char in string "s1" with the char 'a' at index "n"  and print the modified string
10
byte[] barr=s1.getBytes();   
11
          
12
                byte b1 = (byte) c;
13
                barr[n]=b1;
14
        System.out.print(new String(barr)); 
15
       }
0
catch (Exception e){
1
          System.out.println("exception occur");
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
-1
exception occur
exception occur\n
Passed after ignoring Presentation Error
Test Case 2
e
exception occur
exception occur\n
Passed after ignoring Presentation Error
Test Case 3
4
NPTEaJAVA
NPTEaJAVA
Passed



Week 8 : Programming Assignment 1

Due on 2023-09-21, 23:59 IST
Write a program which will print a pyramid of "*" 's of height "n" and print the number of "*" 's in the pyramid.

For example:

Input : 5
Output:  

        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 
25

Select the Language for this assignment. 
File name for this program : 
1
import java.util.*;
2
public class Pattern1 {
3
    public static void main(String[] args) {
4
        Scanner inr = new Scanner(System.in);
5
       int n = inr.nextInt();
6
        int k = 0,sum=0;
7
        for(int i = 1; i <= n; ++i, k = 0) {
8
            for(int space = 1; space <= n - i; ++space) {
9
                System.out.print("  ");
10
            }
11
            while(k != 2 * i - 1) {
12
                System.out.print("* ");
13
                sum=sum+1;
14
                ++k;
15
            }
16
            System.out.println();
17
        }
18
         System.out.print(sum);
19
    }
20
}
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
* \n
  * * * \n
* * * * * \n
9
    * \n
  * * * \n
* * * * * \n
9
Passed after ignoring Presentation Error
Test Case 2
5
* \n
      * * * \n
    * * * * * \n
  * * * * * * * \n
* * * * * * * * * \n
25
        * \n
      * * * \n
    * * * * * \n
  * * * * * * * \n
* * * * * * * * * \n
25
Passed after ignoring Presentation Error


Week 8 : Programming Assignment 2

Due on 2023-09-21, 23:59 IST
Write a program which will print a pascal  pyramid of  "*" 's of height "l" .

For example:

input: 8

output :
       * 
      * * 
     * * * 
    * * * * 
   * * * * * 
  * * * * * * 
 * * * * * * * 
* * * * * * * * 

Select the Language for this assignment. 
File name for this program : 
1
import java.util.*;
2
public class Pattern2 {
3
    public static void main(String[] args) {
4
        Scanner inr = new Scanner(System.in);
5
        int l = inr.nextInt();
6
        int i,j;
7
        int space=l-1;
8
    /*run loop (parent loop) till number of rows*/
9
     for(i=0;i< l;i++)
10
       {
11
        /*loop for initially space, before star printing*/
12
        for(j=0;j< space;j++)
13
        {
14
            System.out.print(" ");
15
        }
16
        for(j=0;j<=i;j++)
17
        {
18
            System.out.print("* ");
19
        }
20
        
21
        System.out.print("\n");
22
        space--;    /* decrement one space after one row*/
23
         }
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
8
* \n
      * * \n
     * * * \n
    * * * * \n
   * * * * * \n
  * * * * * * \n
 * * * * * * * \n
* * * * * * * *
       * \n
      * * \n
     * * * \n
    * * * * \n
   * * * * * \n
  * * * * * * \n
 * * * * * * * \n
* * * * * * * * \n
Passed after ignoring Presentation Error
Test Case 2
7
* \n
     * * \n
    * * * \n
   * * * * \n
  * * * * * \n
 * * * * * * \n
* * * * * * *
      * \n
     * * \n
    * * * \n
   * * * * \n
  * * * * * \n
 * * * * * * \n
* * * * * * * \n
Passed after ignoring Presentation Error




Week 8 : Programming Assignment 3

Due on 2023-09-21, 23:59 IST
Write a program which will print a pyramid of "numbers" 's of height "n" and print the sum of all number's in the pyramid.

For example:

input: 5

output: 
        1 
      1 2 3 
    1 2 3 4 5 
  1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 9 
95

Select the Language for this assignment. 
File name for this program : 
1
import java.util.*;
2
  public class Pattern3 {
3
    public static void main(String[] args) {
4
        Scanner inr = new Scanner(System.in);
5
       int n = inr.nextInt();
6
        int k = 1,sum=0;
7
        for(int i = 1; i <= n; ++i, k = 1) {
8
            for(int space = 1; space <= n-i; ++space) {
9
                System.out.print("  ");
10
            }
11
            while(k <= 2 * i - 1) {
12
                System.out.print(k+" ");
13
                sum=sum+k;
14
                ++k;
15
            }
16
            System.out.println();
17
        }
18
         System.out.println(sum); 
19
    }
20
}
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
1 \n
      1 2 3 \n
    1 2 3 4 5 \n
  1 2 3 4 5 6 7 \n
1 2 3 4 5 6 7 8 9 \n
95
        1 \n
      1 2 3 \n
    1 2 3 4 5 \n
  1 2 3 4 5 6 7 \n
1 2 3 4 5 6 7 8 9 \n
95\n
Passed after ignoring Presentation Error
Test Case 2
3
1 \n
  1 2 3 \n
1 2 3 4 5 \n
22
    1 \n
  1 2 3 \n
1 2 3 4 5 \n
22\n
Passed after ignoring Presentation Error


Week 8 : Programming Assignment 4

Due on 2023-09-21, 23:59 IST
Write a program to print symmetric Pascal's triangle of "*" 's of  height "l" of odd length . If input "l" is even then your program will print "Invalid line number".

For example:

input : 5
output:
  *
 * *
* * *
 * *
  *
input : 6

output:

Invalid line number

Select the Language for this assignment. 
File name for this program : 
1
import java.util.*;
2
public class Pattern4 {
3
    public static void main(String[] args) {
4
        Scanner inr = new Scanner(System.in);
5
      int l = inr.nextInt();
6
        int ul=0; // Upper Line
7
       int ll=0; // Lower Line
8
        
9
    // Check whether line number is odd
10
       if (l%2!=0){
11
       ul=(l/2)+1;          
12
       ll=l-ul;
13
    //Code for upper half
14
    for(int i=1;i<=ul; i++){    
15
    //Space management
16
    for(int s=1;s<=(ul-i); s++){
17
        System.out.print(" ");
18
    }
19
    // Star management
20
    for(int j=1;j<=i; j++){
21
         System.out.print("* ");
22
    }
23
    System.out.println();
24
    }
25
            
26
    //Code for lower half
27
    int llc=ll;
28
    for(int i=1;i<=ll; i++){
29
    //Space management
30
    for(int s=llc;s<ll; s++){
31
       System.out.print(" ");
32
    }
33
    // Star management
34
    for(int j=1;j-1<=ll-i; j++){
35
       System.out.print(" *");
36
     }
37
     llc--;
38
    System.out.println();
39
        }
40
    }
41
        else{
42
       System.out.print("Invalid line number");
43
    }
44
    }
45
}
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
6
Invalid line number
Invalid line number
Passed
Test Case 2
7
* \n
  * * \n
 * * * \n
* * * * \n
 * * *\n
  * *\n
   *
   * \n
  * * \n
 * * * \n
* * * * \n
 * * *\n
  * *\n
   *\n
Passed after ignoring Presentation Error


Week 8 : Programming Assignment 5

Due on 2023-09-21, 23:59 IST
Write a program to display any digit(n) from 0-9 represented as a "7 segment  display"

For example:

input : 5

output :
 _ 
|_ 
 _|

input : 4

output :

|_|
   |

Select the Language for this assignment. 
File name for this program : 
1
import java.util.*;
2
public class Pattern5 {
3
   private static final Map<Integer, Integer> encodings =
4
        new HashMap<Integer, Integer>();
5
    static {
6
        encodings.put(0, 0x7E);
7
        encodings.put(1, 0x30);
8
        encodings.put(2, 0x6D);
9
        encodings.put(3, 0x79);
10
        encodings.put(4, 0x33);
11
        encodings.put(5, 0x5B);
12
        encodings.put(6, 0x5F);
13
        encodings.put(7, 0x70);
14
        encodings.put(8, 0x7F);
15
        encodings.put(9, 0x7B);
16
    }
17
    public static void printDigit(int digit) {
18
        int code = encode(digit);
19
        char[] bits =
20
            String.format("%7s", Integer.toBinaryString(code))
21
                .replace(' ', '0').toCharArray();
22
23
        lightSegment(bits[0] == '1', " _ \n", "   \n");
24
        lightSegment(bits[5] == '1', "|", " ");
25
        lightSegment(bits[6] == '1', "_", " ");
26
        lightSegment(bits[1] == '1', "|\n", " \n");
27
        lightSegment(bits[4] == '1', "|", " ");
28
        lightSegment(bits[3] == '1', "_", " ");
29
        lightSegment(bits[2] == '1', "|\n", " \n");
30
    }
31
32
    private static void lightSegment(boolean on, String onValue, String offValue) {
33
        System.out.print(on ? onValue : offValue);
34
        try {
35
            Thread.sleep(0);
36
        }
37
        catch (InterruptedException e) {
38
            e.printStackTrace();
39
        }
40
    }
41
    private static int encode(int digit) {
42
       return encodings.containsKey(digit) ? encodings.get(digit) : 0x00;
43
    }
44
   public static void main(String[] args) throws Exception {
45
           Scanner inr = new Scanner(System.in);
46
          int n = inr.nextInt();
47
           printDigit(n);       
48
    }
49
}
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
_ \n
|_ \n
 _|
 _ \n
|_ \n
 _|\n
Passed after ignoring Presentation Error
Test Case 2
4
|_|\n
  |
   \n
|_|\n
  |\n
Passed after ignoring Presentation Error





3 comments:

  1. Thank you so much for uploading the answers sir...no words will equal to gratitude...i dont know how to show gratitude to you....thank you so much 100 times once again...your youtube channel and blog is very useful for all.. we will support you always in all forms...also my kind request please upload assignment answers and programming assignment answers for upcoming all weeks till week 12...

    ReplyDelete
  2. I will help you always sir...i will tell about our channel and blog to all friends, share to all ..

    ReplyDelete
  3. Thanks sir , I am very happy given by your answer .

    ReplyDelete

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