Skip to the content.

Programming_In_Java


WEEK 1

Java Week 1:Q1 To find the perimeter and area of a circle given a value of radius.

Java Week 1:Q2 To find the largest among three numbers x, y, and z.

Java Week 1:Q3 Consider First n even numbers starting from zero(0) and calculate sum of all the numbers divisible by 3 from 0 to n. Print the sum.

Java Week 1:Q4 To check whether the number is an Armstrong number or not.

Java Week 1:Q5 To help Ram , find the highest mark and average mark secured by him in “s” number of subjects.

WEEK 2

Java Week 2:Q1 To call the method print() in class Student following the concept of inner class.

Java Week 2:Q2 To call the method print() of class Student first and then call print() method of class School.

Java Week 2:Q3 To call print() method of class que by creating a method named ‘studentMethod()’.

Java Week 2:Q4 To call default constructor first and then any other constructor in the class Answer.

Java Week 2:Q5 To debug the program which is intended to print ‘NPTEL JAVA’.

WEEK 3

Java Week 3:Q1 To the generation of Fibonacci numbers.

Java Week 3:Q2 Define a class Point with two fields x and y each of type double. Also , define a method distance(Point p1, Point p2) to calculate the distance between points p1 and p2 and return the value in double. Complete the code segment given below. Use Math.sqrt( ) to calculate the square root.

Java Week 3:Q3 A class Shape is defined with two overloading constructors in it. Another class Test1 is partially defined which inherits the class Shape. The class Test1 should include two overloading constructors as appropriate for some object instantiation shown in main( ) method. You should define the constructors using the super class constructors. Also, override the method calculate( ) in Test1 to calculate the volume of a Shape.

Java Week 3:Q4 This program to exercise the call of static and non-static methods. A partial code is given defining two methods, namely sum( ) and multiply ( ). You have to call these methods to find the sum and product of two numbers.

Java Week 3:Q5 To swap two numbers using call by object reference.

WEEK 4

Java Week 4:Q1 To execute the following program successfully. You should import the correct package(s) and/or class(s) to complete the code.

Java Week 4:Q2 To print the current year.

Java Week 4:Q3 The program in this assignment is attempted to print the following output:

-----------------OUTPUT-------------------
 This is large
 This is medium
 This is small
 This is extra-large
------------------------------------------

Java Week 4:Q4 To call the default method in the interface First and Second.

Java Week 4:Q5 To print the following output.

-----------------OUTPUT--------------
Circle: This is Shape1
Circle: This is Shape2
-------------------------------------

WEEK 5

Java Week 5:Q1 An interface Number is defined in the following program. You have to declare a class A, which will implement the interface Number. Note that the method findSqr(n) will return the square of the number n.

Java Week 5:Q2 This program is to find the GCD (greatest common divisor) of two integers writing a recursive function findGCD(n1,n2). Your function should return -1, if the argument(s) is(are) other than positive number(s).

Java Week 5:Q3 Complete the code segment to catch the ArithmeticException in the following, if any. On the occurrence of such an exception, your program should print “Exception caught: Division by zero.” If there is no such exception, it will print the result of division operation on two integer values.

Java Week 5:Q4 In the following program, an array of integer data to be initialized. During the initialization, if a user enters a value other than integer value, then it will throw InputMismatchException exception. On the occurrence of such an exception, your program should print “You entered bad data.” If there is no such exception it will print the total sum of the array.

Java Week 5:Q5 In the following program, there may be multiple exceptions. You have to complete the code using only one try-catch block to handle all the possible exceptions.

For example, if user’s input is 1, then it will throw and catch “java.lang.NullPointerException“.

WEEK 6

Java Week 6:Q1 Complete the code segment to print the following using the concept of extending the Thread class in Java:

-----------------OUTPUT-------------------
Thread is Running.

Java Week 6:Q2 In the following program, a thread class ThreadRun is created using the Runnable interface which prints “Thread using Runnable interface”. Complete the main class to create a thread object of the class ThreadRun and run the thread,

Java Week 6:Q3 A part of the Java program is given, which can be completed in many ways, for example using the concept of thread, etc. Follow the given code and complete the program so that your program prints the message “NPTEL Java”. Your program should utilize the given interface/ class.

Java Week 6:Q4 Execution of two or more threads occurs in a random order. The keyword ‘synchronized’ in Java is used to control the execution of thread in a strict sequence. In the following, the program is expected to print some numbers. Do the necessary use of ‘synchronized’ keyword, so that, the program prints the output in the following order:

