NPTEL Programming In Java Programming Assignment week-9 September-2023 Swayam

 

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 : 
import java.util.Scanner;
public class Question91{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine(); // Read as string, e.g., 5+6
// Declare and initialize the required variable(s)
        int i=0;
        int j=0;
        double output=0;
        // Split the input string into character array
        char seq[] = input.toCharArray();
        /*
        Use some method to separate the two operands
        and then perform the required operation.
        */
        for(int a=0; a<seq.length; a++){
            if(seq[a]=='+'){
                i= Integer.parseInt(input.substring(0,a));
                j= Integer.parseInt(input.substring(a+1,seq.length));
                output = (double)i+j;
            }else if(seq[a]=='-'){
                i= Integer.parseInt(input.substring(0,a));
                j= Integer.parseInt(input.substring(a+1,seq.length));
                output = (double)i-j;
            }else if(seq[a]=='/'){
                i= Integer.parseInt(input.substring(0,a));
                j= Integer.parseInt(input.substring(a+1,seq.length));
                output = (double)i/j;
            }else if(seq[a]=='*'){
                i= Integer.parseInt(input.substring(0,a));
                j= Integer.parseInt(input.substring(a+1,seq.length));
                output = (double)i*j;
            }
        }
        // Print the output as stated in the question
        System.out.print(input+" = " + Math.round(output));
}
}
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 : 
import java.util.Scanner;
public class Question92{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
char seq[] = input.toCharArray();
        int outflag=0;
        
        // Start the mapping process for each input character
        for(int i=0; i<seq.length; i++){
            seq[i]=gui_map(seq[i]);
        }
        
        //Print Mapped GUI (remove comment to see the mapped sequence input)
        /*
        for(int i=0; i<seq.length; i++){
            System.out.print(seq[i]);
        }
        */
        
        // Use double type of values for entire calculation
        double operand1=0.0;
        String o1="";
        double operand2=0.0;
        String o2="";
        double output=0.0;
        
        // Perform calculaton operations
        outerloop:
        for(int i=0; i<seq.length; i++){
            int r=0;
            if(seq[i]=='+'||seq[i]=='-'||seq[i]=='/'||seq[i]=='X'||seq[i]=='='){
                for(int j=0; j<i; j++){
                    o1+=Character.toString(seq[j]);
                }
                operand1=Double.parseDouble(o1);
                for(int k=i+1; k<seq.length; k++){
                    if(seq[k]=='='){
                        outflag=1;
                        operand2=Double.parseDouble(o2);
                        if(seq[i]=='+'){
                            output=operand1+operand2;
                        }else if(seq[i]=='-'){
                            output=operand1-operand2;
                        }else if(seq[i]=='/'){
                            output=operand1/operand2;
                        }else if(seq[i]=='X'){
                            output=operand1*operand2;
                        }
                        break outerloop;
                    }else{
                        o2+=Character.toString(seq[k]);
                    }
                }
            }
        }
        
        // Check if output is available and print the output
        if(outflag==1)
            System.out.print(output);
}// The main() method ends here.
    
// A method that takes a character as input and returns the corresponding GUI character 
    static char gui_map(char in){
        char out = 'N';// N = Null/Empty
        char gm[][]={{'a','.'}
                    ,{'b','0'}
                    ,{'c','='}
                    ,{'d','+'}
                    ,{'e','1'}
                    ,{'f','2'}
                    ,{'g','3'}
                    ,{'h','-'}
                    ,{'i','4'}
                    ,{'j','5'}
                    ,{'k','6'}
                    ,{'l','X'}
                    ,{'m','7'}
                    ,{'n','8'}
                    ,{'o','9'}
                    ,{'p','/'}};
                    
        // Checking for maps
        for(int i=0; i<gm.length; i++){
            if(gm[i][0]==in){
                out=gm[i][1];
                break;
            }
        }
        return out;
    }
}
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 : 
import java.util.Scanner;
public class Question93{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
char arr[][]= new char[5][5];
            // Input 2D Array using Scanner Class
            for(int line=0;line<5; line++){
                String input = sc.nextLine();
                char seq[] = input.toCharArray();
                if(seq.length==5){
                    for(int i=0;i<5;i++){
                        arr[line][i]=seq[i];
                    }
                }else{
                    System.out.print("Wrong Input!");
                    System.exit(0);
                }
            }
            // Declaring the array to store Transition
            char tra[][] = new char[5][5];
            String outer[]={"00","10","20","30",
                            "40","41","42","43",
                            "44","34","24","14",
                            "04","03","02","01"};
                            
