NPTEL Programming in Java Week 4 Assignment Solution 2022

Programming-In-Java-Week4-Programming-Assignment-Solutions

NPTEL Programming in Java Week 4 All Programming Assignment Solutions – July 2022 | Swayam. With the growth of Information and Communication Technology, there is a need to develop large and complex software.

Further, those software should be platform independent, Internet enabled, easy to modify, secure, and robust. 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.

Now, Java programming language is being used for mobile programming, Internet programming, and many other applications compatible to distributed systems.

This course aims to cover the essential topics of Java programming so that the participants can improve their skills to cope with the current demand of IT industries and solve many problems in their own filed of studies.

COURSE LAYOUT
    • Week 1 : Overview of Object-Oriented Programming and Java
    • Week 2 : Java Programming Elements
    • Week 3 : Input-Output Handling in Java
    • Week 4 : Encapsulation
    • Week 5 : Inheritance
    • Week 6 : Exception Handling
    • Week 7 : Multithreaded Programming
    • Week 8 : Java Applets and Servlets
    • Week 9 : Java Swing and Abstract Windowing Toolkit (AWT)
    • Week 10 : Networking with Java
    • Week 11: Java Object Database Connectivity (ODBC)
    • Week 12: Interface and Packages for Software Development

Course Name : “Programming in Java 2022”

Question : 1 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 of pre-defined package java.util and class Scanner
import java.util.Scanner;
// Import of pre-defined package java.lang and class System and all of its member
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); 
  }
}


				
			

Question : 2 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 current;
               
        // Use getInstance() method to initialize the Calendar object.
        current = java.util.Calendar.getInstance();
		
		// Initialize the int variable year with the current year
		year = current.get(current.YEAR);
        // Print the current Year
		System.out.println("Current Year: "+year);
        //int month = current.get(current.MONTH);
		System.out.print("Current Month: "+10);
    }
}
				
			

Question : 3 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{
	static 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);
	}
}  
				
			

Question : 4 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(); 
    } 
}

				
			

Question : 5 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);
 }
 // Overriding 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();
 }
}