-----------------OUTPUT-------------------
5
10
15
20
25
100
200
300
400
500

Java Week 6:Q5 Add necessary codes to print the following:

-----------------OUTPUT-------------------
Name of thread 't':Thread-0
New name of thread 't':NPTEL
Thread is running.

WEEK 7

Java Week 7:Q1 Complete the following code fragment to read three integer values from the keyboard and find the sum of the values. Declare a variable “sum” of type int and store the result in it.

Java Week 7:Q2 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”.

Java Week 7:Q3 A byte char array is initialized. You have to enter an index value”n”. According to index your program will print the byte and its corresponding char value. Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “exception occur” .If there is no such exception, it will print the required output.

Java Week 7:Q4 The following program reads a string from the keyboard and is stored in the String variable “s1”. You have to complete the program so that it should should print the number of vowels in s1 . If your input data doesn’t have any vowel it will print “0”.

Java Week 7:Q5 A string “s1” is already initialized. You have to read the index “n” from the keyboard. Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “exception occur” .If there is no such exception, your program should replace the char “a” at the index value “n” of the “s1” ,then it will print the modified string.

WEEK 8

Java Week 8:Q1 Write a program which will print a pyramid of “” ‘s of height “n” and print the number of “” ‘s in the pyramid.

For example:

input : 5
output:

        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
25

Java Week 8:Q2 Write a program which will print a pascal pyramid of “*” ‘s of height “l” .

For example:

input: 8

output :
       *
      * *
     * * *
    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
* * * * * * * *

Java Week 8:Q3 Write a program which will print a pyramid of “numbers” ‘s of height “n” and print the sum of all number’s in the pyramid.

For example:

input: 5

output:

        1
      1 2 3
    1 2 3 4 5
  1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
95

Java Week 8:Q4 Write a program to print symmetric Pascal’s triangle of “*” ‘s of height “l” of odd length . If input “l” is even then your program will print “Invalid line number”.

For example:

input : 5

output:
  *
 * *
* * *
 * *
  *

input : 6

output:

Invalid line number

Java Week 8:Q5 Write a program to display any digit(n) from 0-9 using “7 segment display”.


For example:

input : 5

output :
 _
|_
 _|


input : 4

output :

|_|
  |

WEEK 9

Java Week 9:Q1 Complete the code to develop a BASIC CALCULATOR that can perform operations like Addition, Subtraction, Multiplication and Division.

Note the following points carefully:
1. Use only double datatype to store calculated numeric values.
2. Assume input to be of integer datatype.
3. The output should be rounded using Math.round() method.
4. Take care of the spaces during formatting output (e.g., single space each before and after =).
5. The calculator should be able to perform required operations on a minimum of two operands as shown in the below example:

Input:
	       5+6

Output:
	       5+6 = 11

Java Week 9:Q2 Complete the code to develop an ADVANCED CALCULATOR that emulates all the functions of the GUI Calculator as shown in the image.

Note the following points carefully:
1. Use only double datatype to store all numeric values.
2. Each button on the calculator should be operated by typing the characters from 'a' to 'p'.
3. To calculate 25-6, User should input fjhkc (where, f for 2, j for 5, h for '-', k for 6 and c for '=' ).
3. You may use the already defined function gui_map(char).
4. Without '=', operations won't give output as shown in Input_2 and Output_2 example below.
5. The calculator should be able to perform required operations on two operands as shown in the below example:

Input_1:
	       klgc

Output_1:
		18.0

Input_2:
	       klg

Output_2:

Java Week 9:Q3 Complete the code to perform a 45 degree anti clock wise rotation with respect to the center of a 5 × 5 2D Array as shown below:

INPUT:
              00100
              00100
              11111
              00100
              00100

OUTPUT:

              10001
              01010
              00100
              01010
              10001

Note the following points carefully:
1. Here, instead of 0 and 1 any character may be given.
2. The input and output array size must be of dimension 5 × 5 and nothing else.

Java Week 9:Q4 A program needs to be developed which can mirror reflect any 5 × 5 2D character array into its side-by-side reflection. Write suitable code to achieve this transformation as shown below:

 INPUT:                                       OUTPUT:
               OOXOO                                        OOXOO
               OOXOO                                        OOXOO
               XXXOO                                        OOXXX
               OOOOO                                        OOOOO
               XOABC                                        CBAOX

