This article will help you with the answers of Programming in Java NPTEL 2022 Week 9 Programming Assignment Solutions.
Programming in Java NPTEL Week 9 Programming Assignment 1 Solutions
Complete the code to develop a BASIC CALCULATOR that can perform operations like Addition, Subtraction, Multiplication and Division.
import java.util.Scanner;
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
int i=0;
int j=0;
double ans=0;
char pattern[] = input.toCharArray();
for(int a=0; a<pattern.length; a++) {
if(pattern[a]=='+')
{
i= Integer.parseInt(input.substring(0,a));
j= Integer.parseInt(input.substring(a+1,pattern.length));
ans = (double)i+j;
}
else if(pattern[a]=='-') {
i= Integer.parseInt(input.substring(0,a));
j= Integer.parseInt(input.substring(a+1,pattern.length));
ans = (double)i-j;
}
else if(pattern[a]=='/')
{
i= Integer.parseInt(input.substring(0,a));
j= Integer.parseInt(input.substring(a+1,pattern.length));
ans = (double)i/j;
}
else if(pattern[a]=='*')
{
i= Integer.parseInt(input.substring(0,a));
j= Integer.parseInt(input.substring(a+1,pattern.length));
ans = (double)i*j;
}
}
System.out.print(input+" = " + Math.round(ans));
}
}
Programming Assignment 2 Solution
Complete the code to develop an ADVANCED CALCULATOR that emulates all the functions of the GUI Calculator as shown in the image.
import java.util.Scanner;
public class Question92{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
// Write code below...
char pattern [] = input.toCharArray();
int f=0;
for(int i=0; i<pattern .length; i++){
pattern [i]=gui_map(pattern [i]);
}
double operand1=0.0;
String o1="";
double operand2=0.0;
String o2="";
double ans=0.0;
outerloop:
for(int i=0; i<pattern .length; i++){
int r=0;
if(pattern [i]=='+'||pattern [i]=='-'||pattern [i]=='/'||pattern [i]=='X'||pattern [i]=='='){
for(int j=0; j<i; j++){
o1+=Character.toString(pattern [j]);
}
operand1=Double.parseDouble(o1);
for(int k=i+1; k<pattern .length; k++){
if(pattern [k]=='='){
f=1;
operand2=Double.parseDouble(o2);
if(pattern [i]=='+'){
ans=operand1+operand2;
}else if(pattern [i]=='-'){
ans=operand1-operand2;
}else if(pattern [i]=='/'){
ans=operand1/operand2;
}else if(pattern [i]=='X'){
ans=operand1*operand2;
}
break outerloop;
}else{
o2+=Character.toString(pattern [k]);
}
}
}
}
if(f==1)
System.out.print(ans);
}// 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;
}
}
Programming Assignment 3 Solution
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:
import java.util.Scanner;
public class Question93{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
char mat[][]= new char[5][5];
for(int L=0;L<5; L++)
{
String input = sc.nextLine();
char seq[] = input.toCharArray();
if(seq.length==5){
for(int i=0;i<5;i++)
{
mat[L][i]=seq[i];
}
}
else
{
System.out.print("Wrong Input!");
System.exit(0);
}
}
char tran[][] = 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]));
tran[a][b] = mat[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]));
tran[a][b] = mat[i][j];
break;
}
}
tran[2][2] = mat[2][2];
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
System.out.print(tran[i][j]);
}
System.out.println();
}
}
}
Week 9: Programming Assignment 4 Solution
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:
import java.util.Scanner;
public class Question94{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
char act[][]= new char[5][5];
char ref[][]= new char[5][5];
for(int L=0;L<5; L++)
{
String input = sc.nextLine();
char seq[] = input.toCharArray();
if(seq.length==5)
{
for(int i=0;i<5;i++)
act[L][i]=seq[i];
}
}
for(int i=0; i<5;i++)
{
for(int j=0; j<5;j++)
ref[i][j]=act[i][4-j];
}
for(int i=0; i<5;i++)
{
for(int j=0; j<5;j++)
{
System.out.print(ref[i][j]);
}
System.out.println();
}
}
}
Week 9: Programming Assignment 5 Solution
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:
import java.util.Scanner;
public class Question95{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
char act[][]= new char[5][5];
for(int L=0;L<5; L++)
{
String input = sc.nextLine();
char pat[] = input.toCharArray();
if(pat.length==5){
for(int i=0;i<5;i++)
{
if(pat[i]=='0' || pat[i]=='1')
{
act[L][i]=pat[i];
if(L==4 && i==4)
flipflop(act);
}
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();
}
}
}
For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.