Patterns β Printing Name
20 October 2024 at 15:03
public class PatternMyName { // MY NAME - IN PATTERN PRINTING public static void main(String[] args) { // TODO Auto-generated method stub PatternMyName pattern = new PatternMyName(); int n=9; pattern.printLtr("S",n); pattern.printLtr("U",n); pattern.printLtr("G",n); pattern.printLtr("I",n); pattern.printLtr("R",n); pattern.printLtr("T",n); pattern.printLtr("H",n); pattern.printLtr("A",n); } private void printLtr(String ltr,int n) { int m=n/2 +1; switch(ltr) { case "S": for (int r=1; r<=n; r++) { for (int c=1; c<=n; c++) { if (r==1 || r==n || r==m || (c==1 && r<m) || (c==9 && r>m)) { if ((c==1 && r==1) || (r==n && c==n) || (r==m && (c==1 || c==n))) System.out.print(" "); else System.out.print(" *"); } else System.out.print(" "); } System.out.println(); } System.out.println(); break; case "U": for (int r=1; r<=n; r++) { for (int c=1; c<=n; c++) { if ((c==1 && r<n)|| (c>1 && c<n && r==n) || (r<n && c==n)) System.out.print(" *"); else System.out.print(" "); } System.out.println(); } System.out.println(); break; case "G": for (int r=1; r<=n; r++) { for (int c=1; c<=n; c++) { if ((r==1 && c!=1) || (c==1 && r!=1 && r!=n) || (r==n && c!=1 && c!=n) || (c==n && r>=m && r!=n) || (r==m && c>m)) System.out.print(" *"); else System.out.print(" "); } System.out.println(); } System.out.println(); break; case "I": for (int r=1; r<=n; r++) { for (int c=1; c<=n; c++) { if ( c==m || r==1 || r==n) System.out.print(" *"); else System.out.print(" "); } System.out.println(); } System.out.println(); break; case "R": for (int r=1; r<=n; r++) { for (int c=1; c<=n; c++) { if ( (c==1 && r!=1) || (r==1 && c!=n && c!=1) || (c==n && r<m && r!=1) || (r==m && c!=n) || (r>m && c==m+(r-m))) System.out.print(" *"); else System.out.print(" "); } System.out.println(); } System.out.println(); break; case "T": for (int r=1; r<=n; r++) { for (int c=1; c<=n; c++) { if ( c==m || r==1 ) System.out.print(" *"); else System.out.print(" "); } System.out.println(); } System.out.println(); break; case "H": for (int r=1; r<=n; r++) { for (int c=1; c<=n; c++) { if ( c==1 || c==n || r==m) System.out.print(" *"); else System.out.print(" "); } System.out.println(); } System.out.println(); break; case "A": for (int r=1; r<=n; r++) { for (int c=1; c<=n; c++) { if ( (r<=m && (c==m-r+1 || c==m+r-1)) || r==m || (r>m && (c==1 || c==n))) System.out.print(" *"); else System.out.print(" "); } System.out.println(); } System.out.println(); break; } } }