Note the following points carefully:
1. Here, instead of X and O any character may be present.
2. The input and output array size must be of dimension 5 × 5 and nothing else.
3. Only side-by-side reflection should be performed i.e. ABC || CBA.

Java Week 9:Q5 Write suitable code to develop a 2D Flip-Flop Array with dimension 5 × 5, which replaces all input elements with values 0 by 1 and 1 by 0. An example is shown below:

INPUT:
               00001
               00001
               00001
               00001
               00001

OUTPUT:

               11110
               11110
               11110
               11110
               11110

Note the following points carefully:
1. Here, the input must contain only 0 and 1.
2. The input and output array size must be of dimension 5 × 5.
3. Flip-Flop: If 0 then 1 and vice-versa.

WEEK 10

Java Week 10:Q1 The following code needs some package to work properly. Write appropriate code to import the required package(s) in order to make the program compile and execute successfully.

Java Week 10:Q2 Write the JDBC codes needed to create a Connection interface using the DriverManager class and the variable DB_URL. Check whether the connection is successful using ‘isAlive(timeout)’ method to generate the output, which is either ‘true’ or ‘false’.

Note the following points carefully:
1. Name the connection object as 'conn' only.
2. Use timeout value as 1.

Java Week 10:Q3 Due to some mistakes in the below code, the code is not compiled/executable. Modify and debug the JDBC code to make it execute successfully.

Java Week 10:Q4 Complete the code segment to create a new table named ‘PLAYERS’ in SQL database using the following information.

Coulmn UID First_Name Last_Name Age
Type Integer Varchar(45) Varchar(45) Integer
         

Java Week 10:Q5 Complete the code segment to rename an already created table named ‘PLAYERS’ into ‘SPORTS’.

WEEK 11

Java Week 11:Q1 Complete the code segment to insert the following data using prepared statement in the existing table ‘PLAYERS’.

Coulmn UID First_Name Last_Name Age
Row 1 1 Ram Gopal 26
Row 2 2 John Mayer 22

Java Week 11:Q2 Write the required code in order to update the following data in the table ‘PLAYERS’.

Coulmn UID First_Name Last_Name Age
From 1 Ram Gopal 26
To 1 Rama Gopala 24

Java Week 11:Q3 Write the appropriate code in order to delete the following data in the table ‘PLAYERS’.

Coulmn UID First_Name Last_Name Age
Delete 1 Rama Gopala 24

Java Week 11:Q4 Complete the following program to calculate the average age of the players in the table ‘PLAYERS’.

Structure of Table ‘PLAYERS’ is given below: | Coulmn | UID | First_Name | Last_Name | Age | |——–|———|————-|————-|———| | Type | Integer | Varchar(45) | Varchar(45) | Integer | | | | | | |

Java Week 11:Q5 Complete the code segment to drop the table named ‘PLAYERS’.

WEEK 12

Java Week 12:Q1 Complete the code to develop an extended version of the ADVANCED CALCULATOR with added special functions that emulates all the functions of the GUI Calculator as shown in the image.

Note the following points carefully:
1. Use only double datatype to store all numeric values.
2. Each button on the calculator should be operated by typing the characters from 'a' to 't'.
3. You may use the already defined function gui_map(char).
4. Use predefined methods from java.lang.Math class wherever applicable.
5. Without '=' binary operations won't give output as shown in Input_3 and Output_3 example below.
5. The calculator should be able to perform required operations on one or two operands as shown in the below example:

Input_1:
	       okhid

Output_1:
		100.0

Input_2:
	       ia

Output_2:
		2.0

Input_3:
	       okhi

Output_3:

Java Week 12:Q2 A partial code fragment is given. The URL class object is created in try block.You should write appropriate method( ) to print the protocol name and host name from the given url string.

For example:
https://www.xyz.com:1080/index.htm

protocol://host:port/filename

Java Week 12:Q3 Write a program to create a record by taking inputs using Scanner class as first name as string ,last name as string ,roll number as integer ,subject1 mark as float,subject2 mark as float. Your program should print in the format

  "name  rollnumber avgmark".

For example:
input:
ram
das
123
25.5
24.5
output:
ramdas 123 25.0

Java Week 12:Q4 A program code is given to call the parent class static method and instance method in derive class without creating object of parent class. You should write the appropriate code so that the program print the contents of static method() and instance method () of parent class.

Java Week 12:Q5 Write a recursive function to print the sum of first n odd integer numbers. The recursive function should have the prototype

 " int sum_odd_n(int n) ".

For example :

input : 5
output: 25

input : 6
output : 36