Programming in Java Nptel Week 9 Assignment Answers

Are you looking for Programming in Java Nptel Week 9 Assignment Answers? You’ve come to the right place! Access the latest and most accurate solutions for your Week 9 assignment in the Programming in Java course

Course Link: Click Here


Programming in Java Nptel Week 9 Assignment Answers
Programming in Java Nptel Week 9 Assignment Answers

Programming in Java Nptel Week 9 Assignment Answers (July-Dec 2024)


  1. Which of the following is a one-line input field that allows the user to choose a number or an object value from an ordered sequence?
    A) Jtextarea
    B) Jtextfield
    C) Jspinner
    D) Jslider

Answer: C) Jspinner


  1. What is true about the following code.
    A) Both “OK” and “Cancel” button is added, but only “Cancel” button is visible.
    B) Only “OK” button is added and visible, “Cancel” button is not added.
    C) Only “Cancel” button will be added and visible, “OK” button is not added.
    D) Code throws an ERROR.

Answer: A) Both “OK” and “Cancel” button is added, but only “Cancel” button is visible.


  1. Which of the following is a container for other components and is used to build bespoke panels for organizing and arranging components?
    A) Jpanel
    B) Jframe
    C) Jcombo
    D) JBox

Answer:A) Jpanel


  1. Which of the following component gives a drop-down list of options from which to choose?
    A) Jpanel
    B) Jbutton
    C) JComboBox
    D) Jbox

Answer: C) JComboBox


  1. Which of the following Swing components inherently support the WindowListener interface?
    A) Swing frames (JFrame)
    B) Swing checkboxes (JCheckBox)
    C) None of these
    D) Swing combo boxes (JComboBox)

Answer: A) Swing frames (JFrame)


  1. Which class in Swing provides a graphical way to display images, icons, or custom graphics?
    A) Jimage
    B) Jlabel
    C) JImageIcon
    D) JDialog

Answer: B) Jlabel


  1. What is/are the way(s) to create a Frame in Java Swing?
    A) By creating the object of Frame class (association)
    B) None of these
    C) By importing a package named Jframe
    D) By declaring a class with name JFrame

Answer: A) By creating the object of Frame class (association)


  1. Which method is used to set the graphics current color to the specified color in the graphics class?
    A) public abstract void setFont(Font font)
    B) public abstract void setColor(Color c)
    C) public abstract void drawString(String str, int x, int y)
    D) None of the above

Answer: B) public abstract void setColor(Color c)


  1. When are the keyboard events fired?
    A) When the user manually calls the button
    B) When the user right clicks the mouse
    C) When the user calls the modifier
    D) When the user clicks a key

Answer: D) When the user clicks a key


  1. Which of the following function is used to specify the layout of a container.
    A) UseLayeout()
    B) setLayout()
    C) layout()
    D) DesignLayout()

Answer:B) setLayout()


These are NPTEL Programming in Java Nptel Week 9 Assignment Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: https://progiez.com/nptel-assignment-answers


Programming in Java Nptel Week 9 Assignment Answers (JAN-APR 2024)

Course name: Programming In Java

Course Link: Click Here

For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Assignment 9 Answers


Q1. What is the parent class of all AWT components?
a. java.awt.Panel
b. java.awt.Component
c. java.awt.Container
d. java.awt.Frame

Answer: b. java.awt.Component


Q2. Which event is generated when a user clicks a button in AWT?
a. MouseEvent
b. ActionListener
c. KeyEvent
d. WindowEvent

Answer: b. ActionListener


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Assignment 9 Answers


Q3. Which of the following architecture does the Swing framework use?
a. MVC
b. MVP
c. Layered architecture
d. Master-Slave architecture

Answer: a. MVC


Q4. A __________ is the basic class for all SWING UI components?
a. Container
b. JComponent
c. Component
d. Jbox

Answer: b. JComponent


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Assignment 9 Answers


Q5. Which event is generated when a window is resized in AWT?
a. WindowEvent
b. ComponentEvent
c. ResizeEvent
d. ContainerEvent

Answer: b. ComponentEvent


