NPTEL » Programming In Java Assignment week 12 Sep-2020

NPTEL Programming In Java

NPTEL Programming in Java Week 12,NPTEL Java Assignment week 12

Join Our Official Telegram Channel

With the growth of Information and Communication Technology, there is a need to develop large and complex software. To meet this requirement object-oriented paradigm has been developed and based on this paradigm the Java programming language emerges as the best programming environment.

Programming in Java is a MOOC based course which is 12 weeks in duration and can fulfill the criteria of 4 credits in a year. You can visit the NPTEL SWAYAM platform and register yourself for the course. This course is brought to you by Prof. Dr. Debasis Samanta who holds a Ph.D. in Computer Science and Engineering from the Indian Institute of Technology Kharagpur.

Programming In Java 2020 Details:-

  1. Who Can Join: The undergraduate students from the engineering disciplines namely CSE, IT, EE, ECE, etc. might be interested in this course.
  2. Requirements/Prerequsites: The course requires that the students are familiar with a programming language such as C/C++ and data structures, algorithms.
  3. INDUSTRY SUPPORT:   All IT companies.

Java Assignment Week 12 Answers:-

Java Week 12: Q1. – Complete the code to develop an extended version of the ADVANCED CALCULATOR with added special functions that emulate all the functions of the GUI Calculator as shown in the image.


Answer/Code:-

import java.util.Scanner;

public class Question121{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		String input = sc.nextLine();

// Write code below
        char sumit[]=input.toCharArray(); 
int outflag=0; 
double sumitnce=0.0,lifeline=0.0;
String o1="" ,o2=""; 
double nce=0.0;

for(int i=0; i<sumit.length; i++)
	sumit[i]=gui_map(sumit[i]);

outerloop:
	for (int i=0; i<sumit.length; i++)
	{
  		int r=0;
  		if(sumit[i]=='+'||sumit[i]=='-'||sumit[i]=='/'||sumit[i]=='X'||sumit[i]=='=')
  		{
   		 for (int j=0; j<i; j++)
      		o1+=Character.toString(sumit[j]);
    		sumitnce=Double.parseDouble(o1);
    	 for(int k=i+1; k<sumit.length; k++)
    	{
      	   if(sumit[k]=='=')
      		{
        	outflag=1;
        	lifeline=Double.parseDouble(o2);
        	if(sumit[i]=='+')
        		nce=sumitnce+lifeline;
        	else if(sumit[i]=='-')
       		  nce=sumitnce-lifeline;
       	    else if(sumit[i]=='/')
         	 nce=sumitnce/lifeline;
        	else if(sumit[i]=='X')
         	 nce=sumitnce*lifeline;
        	break outerloop;
      }
      else
        o2+=Character.toString(sumit[k]);
    }
 }
 else if(sumit[i]=='R'||sumit[i]=='S'||sumit[i]=='F')
 {
   for (int j=0;j<i;j++)
     o1+=Character.toString(sumit[j]);
     sumitnce=Double.parseDouble(o1);
   		if(sumit[i]=='R')
          System.out.print(Math.sqrt(sumitnce));
   		else if(sumit[i]=='S')
          System.out.print(sumitnce*sumitnce);
        else if(sumit[i]=='F')
          System.out.print(1/sumitnce);
    }
}
	if(outflag==1)
      System.out.print(nce);
      
  }// 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;
	}
}

ALSO CHECK:-

Amazon Quiz Answers 25 November – Win ₹5,000 Flipkart Fake or Not Fake Quiz Answers – 25 Nov 2020 – Win Vouchers

Java Week 12: Q2 – 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.

Answer/Code:-

import java.net.*;  
public class Question122{  
   public static void main(String[] args){  

   
     try{  
     URL url=new URL("http://www.Nptel.com/java-tutorial");    
    //use appropriate method() to print the protocol name and host name from url 
    //Write the method() by replacing the blank space.
     System.out.println("Protocol: "+url.getProtocol());  
     System.out.println("Host Name: "+url.getHost());
 
      }
   catch(Exception e){System.out.println(e);}  
   }  
} 
 

Java Week 12: Q3 – 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”.

Answer/code:-

import java.util.*;
public class Question123{
  public static void main(String[] args){
      Scanner s1 = new Scanner(System.in);

     String sumitnce=s1.nextLine();
String nce=s1.nextLine();

int roll=s1.nextInt();

double s1nce=s1.nextDouble();
double s2nce=s1.nextDouble();

double avg=(s1nce+s2nce)/2;

System.out.print(sumitnce+nce+" "+roll+" "+avg);

   
    }
}

NPTEL » Programming In Java Assignment week 10 Sep-2020

NPTEL » Programming In Java Assignment week 6 Sep-2020

Java Week 12: Q4A 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.

Answer/Code:-

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) {
        
     Child nce=new Child(); 
            nce.testInstanceMethod();
            Parent.testClassMethod();
  }
}

Java Week 12: Q5 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) “.

Answer/Code:-

import java.util.*;
public class Question125 { 
    static int sum_odd_n(int n){ 
          if(n==1)
              return 1;
           if (n <= 0) 
                return 0;

//Call the method recursively.int nce=n,sumit=0;
 
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); 	  
    }  
}

Find Other Quiz Here:

Amazon Fashion Quiz Answers & Win ₹1000

Amazon Great Indian Festival Quiz Answers: Win Rs. 50,000

NPTEL » Programming In Java Assignment week 6 Sep-2020

NPTEL » Programming In Java Assignment week 7 Sep-2020

NOTE: These codes are based on our knowledge. Answers might be incorrect, we suggest you to not the copy-paste answers blindly.