NPTEL Programming in Java Jan 2024 Week 9 Programming Assignment

 


Week 9 : Programming Assignment 1

Due on 2024-03-28, 23:59 IST
Write a Java program that utilizes multithreading to calculate and print the squares of numbers from a specified begin to a specified end.
The main method is already created.
You need to design a SquareThread class that has two members,
  • int begin;
  • int end;
Each thread should sequentially print the squares of numbers from begin to end (both inclusive).
The same code will be used to create another thread that prints the sqaure of numbers from end to begin in reverse order.
(if begin is greater than end, print the square of each number in reverse order first)
The main method will first call SquareThread with begin and end and then in reverse order.
The class you create should be able to handle such case and print as required in the correct order.
HINT: use the keyword `synchronized` in the run method.
Your last recorded submission was on 2024-03-18, 17:40 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
class SquareThread extends Thread {
    private int begin;
    private int end;
SquareThread(int begin, int end){
  this.begin = begin;
  this.end = end;
}
 
synchronized public void run() {
        // print the square of each number from begin to end
       if( begin <= end )
        for (int i=begin; i<=end; i++)
          System.out.println(i*i);  
        // if begin is greater than end, 
        // print the square of each number in reverse order from end to begin
       else
        for (int i=begin; i>=end; i--)
          System.out.println(i*i);
}
}
 
public class W09_P1 {
    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();
    }
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1
5
1\n
4\n
9\n
16\n
25\n
25\n
16\n
9\n
4\n
1
1\n
4\n
9\n
16\n
25\n
25\n
16\n
9\n
4\n
1\n
Passed after ignoring Presentation Error
Test Case 2
9
6
81\n
64\n
49\n
36\n
36\n
49\n
64\n
81
81\n
64\n
49\n
36\n
36\n
49\n
64\n
81\n
Passed after ignoring Presentation Error


Week 9 : Programming Assignment 2

Due on 2024-03-28, 23:59 IST

Complete the code segment to catch the exception in the following, if any.

On the occurrence of such an exception, your program should print

§ Please enter valid data

If there is no such exception, it will print the square of the number entered.

Your last recorded submission was on 2024-03-18, 19:12 IST
Select the Language for this assignment. 
File name for this program : 
import java.io.*;  
class W09_P2{  
        public static void main(String args[]){
try{
  java.util.Scanner r=new java.util.Scanner(System.in);  
  String number=r.nextLine();
  int x = Integer.parseInt(number);
  System.out.print(x*x);
}
catch( Exception e )
{
  System.out.print("Please enter valid data");
}
}
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
4
4
Passed
Test Case 2
q
Please enter valid data
Please enter valid data
Passed


Week 9 : Programming Assignment 3

Due on 2024-03-28, 23:59 IST

The program given below stores characters in a byte array named byte_array, which means ‘A’ is stored as 65.

Your task is the following:

§  Given a user input `n`, print the output in the given format, if `n` = 1, print:

o   byte_array[1] = ‘P’

§ If the value of n is negative or is out of bound, then use TRY_CATCH to print the following:

o   Array index is out of range

Your last recorded submission was on 2024-03-18, 11:51 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.*;
 
public class W09_P3 {
    public static void main(String[] args) {
        try {
            byte byte_array[] = {
                'N', 'P', 'T', 'E', 'L', ' ', 
                '-', ' ', 
                'P', 'R', 'O', 'G', 'R', 'A', 'M', 'M', 'I', 'N', 'G', ' ', 
                'I', 'N', ' ', 
                'J', 'A', 'V', 'A'};
            Scanner inr = new Scanner(System.in);
            int n = inr.nextInt();
            inr.close();
// print the required output
 
System.out.print("byte_array[" + n + "] = '" + (char)byte_array[n] + "'");
}
// close try and add a catch block for index out of bounds
catch (IndexOutOfBoundsException e2) {
      System.out.print("Array index is out of range");
    }
catch (Exception e) {
            System.out.println("Exception occurred");
        }
    }
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
0
byte_array[0] = 'N'
byte_array[0] = 'N'
Passed
Test Case 2
-1
Array index is out of range
Array index is out of range
Passed


Week 9 : Programming Assignment 4

Due on 2024-03-28, 23:59 IST

Define a class Point with members

§ private double x;

§ private double y;

and methods:

§ public Point(double x, double y){}  // Constructor to create a new point?

§ public double distance(Point p2){} // Function to return the distance of this Point from another Point

Your last recorded submission was on 2024-03-18, 11:23 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
public class W09_P4{
            
    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));
    }
 
}
//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;
  
  public Point(double x, double y){ //Constructor to initialize a Shape object  
    this.x = x;
    this.y= y;
  }
 
  public double distance(Point p2){
    double d;
    d=Math.sqrt((p2.x-x)*(p2.x-x) + (p2.y-y)*(p2.y-y));
    return d;
  }
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
0 0
1 1
1.4142135623730951
1.4142135623730951
Passed
Test Case 2
0 0
0 5
5.0
5.0
Passed


Week 9 : Programming Assignment 5

Due on 2024-03-28, 23:59 IST

Complete the code below with a catch statement to print the following if the denominator (b) is zero

§ Cannot Divide by ZERO

Your last recorded submission was on 2024-03-18, 10:12 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
public class W09_P5 {
 
    public static void main(String[] args) {
        int a, b;
        Scanner input = new Scanner(System.in);
        // Read any two values for a and b.
        // Get the result of a/b;
        int result;
 
        a = input.nextInt();
        b = input.nextInt();
        input.close();
        // try block to divide two numbers and display the result
        try {
            result = a / b;
            System.out.println(result);
        }
// catch block to catch the Error
catch (ArithmeticException e) {
             System.out.print("Cannot Divide by ZERO");
          }
catch (Exception e) {
            System.out.println("Exception Occoured");
}
}
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
6 3
2
2\n
Passed after ignoring Presentation Error
Test Case 2
1 0
Cannot Divide by ZERO
Cannot Divide by ZERO
Passed

No comments

File Uploading in PHP

  File Uploading in PHP PHP allow you to upload any type of a file i.e. image, binary or text files.etc..,PHP has one in built global variab...