Here are two code fragments. Below them are three pictures. These fragments draw
ID: 3763338 • Letter: H
Question
Here are two code fragments. Below them are three pictures. These fragments draw two of the three pictures. Determine which fragment draws which picture. Explain your answer by detailing which part of the if-statements draw which part of the pictures. Be very specific about how you can tell which fragment draws which picture. (In each code fragment, assume that n is an odd integer.) // assume that n is odd // assume that n is odd for (int i = 1; i <= n; i++) for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int j = 1; j <= n; j++) { { if (i==1 || i==n || if (i==1 || i==n || j==1 || j==n || j==1 || j==n || (i==(n/2)+1 && j>=(n/2)+1) || (i==(n/2)+1 && j<=(n/2)+1) || (i==j && i<=(n/2)+1) ) (i==j && i>=(n/2)+1) ) System.out.print("*"); System.out.print("*"); else else System.out.print(" "); System.out.print(" "); } } System.out.println(); System.out.println(); } } *************** *************** *************** * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ******** * * ******** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * ** *************** *************** ***************
Explanation / Answer
first fragment draw second picture.
Hello2.java
public class Hello2 {
public static void main(String[] args) {
int n=15;
for (int i = 1; i <= n; i++)
{ for (int j = 1; j <= n; j++)
{
if (i==1 || i==n ||
j==1 || j==n ||
(i==(n/2)+1 && j>=(n/2)+1) ||
(i==j && i<=(n/2)+1) )
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
Output :
***************
** *
* * *
* * *
* * *
* * *
* * *
* ********
* *
* *
* *
* *
* *
* *
***************
fragment second draw first picture.
Hello3.java
public class Hello3 {
public static void main(String[] args) {
int n=15;
for (int i = 1; i <= n; i++)
{ for (int j = 1; j <= n; j++)
{
if (i==1 || i==n ||
j==1 || j==n ||
(i==(n/2)+1 && j<=(n/2)+1) ||
(i==j && i>=(n/2)+1) )
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
Output :
***************
* *
* *
* *
* *
* *
* *
******** *
* * *
* * *
* * *
* * *
* * *
* **
***************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.