Menu


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




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

Week 07 : Programming Assignment 7 - Programming In Java

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


Week 07 : Programming Assignment 1

Solution :


import java.util.Scanner;

public class W07_P1 {
		

		// Answer
		//// Method to find the longest word in a given text
		public static String findLongestWord(String text) {
		String longestWord = "";
    
		// Split the text into words based on whitespace
		String[] words = text.split("\\s+");
    
		// Iterate through each word to find the longest one
		for (String word : words) {
        if (word.length() > longestWord.length()) {
				longestWord = word;
			}
		}
    		return longestWord;
		}
		//End
		

	public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Prompt user to enter text
       // System.out.println("Enter some text:");
        String text = scanner.nextLine();

        // Close the scanner
        scanner.close();

        // Call the method to find the longest word
        String longestWord = findLongestWord(text);

        // Print the longest word found
        System.out.println("The longest word in the text is: " + longestWord);
    }
}



Week 07 : Programming Assignment 2

Solution :


import java.util.*;

public class W07_P2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
            // Input array
      //  System.out.print("Enter the number of elements in the array: ");
        int n = scanner.nextInt();
        int[] array = new int[n];

       // System.out.println("Enter the elements of the array:");
        for (int i = 0; i < n; i++) {
            array[i] = scanner.nextInt();
        }

        // Element to remove
    //    System.out.print("Enter the element to remove: ");
        int elementToRemove = scanner.nextInt();

        // Close the scanner
        scanner.close();

        // Removing element and printing result
        System.out.println("Original Array: " + Arrays.toString(array));
        array = removeAll(array, elementToRemove);
        System.out.print("Array after removing " + elementToRemove + ": " + Arrays.toString(array));
    }

		// Answer
		// program to remove all occurrences of an element from array in Java.
	public static int[] removeAll(int[] array, int elementToRemove) {
    int[] result = new int[array.length];
    int index = 0;

    // Iterate through the original array
    for (int value : array) {
        // Copy only if the current element is not equal to the element to be removed
        if (value != elementToRemove) {
            result[index++] = value;
        }
    }

    // If the resulting array is smaller than the original, resize it
    return Arrays.copyOf(result, index);
}

		//End
		

	
}



Week 07 : Programming Assignment 3

Solution :


import java.util.Scanner;

public class W07_P3{


		// Answer
		//Code to create function primesum(), compute the sum of all prime numbers in a given range.
		public static boolean isPrime(int n) {
        if (n < 2) return false;
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) return false;
        }
        return true;
    }

    // Function to compute the sum of primes in the range [x, y]
    public static int primeSum(int x, int y) {
        int sum = 0;
        for (int i = x; i <= y; i++) {
            if (isPrime(i)) {
                sum += i;
            }
        }
        return sum;
    }

			// End
		

	  public static void main(String[] args)
	{
       Scanner sc = new Scanner(System.in);
	   int x=sc.nextInt();
	   int y=sc.nextInt();
		
	    System.out.println(primeSum(x, y));
	}
}


Week 07 : Programming Assignment 4

Solution :


import java.util.Scanner;
class PrintNumbers implements Runnable {

		// ANSWER
		//Create a constructor of this class that takes two private parameters (start and end) and initializes the instance variables with the provided values.
	private int start;
    private int end;

    // Constructor to initialize start and end
    public PrintNumbers(int start, int end) {
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {
        for (int i = start; i <= end; i += 2) {
            System.out.println(Thread.currentThread().getName() + ": " + i);
        }
    }
		// END

	}
class W07_P4{
    //Code to create two threads, one printing even numbers and the other printing odd numbers
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //System.out.print("Enter the starting value for even numbers: ");
        int evenStart = scanner.nextInt();
       // System.out.print("Enter the ending value for even numbers: ");
        int evenEnd = scanner.nextInt();
       // System.out.print("Enter the starting value for odd numbers: ");
        int oddStart = scanner.nextInt();
       // System.out.print("Enter the ending value for odd numbers: ");
        int oddEnd = scanner.nextInt();
        Thread evenThread = new Thread(new PrintNumbers(evenStart, evenEnd), "EvenThread");
        Thread oddThread = new Thread(new PrintNumbers(oddStart, oddEnd), "OddThread");

        evenThread.start();
try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        oddThread.start();
        scanner.close();
    }
}


Week 07 : Programming Assignment 5

Solution :


import java.util.Scanner;

public class W07_P5 {
    private String password;

    // Step 1: Constructor to initialize the password variable
    public W07_P5(String password) { 
        this.password = password; // Assign the passed password to the instance variable
    }
    // ================================
    // Note: Try solving it without hints first-only check if you're truly stuck.
    // Avoid AI or internet searches; quick answers won't build real skills.
    // Struggle a bit, learn for life! Be honest with yourself!
    //
		

	//ANSWER
	public boolean isValidPassword(String password) {
    // Step 1: Check if the password length is at least 8 characters
    if (password.length() < 8) {
        return false;
    }

    // Flags to track conditions
    boolean hasUpperCase = false;
    boolean hasDigit = false;

    // Step 2: Loop through each character in the password
    for (char ch : password.toCharArray()) {
        if (Character.isUpperCase(ch)) {
            hasUpperCase = true; // Found an uppercase letter
        }
        if (Character.isDigit(ch)) {
            hasDigit = true; // Found a number
        }

        // If both conditions are met, return true early
        if (hasUpperCase && hasDigit) {
            return true;
        }
    }

    // Step 3: If either condition is not met, return false
    return false;
}

		//END
		

	public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // Read password input from user
        String inputPassword = scanner.nextLine();
        scanner.close();
        W07_P5 validator = new W07_P5(inputPassword);
        
        // Check password validity and print result
        if (validator.isValidPassword(inputPassword)) {
            System.out.print("Valid Password");
        } else {
            System.out.print("Invalid Password");
        }
  
        scanner.close();
    }
  }


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