Q6. Which method is used to remove a component from a container in AWT?
a. remove()
b. deleteComponent()
c. removeComponent()
d. destroy()

Answer: a. remove()


For answers or latest updates join our telegram channel: Click here to join

See also  Programming in Java Nptel Week 4 Assignment Answers

These are NPTEL Programming In Java Assignment 9 Answers


Q7. What is true about the following code.
a. Both “OK” and “Cancel” button is added, but only “Cancel” button is visble.
b. Only “OK” button is added and visible, “Cancel” button is not added.
c. Only “Cancel” button will be added and visible, “OK” button is not added.
d. Code throws an ERROR.

Answer: a. Both “OK” and “Cancel” button is added, but only “Cancel” button is visble.


Q8. Which of the following function is used to generate the application’s top-level window?
a. JPanel
b. JFrame
c. JCombo
d. JBox

Answer: b. JFrame


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Assignment 9 Answers


Q9. In Java, what is the primary purpose of a layout manager?
a. To manage memory allocation
b. To arrange GUI components within a container
c. To handle exception handling
d. To control database connections

Answer: b. To arrange GUI components within a container


Q10. Which layout manager divides the container into five regions: North, South, East, West, and Center?
a. Border Layout
b. Grid Layout
c. Flow Layout
d. Card Layout

Answer: a. Border Layout


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Assignment 9 Answers


Programming Assignment

Question 1
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.

Solution:

SquareThread(int begin, int end){
  this.begin = begin;
  this.end = end;
}
 
synchronized public void run() {
       if( begin <= end )
        for (int i=begin; i<=end; i++)
          System.out.println(i*i);
       else
        for (int i=begin; i>=end; i--)
          System.out.println(i*i);
}

These are NPTEL Programming In Java Assignment 9 Answers


Question 2
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.

Solution:

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");
}

These are NPTEL Programming In Java Assignment 9 Answers


Question 3
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

Solution:

System.out.print("byte_array[" + n + "] = '" + (char)byte_array[n] + "'");
}
catch (IndexOutOfBoundsException e2) {
      System.out.print("Array index is out of range");
    }

These are NPTEL Programming In Java Assignment 9 Answers


Question 4
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

Solution:

class Point{
  private double x;
  private double y;
  
  public Point(double x, double y){
    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;
  }
}

These are NPTEL Programming In Java Assignment 9 Answers


Question 5
Complete the code below with a catch statement to print the following if the denominator (b) is zero
§ Cannot Divide by ZERO

Solution:

catch (ArithmeticException e) {
             System.out.print("Cannot Divide by ZERO");
          }

For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Assignment 9 Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: https://progiez.com/nptel-assignment-answers


Programming in Java Nptel Week 9 Assignment Answers (July-Dec 2023)

Course Name: Programming In Java

Course Link: Click Here

See also  Programming in Java | Week 11

These are NPTEL Programming In Java Assignment 9 Answers


Programming Assignment

Question 1
Problem statement:
Complete the code to develop a BASIC CALCULATOR that can perform operations like Addition, Subtraction, Multiplication and Division.

Solution:

		int i=0,j=0;
		double output=0;
		char seq[] = input.toCharArray();
		for(int a=0; a<seq.length; a++){
			if(seq[a]=='+'){
				i= Integer.parseInt(input.substring(0,a));
				j= Integer.parseInt(input.substring(a+1,seq.length));
				output = (double)i+j;
			}else if(seq[a]=='-'){
				i= Integer.parseInt(input.substring(0,a));
				j= Integer.parseInt(input.substring(a+1,seq.length));
				output = (double)i-j;
			}else if(seq[a]=='/'){
				i= Integer.parseInt(input.substring(0,a));
				j= Integer.parseInt(input.substring(a+1,seq.length));
				output = (double)i/j;
			}else if(seq[a]=='*'){
				i= Integer.parseInt(input.substring(0,a));
				j= Integer.parseInt(input.substring(a+1,seq.length));
				output = (double)i*j;
			}
		}
		System.out.print(input+" = " + Math.round(output));

Question 2
Complete the code to develop an ADVANCED CALCULATOR that emulates all the functions of the GUI Calculator as shown in the image.

