Menu


NPTEL - Programming in Java - Week 09 : Programming Assignments Answers- 2025




Programming in Java
NPTEL Week 09 : Programming Assignments Answers- 2025

Week 09 : Programming Assignment 9 - Programming In Java

Week 09 : Programming Assignments - NPTEL >> Programming In Java - 2025


Week 09 : Programming Assignment 1

Solution :


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

		// Answer
		int[][] matrix = new int[5][5];       
for (int i = 0; i < 5; i++) {
            String line;
            while (true) {
                line = sc.nextLine().trim();
                if (line.length() == 5 && line.matches("[01]{5}")) {
                    break;
                } 
            }

            for (int j = 0; j < 5; j++) {
                matrix[i][j] = line.charAt(j) - '0'; // Convert char to int
            }
        }
        // Flip-Flop Operation (Invert the matrix)
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                matrix[i][j] = 1 - matrix[i][j];
            }
        }

        // Output the flipped matrix
               for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(matrix[i][j]);
            }
            System.out.println();
        }

        sc.close();
		//End
		

	}
    }


Week 09 : Programming Assignment 2

Solution :


import java.util.Scanner;
public class W09_P2{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		String input = sc.nextLine(); // Read as string, e.g., 5+6

		// Answer
		input = input.trim(); // Read full expression

        int operand1 = 0, operand2 = 0;
        char operator = ' ';
        int result = 0;

        // Parse the input
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
                operator = ch;
                operand1 = Integer.parseInt(input.substring(0, i).trim());
                operand2 = Integer.parseInt(input.substring(i + 1).trim());
                break;
            }
        }

        // Perform the operation
        switch (operator) {
            case '+':
                result = operand1 + operand2;
                break;
            case '-':
                result = operand1 - operand2;
                break;
            case '*':
                result = operand1 * operand2;
                break;
            case '/':
                if (operand2 != 0) {
                    result = operand1 / operand2;
                } else {
                    System.out.println("Error: Division by zero!");
                    return;
                }
                break;
            default:
                System.out.println("Invalid operator.");
                return;
        }

        // Output the result
        System.out.print(input + " = " + result);

        sc.close();
		//End
		

		}
    }


Week 09 : Programming Assignment 3

Solution :


import java.util.Scanner;

class SquareThread extends Thread {
    private int begin;
    private int end;

		// Answer
		public SquareThread(int begin,int end) {
    this.begin = begin;
        this.end = end;
    }
// HINT: Some keyword should be added below in the _____ and the program should run correctly
  @Override public void run() {
        // print the square of each number from begin to end
        // if begin is greater than end, 
        // print the square of each number in reverse order from end to begin
if (begin > end) {
            for (int i = begin; i >= end; i--) {
                System.out.println(i * i);
            }
        }
        else{
            for (int i = begin; i <= end; i++) {
                System.out.println(i * i);
            }
        }

}
			// End
		

	  }

public class W09_P3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        //System.out.print("Enter the begin for square calculation: ");
        int begin = scanner.nextInt();
        //System.out.print("Enter the end for square calculation: ");
        int end = scanner.nextInt();
        scanner.close();

        SquareThread thread1 = new SquareThread(begin, end);
        SquareThread thread2 = new SquareThread(end, begin);

        thread1.start();
        thread2.start();
    }
}


Week 09 : Programming Assignment 4

Solution :


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

		// ANSWER
		 //Use appropriate try...catch block to complete the code  
		try {
            InputStreamReader r = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(r);
            String number = br.readLine();
            int x = Integer.parseInt(number);
            System.out.print(x * x);
        } catch (IOException | NumberFormatException e) {
            System.out.print("Please enter valid data");
        }
		// END

	}
}


Week 09 : Programming Assignment 5

Solution :


import java.util.Scanner;

public class W09_P5{
            
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        double x1 = sc.nextDouble();
        double y1 = sc.nextDouble();
        double x2 = sc.nextDouble();
        double y2 = sc.nextDouble();
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        
        System.out.print(p1.distance(p2));
    }

}
		

	//ANSWER
	//Complete the code segment to define a class Point with parameter x,y and method distance()for calculating distance between two points.
// Note: Pass objectsof type class Point as argument in distance() method.
class Point {
    private double x;
    private double y;

    // Constructor to initialize x and y
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    // Method to calculate distance from another point
    public double distance(Point p2) {
        double dx = this.x - p2.x;
        double dy = this.y - p2.y;
        return Math.sqrt(dx * dx + dy * dy);
    }
}
		//END
		


Relevant blogs :

NPTEL - Programming in Java - QUIZ : Week 12:Assignment 12 Answers- 2025

NPTEL - Programming in Java - Week 12 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 11:Assignment 11 Answers- 2025

NPTEL - Programming in Java - Week 11 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 10:Assignment 10 Answers- 2025

NPTEL - Programming in Java - Week 10 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 9:Assignment 9 Answers- 2025

NPTEL - Programming in Java - Week 09 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 8:Assignment 8 Answers- 2025

NPTEL - Programming in Java - Week 08 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 7:Assignment 7 Answers- 2025

NPTEL - Programming in Java - Week 07 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 6:Assignment 6 Answers- 2025

NPTEL - Programming in Java - Week 06 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 5:Assignment 5 Answers- 2025

NPTEL - Programming in Java - Week 05 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 4:Assignment 4 Answers- 2025

NPTEL - Programming in Java - Week 04 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 3:Assignment 3 Answers- 2025

NPTEL - Programming in Java - Week 03 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 2:Assignment 2 Answers- 2025

NPTEL - Programming in Java - Week 02 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 1:Assignment 1 Answers- 2025

NPTEL - Programming in Java - Week 01 : Programming Assignments Answers- 2025



Other blogs :

NPTEL - Introduction to Programming in C - Week 7:Assignment 7 Answers- 2025

NPTEL - Introduction to Programming in C - Week 6:Assignment 6 Answers- 2025

NPTEL - Introduction to Programming in C - Week 5:Assignment 5 Answers- 2025

NPTEL - Introduction to Programming in C - Week 4:Assignment 4 Answers- 2025

NPTEL - Introduction to Programming in C - Week 3:Assignment 3 Answers- 2025

NPTEL - Introduction to Programming in C - Week 2:Assignment 2 Answers- 2025

NPTEL - Introduction to Programming in C - Week 1:Assignment 1 Answers- 2025