NPTEL Programming in Java Jan 2024 Week-10

 


Week 10 : Programming Assignment 1

Program to sort the elements of an array in ascending order. Follow the naming convention as given in the main method of the suffix code.
Your last recorded submission was on 2024-03-25, 08:55 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
class W10_P1
{
   public static void printArray(int[] array)
    {
        // Iterating using for loops
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
       // System.out.println();
    }
//code to sort the elements of an array in ascending order.
public static void sortArray(int[] array)
{
  for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array.length-i-1; j++) {
      if(array[j]>array[j+1])
      {
        int tmp = array[j];
        array[j] = array[j+1];
        array[j+1] = tmp;
      }
    }
  }
}
public static void main(String... a)
    {        
        Scanner s = new Scanner(System.in);
        int size = s.nextInt();
        int[] array = new int[size];
        int i;
        for (i = 0; i < size; i++) {
            array[i] = s.nextInt();
        }
        sortArray(array);
// Displaying elements of array after sorting
         System.out.println(
            "Elements of array sorted in ascending order:");
        printArray(array);
 
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
6
-5 -9 8 12 1 3
Elements of array sorted in ascending order:\n
-9 -5 1 3 8 12
Elements of array sorted in ascending order:\n
-9 -5 1 3 8 12 
Passed after ignoring Presentation Error



Week 10 : Programming Assignment 2

Due on 2024-04-04, 23:59 IST
Print a given matrix in spiral form. Follow the naming convention as given in the main method of the suffix code.
Your last recorded submission was on 2024-03-25, 15:15 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.*;
 
public class W10_P2{
//code to print a given matrix in spiral form.
public static void spiral_method(int [][] a, int dimension1, int dimension2)
{
  int k = 0;
  int l = 0;
  
  while(k<dimension1 && l<dimension2){
    
    for (int i = l; i < dimension2 ; i++)
      System.out.print(a[k][i] + " ");
    
    k++;
    
    for (int i = k; i < dimension1 ; i++)
      System.out.print(a[i][dimension2-1] + " ");
    
    dimension2--;
    
    if(k<dimension1){
      for (int i = dimension2-1; i > l-1 ; i--)
      System.out.print(a[dimension1-1][i] + " ");
    
    dimension1--;
    }
    
    if(l<dimension2){
      for (int i = dimension1-1; i > k-1 ; i--)
      System.out.print(a[i][l] + " ");
    
    l++;
    }
  }
}
public static void main(String args[]) {
        int i, j;
        // double sum = 0, square = 0, result = 0;
        Scanner s = new Scanner(System.in);
        int dimension = s.nextInt();
 
        int[][] spiral = new int[dimension][dimension];
        for (i = 0; i < dimension ; i++) {
            // loop for columns
            for (j = 0; j < dimension ; j++) {
                // reads the matrix elements
                spiral[i][j] = s.nextInt();
            }
        }
        spiral_method(spiral, dimension,dimension);
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
6 3 2 1 4
7 8 3 12 9
1 11 10 5 8
5 18 6 9 14
19 23 3 12 11
6 3 2 1 4 9 8 14 11 12 3 23 19 5 1 7 8 3 12 5 9 6 18 11 10
6 3 2 1 4 9 8 14 11 12 3 23 19 5 1 7 8 3 12 5 9 6 18 11 10 
Passed after ignoring Presentation Error






Week 10 : Programming Assignment 3

Due on 2024-04-04, 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.
  • Hints: 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.
Your last recorded submission was on 2024-03-25, 10:52 IST
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 W10_P3{
    //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



Week 10 : Programming Assignment 4

Due on 2024-04-04, 23:59 IST
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-03-25, 11:12 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
public class W10_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.println(primeSum(x, y));
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4
13
36
36\n
Passed after ignoring Presentation Error


Week 10 : Programming Assignment 5

Due on 2024-04-04, 23:59 IST
Write a program that converts temperatures between Celsius and Fahrenheit. Implement two methods, celsiusToFahrenheit and fahrenheitToCelsius, to perform the conversions.
  • Use exception handling to handle invalid input temperatures (if  celsius < -273.15 and fahrenheit < -459.67 , program should throw exception error).
  • The TemperatureConverter class contains two methods for temperature conversion (celsiusToFahrenheit and fahrenheitToCelsius).
  • The TemperatureException class is a custom exception class that extends Exception and is used to handle invalid temperatures.
  • The TemperatureConverterApp class is the main class that takes user input for temperatures and handles potential exceptions during the conversion process.
  • celsiusToFahrenheit = (celsius * 9 / 5) + 32
  • fahrenheitToCelsius = (fahrenheit - 32) * 5 / 9
Follow the naming convention as given in the main method of the suffix code.
Your last recorded submission was on 2024-03-25, 11:35 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
class TemperatureException extends Exception {
    public TemperatureException(String message) {
        super(message);
    }
}
 
class TemperatureConverter {
//Code to create two function for temperature conversion (celsiusToFahrenheit and fahrenheitToCelsius).
public static double celsiusToFahrenheit(double celsius) throws TemperatureException {
  if ( celsius < -273.15 )
    throw new TemperatureException("Invalid Celsius temperature (below absolute zero)");
  else
    return (celsius * 9 / 5) + 32;
}
 
public static double fahrenheitToCelsius (double fahrenheitInput) throws TemperatureException {
  if ( fahrenheitInput < -459.67 )
    throw new TemperatureException("Invalid Fahrenheit temperature (below absolute zero)");
  else
    return (fahrenheitInput - 32) * 5 / 9;
}
 
}
public class W10_P5{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        try {
            //System.out.print("Enter temperature in Celsius: ");
            double celsius = scanner.nextDouble();
            double fahrenheit = TemperatureConverter.celsiusToFahrenheit(celsius);
            System.out.println("Temperature in Fahrenheit: " + fahrenheit);
 
          //  System.out.print("Enter temperature in Fahrenheit: ");
            double fahrenheitInput = scanner.nextDouble();
            double celsiusOutput = TemperatureConverter.fahrenheitToCelsius(fahrenheitInput);
            System.out.println("Temperature in Celsius: " + celsiusOutput);
        } catch (TemperatureException e) {
            System.out.println("Error: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("Error: Invalid input");
        } finally {
            scanner.close();
        }
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
-278
Error: Invalid Celsius temperature (below absolute zero)
Error: Invalid Celsius temperature (below absolute zero)\n
Passed after ignoring Presentation Error
Test Case 2
-1
-500
Temperature in Fahrenheit: 30.2\n
Error: Invalid Fahrenheit temperature (below absolute zero)
Temperature in Fahrenheit: 30.2\n
Error: Invalid Fahrenheit temperature (below absolute zero)\n
Passed after ignoring Presentation Error
Test Case 3
45
112
Temperature in Fahrenheit: 113.0\n
Temperature in Celsius: 44.44444444444444
Temperature in Fahrenheit: 113.0\n
Temperature in Celsius: 44.44444444444444\n
Passed after ignoring Presentation Error

No comments

Solutions to JavaScript Exercises

Solutions to JavaScript Exercises Solutions to JavaScript Exercises Exercise 1 solution - Click Here Exercise 2 solution - Click H...