NPTEL Java Programming Week 12 : Programming Assignment 1-5

 

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 : 
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;
        
        for(int i=0; i<seq.length; i++){
            seq[i]=gui_map(seq[i]);
           if (seq[i]=='R' || seq[i]=='S' || seq[i]=='F' ||seq[i]=='C')
                break;
        }
        //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;
// Write code below(sample code is given for hint. Edit it )
// Perform calculaton operations
        outerloop:
        for(int i=0; i<seq.length; i++){
            if(seq[i]=='C'){                //Clear
                operand1=0.0;
                operand2=0.0;
                output=0.0;
                outflag=0;
                break outerloop;
            }else if(seq[i]=='R'){          //Square Root
                for(int j=0; j<i; j++){
                    o1+=Character.toString(seq[j]);
                }
                operand1=Double.parseDouble(o1);
                output=Math.sqrt(operand1);
                outflag=1;
                break outerloop;
            }
            else if(seq[i]=='S'){           //Square
                for(int j=0; j<i; j++){
                    o1+=Character.toString(seq[j]);
                }
                operand1=Double.parseDouble(o1);
                output=Math.pow(operand1,2);
                outflag=1;
                break outerloop;
            }else if(seq[i]=='F'){          //Inverse
                for(int j=0; j<i; j++){
                    o1+=Character.toString(seq[j]);
                }
                operand1=Double.parseDouble(o1);
                output=Math.pow(operand1,-1);
                outflag=1;
                break outerloop;
            }else{
                int r=0;
                         if(seq[i]=='+'||seq[i]=='/'||seq[i]=='*'||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]=='*'){
                                            
                      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','R'}
                    ,{'b','0'}
                    ,{'c','.'}
                    ,{'d','='}
                    ,{'e','1'}
                    ,{'f','2'}
                    ,{'g','3'}
                    ,{'h','+'}
                    ,{'i','4'}
                    ,{'j','5'}
                    ,{'k','6'}
                    ,{'l','-'}
                    ,{'m','7'}
                    ,{'n','8'}
                    ,{'o','9'}
                    ,{'p','*'}
                    ,{'q','S'}
                    ,{'r','F'}
                    ,{'s','C'}
                    ,{'t','/'}};
                    
                    /* R = Square root
                    C = Clear/Restart
                    F = Fraction
                    S = Square
                    */
                    
        // Checking for maps
        for(int i=0; i<gm.length; i++){
            if(gm[i][0]==in){
                out=gm[i][1];
                break;
            }
        }
        return out;
    }
}
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 : 
import java.net.*;  
public class Question2{  
   public static void main(String[] args){
try{  
     URL url=new URL("http://www.Nptel.com/java-tutorial");    
    //use appropriate code to print the protocol name and host name from url 
    
     System.out.println("Protocol: "+url.getProtocol());  
     System.out.print("Host Name: "+url.getHost());
 
      }
   catch(Exception e){System.out.println(e);}  
   }  
}
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 : 
import java.util.*;
public class Question3{
  public static void main(String[] args){
      Scanner s1 = new Scanner(System.in);
//Read your first name
    String f = s1.next();
    //Read your last name
    String l = s1.next();
    //Read rollnumber
    int n = s1.nextInt();
    //Read 1st subject mark
       double db = s1.nextDouble();
    //Read 2nd subject mark
    double db1 = s1.nextDouble();
    double avg=(db+db1)/2;
    System.out.println(f + l +" "+ n +" "+avg );
}
}
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 : 
class Parent {
    public static void testClassMethod() {
        System.out.println("The static method.");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method.");
    }
}
public class Child extends Parent {
   public static void testClassMethod() { }
public static void main(String[] args) {
        
        
     // Call the instance method in the Parent class 
    Child c= new Child();
    c.testInstanceMethod();
        
     // Call the static method in the Parent class
    Parent.testClassMethod();
    
    //Parent ob=c;
   //ob.testClassMethod();
  }
}
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 : 
import java.util.*;
public class Question5 { 
    static int sum_odd_n(int n){ 
          if(n==1)
              return 1;
           if (n <= 0) 
                return 0;
//Call the method recursively.
return 2*n-1 + sum_odd_n(n-1);
}
   public static void main(String[] args) {  
      Scanner in=new Scanner(System.in);
      int count=in.nextInt();      
      int r=sum_odd_n(count);
      System.out.println(r);      
    }  
}
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

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...