Howdy. 

-bash-4.2$ nano -w 0923a.phps
-bash-4.2$ date
Mon Sep 23 14:29:44 EDT 2019
-bash-4.2$

Java 
classes
javac
java
objects
primitive types: (int, byte, long, short), (double, float), boolean, char 
everything else: reference type, user-defined types 
assignment statements
if statements 
while statements (loops) 
for loops 

--

 1. Can you write a name? A sentence? A bunch of stars?

public class One {
  public static void main(String[] args) {
    System.out.println("*****");  
  }
}

 2. Can you write n stars where n comes from the user? 

2.1 Let's see how I read n from the user.

public class One {
  public static void main(String[] args) {
    String number = args[0];
    int n = Integer.parseInt( number ); 
    System.out.println( n + 0 ); 
  }
}

--

public class One {
  public static void main(String[] args) {
    int n = Integer.parseInt( args[0] ); 
    System.out.println( n + 0 ); 
  }
}

--

public class One {
  public static void main(String[] args) {
    int n = Integer.parseInt( args[0] ); 
    int count = 1; 
    while (count <= n) {
      System.out.print("*"); // print a star 
      count = count + 1; // increment count by 1 
    }
  }
}

--

 3. Do the same but first create the string of stars and then print it. 

public class One {
  public static void main(String[] args) {
    int n = Integer.parseInt( args[0] ); 
    String line = ""; 
    int count = 1; 
    while (count <= n) {
      line = line + "*"; // add a star to line 
      count = count + 1; // increment count by 1 
    }
    System.out.println( line ); 
  }
}

 4. Give n from the user print a string of n stars using a for loop. 

 Every while can be transformed into a for loop. 

 Every for likewise can be transformed into a while loop. 

 INIT
 while (COND) {
   BODY
   STEP 
 }

 for ( INIT ; COND ; STEP ) {
   BODY
 }

Therefore the code looks like:

public class One {
  public static void main(String[] args) {
    int n = Integer.parseInt( args[0] ); 
    String line = ""; 
    int count = 1; 
    for (       ; count <= n; count = count + 1 ) { 
      line = line + " *"; 
    }
    System.out.println( line ); 
  }
}

--

 Same thing slightly different:

public class One {
  public static void main(String[] args) {
    int n = Integer.parseInt( args[0] ); 
    String line = ""; 

    for ( int count = 1; count <= n; count = count + 1 ) { 
      line = line + " *"; 
    }
    
    System.out.println( line ); 
  }
}

 5. Given n print n lines of n stars each.

public class One {
  public static void main(String[] args) {
    int a = 3; 
    {
      int b = 4; 
      System.out.println(a + " " + b); 
    }
    {
      int b = -2; 
      System.out.println(a + " " + b); 
    }

  }
}

But notice this:

public class One {
  public static void main(String[] args) {
    int a = 3; 
    {
      int b = 4; 
      int a = 100; 
      System.out.println(a + " " + b); 
    }
    {
      int b = -2; 
      System.out.println(a + " " + b); 
    }

  }
}



--

public class One {
  public static void main(String[] args) {
    int n = Integer.parseInt( args[0] ); 
    int lines = n; 
    int row = 1; 
    while (row <= n) 
    { String line = ""; 
      for ( int cols = 1; cols <= n; cols = cols + 1 ) { 
        line = line + " *"; 
      }
      System.out.println( line ); 
      row = row + 1; // step 
    }
  }
}

6. Use for loops throughout. 

public class One {
  public static void main(String[] args) {
    int n = Integer.parseInt( args[0] ); 
    for (int row = 1; row <= n; row = row + 1) { 
      String line = ""; 
      for ( int cols = 1; cols <= n; cols = cols + 1 ) { 
        line = line + " *"; 
      }
      System.out.println( line ); 
    }
  }
}

7. Change the code to print a giant + or a giant X

public class One {
  public static void main(String[] args) {
    int n = Integer.parseInt( args[0] ); 
    for (int row = 1; row <= n; row = row + 1) { 
      String line = ""; 
      for ( int cols = 1; cols <= n; cols = cols + 1 ) { 
        if ( row == n / 2 || cols == n / 2) { 
          line = line + " *"; 
        } else {
          line = line + "  ";  
        }
      }
      System.out.println( line ); 
    }
  }
}

8. Same with X 

public class One {
  public static void main(String[] args) {
    int n = Integer.parseInt( args[0] ); 
    for (int row = 1; row <= n; row = row + 1) { 
      String line = ""; 
      for ( int cols = 1; cols <= n; cols = cols + 1 ) { 
        if (  row == cols || row + cols == n + 1  ) { 
          line = line + " *"; 
        } else {
          line = line + "  ";  
        }
      }
      System.out.println( line ); 
    }
  }
}

Next I want to print a scalable Z:

public class One {
  public static void main(String[] args) {
    int n = Integer.parseInt( args[0] ); 
    for (int row = 1; row <= n; row = row + 1) { 
      String line = ""; 
      for ( int cols = 1; cols <= n; cols = cols + 1 ) { 
        if ( row + cols == n  || 
             row == 1 || 
             row == n-1 || 
             (row == n / 2 && cols >= n / 3 && cols <= n * 2 / 3)
            ) { 
          line = line + " *"; 
        } else {
          line = line + "  ";  
        }
      }
      System.out.println( line ); 
    }
  }
}

--

alicia n freel anfreel