Programming in Java | NPTEL 2022 | Week 4 Programming Assignments Solutions

This article will help you with the answers of Programming in Java NPTEL 2022 Week 4 Programming Assignment Solutions.

Programming in Java NPTEL Week 4 Programming Assignment 1 Solutions

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.

// Import the required package(s) and/or class(es)
import java.io.*;
import java.util.*;
import static java.lang.System.*;


// main class Question is created
public class Question41{
  public static void main(String[] args) {
	// Scanner object is created
    Scanner scanner = new Scanner(System.in);
     // Read String input using scanner class
    String courseName = scanner.nextLine(); 
     // Print the scanned String
    out.println("Course: " + courseName); 
  }
}

Programming Assignment 2 Solution

Complete the code segment to print the current year. Your code should compile successfully.

// The following is the declaration of the main class named Question42
public class Question42 { 
	public static void main(String args[]){
		int year; // integer type variable to store year


                // Create an object of Calendar class. 
                java.util.Calendar cal_obj;

		// Use getInstance() method to initialize the Calendar object.
                cal_obj = java.util.Calendar.getInstance();
	
		// Initialize the int variable year with the current year
                year = cal_obj.get(cal_obj.YEAR);


                // Print the current Year
		System.out.println("Current Year: "+year);

         }
}

Programming Assignment 3 Solution

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.

interface ExtraLarge{
	String extra = "This is extra-large";
	public static void display();
}

class Large {
    public void Print() {
        System.out.println("This is large")
    }
}
 
class Medium extends Large {
    public void Print() {
    	  super.super.Print();  
        System.out.println("This is medium");
    }
}
class Small extends Medium {
    public void Print() {
        super.Print(); ; 
        System.out.println("This is small");
    }
  }
}
 
class Question43{
    public static void main(String[] args) {
        Small s = new Small();
        s.Print();
	  Question43 q = new Question43();
	  q.display();
    }
}

Correct Code:

interface ExtraLarge{
	String extra = "This is extra-large";
	void display();
}

class Large {
    public void Print() {
        System.out.println("This is large");
    }
}
 
class Medium extends Large {
    public void Print() {
    	  super.Print();  
        System.out.println("This is medium");
    }
}
class Small extends Medium {
    public void Print() {
        super.Print(); ; 
        System.out.println("This is small");
    }
  }

 
class Question43 implements ExtraLarge{
    public static void main(String[] args) {
        Small s = new Small();
        s.Print();
	  Question43 q = new Question43();
	  q.display();
      
    }
    public void display(){
        System.out.print(extra);
    }
}

Week 4: Programming Assignment 4 Solution

Complete the code segment to call the default method in the interface First and Second.

interface First{ 
    // default method 
    default void show(){ 
        System.out.println("Default method implementation of First interface."); 
    } 
} 
  
interface Second{ 
    // Default method 
    default void show(){ 
        System.out.println("Default method implementation of Second interface."); 
    } 
} 
  
// Implementation class code 
class Question44 implements First, Second{ 
    // Overriding default show method 
    public void show(){


// Call show() of First interface.
First.super.show();

// Call show() of Second interface.
Second.super.show();


 } 
  
    public static void main(String args[]){ 
        Question44 q = new Question44(); 
        q.show(); 
    } 
}

Week 4: Programming Assignment 5 Solution

Modify the code segment to print the following output.
—————–OUTPUT——————-
Circle: This is Shape1
Circle: This is Shape2
————————————————-

// Interface ShapeX is created
interface ShapeX {
 public String base = "This is Shape1";
 public void display1();
}

// Interface ShapeY is created which extends ShapeX
interface ShapeY extends ShapeX {
 public String base = "This is Shape2";
 public void display2();
}

// Class ShapeG is created which implements ShapeY
class ShapeG implements ShapeY {
 public String base = "This is Shape3";
 //Overriding method in ShapeX interface
 public void display1() {
  System.out.println("Circle: " + ShapeX.base);
 }
 // Override method in ShapeY interface
  public void display2(){
  System.out.print("Circle: " + ShapeY.base);
  }
}


// Main class Question 
public class Question45{
 public static void main(String[] args) {
  //Object of class shapeG is created and display methods are called.
  ShapeG circle = new ShapeG();
  circle.display1();
  circle.display2();
 }
}

The above question set contains all the correct answers. But in any case, you find any typographical, grammatical or any other error in our site then kindly inform us. Don’t forget to provide the appropriate URL along with error description. So that we can easily correct it.

Thanks in advance.

For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.

Leave a Comment

Your email address will not be published. Required fields are marked *