NPTEL Java Assignment week 9
With the growth of Information and Communication Technology, there is a need to develop large and complex software. To meet this requirement object-oriented paradigm has been developed and based on this paradigm the Java programming language emerges as the best programming environment.
Programming in Java is a MOOC based course which is 12 weeks in duration and can fulfill the criteria of 4 credits in a year. You can visit the NPTEL SWAYAM platform and register yourself for the course. This course is brought to you by Prof. Dr. Debasis Samanta who holds a Ph.D. in Computer Science and Engineering from the Indian Institute of Technology Kharagpur.
Programming In Java 2020 Details:-
- Who Can Join: The undergraduate students from the engineering disciplines namely CSE, IT, EE, ECE, etc. might be interested in this course.
- Requirements/Prerequsites: The course requires that the students are familiar with a programming language such as C/C++ and data structures, algorithms.
- INDUSTRY SUPPORT: All IT companies.
Java Assignment Week 9 Answers:-
Java Week 9: Q1.
Complete the code to develop a BASIC CALCULATOR that can perform operations like Addition, Subtraction, Multiplication, and Division.
Answer/Code:-
import java.util.*;
public class Question91{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String input = sc.nextLine(); // Read as string, e.g., 5+6
// Declare and initialize the required variable(s)
int i=0; int j=0;
double k=0;
// Split the input string into character array
char s[] = input.toCharArray();
/*
Write your method to separate two operands
and operators and then perform the required operation.
*/
for(int a=0; a<s.length; a++){
if(s[a]=='+')
{
i=Integer.parseInt(input.substring(0,a));
j=Integer.parseInt(input.substring(a+1,s.length));
k=(double)i+j;
}
else if(s[a]=='-')
{
i=Integer.parseInt(input.substring(0,a));
j=Integer.parseInt(input.substring(a+1,s.length));
k=(double)i-j;
}
else if(s[a]=='*')
{
i=Integer.parseInt(input.substring(0,a));
j=Integer.parseInt(input.substring(a+1,s.length));
k=(double)i*j;
}
else if(s[a]=='/')
{
i=Integer.parseInt(input.substring(0,a));
j=Integer.parseInt(input.substring(a+1,s.length));
k=(double)i/j;
}
}
// Print the output as stated in the question
System.out.print(input+" = "+Math.round(k));
}
}
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.
Answer/Code:-
import java.util.*;
public class Question92{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
// Write code below...
String num1="",num2="";
char op='a';
int equal=0,flag=0,check=0,j=0,ch=0;
char[] charArray = input.toCharArray();
char[] numarray = new char[input.length()];
for(int i=0; i<input.length(); i++)
{
if(charArray[i]<'a' || charArray[i]>'p')
{
check=1;
break;
}
if(charArray[i]=='c')
ch=1;
char out=gui_map(charArray[i]);
numarray[i]=out;
if(out=='+' || out=='-'|| out=='X'|| out=='/')
{
flag=1;
op=out;
}
if(flag==0)
num1=num1 + String.valueOf(out);
else
{
if(j==0)
{
j++;
continue;
}
if(out== '=')
{
equal=1;
break;
}
num2=num2 + String.valueOf(out);
}
}
if(ch==1 && check==0)
{
double a=Double.parseDouble(num1);
double b=Double.parseDouble(num2);
if(op=='+') System.out.print(a+b);
else if(op=='-') System.out.print(a-b);
else if(op=='X') System.out.print(a*b);
else if(op=='/') System.out.print(a/b);
}
}// The main() method ends here.
// A method that takes a character as input and returns the corresponding GUI character
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','/'}};
// Checking for maps
for(int i=0; i<gm.length; i++){
if(gm[i][0]==in){
out=gm[i][1];
break;
}
}
return out;
}
}
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:
Answer/code:-
import java.util.*;
import java.util.Scanner;
public class Question93{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String arr[]=new String[5];
for(int i=0;i<5;i++)
arr[i]=sc.nextLine();
char matrix[][]=new char[5][5];
for(int i=0;i<5;i++){
char[] chararray=arr[i].toCharArray();
for(int j=0;j<5;j++){
matrix[i][j]=chararray[j];
}
}
char newmatrix[][]=new char [5][5];
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(i==0 && j<3)
newmatrix[i][j+2]=matrix[i][j];
else if (i==0 && j==3)
newmatrix[i+1][j+1]=matrix[i][j];
else if(i<3 && j==4)
newmatrix[i+2][j]=matrix[i][j];
else if (i==3 && j==4)
newmatrix[i+1][j-1]=matrix[i][j];
else if(i==4 && j>1)
newmatrix[i][j-2]=matrix[i][j];
else if (i==4 && j==1)
newmatrix[i-1][j-1]=matrix[i][j];
else if(i>1 && j==0)
newmatrix[i-2][j]=matrix[i][j];
else if (i==1 && j==0)
newmatrix[i-1][j+1]=matrix[i][j];
else if(i==1 && j>0 && j<4){
if(j==3)
newmatrix[i+1][j]=matrix[i][j];
else
newmatrix[i][j+1]=matrix[i][j];
}
else if(i==2 && j>0 && j<4){
if(j==2)
newmatrix[i][j]=matrix[i][j];
else if (j==3)
newmatrix[i+1][j]=matrix[i][j];
else
newmatrix[i-1][j]=matrix[i][j];
}
else{
if(j==1)
newmatrix[i-1][j]=matrix[i][j];
else
newmatrix[i][j-1]=matrix[i][j];
}
}
}
for(int i=0;i<5;i++){
for(int j=4;j>=0;j--)
System.out.print(newmatrix[i][j]);
System.out.println();
}
} // The main() method ends here
} // The main class ends here
NPTEL » Programming In Java Assignment week 6 Sep-2020
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:
Answer/Code:-
import java.util.*;
import java.util.Scanner;
public class Question94{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
// Declare necessary variables
String a[] = new String[5];
char r[][] = new char[5][5];
int i,j,k=0;
// Input 5x5 2D Array using Scanner Class
for(j=0; j<5; j++)
a[j] = sc.nextLine();
// Perform the reflection operation
for( i=0; i<5; i++)
for( j=4,k=0; j>=0; j--,k++)
r[i][k] = a[i].charAt(j);
// Output 5x5 2D Reflection Array
for(i=0; i<5; i++)
{
for(k=0; k<5; k++)
System.out.print(r[i][k]);
System.out.println();
}
} // The main() method ends here
} // The main class ends here
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:
Answer/Code:-
import java.util.Scanner;
public class Question95{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
// Declare the 5X5 2D array to store the input
String a[] = new String[5];
int i,j;
char matrix[][] = new char[5][5];
// Input 2D Array using Scanner Class and check data validity
for(i=0; i<5; i++)
a[i] = sc.nextLine();
// Perform the Flip-Flop Operation
for(i=0; i<5; i++)
{
char[] chararray = a[i].toCharArray();
for(j=0; j<5; j++)
matrix[i][j] = chararray[j];
}
// Output the 2D Flip-Flop Array
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
if(matrix[i][j]=='0')
System.out.print('1');
else
System.out.print('0');
}
System.out.println();
}
} // The main() ends here
} // The main class ends here
Find Other Quiz Here:
Amazon Fashion Quiz Answers & Win ₹1000
Amazon Great Indian Festival Quiz Answers: Win Rs. 50,000
NPTEL » Programming In Java Assignment week 6 Sep-2020
NPTEL » Programming In Java Assignment week 7 Sep-2020
NOTE: These codes are based on our knowledge. Answers might be incorrect, we suggest you to not the copy-paste answers blindly.