75% found this document useful (4 votes)
5K views6 pages

NPTEL Java Week 9 Assignment Answers

This document contains the answers to quizzes and programs related to Java programming from NPTEL week 9. It includes 5 programs that take in input, perform operations like flipping bits, reflection, rotation, arithmetic calculations, and outputting the results. The programs demonstrate skills like 2D arrays, string parsing, and mathematical operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
75% found this document useful (4 votes)
5K views6 pages

NPTEL Java Week 9 Assignment Answers

This document contains the answers to quizzes and programs related to Java programming from NPTEL week 9. It includes 5 programs that take in input, perform operations like flipping bits, reflection, rotation, arithmetic calculations, and outputting the results. The programs demonstrate skills like 2D arrays, string parsing, and mathematical operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

NPTEL Programming In Java

Week 9 Answers:

Quiz Answers:

1) A,B,D
2) A
3) B
4) C
5) D
6) A,C
7) B,C
8) B
9) B
10)C

__________________________________________________

Programs :
___________________________________________________

1.

char original00[][]= new char[5][5];

for(int line7=0;line7 < 5; line7++){


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'){
original00[line7][i]=seq[i];
if(line7==4 && i==4)
flipflop(original00);
}
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();
}

_________________________________________________________

2)

char originalp[][]= new char[5][5];

char reflectionn[][]= new char[5][5];

for(int linez=0;linez < 5; linez++){


String input = sc.nextLine();
char seq[] = input.toCharArray();
if(seq.length==5){
for(int i=0;i < 5;i++){
originalp[linez][i]=seq[i];
}
}
}

for(int i=0; i < 5;i++){


for(int j=0; j < 5;j++){
reflectionn[i][j]=originalp[i][4-j];
}
}

for(int i=0; i < 5;i++){


for(int j=0; j < 5;j++){
System.out.print(reflectionn[i][j]);
}
System.out.println();
}
________________________________________________________

3)

char matrix402[][]= new char[5][5];

for(int LL=0;LL < 5; LL++)


{
String input = sc.nextLine();
char seq[] = input.toCharArray();
if(seq.length==5){
for(int i=0;i < 5;i++)
{
matrix402[LL][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] = matrix402[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] = matrix402[i][j];
break;
}
}

tran[2][2] = matrix402[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();
}

___________________________________________________________________

4)

char mypattern [] = input.toCharArray();


int f=0;

for(int ip=0; ip < mypattern .length; ip++){


mypattern [ip]=gui_map(mypattern [ip]);
}

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

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

if(f==1)
System.out.print(ans);

__________________________________________________________________________
5)

int i=0;
int j=0;
double ans=0;

char pattern60[] = input.toCharArray();

for(int ab=0; ab < pattern60.length; ab++)


{
if(pattern60[ab]=='+')
{
i= Integer.parseInt(input.substring(0,ab));
j=
Integer.parseInt(input.substring(ab+1,pattern60.length));
ans = (double)i+j;
}else if(pattern60[ab]=='-')
{
i= Integer.parseInt(input.substring(0,ab));
j=
Integer.parseInt(input.substring(ab+1,pattern60.length));
ans = (double)i-j;
}else if(pattern60[ab]=='/')
{
i= Integer.parseInt(input.substring(0,ab));
j=
Integer.parseInt(input.substring(ab+1,pattern60.length));
ans = (double)i/j;
}else if(pattern60[ab]=='*')
{
i= Integer.parseInt(input.substring(0,ab));
j=
Integer.parseInt(input.substring(ab+1,pattern60.length));
ans = (double)i*j;
}
}

System.out.print(input+" = " + Math.round(ans));

You might also like