Zdrojový kód k 3. prednáške

Trieda SmartTurtle:

package paz1a.lectures.lecture3;

import sk.upjs.jpaz2.*;

public class SmartTurtle extends Turtle {

        /**
         * Nahodna pochodzka s 90-stupnovym natocenim.
         *
         * @param stepCount
         *            pocet krokov.
         */

        public void randomWalk(int stepCount) {
                double xStart = this.getX();
                double yStart = this.getY();
                double direction = this.getDirection();

                for (int i = 0; i < stepCount; i++) {
                        // Vygenerujeme realne nahodne cislo z <0, 4). Orezanim desatinnej casti
                        // (pretypovanim) ziskame cele cislo medzi 0 a 3. Neskor jeho vynasobenim cislom
                        // 90 ziskame nahodny uhol natocenia, ktory je nasobkom 90.
                        int multiplicator = (int) (Math.round(Math.random() * 4));
                        // Premenna aj literal 90 su typu int, takze aj sucin je int. Metoda turn
                        // ocakava double. Na tomto mieste sa udeje implicitne pretypovanie hodnoty z
                        // intu na double.
                        this.turn(multiplicator * 90);
                        this.step(10);
                        JPAZUtilities.delay(30);
                }

                this.setPosition(xStart, yStart);
                this.setDirection(direction);
        }

        /**
         * Stvorcova spirala.
         *
         * @return dlzka trajektorie.
         */

        public double squareSpiral() {
                // Aktualna dlzka kroku - inicializujeme na 150
                double currentStepLength = 150;
                // Celkova prejdena dlzka
                double odometer = 0;

                // Kym je krok "rozumnej dlzky", kreslime ciary
                while (currentStepLength > 0.5) {
                        // Spravime krok a dlzku pridame k premennej odometer
                        this.step(currentStepLength);
                        odometer = odometer + currentStepLength;
                        this.turn(90);

                        currentStepLength = currentStepLength * 0.9;
                        JPAZUtilities.delay(50);
                }

                // Vratime prejdenu dlzku.
                return odometer;
        }

        /**
         * Stvorcova spirala ale s pouzitim while(true).
         *
         * @return prejdena dlzka.
         */

        public double squareSpiral2() {
                double currentStepLength = 150;
                double odometer = 0;

                while (true) {
                        this.step(currentStepLength);
                        odometer += currentStepLength;
                        this.turn(90);
                        currentStepLength = currentStepLength * 0.9;

                        // Ak je krok prilis maly, koncime a vratime prejdenu dlzku.
                        if (currentStepLength < 0.5) {
                                // return ukoncuje vykonavanie metody
                                return odometer;
                        }

                        JPAZUtilities.delay(50);
                }

                // currentStepLength = 0;
        }

        /**
         * Vrati pocet prirodzenych delitelov cisla n.
         *
         * @param n
         *            cislo
         * @return pocet delitelov.
         */

        public int countDivisors(int n) {
                n = Math.abs(n);
                int counter = 0;
                for (int i = 1; i <= n; i++) {
                        if (n % i == 0) {
                                counter++;
                        }
                }

                return counter;
        }

        /**
         * Vrati, ci je cislo prvocislo.
         *
         * @param n
         *            cislo.
         * @return true, ak je cislo prvocislo, false inak.
         */

        public boolean isPrime(int n) {
                if (this.countDivisors(n) == 2) {
                        return true;
                } else {
                        return false;
                }

                // return this.countDivisors(n) == 2;
        }

        /**
         * Vrati ciferny sucet.
         *
         * @param n
         *            cislo
         * @return ciferny sucet cisla.
         */

        public int digitSum(int n) {
                n = Math.abs(n);
                int result = 0;
                while (n > 0) {
                        // vyberiem poslednu cifru
                        int digit = n % 10;
                        // pridam cifru k cifernemu suctu
                        result = result + digit;
                        // odstranim z n poslednu cifru
                        n = n / 10;
                }

                return result;
        }

}