// BoxMoreElegant.java Copyright (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-02-15 File created. // 2006-02-15 Last modification. // A more elegant solution to Exercise 9-4. // This solution can be considered more elegant than BoxKL.java // because in this program a method named print_box() is // used to print the box. The printed box is, though, equally // unelegant as the box produced by BoxKL.java. import java.util.* ; class BoxMoreElegant { static void multiprint_character( char character_from_caller, int number_of_times_to_repeat ) { int repetition_counter = 0 ; while ( repetition_counter < number_of_times_to_repeat ) { System.out.print( character_from_caller ) ; repetition_counter ++ ; } } static void print_box( int desired_box_width, int desired_box_height ) { System.out.print( "\n Here is your box: \n" ) ; for ( int line_index = 0 ; line_index < desired_box_height ; line_index ++ ) { System.out.print( "\n " ) ; multiprint_character( 'X', desired_box_width ) ; } } public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; System.out.print( "\n Give box width: " ) ; int box_width = keyboard.nextInt( ) ; System.out.print( "\n Give box height: " ) ; int box_height = keyboard.nextInt( ) ; print_box( box_width, box_height ) ; } }