NPTEL » Programming in Java Week 07 : Programming Assignment 1 2 3 4 and 5

 


Week 07 : Programming Assignment 1

Due on 2024-09-12, 23:59 IST
Write a Java program to find longest word in given input.
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
public class W07_P1 {
//code for print the longest word.
public static String findLongestWord(String text) {
  String longest = "";
  String[] arrOfStr = text.split(" ");
  for (String a : arrOfStr)
      if ( a.length() >= longest.length() )
        longest = a;
  return longest;
}
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.print("The longest word in the text is: " + longestWord);
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
Believe in yourself
The longest word in the text is: yourself
The longest word in the text is: yourself
Passed



Week 07 : Programming Assignment 2

Due on 2024-09-12, 23:59 IST

Write a program to print Swastika Pattern in Java. 

Input is 2 numbers. 

R

(Rows and Columns)

 

For Example:

 

Input:

5

5

 

Output:

* ***

* *

*****

  * *

*** *

 

NOTE: Do not print any spaces between the ‘*’

Output should match exactly as specified by the question

(Remember to match the output given exactly, including the spaces and new lines)

(passed with presentation error means you will get full marks)


Your last recorded submission was on 2024-09-01, 14:00 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;  
class W07_P2  
{  
    // main() method start  
    public static void main (String[] args)  
    {  
        int rows, cols;  
        Scanner sc = new Scanner(System.in);  
     //   System.out.println("Please enter odd numbers for rows and colums to get perfect Swastika.");  
       // System.out.println("Enter total rows");  
        rows = sc.nextInt();  
       //System.out.println("Enter total colums");  
        cols = sc.nextInt();  
          
        // close Scanner class  
        sc.close();  
          
        // call swastika() method that will design Swastika for the specified rows and cols  
        swastika(rows, cols);  
    }  
static void swastika(int rows, int cols)  
{
// program to print Swastika Pattern in Java.
// NOTE: Do not print any spaces between the '*'
// Output should match exactly as specified by the question
int rmid = rows/2 +1;
int cmid = cols/2 +1;
for ( int i = 1; i <= rows ; i++)
{
  for ( int j = 1; j <= cols ; j++ )
  {
    if ( i == rmid )
      System.out.print("*");
    else if ( j == cmid )
      System.out.print("*");
    else if ( j == 1 && i < rmid || j == cols && i > rmid)
      System.out.print("*");
    else if ( i == 1 && j > cmid || i == rows && j < cmid )
      System.out.print("*");
    else if ( i > 1 && i < rmid && j > cmid )
      continue;
    else
      System.out.print(" ");
  }
  System.out.print("\n");
}
}
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
7
9
*   *****\n
*   *\n
*   *\n
*********\n
    *   *\n
    *   *\n
*****   *
*   *****\n
*   *\n
*   *\n
*********\n
    *   *\n
    *   *\n
*****   *\n
Passed after ignoring Presentation Error



Week 07 : Programming Assignment 3

Due on 2024-09-12, 23:59 IST

Write a program to remove all occurrences of an element from array in Java.

Your last recorded submission was on 2024-09-01, 13:39 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.*;
 
public class W07_P3 {
 
    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));
    }
// program to remove all occurrences of an element from array in Java.
static int[] removeAll(int[] arr, int elementToRemove){
  int count = 0;
  for(int i=0; i<arr.length; i++)
    if(arr[i]==elementToRemove)
      count++;
  int new_arr[] = new int[arr.length-count];
  int j = 0;
  for(int i=0; i<arr.length; i++)
    if(arr[i]!=elementToRemove)
      new_arr[j++] = arr[i];
  return new_arr;
  }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
12 23 34 23 45  
23
Original Array: [12, 23, 34, 23, 45]\n
Array after removing 23: [12, 34, 45]
Original Array: [12, 23, 34, 23, 45]\n
Array after removing 23: [12, 34, 45]
Passed



Week 07 : Programming Assignment 4

Due on 2024-09-12, 23:59 IST

Write a program to compute the sum of all prime numbers in a given range.
The range value will be positive.

Follow the naming convention as given in the main method of the suffix code.

Your last recorded submission was on 2024-09-01, 13:10 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
public class W07_P4{
//Code to create function primesum(), compute the sum of all prime numbers in a given range.
public static int primeSum(int x, int y){
  int sum = 0;
  for(int n=x;n<=y;n++){
    int r = 1;
    for(int i=n/2;i>1;i--){
      r=n%i;
      if(r==0)
        break;
    }
    if(r!=0)
      sum+=n;
  }
  return sum;
}
public static void main(String[] args)
    {
       Scanner sc = new Scanner(System.in);
       int x=sc.nextInt();
       int y=sc.nextInt();
        
        System.out.print(primeSum(x, y));
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4
13
36
36
Passed



Week 07 : Programming Assignment 5

Due on 2024-09-12, 23:59 IST

Code to create two threads, one printing even numbers and the other printing odd numbers.

§ The PrintNumbers class is declared, and it implements the Runnable interface. This interface is part of Java's concurrency support and is used to represent a task that can be executed concurrently by a thread.

§ Create a constructor of this class that takes two private instance variables (start and end) to represent the range of numbers that will be printed by the thread.

§ Create a run method that is required by the Runnable interface and contains the code that will be executed when the thread is started. In this case, it should prints odd numbers within the specified range (start to end) using a for loop.

§ Hint: Thread.currentThread().getName() returns the name of the currently executing thread, which is useful for identifying which thread is printing the numbers.

Follow the naming convention as given in the main method of the suffix code.

(Remember to match the output given exactly, including the spaces and new lines)

(passed with presentation error means you will get full marks)

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
class PrintNumbers implements Runnable {
//Create a constructor of this class that takes two private parameters (start and end) and initializes the instance variables with the provided values.
//Create Run() method
int start;
int end;
 
PrintNumbers(int start, int end){
  this.start = start;
  this.end = end;
}
 
@Override
public void run() {
  for (int i = start; i <= end; i= i+2) {
    System.out.println(Thread.currentThread().getName() + ": " + i);
  }
}
}
class W07_P5{
    //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();
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
10
3
7
EvenThread: 2\n
EvenThread: 4\n
EvenThread: 6\n
EvenThread: 8\n
EvenThread: 10\n
OddThread: 3\n
OddThread: 5\n
OddThread: 7
EvenThread: 2\n
EvenThread: 4\n
EvenThread: 6\n
EvenThread: 8\n
EvenThread: 10\n
OddThread: 3\n
OddThread: 5\n
OddThread: 7\n
Passed after ignoring Presentation Error



No comments

VR23 Web Technologies Lab MCA II Year I Semester Experiment 7:

  Aim: PHP script to a) Find the length of a string. b) Count no of words in a string. c) Reverse a string. d) Search for a specific string....