Solution:

		char seq[] = input.toCharArray();
		int outflag=0;

		for(int i=0; i<seq.length; i++){
			seq[i]=gui_map(seq[i]);
		}

		double operand1=0.0;
		String o1="";
		double operand2=0.0;
		String o2="";
		double output=0.0;

		outerloop:
		for(int i=0; i<seq.length; i++){
			int r=0;
			if(seq[i]=='+'||seq[i]=='-'||seq[i]=='/'||seq[i]=='X'||seq[i]=='='){
				for(int j=0; j<i; j++){
					o1+=Character.toString(seq[j]);
				}
				operand1=Double.parseDouble(o1);
				for(int k=i+1; k<seq.length; k++){
					if(seq[k]=='='){
						outflag=1;
						operand2=Double.parseDouble(o2);
						if(seq[i]=='+'){
							output=operand1+operand2;
						}else if(seq[i]=='-'){
							output=operand1-operand2;
						}else if(seq[i]=='/'){
							output=operand1/operand2;
						}else if(seq[i]=='X'){
							output=operand1*operand2;
						}
						break outerloop;
					}else{
						o2+=Character.toString(seq[k]);
					}
				}
			}
		}
		if(outflag==1)
			System.out.print(output);

These are NPTEL Programming In Java Assignment 9 Answers


Question 3
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

Solution:

		    char arr[][]= new char[5][5];
			// Input 2D Array using Scanner Class
			for(int line=0;line<5; line++){
				String input = sc.nextLine();
				char seq[] = input.toCharArray();
				if(seq.length==5){
					for(int i=0;i<5;i++){
						arr[line][i]=seq[i];
					}
				}else{
					System.out.print("Wrong Input!");
					System.exit(0);
				}
			}
			char tra[][] = new char[5][5];
			String outer[]={"00","10","20","30",
							"40","41","42","43",
							"44","34","24","14",
							"04","03","02","01"};

			String inner[]={"11","21","31","32",
							"33","23","13","12"};

			// 45-Degree rotation
			for(int i=0;i<5;i++){
				for(int j=0;j<5;j++){
					// Transform outer portion
					for(int k=0; k<outer.length; k++){
						char indices[]=outer[k].toCharArray();
						int a = Integer.parseInt(String.valueOf(indices[0]));
						int b = Integer.parseInt(String.valueOf(indices[1]));
						if(a==i && b==j){
							if(k==15){k=1;}
							else if(k==14){k=0;}
							else {k+=2;}
							indices=outer[k].toCharArray();
							a = Integer.parseInt(String.valueOf(indices[0]));
							b = Integer.parseInt(String.valueOf(indices[1]));
							tra[a][b] = arr[i][j];
							break;
						}
					}
					// Transform inner portion
					for(int k=0; k<inner.length; k++){
						char indices[]=inner[k].toCharArray();
						int a = Integer.parseInt(String.valueOf(indices[0]));
						int b = Integer.parseInt(String.valueOf(indices[1]));
						if(a==i && b==j){
							if(k==7){k=0;}
							else {k+=1;}
							indices=inner[k].toCharArray();
							a = Integer.parseInt(String.valueOf(indices[0]));
							b = Integer.parseInt(String.valueOf(indices[1]));
							tra[a][b] = arr[i][j];
							break;
						}
					}
					// Keeping center same
					tra[2][2] = arr[2][2];
				}
			}
			// Print the transformed output
			for(int i=0;i<5;i++){
				for(int j=0;j<5;j++){
					System.out.print(tra[i][j]);
				}
				System.out.println();
		}

Question 4
Problem statement:
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

		char original[][]= new char[5][5];

		// Declaring 5x5 2D char array to store reflection
		char reflection[][]= new char[5][5];

		// Input 2D Array using Scanner Class
		for(int line=0;line<5; line++){
			String input = sc.nextLine();
			char seq[] = input.toCharArray();
			if(seq.length==5){
				for(int i=0;i<5;i++){
					original[line][i]=seq[i];
				}
			}
		}

		// Performing the reflection operation
		for(int i=0; i<5;i++){
			for(int j=0; j<5;j++){
				reflection[i][j]=original[i][4-j];
			}
		}

		// Output the 2D Reflection Array
		for(int i=0; i<5;i++){
			for(int j=0; j<5;j++){
				System.out.print(reflection[i][j]);
			}
			System.out.println();
		}

