// CSE 142 Autumn 2007, Marty Stepp // // This program prints many lines and boxes of stars in a loop. // This version uses for loops and also shows an advanced concept: // The String data type. (hasn't been covered in lecture yet) // public class StarsWithLoops { public static void main(String[] args) { // the original program's output (lines and boxes of stars) drawLineOfStars(13); drawLineOfStars(7); drawLineOfStars(35); drawBox(5, 4); drawBox(10, 6); System.out.println(); // additional output: 3x6, 4x8, 5x10, ..., 10x20 boxes for (int i = 3; i <= 10; i++) { drawBox(2 * i, i); } } // Prints the given character the given number of times. // String is the data type for text characters and messages in Java. public static void printCharacter(String character, int times) { for (int i = 1; i <= times; i++) { System.out.print(character); } } // Draws a line of stars with the given length. public static void drawLineOfStars(int stars) { for (int i = 1; i <= stars; i++) { System.out.print("*"); } System.out.println(); } // Draws a box of stars with the given dimensions. public static void drawBox(int width, int height) { // top drawLineOfStars(width); // middle for (int line = 1; line <= height - 2; line++) { System.out.print("*"); printCharacter(" ", width - 2); System.out.println("*"); } // bottom drawLineOfStars(width); } }