Source of Shapes.java


  2: /**
  3:  * A class to draw some shapes (currently just Rectangles).
  4:  * The shape-drawing method is overloaded and shows how we do "default 
  5:  * arguments" in Java.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class Shapes {

 11:     /**
 12:      * Test the methods of this class.
 13:      *
 14:      * @param args  (ignored)
 15:      */
 16:     public static void main(String[] args) {
 17:         // draw a couple of rectangles using the three-argument version
 18:         System.out.println("\ndrawRectangle(7, 4, '+')");
 19:         Shapes.drawRectangle(7, 4, '+');
 20:         System.out.println("\ndrawRectangle(5, 7, '@')");
 21:         Shapes.drawRectangle(5, 7, '@');

 23:         // draw a rectangle using the tw-argument version
 24:         System.out.println("\ndrawRectangle(3, 15)");
 25:         Shapes.drawRectangle(3, 15);

 27:         // add space at the bottom of the output (it looks nicer)
 28:         System.out.println();
 29:     }

 31:     /**
 32:      * Draw a rectangle of the given dimensions using the given character.
 33:      *
 34:      * @param rows  how tall the rectangle is (in lines)
 35:      * @param cols  how wide the rectangle is (in characters)
 36:      * @param ch    the character the rectangle is made of
 37:      */
 38:     public static void drawRectangle(int rows, int cols, char ch) {
 39:         for (int r = 1; r <= rows; ++r) {
 40:             for (int c = 1; c <= cols; ++c) {
 41:                 System.out.print(ch);
 42:             }
 43:             System.out.println();
 44:         }
 45:     }

 47:     /**
 48:      * Draw a rectangle of the given dimensions.
 49:      *
 50:      * @param rows  how tall the rectangle is (in lines)
 51:      * @param cols  how wide the rectangle is (in characters)
 52:      */
 53:     public static void drawRectangle(int rows, int cols) {
 54:         // no fill character specified, so use a star/asterisk
 55:         Shapes.drawRectangle(rows, cols, '*');
 56:     }

 58: }