Programming in Java NPTEL Week 9 Assignment

                     

Course Name: Programming in Java NPTEL

Link of Course: https://onlinecourses.nptel.ac.in/noc22_cs47/preview

These are answers for Programming In Java NPTEL Week 9 Assignment


Q1) Which of the following Listener(s) is/are supported by button Swing component ?
a. Action Listner
b. Change Listner
c. Item Listner
d. Window Listner

Answer: a, b, c


Q2) Which of the following is/are NOT interface(s) in javax.swing package?
a. MenuElement
b. BoxLayout
c. JComponent
d. Scrollable

Answer: b, c


These are answers for Programming In Java NPTEL Week 9 Assignment


Q3) Which of the following statement(s) is/are false?
a. Swing component frame support Window Listner
b. Swing component combo box support Window Listner.
c. Swing component check box support Window Listner.
d. Swing component dialog support Window Listner.

Answer: b, c


Q4) Which package provides many event classes and Listener interfaces for event handling?
a. java.awt.activeevent
b. java.awt.event
c. java.awt.listener
d. none of these

Answer: b. java.awt.event


These are answers for Programming In Java NPTEL Week 9 Assignment


Q5) Select the correct information
a. JTextField cannot be used as a JLabel.
b. JLabel cannot be used as a JTextField.
c. Button grouped radio button cannot be used instead of JComboBox.
d. JPassword Field extends JLabel.

Answer: b. JLabel cannot be used as a JTextField.


Q6) Which of the following is/are incorrect regarding events in Java?
a. EventClass 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.awt package.
d. getID() method can be used to determine the type of event.

Answer: a. EventClass is super class of all the events.


These are answers for Programming In Java NPTEL Week 9 Assignment


Q7) Which of the following is/are interface(s) of java.awt package?
a. Button
b. Choice
c. Dialog
d. Paint

Answer: d. Paint


Q8) Which of the following displays components row-by-row in the order in which they were added to the JFrame?
a. CardLayout
b. FlowLayout
c. BorderLayout
d. GridLayout

Answer: b. FlowLayout


These are answers for Programming In Java NPTEL Week 9 Assignment


Q9) Which of the following statements are true?
a. Java AWT components are platform-dependent.
b. Java Swing componets are platform-independent.
c. MVC pattern is not supported by AWT.
d. MVC pattern is supported by Swing.

Answer: a, b, c, d


Q10) Which of the following method is used to set a frame, say f with size 300 × 200 pixels?
JFrame f = new JFrame ();

a. f.setsize (300, 200);
b. f.setSize (300, 200);
c. f.paint (300, 200);
d. f.setVisible (300, 200);

Answer: b. f.setSize (300, 200);


These are answers for Programming In Java NPTEL Week 9 Assignment


Programming Assignment

Question 1
Complete the code to develop a BASIC CALCULATOR that can perform operations like Addition, Subtraction, Multiplication and Division.
Input:  5+6
Output: 5+6 = 11

Solution:

import java.util.Scanner;
public class Question91{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		String input = sc.nextLine();
				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));
	}
}

These are answers for Programming In Java NPTEL Week 9 Assignment


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

Solution:

import java.util.Scanner;
public class Question92{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		String input = sc.nextLine();
				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);
	}
	static char gui_map(char in){
		char out = 'N';// N = Null/Empty
		char gm[][]={{'a','.'},{'b','0'},{'c','='},{'d','+'},{'e','1'},{'f','2'},{'g','3'},{'h','-'},{'i','4'},{'j','5'},{'k','6'},{'l','X'},{'m','7'},{'n','8'},{'o','9'},{'p','/'}};
		for(int i=0; i< gm.length; i++){
			if(gm[i][0]==in){
				out=gm[i][1];
				break;
			}
		}
		return out;
	}
}

These are answers for Programming In Java NPTEL Week 9 Assignment


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:

import java.util.Scanner;
public class Question93{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
			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();
			}
		}
	}

These are answers for Programming In Java NPTEL Week 9 Assignment


Question 4
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:
OOXOO OOXOO XXXOO  OOOOO          XOABC
OUTPUT:
OOXOO OOXOO OOXXX OOOOO CBAOX

Solution:

import java.util.Scanner;
public class Question94{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		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 answers for Programming In Java NPTEL Week 9 Assignment


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:

import java.util.Scanner;
public class Question95{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		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();
		}
	}
}

These are answers for Programming In Java NPTEL Week 9 Assignment

All Weeks of Programming In Java: https://progies.in/answers/nptel/programming-in-java

More NPTEL Solutions: https://progies.in/answers/nptel


* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.


These are answers for Programming In Java NPTEL Week 9 Assignment