Consider the following figure constructed using (only) the characters + = I spac
ID: 663309 • Letter: C
Question
Consider the following figure constructed using (only) the characters + = I space and newline. 4a) Write a Java method called window that produces this figure as output. Use nested for-loops to print the repeated parts of the figure. 4b) Explain how you would modify your method so that it takes an input parameter for size (e.g. size=3 in the above example) and produces a figure that is larger and wider proportionally. The size parameter should always be greater than or equal to 3. You do not need to write the code for this more general method, just explain how you would do it.Explanation / Answer
import java.util.Scanner;
class MyPattern
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String st;int i,j,k,m,n,o;
st=in.nextLine();
int lt=st.length();
System.out.println();
for(i=0;i<lt;i++)
{
for(k=(lt-1)-i;k>0;k–)
{
System.out.print(” “);
}
for(j=0;j<=i;j++)
{
System.out.print(st.charAt(j)+” “);
}
System.out.println();
}
for(m=(lt-2);m>=0;m–)
{
for(n=(lt-1)-m;n>0;n–)
{
System.out.print(” “);
}
for(o=0;o<=m;o++)
{
System.out.print(st.charAt(o)+” “);
}
System.out.println();
}
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.