These are NPTEL Programming In Java Assignment 9 Answers


Question 5
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

Solution:

				char original[][]= new char[5][5];

		// Input 2D Array using Scanner Class and check data validity
		for(int line=0;line<5; line++){
			String input = sc.nextLine();
			char seq[] = input.toCharArray();
			if(seq.length==5){
				for(int i=0;i<5;i++){
					if(seq[i]=='0' || seq[i]=='1'){
						original[line][i]=seq[i];
						if(line==4 && i==4)
							flipflop(original);
					}
					else{
						System.out.print("Only 0 and 1 supported.");
						break;
					}
				}
			}else{
				System.out.print("Invalid length");
				break;
			}

		}
	}
	static void flipflop(char[][] flip){
		// Flip-Flop Operation
		for(int i=0; i<5;i++){
			for(int j=0; j<5;j++){
				if(flip[i][j]=='1')
					flip[i][j]='0';
				else
					flip[i][j]='1';
			}
		}

		// Output the 2D FlipFlopped Array
		for(int i=0; i<5;i++){
			for(int j=0; j<5;j++){
				System.out.print(flip[i][j]);
			}
			System.out.println();
		}

These are NPTEL Programming In Java Assignment 9 Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: Click here


Programming in Java Nptel Week 9 Assignment Answers (JAN-APR 2023)

Course Name: Programming in Java

Course Link: Click Here

These are NPTEL Programming In Java Assignment 9 Answers


Q1. Which of the following is/are correct regarding events in Java?
a. EventObject is super class of all the events.
b. AdjustmentEvent will be notified if scroll bar is manipulated.
c. All the classes and methods required for even handling in Java is in java.io package.
d. getID() method can be used to determine the name of an event.

Answer: a, b


Q2. Which of the following classes is used to display a message dialog in Java Swing?
a. JOptionPane
b. IDialog
c. JMessageDialog
d. JFrame

Answer: a. JOptionPane


These are NPTEL Programming In Java Assignment 9 Answers


Q3. Which of the following event is occurred when a button is pressed, a list item is double-clicked or a menu item is selected?
a. AdjustmentEvent
b. ActionEvent
c. ContainerEvent
d. ComponentEvent

Answer: b. ActionEvent


Q4. Which of the statements are correct about Swing programming?
a. AWT is a heavyweight programming.
b. Swing is heavyweight programming.
c. Swing is lightweight programming.
d. Both AWT and Swing are lightweight programming.

Answer: c. Swing is lightweight programming.


These are NPTEL Programming In Java Assignment 9 Answers


Q5. Event class is defined in which of the following libraries?
a. java.io
b. javalang
c. java.net
d. java.util

Answer: d. java.util


Q6. Which of the following is/are class(es) in javax.swing package?
a. BoxLayout
b. MenuElement
c. JComponent
d. Scrollable

Answer: a, c


These are NPTEL Programming In Java Assignment 9 Answers


Q7. Which of the following statement(s) is/are true?
a. Swing component frame does not support Window Listner.
b. Swing component combobox does not support Window Listner.
c. Swing component checkbox does not support Window Listner.
d. Swing component dialog does not support Window Listner.

Answer: b, c


Q8. A class which implements the ActionListener interface, also it must implement which of the following methods?
a. void handle( ActionEvent e )
b. void actionPerformed( ActionEvent e )
c. void eventDispatched( AWTEvent e )
d. String getActionCommand( ActionEvent e )

See also  All About NPTEL

Answer: b. void actionPerformed( ActionEvent e )


These are NPTEL Programming In Java Assignment 9 Answers


Q9. When a component is added or removed, which of the following events is generated?
a. ComponentEvent
b. ContainerEvent
c. FocusEvent
d. InputEvent

Answer: b. ContainerEvent


Q10. Which of these packages contains all the event handling interfaces?
a. java.lang
b. java.awt
c. java.awt.event
d. java.event

Answer: c. java.awt.event


Programming Assignment

Question 1

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

Solution:

char original[][]= new char[5][5];
for(int line=0;line<5; line++)
{
    String input = sc.nextLine();
    char seq[] = input.toCharArray();
    if(seq.length==5)
    {
        for(int i=0;i<5;i++)
        {
            if(seq[i]=='0' || seq[i]=='1')
            {
                original[line][i]=seq[i];
                if(line==4 && i==4)
                flipflop(original);
            }
            else
            {
                System.out.print("Only 0 and 1 supported.");
                break;
            }
        }
    }
    else
    {
        System.out.print("Invalid length");
        break;
    }
}
}
static void flipflop(char[][] flip)
{
    for(int i=0; i<5;i++)
    {
        for(int j=0; j<5;j++)
        {
            if(flip[i][j]=='1')
            flip[i][j]='0';
            else
            flip[i][j]='1';
        }
    }
    for(int i=0; i<5;i++)
    {
        for(int j=0; j<5;j++)
        {
            System.out.print(flip[i][j]);
        }
        System.out.println();
    }

Question 2

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

Solution:

char original[][]= new char[5][5];
char reflection[][]= new char[5][5];
for(int line=0;line<5; line++)
{
    String input = sc.nextLine();
    char seq[] = input.toCharArray();
    if(seq.length==5)
    {
        for(int i=0;i<5;i++)
        {
            original[line][i]=seq[i];
        }
    }
}
for(int i=0; i<5;i++)
{
    for(int j=0; j<5;j++)
    {
        reflection[i][j]=original[i][4-j];
    }
}
for(int i=0; i<5;i++)
{
    for(int j=0; j<5;j++)
    {
        System.out.print(reflection[i][j]);
    }
    System.out.println();
}

These are NPTEL Programming In Java Assignment 9 Answers


Question 3

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

Solution:

char arr[][]= new char[5][5];
for(int line=0;line<5; line++)
{
    String input = sc.nextLine();
    char seq[] = input.toCharArray();
    if(seq.length==5)
    {
        for(int i=0;i<5;i++)
        {
            arr[line][i]=seq[i];
        }
    }
    else
    {
        System.out.print("Wrong Input!");
        System.exit(0);
    }
}
char tra[][] = new char[5][5];
String outer[]={"00","10","20","30",
"40","41","42","43",
"44","34","24","14",
"04","03","02","01"};
String inner[]={"11","21","31","32",
"33","23","13","12"};
for(int i=0;i<5;i++)
{
    for(int j=0;j<5;j++)
    {
        for(int k=0; k<outer.length; k++)
        {
            char indices[]=outer[k].toCharArray();
            int a = Integer.parseInt(String.valueOf(indices[0]));
            int b = Integer.parseInt(String.valueOf(indices[1]));
            if(a==i && b==j)
            {
                if(k==15)
                {
                    k=1;
                }
                else if(k==14)
                {
                    k=0;
                }
                else
                {
                    k+=2;
                }
                indices=outer[k].toCharArray();
                a = Integer.parseInt(String.valueOf(indices[0]));
                b = Integer.parseInt(String.valueOf(indices[1]));
                tra[a][b] = arr[i][j];
                break;
            }
        }
        for(int k=0; k<inner.length; k++)
        {
            char indices[]=inner[k].toCharArray();
            int a = Integer.parseInt(String.valueOf(indices[0]));
            int b = Integer.parseInt(String.valueOf(indices[1]));
            if(a==i && b==j)
            {
                if(k==7)
                {
                    k=0;
                }
                else
                {
                    k+=1;
                }
                indices=inner[k].toCharArray();
                a = Integer.parseInt(String.valueOf(indices[0]));
                b = Integer.parseInt(String.valueOf(indices[1]));
                tra[a][b] = arr[i][j];
                break;
            }
        }
        tra[2][2] = arr[2][2];
    }
}
for(int i=0;i<5;i++)
{
    for(int j=0;j<5;j++)
    {
        System.out.print(tra[i][j]);
    }
    System.out.println();
}