Παράδειγμα χρήσης κλάσεων: γιορτινή κάρτα

Διομήδης Σπινέλλης
Τμήμα Διοικητικής Επιστήμης και Τεχνολογίας
Οικονομικό Πανεπιστήμιο Αθηνών
dds@aueb.gr

Κλάση Tree

import gr.aueb.dds.BIO;

class Tree {
        // Tree properties
        private int treeWidth;          // Width of the tree
        private int trunkWidth;         // Width of the trunk
        private int trunkHeight;        // Height of the trunk
        private char treeFill;          // Fill character

        // Constructor
        // ew : Tree body width
        // uw : Trunk width
        // uh : Trunk heigth
        // f : fill character
        Tree(int ew, int uw, int uh, char f) {
                treeWidth = ew;
                trunkWidth = uw;
                trunkHeight = uh;
                treeFill = f;
        }

        // Draw a tree
        public void draw() {
                body();
                trunk();
        }

        // Draw the tree's body
        private void body() {
                int i;

                for (i = 1; i < treeWidth; i = i + 2) {
                        DrawChar.space((treeWidth - i) / 2);
                        fill(i);
                        BIO.println();
                }
        }

        // Draw the tree's trunk
        private void trunk() {
                int j;

                for (j = 0; j < trunkHeight; j = j + 1) {
                        DrawChar.space((treeWidth - trunkWidth) / 2);
                        fill(trunkWidth);
                        BIO.println();
                }
        }

        // Display n space characters
        static private void space(int n) {
                DrawChar.repeatChar(n, ' ');
        }

        // Display n treeFill characters
        private void fill(int n) {
                DrawChar.repeatChar(n, treeFill);
        }

        public static void main(String args[]) {
                Tree t = new Tree(822'!');
                Tree bigt = new Tree(1334'#');

                DrawChar.repeatChar(79'*');
                BIO.println();
                t.draw();
                bigt.draw();
                t.draw();
        }
}

Κλάση Snow

import gr.aueb.dds.BIO;

class Snow {
        private double density;         // Snow density: 0 very dense, 1 no snow
        private char symbol;            // Symbol to use for snowflakes
        private int lines;              // Lines to draw

        // Constructor
        Snow(double d, char s, int l) {
                density =  d;
                symbol = s;
                lines = l;
        }

        // Draw the snow
        public void draw() {
                int i;

                for (i = 0; i < lines; i++) {
                        if (Math.random() > density)
                                snowFlakes();
                        BIO.println();
                }
        }

        // Draw a line of random snowflakes
        private void snowFlakes() {
                int width = 70;

                while (width > 0) {
                        int s = (int)(Math.random() * 10 * density);
                        DrawChar.space(s);
                        BIO.print(symbol);
                        width = width - s - 1;
                }
        }

        // Test the class
        public static void main(String args[]) {
                Snow s = new Snow(0.1'*'4);
                s.draw();
        }
}

Κλάση DrawChar

import gr.aueb.dds.BIO;

// Character drawing functions
class DrawChar {
        // Repeat character c, n times on screen
        public static void repeatChar(int n, char c) {
                int k;

                for (k = 0; k < n; k++)
                        BIO.print(c);
        }

        // Display n space characters
        public static void space(int n) {
                repeatChar(n, ' ');
        }

        public static void main(String args[]) {
                repeatChar(79'*');
        }
}

Κλάση που τις χρησιμοποιεί: Xmas.java

/*
 * Draw a christmas card
 * Uses classes Snow and Tree
 */
class Xmas {
        public static void main(String args[]) {
                // Three types of snow
                Snow top = new Snow(0.2'*'5);
                Snow middle = new Snow(0.3','7);
                Snow bot = new Snow(0.4'.'7);

                // And three different trees
                Tree small = new Tree(822'!');
                Tree big = new Tree(1834'#');
                Tree tiny = new Tree(612'!');

                top.draw();
                middle.draw();
                bot.draw();
                small.draw();
                big.draw();
                tiny.draw();
        }
}

Αποτέλεσμα

 * *** ** *** ** * * * ** ** * **** * ** **** * * *** *** **** * * * *
* * ***** ** ***** ** ** * *** ** * * * ** ** *** * *** ** * * * ** * *
* ** * * * ** * * ** ** * ** **** * **** ** * * * ** * * *** * ** * **
 * ** * * ** ***** ** * *** ** * * *** ** * * * ** ** ** * * * ** * * *
* ** ******* * * **** * * **** ***** ** **** ** ** * * ** ** * ** * * *

, , , , ,  , ,  ,,,, , , ,  ,  , ,,,, ,, ,  , ,  , ,,  ,,  ,,  ,,  ,,,
  ,  ,,  , ,, , , ,, , ,  ,  ,,,  ,  , , , , ,, ,, , ,, ,, ,,,,  ,,,  ,

 ,  , , ,,, , , , ,,  ,,,  , ,  ,,  , ,,,  , , ,  ,  , ,  ,  ,,  , ,,  ,


 .  .  .   . .. . ...  . ....  .  ...  .  . .  ..   . .  .   ...   . .

  .   . . .   . . ...   . .  ..   .  .   .   .   .   .   .  .. .   .  .

 .   .  .  .  .   .... .. . .  .....  ...  .   .  .  . .  . ..   .  . .

   ..  .   ......   ..  .  .   .   .. . .  . .  ..   .... . ...  .  .   .
   !
  !!!
 !!!!!
!!!!!!!
   !!
   !!
        #
       ###
      #####
     #######
    #########
   ###########
  #############
 ###############
#################
       ###
       ###
       ###
       ###
  !
 !!!
!!!!!
  !
  !