NPTEL Course (Programming in Java): Week 4 Programming Assignment Solutions

Assignment 1:

  • Problem: Read a line of input and print it with a prefix.
  • Solution:
      import java.util.Scanner;
import static java.lang.System.*;

public class Main{
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String courseName = scanner.nextLine(); 
    out.println("Course: " + courseName); 
  }
}
    

Assignment 2:

  • Problem: Print the current year using the Calendar class.
  • Solution:
      public class Main{ 
    public static void main(String args[]){
        int year;     
        java.util.Calendar current; 
        current = java.util.Calendar.getInstance();
        year = current.get(java.util.Calendar.YEAR);
        System.out.println("Current Year: "+year);
    } 
}
    

Assignment 3:

  • Problem: Debug a program to print specific output lines in a particular order, making use of inheritance.
  • Solution:
      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");
    }
}
public class Main{

    public static void main(String[] args) {
      Small s = new Small();
      s.Print();
        ExtraLarge xl = new ExtraLarge() {
            @Override
            public void display() {
                System.out.println(extra);
            }
        };
        xl.display();
  }
}
    

Assignment 4:

  • Problem: Implement a class that inherits from two interfaces with default methods and call those default methods.
  • Solution:
      interface First{ 
    default void show(){ 
        System.out.println("Default method implementation of First interface."); 
    } 
} 

interface Second{ 
    default void show(){ 
        System.out.println("Default method implementation of Second interface."); 
    } 
} 

class Main implements First, Second{ 
    public void show(){
        First.super.show();
        Second.super.show();
    } 

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

Assignment 5:

  • Problem: Correctly access interface variables from a class implementing another interface that extends the first one.
  • Solution:
      // Interface ShapeX is created
interface ShapeX {
    String base = "This is Shape1";
    void display1();
}

// Interface ShapeY is created which extends ShapeX
interface ShapeY extends ShapeX {
    String base = "This is Shape2";
    void display2();
}
//the code in this cell is sample code --please edit this to perform as asked in the question


// 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.println("Circle: " + ShapeY.base);
    }
}
public class Main{
    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();
    }
}
    

Disclaimer: These solutions are provided for educational purposes. Please use them responsibly and make sure you understand the underlying concepts. Always attempt to solve the problems yourself before referring to the solutions.

Leave a Comment

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

Scroll to Top