Home

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

NPTEL Programming in Java July 2023 Week 9 to 12

 

  Please scroll down for latest Programs. ðŸ‘‡ 


Week 9 : Programming Assignment 1

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

Problem statement:

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

 

Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Question91{
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
// Declare and initialize the required variable(s)
7
        int i=0;
8
        int j=0;
9
        double output=0;
10
        // Split the input string into character array
11
        char seq[] = input.toCharArray();
12
        /*
13
        Use some method to separate the two operands
14
        and then perform the required operation.
15
        */
16
        for(int a=0; a<seq.length; a++){
17
            if(seq[a]=='+'){
18
                i= Integer.parseInt(input.substring(0,a));
19
                j= Integer.parseInt(input.substring(a+1,seq.length));
20
                output = (double)i+j;
21
            }else if(seq[a]=='-'){
22
                i= Integer.parseInt(input.substring(0,a));
23
                j= Integer.parseInt(input.substring(a+1,seq.length));
24
                output = (double)i-j;
25
            }else if(seq[a]=='/'){
26
                i= Integer.parseInt(input.substring(0,a));
27
                j= Integer.parseInt(input.substring(a+1,seq.length));
28
                output = (double)i/j;
29
            }else if(seq[a]=='*'){
30
                i= Integer.parseInt(input.substring(0,a));
31
                j= Integer.parseInt(input.substring(a+1,seq.length));
32
                output = (double)i*j;
33
            }
34
        }
35
        // Print the output as stated in the question
36
        System.out.print(input+" = " + Math.round(output));
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+6
5+6 = 11
5+6 = 11
Passed


Week 9 : Programming Assignment 2

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

Complete the code to develop an ADVANCED CALCULATOR that emulates all the functions of the GUI Calculator as shown in the image.


Note the following points carefully:
1. Use only double datatype to store all numeric values.
2. Each button on the calculator should be operated by typing the characters from 'a' to 'p'.
3. To calculate 25-6, User should input fjhkc (where, f for 2, j for 5, h for '-', k for 6 and c for '=' ).
3. You may use the already defined function 
gui_map(char).
4. Without '=', operations won't give output as shown in Input_2 and Output_2 example below.
5. The calculator should be able to perform required operations on two operands as shown in the below example:


Input_1:
                   klgc

Output_1:
                        
18.0


Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Question92{
3
    public static void main(String args[]){
4
        Scanner sc = new Scanner(System.in);
5
        String input = sc.nextLine();
6
char seq[] = input.toCharArray();
7
        int outflag=0;
8
        
9
        // Start the mapping process for each input character
10
        for(int i=0; i<seq.length; i++){
11
            seq[i]=gui_map(seq[i]);
12
        }
13
        
14
        //Print Mapped GUI (remove comment to see the mapped sequence input)
15
        /*
16
        for(int i=0; i<seq.length; i++){
17
            System.out.print(seq[i]);
18
        }
19
        */
20
        
21
        // Use double type of values for entire calculation
22
        double operand1=0.0;
23
        String o1="";
24
        double operand2=0.0;
25
        String o2="";
26
        double output=0.0;
27
        
28
        // Perform calculaton operations
29
        outerloop:
30
        for(int i=0; i<seq.length; i++){
31
            int r=0;
32
            if(seq[i]=='+'||seq[i]=='-'||seq[i]=='/'||seq[i]=='X'||seq[i]=='='){
33
                for(int j=0; j<i; j++){
34
                    o1+=Character.toString(seq[j]);
35
                }
36
                operand1=Double.parseDouble(o1);
37
                for(int k=i+1; k<seq.length; k++){
38
                    if(seq[k]=='='){
39
                        outflag=1;
40
                        operand2=Double.parseDouble(o2);
41
                        if(seq[i]=='+'){
42
                            output=operand1+operand2;
43
                        }else if(seq[i]=='-'){
44
                            output=operand1-operand2;
45
                        }else if(seq[i]=='/'){
46
                            output=operand1/operand2;
47
                        }else if(seq[i]=='X'){
48
                            output=operand1*operand2;
49
                        }
50
                        break outerloop;
51
                    }else{
52
                        o2+=Character.toString(seq[k]);
53
                    }
54
                }
55
            }
56
        }
57
        
58
        // Check if output is available and print the output
59
        if(outflag==1)
60
            System.out.print(output);
0
}// The main() method ends here.
1
    
2
// A method that takes a character as input and returns the corresponding GUI character 
3
    static char gui_map(char in){
4
        char out = 'N';// N = Null/Empty
5
        char gm[][]={{'a','.'}
6
                    ,{'b','0'}
7
                    ,{'c','='}
8
                    ,{'d','+'}
9
                    ,{'e','1'}
10
                    ,{'f','2'}
11
                    ,{'g','3'}
12
                    ,{'h','-'}
13
                    ,{'i','4'}
14
                    ,{'j','5'}
15
                    ,{'k','6'}
16
                    ,{'l','X'}
17
                    ,{'m','7'}
18
                    ,{'n','8'}
19
                    ,{'o','9'}
20
                    ,{'p','/'}};
21
                    
22
        // Checking for maps
23
        for(int i=0; i<gm.length; i++){
24
            if(gm[i][0]==in){
25
                out=gm[i][1];
26
                break;
27
            }
28
        }
29
        return out;
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
klgc
18.0
18.0
Passed


Week 9 : Programming Assignment 3

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

Complete the code to perform a 45 degree anti clock wise rotation with respect to the center of a 5 × 5 2D Array as shown below:

INPUT:

              00100

              00100

              11111

              00100

              00100


OUTPUT:


              10001

              01010

              00100

              01010

              10001


Note the following points carefully
:
1. Here, instead of 0 and 1 any character may be given.

2. The input and output array size must be of dimension 5 × 5 and nothing else.

Your last recorded submission was on 2023-09-19, 15:31 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Question93{
3
    public static void main(String args[]){
4
        Scanner sc = new Scanner(System.in);
5
char arr[][]= new char[5][5];
6
            // Input 2D Array using Scanner Class
7
            for(int line=0;line<5; line++){
8
                String input = sc.nextLine();
9
                char seq[] = input.toCharArray();
10
                if(seq.length==5){
11
                    for(int i=0;i<5;i++){
12
                        arr[line][i]=seq[i];
13
                    }
14
                }else{
15
                    System.out.print("Wrong Input!");
16
                    System.exit(0);
17
                }
18
            }
19
            // Declaring the array to store Transition
20
            char tra[][] = new char[5][5];
21
            String outer[]={"00","10","20","30",
22
                            "40","41","42","43",
23
                            "44","34","24","14",
24
                            "04","03","02","01"};
25
                            
26
            String inner[]={"11","21","31","32",
27
                            "33","23","13","12"};
28
            
29
            // 45-Degree rotation
30
            for(int i=0;i<5;i++){
31
                for(int j=0;j<5;j++){
32
                    // Transform outer portion
33
                    for(int k=0; k<outer.length; k++){
34
                        char indices[]=outer[k].toCharArray();
35
                        int a = Integer.parseInt(String.valueOf(indices[0]));
36
                        int b = Integer.parseInt(String.valueOf(indices[1]));
37
                        if(a==i && b==j){
38
                            if(k==15){k=1;}
39
                            else if(k==14){k=0;}
40
                            else {k+=2;}
41
                            indices=outer[k].toCharArray();
42
                            a = Integer.parseInt(String.valueOf(indices[0]));
43
                            b = Integer.parseInt(String.valueOf(indices[1]));
44
                            tra[a][b] = arr[i][j];
45
                            break;
46
                        }
47
                    }
48
                    // Transform inner portion
49
                    for(int k=0; k<inner.length; k++){
50
                        char indices[]=inner[k].toCharArray();
51
                        int a = Integer.parseInt(String.valueOf(indices[0]));
52
                        int b = Integer.parseInt(String.valueOf(indices[1]));
53
                        if(a==i && b==j){
54
                            if(k==7){k=0;}
55
                            else {k+=1;}
56
                            indices=inner[k].toCharArray();
57
                            a = Integer.parseInt(String.valueOf(indices[0]));
58
                            b = Integer.parseInt(String.valueOf(indices[1]));
59
                            tra[a][b] = arr[i][j];
60
                            break;
61
                        }
62
                    }
63
                    // Keeping center same
64
                    tra[2][2] = arr[2][2];
65
                }
66
            }
67
            // Print the transformed output 
68
            for(int i=0;i<5;i++){
69
                for(int j=0;j<5;j++){
70
                    System.out.print(tra[i][j]);
71
                }
72
                System.out.println();
73
            }
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
00100
00100
11111
00100
00100
10001\n
01010\n
00100\n
01010\n
10001
10001\n
01010\n
00100\n
01010\n
10001\n
Passed after ignoring Presentation Error






Week 9 : Programming Assignment 4

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

Problem statement:

A program needs to be developed which can mirror reflect any 5 × 5 2D character array into its side-by-side reflection. Write suitable code to achieve this transformation as shown below:

 INPUT:                                       OUTPUT:
               OOXOO                                        OOXOO
               OOXOO                                        OOXOO
               XXXOO                                        OOXXX
               OOOOO                                        OOOOO
               XOABC                                        CBAOX

Note the following points carefully
:
1. Here, instead of X and O any character may be present.

2. The input and output array size must be of dimension 5 × 5 and nothing else.
3. Only side-by-side reflection should be performed i.e. ABC || CBA.

Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Question94{
3
    public static void main(String args[]){
4
        Scanner sc = new Scanner(System.in);
5
// Declaring 5x5 2D char array to store input
6
        char original[][]= new char[5][5];
7
        
8
        // Declaring 5x5 2D char array to store reflection
9
        char reflection[][]= new char[5][5];
10
        
11
        // Input 2D Array using Scanner Class
12
        for(int line=0;line<5; line++){
13
            String input = sc.nextLine();
14
            char seq[] = input.toCharArray();
15
            if(seq.length==5){
16
                for(int i=0;i<5;i++){
17
                    original[line][i]=seq[i];
18
                }
19
            }
20
        }
21
        
22
        // Performing the reflection operation
23
        for(int i=0; i<5;i++){
24
            for(int j=0; j<5;j++){      
25
                reflection[i][j]=original[i][4-j];
26
            }
27
        }
28
        
29
        // Output the 2D Reflection Array
30
        for(int i=0; i<5;i++){
31
            for(int j=0; j<5;j++){      
32
                System.out.print(reflection[i][j]);
33
            }
34
            System.out.println();
35
        }
0
} // The main() method ends here
1
} // The main class end
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
OOXOO
OOXOO
XXXOO
OOOOO
XOABC
OOXOO\n
OOXOO\n
OOXXX\n
OOOOO\n
CBAOX
OOXOO\n
OOXOO\n
OOXXX\n
OOOOO\n
CBAOX\n
Passed after ignoring Presentation Error


Week 9 : Programming Assignment 5

Due on 2023-09-28, 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.

Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Question95{
3
    public static void main(String args[]){
4
        Scanner sc = new Scanner(System.in);
5
// Declare the 5X5 2D array to store the input
6
        char original[][]= new char[5][5];
7
        
8
        // Input 2D Array using Scanner Class and check data validity
9
        for(int line=0;line<5; line++){
10
            String input = sc.nextLine();
11
            char seq[] = input.toCharArray();
12
            if(seq.length==5){
13
                for(int i=0;i<5;i++){
14
                    if(seq[i]=='0' || seq[i]=='1'){
15
                        original[line][i]=seq[i];
16
                        if(line==4 && i==4)
17
                            flipflop(original);
18
                    }
19
                    else{
20
                        System.out.print("Only 0 and 1 supported.");
21
                        break;
22
                    }
23
                }
24
            }else{
25
                System.out.print("Invalid length");
26
                break;
27
            }
28
29
        }
30
    }
31
    static void flipflop(char[][] flip){
32
        // Flip-Flop Operation
33
        for(int i=0; i<5;i++){
34
            for(int j=0; j<5;j++){      
35
                if(flip[i][j]=='1')
36
                    flip[i][j]='0';
37
                else
38
                    flip[i][j]='1';
39
            }
40
        }
41
    
42
        // Output the 2D FlipFlopped Array
43
        for(int i=0; i<5;i++){
44
            for(int j=0; j<5;j++){      
45
                System.out.print(flip[i][j]);
46
            }
47
            System.out.println();
48
        }
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 10 : Programming Assignment 1

Due on 2023-10-05, 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.
Select the Language for this assignment. 
File name for this program : 
1
// Import required packages
2
import java.sql.*;
3
import java.lang.*;
0
public class Question101 {
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");
7
              
8
              // JDBC Codes in the hidden section
9
10
              // Open a connection
11
              conn = DriverManager.getConnection(DB_URL);
12
              System.out.print(conn.isValid(1));
13
              conn.close();
14
15
// JDBC Codes in the visible section
16
17
        }
18
       catch(Exception e){ System.out.println(e);}  
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
true
true
Passed




Week 10 : Programming Assignment 2

Due on 2023-10-05, 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 'isAlive(timeout)' method to generate the output, which is either 'true' or 'false'.

Note the following points carefully:
1. Name the connection object as 'conn' only.
2. Use timeout value as 1.

Your last recorded submission was on 2023-09-26, 17:38 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.sql.*;
2
import java.lang.*;
3
import java.util.Scanner;
4
public class Question102 {
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
// Open a connection
12
              conn = DriverManager.getConnection(DB_URL);
13
              System.out.print(conn.isValid(1));
14
// Private test case 
15
                Scanner sc = new Scanner(System.in);
16
                int s=sc.nextInt();
17
                if(s==1){
18
                    conn.close();
19
                    System.out.print(conn.isValid(1));
20
                }
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
0
true
true
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed




Week 10 : Programming Assignment 3

Due on 2023-10-05, 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.
Select the Language for this assignment. 
File name for this program : 
1
import java.sql.*;  // All sql classes are imported
2
import java.lang.*; // Semicolon is added
3
import java.util.Scanner;
4
public class Question103 {
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
              // Connection object is created
12
              conn = DriverManager.getConnection(DB_URL);
13
              conn.close();
14
              System.out.print(conn.isClosed());
15
       }
16
       catch(Exception e){ System.out.println(e);}  
17
    }
18
}
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
true
true
Passed


Week 10 : Programming Assignment 4

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

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

Column

UID

First_Name

Last_Name

Age

Type

Integer

Varchar (45)

Varchar (45)

Integer


Select the Language for this assignment. 
File name for this program : 
1
import java.sql.*;
2
import java.lang.*;
3
public class CreateTable {
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
            
11
              // Open a connection
12
              conn = DriverManager.getConnection(DB_URL);
13
              stmt = conn.createStatement();
14
// The statement containing SQL command to create table "players"
15
String CREATE_TABLE_SQL="CREATE TABLE players (UID INT, First_Name VARCHAR(45), Last_Name VARCHAR(45), Age INT);";
16
// Execute the statement containing SQL command
17
stmt.executeUpdate(CREATE_TABLE_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
NA
No. of columns : 4\n
Column 1 Name: UID\n
Column 1 Type : INT\n
Column 2 Name: First_Name\n
Column 2 Type : VARCHAR\n
Column 3 Name: Last_Name\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: First_Name\n
Column 2 Type : VARCHAR\n
Column 3 Name: Last_Name\n
Column 3 Type : VARCHAR\n
Column 4 Name: Age\n
Column 5 Type : INT\n
Passed after ignoring Presentation Error




Week 10 : Programming Assignment 5

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

Complete the code segment to rename an already created table named ‘PLAYERS’ into ‘SPORTS’.

Select the Language for this assignment. 
File name for this program : 
1
import java.sql.*;
2
import java.lang.*;
3
public class RenameTable {
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
            
11
              // Open a connection
12
              conn = DriverManager.getConnection(DB_URL);
13
              stmt = conn.createStatement();
14
~~~THERE IS SOME INVISIBLE CODE HERE~~~
15
// Write the SQL command to rename a table
16
String alter="ALTER TABLE players RENAME TO sports;";
17
18
// Execute the SQL command
19
stmt.executeUpdate(alter);
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
NA
No. of columns : 4\n
Column 1 Name: UID\n
Column 1 Type : INT\n
Column 2 Name: First_Name\n
Column 2 Type : VARCHAR\n
Column 3 Name: Last_Name\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: First_Name\n
Column 2 Type : VARCHAR\n
Column 3 Name: Last_Name\n
Column 3 Type : VARCHAR\n
Column 4 Name: Age\n
Column 5 Type : INT\n
Passed after ignoring Presentation Error



Week 11 : Programming Assignment 1

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

Complete the code segment to insert the following data using prepared statement in the existing table ‘PLAYERS.

Column

UID

First_Name

Last_Name

Age

Row 1

1

Ram

Gopal

26

Row 2

2

John

Mayer

22


Your last recorded submission was on 2023-10-07, 15:18 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.sql.*;
2
import java.lang.*;
3
public class InsertData {
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
            
11
              // Open a connection
12
              conn = DriverManager.getConnection(DB_URL);
13
              stmt = conn.createStatement();
14
~~~THERE IS SOME INVISIBLE CODE HERE~~~
15
String query = " insert into PLAYERS (UID, first_name, last_name, age)"  + " values (?, ?, ?, ?)";
16
17
            PreparedStatement preparedStmt = conn.prepareStatement(query);
18
            preparedStmt.setInt (1, 1);
19
            preparedStmt.setString (2, "Ram");
20
            preparedStmt.setString (3, "Gopal");
21
            preparedStmt.setInt(4, 26);
22
23
            preparedStmt.execute();
24
      
25
            preparedStmt.setInt (1, 2);
26
            preparedStmt.setString (2, "John");
27
            preparedStmt.setString (3, "Mayer");
28
            preparedStmt.setInt(4, 22);
29
30
            preparedStmt.execute();
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.
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 11 : Programming Assignment 2

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

Write the required code in order to update the following data in the table ‘PLAYERS.

Column

UID

First_Name

Last_Name

Age

From

1

Ram

Gopal

26

To

1

Rama

Gopala

24

Your last recorded submission was on 2023-10-07, 15:23 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.sql.*;
2
import java.lang.*;
3
public class UpdateData {
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
              String query="";
11
            
12
              // Open a connection
13
              conn = DriverManager.getConnection(DB_URL);
14
              stmt = conn.createStatement();
15
~~~THERE IS SOME INVISIBLE CODE HERE~~~
16
query = " UPDATE Players SET First_name ='Rama',Last_name = 'Gopala',Age = 24  WHERE UID=1;";
17
stmt.executeUpdate(query);
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.
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 11 : Programming Assignment 3

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

Write the appropriate code in order to delete the following data in the table ‘PLAYERS.

Column

UID

First_Name

Last_Name

Age

Delete

1

Rama

Gopala

24

Your last recorded submission was on 2023-10-07, 15:25 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.sql.*;
2
import java.lang.*;
3
public class DeleteData {
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
              String query="";
11
            
12
              // Open a connection
13
              conn = DriverManager.getConnection(DB_URL);
14
              stmt = conn.createStatement();
15
~~~THERE IS SOME INVISIBLE CODE HERE~~~
16
stmt.executeUpdate("DELETE FROM Players WHERE UID = 1;");
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.
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 11 : Programming Assignment 4

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

Complete the following program to calculate the average age of the players in the table ‘PLAYERS.

Structure of Table 'PLAYERS' is given below:

Column

UID

First_Name

Last_Name

Age

Type

Integer

Varchar (45)

Varchar (45)

Integer

Your last recorded submission was on 2023-10-07, 15:39 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.sql.*;
2
import java.lang.*;
3
public class CalAverage {
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
              String query="";
11
              // Open a connection
12
              conn = DriverManager.getConnection(DB_URL);
13
              stmt = conn.createStatement();
14
~~~THERE IS SOME INVISIBLE CODE HERE~~~
15
ResultSet rs = stmt.executeQuery("SELECT * FROM players;");
16
            int count=0,total=0;
17
            while(rs.next()){
18
                count++;
19
                total = total + Integer.parseInt(rs.getString(4));
20
            }
21
            
22
//Output
23
System.out.println("Average age of players is " +(total/count));
24
conn.close();
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.
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 11 : Programming Assignment 5

Due on 2023-10-12, 23:59 IST
Complete the code segment to drop the table named ‘PLAYERS.
Your last recorded submission was on 2023-10-07, 16:05 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.sql.*;
2
import java.lang.*;
3
public class DropTable {
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
              String query="";
11
            
12
              // Open a connection
13
              conn = DriverManager.getConnection(DB_URL);
14
              stmt = conn.createStatement();
15
~~~THERE IS SOME INVISIBLE CODE HERE~~~
16
// Write the SQL command to drop a table
17
    query = "DROP TABLE players;";
18
19
// Execute the SQL command to drop a table
20
    stmt.executeUpdate(query);
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.
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed





Week 12 : Programming Assignment 1

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

Complete the code to develop an extended version of the ADVANCED CALCULATOR with added special functions that emulates all the functions of the GUI Calculator as shown in the image.

Note the following points carefully:
1. Use only double datatype to store all numeric values.
2. Each button on the calculator should be operated by typing the characters from 'a' to 't'.
3. You may use the already defined function 
gui_map(char).
4. Use predefined methods from java.lang.Math class wherever applicable.
5. Without '=' binary operations won't give output as shown in Input_3 and Output_3 example below.
5. The calculator should be able to perform required operations on one or two operands as shown in the below example:


Input_1:
                   okhid

Output_1:
               
100.0

Input_2:
                   ia

Output_2:

                        2.0


Your last recorded submission was on 2023-10-07, 21:38 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Question92{
3
    public static void main(String args[]){
4
        Scanner sc = new Scanner(System.in);
5
        String input = sc.nextLine();
6
         char seq[] = input.toCharArray();
7
        int outflag=0;
8
        
9
        for(int i=0; i<seq.length; i++){
10
            seq[i]=gui_map(seq[i]);
11
           if (seq[i]=='R' || seq[i]=='S' || seq[i]=='F' ||seq[i]=='C')
12
                break;
13
        }
14
        //Print Mapped GUI (remove comment to see the mapped sequence input)
15
        /*
16
        for(int i=0; i<seq.length; i++){
17
            System.out.print(seq[i]);
18
        }
19
        */
20
        // Use double type of values for entire calculation
21
        double operand1=0.0;
22
        String o1="";
23
        double operand2=0.0;
24
        String o2="";
25
        double output=0.0;
26
// Write code below(sample code is given for hint. Edit it )
27
// Perform calculaton operations
28
        outerloop:
29
        for(int i=0; i<seq.length; i++){
30
            if(seq[i]=='C'){                //Clear
31
                operand1=0.0;
32
                operand2=0.0;
33
                output=0.0;
34
                outflag=0;
35
                break outerloop;
36
            }else if(seq[i]=='R'){          //Square Root
37
                for(int j=0; j<i; j++){
38
                    o1+=Character.toString(seq[j]);
39
                }
40
                operand1=Double.parseDouble(o1);
41
                output=Math.sqrt(operand1);
42
                outflag=1;
43
                break outerloop;
44
            }
45
            else if(seq[i]=='S'){           //Square
46
                for(int j=0; j<i; j++){
47
                    o1+=Character.toString(seq[j]);
48
                }
49
                operand1=Double.parseDouble(o1);
50
                output=Math.pow(operand1,2);
51
                outflag=1;
52
                break outerloop;
53
            }else if(seq[i]=='F'){          //Inverse
54
                for(int j=0; j<i; j++){
55
                    o1+=Character.toString(seq[j]);
56
                }
57
                operand1=Double.parseDouble(o1);
58
                output=Math.pow(operand1,-1);
59
                outflag=1;
60
                break outerloop;
61
            }else{
62
                int r=0;
63
                         if(seq[i]=='+'||seq[i]=='/'||seq[i]=='*'||seq[i]=='='){
64
                    for(int j=0; j<i; j++){
65
                        o1+=Character.toString(seq[j]);
66
                }
67
            operand1=Double.parseDouble(o1);
68
            for(int k=i+1; k<seq.length; k++){
69
             if(seq[k]=='='){
70
71
                  outflag=1;
72
                           
73
                  operand2=Double.parseDouble(o2);
74
             if(seq[i]=='+'){
75
                                     
76
                         output=operand1+operand2;
77
                }else if(seq[i]=='-'){
78
             output=operand1-operand2;
79
                }else if(seq[i]=='/'){
80
                                              
81
                       output=operand1/operand2;
82
                }else if(seq[i]=='*'){
83
                                            
84
                      output=operand1*operand2;
85
                 }
86
                break outerloop;
87
                }else{
88
            o2+=Character.toString(seq[k]);
89
                        }
90
                    }
91
                }
92
            }
93
        }
0
// Check if output is available and print the output
1
        if(outflag==1)
2
            System.out.print(output);
3
4
5
}// The main() method ends here.
6
    
7
// A method that takes a character as input and returns the corresponding GUI character
8
    static char gui_map(char in){
9
        char out = 'N';// N = Null/Empty
10
        char gm[][]={{'a','R'}
11
                    ,{'b','0'}
12
                    ,{'c','.'}
13
                    ,{'d','='}
14
                    ,{'e','1'}
15
                    ,{'f','2'}
16
                    ,{'g','3'}
17
                    ,{'h','+'}
18
                    ,{'i','4'}
19
                    ,{'j','5'}
20
                    ,{'k','6'}
21
                    ,{'l','-'}
22
                    ,{'m','7'}
23
                    ,{'n','8'}
24
                    ,{'o','9'}
25
                    ,{'p','*'}
26
                    ,{'q','S'}
27
                    ,{'r','F'}
28
                    ,{'s','C'}
29
                    ,{'t','/'}};
30
                    
31
                    /* R = Square root
32
                    C = Clear/Restart
33
                    F = Fraction
34
                    S = Square
35
                    */
36
                    
37
        // Checking for maps
38
        for(int i=0; i<gm.length; i++){
39
            if(gm[i][0]==in){
40
                out=gm[i][1];
41
                break;
42
            }
43
        }
44
        return out;
45
    }
46
}
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
okhid
100.0
100.0
Passed
Test Case 2
ia
2.0
2.0
Passed



Week 12 : Programming Assignment 2

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

A partial code fragment is given. The URL class object is created in try block.You should write appropriate method( )  to print the protocol name and host name from the given url string.
For example:

https://www.xyz.com:1080/index.htm

 

protocol://host:port/filename

 

Select the Language for this assignment. 
File name for this program : 
1
import java.net.*;  
2
public class Question2{  
3
   public static void main(String[] args){
4
try{  
5
     URL url=new URL("http://www.Nptel.com/java-tutorial");    
6
    //use appropriate code to print the protocol name and host name from url 
7
    
8
     System.out.println("Protocol: "+url.getProtocol());  
9
     System.out.print("Host Name: "+url.getHost());
10
 
11
      }
12
   catch(Exception e){System.out.println(e);}  
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
Protocol: http\n
Host Name: www.Nptel.com
Protocol: http\n
Host Name: www.Nptel.com
Passed


Week 12 : Programming Assignment 3

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

Write a program to create a record by taking inputs using Scanner class as first name as string ,last name as string ,roll number as integer ,subject1 mark as float,subject2 mark as float. Your program should print in the format 
  "name  rollnumber avgmark".

For example:
input:

 

ram

das

123

25.5

24.5
output:


ramdas 123 25.0

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
      Scanner s1 = new Scanner(System.in);
5
//Read your first name
6
    String f = s1.next();
7
    //Read your last name
8
    String l = s1.next();
9
    //Read rollnumber
10
    int n = s1.nextInt();
11
    //Read 1st subject mark
12
       double db = s1.nextDouble();
13
    //Read 2nd subject mark
14
    double db1 = s1.nextDouble();
15
    double avg=(db+db1)/2;
16
    System.out.println(f + l +" "+ n +" "+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
ram
das
123
25.5
24.5
ramdas 123 25.0
ramdas 123 25.0\n
Passed after ignoring Presentation Error


Week 12 : Programming Assignment 4

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

A program code is given to call the parent class static method and instance method in derive class without creating object of parent class. You should write the appropriate code so that the program print the contents of static method() and instance method () of parent class.

Select the Language for this assignment. 
File name for this program : 
1
class Parent {
2
    public static void testClassMethod() {
3
        System.out.println("The static method.");
4
    }
5
    public void testInstanceMethod() {
6
        System.out.println("The instance method.");
7
    }
8
}
9
public class Child extends Parent {
10
   public static void testClassMethod() { }
11
public static void main(String[] args) {
12
        
13
        
14
     // Call the instance method in the Parent class 
15
    Child c= new Child();
16
    c.testInstanceMethod();
17
        
18
     // Call the static method in the Parent class
19
    Parent.testClassMethod();
20
    
21
    //Parent ob=c;
22
   //ob.testClassMethod();
23
  }
24
}
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
The instance method.\n
The static method.
The instance method.\n
The static method.\n
Passed after ignoring Presentation Error
Test Case 2
NA
The instance method.\n
The static method.
The instance method.\n
The static method.\n
Passed after ignoring Presentation Error


Week 12 : Programming Assignment 5

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

Write a recursive function to print the sum of  first n odd integer numbers. The recursive function should have the prototype
 " int sum_odd_n(int n) ".

For example :

input : 
5
output: 
25 

input 
: 6
output : 
36

Select the Language for this assignment. 
File name for this program : 
1
import java.util.*;
2
public class Question5 { 
3
    static int sum_odd_n(int n){ 
4
          if(n==1)
5
              return 1;
6
           if (n <= 0) 
7
                return 0;
8
//Call the method recursively.
9
return 2*n-1 + sum_odd_n(n-1);
0
}
1
   public static void main(String[] args) {  
2
      Scanner in=new Scanner(System.in);
3
      int count=in.nextInt();      
4
      int r=sum_odd_n(count);
5
      System.out.println(r);      
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
5
25
25\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.