            String inner[]={"11","21","31","32",
                            "33","23","13","12"};
            
            // 45-Degree rotation
            for(int i=0;i<5;i++){
                for(int j=0;j<5;j++){
                    // Transform outer portion
                    for(int k=0; k<outer.length; k++){
                        char indices[]=outer[k].toCharArray();
                        int a = Integer.parseInt(String.valueOf(indices[0]));
                        int b = Integer.parseInt(String.valueOf(indices[1]));
                        if(a==i && b==j){
                            if(k==15){k=1;}
                            else if(k==14){k=0;}
                            else {k+=2;}
                            indices=outer[k].toCharArray();
                            a = Integer.parseInt(String.valueOf(indices[0]));
                            b = Integer.parseInt(String.valueOf(indices[1]));
                            tra[a][b] = arr[i][j];
                            break;
                        }
                    }
                    // Transform inner portion
                    for(int k=0; k<inner.length; k++){
                        char indices[]=inner[k].toCharArray();
                        int a = Integer.parseInt(String.valueOf(indices[0]));
                        int b = Integer.parseInt(String.valueOf(indices[1]));
                        if(a==i && b==j){
                            if(k==7){k=0;}
                            else {k+=1;}
                            indices=inner[k].toCharArray();
                            a = Integer.parseInt(String.valueOf(indices[0]));
                            b = Integer.parseInt(String.valueOf(indices[1]));
                            tra[a][b] = arr[i][j];
                            break;
                        }
                    }
                    // Keeping center same
                    tra[2][2] = arr[2][2];
                }
            }
            // Print the transformed output 
            for(int i=0;i<5;i++){
                for(int j=0;j<5;j++){
                    System.out.print(tra[i][j]);
                }
                System.out.println();
            }
} // The main() method ends here
} // 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 : 
import java.util.Scanner;
public class Question94{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
// Declaring 5x5 2D char array to store input
        char original[][]= new char[5][5];
        
        // Declaring 5x5 2D char array to store reflection
        char reflection[][]= new char[5][5];
        
        // Input 2D Array using Scanner Class
        for(int line=0;line<5; line++){
            String input = sc.nextLine();
            char seq[] = input.toCharArray();
            if(seq.length==5){
                for(int i=0;i<5;i++){
                    original[line][i]=seq[i];
                }
            }
        }
        
        // Performing the reflection operation
        for(int i=0; i<5;i++){
            for(int j=0; j<5;j++){      
                reflection[i][j]=original[i][4-j];
            }
        }
        
        // Output the 2D Reflection Array
        for(int i=0; i<5;i++){
            for(int j=0; j<5;j++){      
                System.out.print(reflection[i][j]);
            }
            System.out.println();
        }
} // The main() method ends here
} // 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 : 
import java.util.Scanner;
public class Question95{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
// Declare the 5X5 2D array to store the input
        char original[][]= new char[5][5];
        
        // Input 2D Array using Scanner Class and check data validity
        for(int line=0;line<5; line++){
            String input = sc.nextLine();
            char seq[] = input.toCharArray();
            if(seq.length==5){
                for(int i=0;i<5;i++){
                    if(seq[i]=='0' || seq[i]=='1'){
                        original[line][i]=seq[i];
                        if(line==4 && i==4)
                            flipflop(original);
                    }
                    else{
                        System.out.print("Only 0 and 1 supported.");
                        break;
                    }
                }
            }else{
                System.out.print("Invalid length");
                break;
            }
 
        }
    }
    static void flipflop(char[][] flip){
        // Flip-Flop Operation
        for(int i=0; i<5;i++){
            for(int j=0; j<5;j++){      
                if(flip[i][j]=='1')
                    flip[i][j]='0';
                else
                    flip[i][j]='1';
            }
        }
    
        // Output the 2D FlipFlopped Array
        for(int i=0; i<5;i++){
            for(int j=0; j<5;j++){      
                System.out.print(flip[i][j]);
            }
            System.out.println();
        }
} // The main() ends here
} // 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




No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...